Allow passing props through to determine modal options

This commit is contained in:
Dane Everitt 2020-08-17 22:04:24 -07:00
parent c28cba92e2
commit e873c597bb
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 39 additions and 26 deletions

View file

@ -32,7 +32,7 @@ const ApiKeyModal = ({ apiKey }: Props) => {
ApiKeyModal.displayName = 'ApiKeyModal';
export default asModal({
export default asModal<Props>({
closeOnEscape: false,
closeOnBackground: false,
})(ApiKeyModal);

View file

@ -1,4 +1,4 @@
import React, { useContext, useEffect } from 'react';
import React, { useContext } from 'react';
import tw from 'twin.macro';
import Button from '@/components/elements/Button';
import asModal from '@/hoc/asModal';
@ -12,12 +12,8 @@ type Props = {
showSpinnerOverlay?: boolean;
};
const ConfirmationModal = ({ title, children, buttonText, onConfirmed, showSpinnerOverlay }: Props) => {
const { dismiss, toggleSpinner } = useContext(ModalContext);
useEffect(() => {
toggleSpinner(showSpinnerOverlay);
}, [ showSpinnerOverlay ]);
const ConfirmationModal = ({ title, children, buttonText, onConfirmed }: Props) => {
const { dismiss } = useContext(ModalContext);
return (
<>
@ -37,4 +33,6 @@ const ConfirmationModal = ({ title, children, buttonText, onConfirmed, showSpinn
ConfirmationModal.displayName = 'ConfirmationModal';
export default asModal()(ConfirmationModal);
export default asModal<Props>(props => ({
showSpinnerOverlay: props.showSpinnerOverlay,
}))(ConfirmationModal);

View file

@ -86,12 +86,14 @@ const Modal: React.FC<ModalProps> = ({ visible, appear, dismissable, showSpinner
</div>
}
{showSpinnerOverlay &&
<Fade timeout={150} appear in>
<div
css={tw`absolute w-full h-full rounded flex items-center justify-center`}
style={{ background: 'hsla(211, 10%, 53%, 0.25)' }}
>
<Spinner/>
</div>
</Fade>
}
<div css={tw`bg-neutral-800 p-6 rounded shadow-md overflow-y-scroll transition-all duration-150`}>
{children}

View file

@ -1,6 +1,7 @@
import React from 'react';
import Modal, { ModalProps } from '@/components/elements/Modal';
import ModalContext from '@/context/ModalContext';
import isEqual from 'react-fast-compare';
export interface AsModalProps {
visible: boolean;
@ -12,26 +13,34 @@ type ExtendedModalProps = Omit<ModalProps, 'appear' | 'visible' | 'onDismissed'>
interface State {
render: boolean;
visible: boolean;
showSpinnerOverlay: boolean;
modalProps: ExtendedModalProps | undefined;
}
function asModal (modalProps?: ExtendedModalProps) {
type ExtendedComponentType<T> = (C: React.ComponentType<T>) => React.ComponentType<T & AsModalProps>;
// eslint-disable-next-line @typescript-eslint/ban-types
return function <T extends object> (Component: React.ComponentType<T>) {
return class extends React.PureComponent <T & AsModalProps, State> {
function asModal<P extends object> (modalProps?: ExtendedModalProps | ((props: P) => ExtendedModalProps)): ExtendedComponentType<P> {
return function (Component) {
return class extends React.PureComponent <P & AsModalProps, State> {
static displayName = `asModal(${Component.displayName})`;
constructor (props: T & AsModalProps) {
constructor (props: P & AsModalProps) {
super(props);
this.state = {
render: props.visible,
visible: props.visible,
showSpinnerOverlay: modalProps?.showSpinnerOverlay || false,
modalProps: typeof modalProps === 'function' ? modalProps(this.props) : modalProps,
};
}
componentDidUpdate (prevProps: Readonly<T & AsModalProps>) {
componentDidUpdate (prevProps: Readonly<P & AsModalProps>) {
const mapped = typeof modalProps === 'function' ? modalProps(this.props) : modalProps;
if (!isEqual(this.state.modalProps, mapped)) {
// noinspection JSPotentiallyInvalidUsageOfThis
this.setState({ modalProps: mapped });
}
if (prevProps.visible && !this.props.visible) {
// noinspection JSPotentiallyInvalidUsageOfThis
this.setState({ visible: false });
@ -43,7 +52,12 @@ function asModal (modalProps?: ExtendedModalProps) {
dismiss = () => this.setState({ visible: false });
toggleSpinner = (value?: boolean) => this.setState({ showSpinnerOverlay: value || false });
toggleSpinner = (value?: boolean) => this.setState(s => ({
modalProps: {
...s.modalProps,
showSpinnerOverlay: value || false,
},
}));
render () {
return (
@ -58,13 +72,12 @@ function asModal (modalProps?: ExtendedModalProps) {
<Modal
appear
visible={this.state.visible}
showSpinnerOverlay={this.state.showSpinnerOverlay}
onDismissed={() => this.setState({ render: false }, () => {
if (typeof this.props.onModalDismissed === 'function') {
this.props.onModalDismissed();
}
})}
{...modalProps}
{...this.state.modalProps}
>
<Component {...this.props}/>
</Modal>