2020-08-18 06:35:11 +02:00
|
|
|
import React from 'react';
|
|
|
|
import Modal, { ModalProps } from '@/components/elements/Modal';
|
|
|
|
import ModalContext from '@/context/ModalContext';
|
2020-08-18 07:04:24 +02:00
|
|
|
import isEqual from 'react-fast-compare';
|
2020-08-18 06:35:11 +02:00
|
|
|
|
|
|
|
export interface AsModalProps {
|
|
|
|
visible: boolean;
|
|
|
|
onModalDismissed?: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExtendedModalProps = Omit<ModalProps, 'appear' | 'visible' | 'onDismissed'>;
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
render: boolean;
|
|
|
|
visible: boolean;
|
2020-08-18 07:04:24 +02:00
|
|
|
modalProps: ExtendedModalProps | undefined;
|
2020-08-18 06:35:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 07:04:24 +02: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 06:35:11 +02:00
|
|
|
static displayName = `asModal(${Component.displayName})`;
|
|
|
|
|
2020-08-18 07:04:24 +02:00
|
|
|
constructor (props: P & AsModalProps) {
|
2020-08-18 06:35:11 +02:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
render: props.visible,
|
|
|
|
visible: props.visible,
|
2020-08-18 07:04:24 +02:00
|
|
|
modalProps: typeof modalProps === 'function' ? modalProps(this.props) : modalProps,
|
2020-08-18 06:35:11 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-18 07:04:24 +02:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:35:11 +02:00
|
|
|
if (prevProps.visible && !this.props.visible) {
|
|
|
|
// noinspection JSPotentiallyInvalidUsageOfThis
|
|
|
|
this.setState({ visible: false });
|
|
|
|
} else if (!prevProps.visible && this.props.visible) {
|
|
|
|
// noinspection JSPotentiallyInvalidUsageOfThis
|
|
|
|
this.setState({ render: true, visible: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dismiss = () => this.setState({ visible: false });
|
|
|
|
|
2020-08-18 07:04:24 +02:00
|
|
|
toggleSpinner = (value?: boolean) => this.setState(s => ({
|
|
|
|
modalProps: {
|
|
|
|
...s.modalProps,
|
|
|
|
showSpinnerOverlay: value || false,
|
|
|
|
},
|
|
|
|
}));
|
2020-08-18 06:35:11 +02:00
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<ModalContext.Provider
|
|
|
|
value={{
|
|
|
|
dismiss: this.dismiss.bind(this),
|
|
|
|
toggleSpinner: this.toggleSpinner.bind(this),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
this.state.render ?
|
|
|
|
<Modal
|
|
|
|
appear
|
|
|
|
visible={this.state.visible}
|
|
|
|
onDismissed={() => this.setState({ render: false }, () => {
|
|
|
|
if (typeof this.props.onModalDismissed === 'function') {
|
|
|
|
this.props.onModalDismissed();
|
|
|
|
}
|
|
|
|
})}
|
2020-08-18 07:04:24 +02:00
|
|
|
{...this.state.modalProps}
|
2020-08-18 06:35:11 +02:00
|
|
|
>
|
|
|
|
<Component {...this.props}/>
|
|
|
|
</Modal>
|
|
|
|
:
|
|
|
|
null
|
|
|
|
}
|
|
|
|
</ModalContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default asModal;
|