2020-08-18 04:35:11 +00:00
|
|
|
import React from 'react';
|
|
|
|
import Modal, { ModalProps } from '@/components/elements/Modal';
|
|
|
|
import ModalContext from '@/context/ModalContext';
|
|
|
|
|
|
|
|
export interface AsModalProps {
|
|
|
|
visible: boolean;
|
|
|
|
onModalDismissed?: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExtendedModalProps = Omit<ModalProps, 'appear' | 'visible' | 'onDismissed'>;
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
render: boolean;
|
|
|
|
visible: boolean;
|
2020-10-25 22:47:50 +00:00
|
|
|
showSpinnerOverlay?: boolean;
|
2020-08-18 04:35:11 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 05:04:24 +00:00
|
|
|
type ExtendedComponentType<T> = (C: React.ComponentType<T>) => React.ComponentType<T & AsModalProps>;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
function asModal<P extends object> (modalProps?: ExtendedModalProps | ((props: P) => ExtendedModalProps)): ExtendedComponentType<P> {
|
|
|
|
return function (Component) {
|
|
|
|
return class extends React.PureComponent <P & AsModalProps, State> {
|
2020-08-18 04:35:11 +00:00
|
|
|
static displayName = `asModal(${Component.displayName})`;
|
|
|
|
|
2020-08-18 05:04:24 +00:00
|
|
|
constructor (props: P & AsModalProps) {
|
2020-08-18 04:35:11 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
render: props.visible,
|
|
|
|
visible: props.visible,
|
2020-10-25 22:47:50 +00:00
|
|
|
showSpinnerOverlay: undefined,
|
2020-08-18 04:35:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-25 22:47:50 +00:00
|
|
|
get modalProps () {
|
|
|
|
return {
|
|
|
|
...(typeof modalProps === 'function' ? modalProps(this.props) : modalProps),
|
|
|
|
showSpinnerOverlay: this.state.showSpinnerOverlay,
|
|
|
|
};
|
|
|
|
}
|
2020-08-18 05:04:24 +00:00
|
|
|
|
2020-10-25 22:47:50 +00:00
|
|
|
componentDidUpdate (prevProps: Readonly<P & AsModalProps>) {
|
2020-08-18 04:35:11 +00:00
|
|
|
if (prevProps.visible && !this.props.visible) {
|
|
|
|
// noinspection JSPotentiallyInvalidUsageOfThis
|
2020-11-03 04:03:28 +00:00
|
|
|
this.setState({ visible: false, showSpinnerOverlay: false });
|
2020-08-18 04:35:11 +00:00
|
|
|
} else if (!prevProps.visible && this.props.visible) {
|
|
|
|
// noinspection JSPotentiallyInvalidUsageOfThis
|
|
|
|
this.setState({ render: true, visible: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dismiss = () => this.setState({ visible: false });
|
|
|
|
|
2020-10-25 22:47:50 +00:00
|
|
|
toggleSpinner = (value?: boolean) => this.setState({ showSpinnerOverlay: value });
|
2020-08-18 04:35:11 +00:00
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
2020-10-25 22:47:50 +00:00
|
|
|
this.state.render ?
|
|
|
|
<Modal
|
|
|
|
appear
|
|
|
|
visible={this.state.visible}
|
|
|
|
onDismissed={() => this.setState({ render: false }, () => {
|
|
|
|
if (typeof this.props.onModalDismissed === 'function') {
|
|
|
|
this.props.onModalDismissed();
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
{...this.modalProps}
|
|
|
|
>
|
|
|
|
<ModalContext.Provider
|
|
|
|
value={{
|
|
|
|
dismiss: this.dismiss.bind(this),
|
|
|
|
toggleSpinner: this.toggleSpinner.bind(this),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Component {...this.props}/>
|
|
|
|
</ModalContext.Provider>
|
|
|
|
</Modal>
|
|
|
|
:
|
|
|
|
null
|
2020-08-18 04:35:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default asModal;
|