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'; ApiKeyModal.displayName = 'ApiKeyModal';
export default asModal({ export default asModal<Props>({
closeOnEscape: false, closeOnEscape: false,
closeOnBackground: false, closeOnBackground: false,
})(ApiKeyModal); })(ApiKeyModal);

View file

@ -1,4 +1,4 @@
import React, { useContext, useEffect } from 'react'; import React, { useContext } from 'react';
import tw from 'twin.macro'; import tw from 'twin.macro';
import Button from '@/components/elements/Button'; import Button from '@/components/elements/Button';
import asModal from '@/hoc/asModal'; import asModal from '@/hoc/asModal';
@ -12,12 +12,8 @@ type Props = {
showSpinnerOverlay?: boolean; showSpinnerOverlay?: boolean;
}; };
const ConfirmationModal = ({ title, children, buttonText, onConfirmed, showSpinnerOverlay }: Props) => { const ConfirmationModal = ({ title, children, buttonText, onConfirmed }: Props) => {
const { dismiss, toggleSpinner } = useContext(ModalContext); const { dismiss } = useContext(ModalContext);
useEffect(() => {
toggleSpinner(showSpinnerOverlay);
}, [ showSpinnerOverlay ]);
return ( return (
<> <>
@ -37,4 +33,6 @@ const ConfirmationModal = ({ title, children, buttonText, onConfirmed, showSpinn
ConfirmationModal.displayName = 'ConfirmationModal'; 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> </div>
} }
{showSpinnerOverlay && {showSpinnerOverlay &&
<Fade timeout={150} appear in>
<div <div
css={tw`absolute w-full h-full rounded flex items-center justify-center`} css={tw`absolute w-full h-full rounded flex items-center justify-center`}
style={{ background: 'hsla(211, 10%, 53%, 0.25)' }} style={{ background: 'hsla(211, 10%, 53%, 0.25)' }}
> >
<Spinner/> <Spinner/>
</div> </div>
</Fade>
} }
<div css={tw`bg-neutral-800 p-6 rounded shadow-md overflow-y-scroll transition-all duration-150`}> <div css={tw`bg-neutral-800 p-6 rounded shadow-md overflow-y-scroll transition-all duration-150`}>
{children} {children}

View file

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