2020-08-18 04:35:11 +00:00
|
|
|
import React from 'react';
|
2021-05-04 04:26:09 +00:00
|
|
|
import PortaledModal, { ModalProps } from '@/components/elements/Modal';
|
2021-05-16 19:35:49 +00:00
|
|
|
import ModalContext, { ModalContextValues } from '@/context/ModalContext';
|
2021-05-29 17:58:24 +00:00
|
|
|
import isEqual from 'react-fast-compare';
|
2020-08-18 04:35:11 +00:00
|
|
|
|
|
|
|
export interface AsModalProps {
|
|
|
|
visible: boolean;
|
|
|
|
onModalDismissed?: () => void;
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:35:49 +00:00
|
|
|
export type SettableModalProps = Omit<ModalProps, 'appear' | 'visible' | 'onDismissed'>;
|
2020-08-18 04:35:11 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
render: boolean;
|
|
|
|
visible: boolean;
|
2021-05-16 19:35:49 +00:00
|
|
|
propOverrides: Partial<SettableModalProps>;
|
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
|
2022-06-26 19:13:52 +00:00
|
|
|
function asModal<P extends {}>(
|
|
|
|
modalProps?: SettableModalProps | ((props: P) => SettableModalProps)
|
|
|
|
): ExtendedComponentType<P> {
|
2020-08-18 05:04:24 +00:00
|
|
|
return function (Component) {
|
2022-06-26 19:13:52 +00:00
|
|
|
return class extends React.PureComponent<P & AsModalProps, State> {
|
2020-08-18 04:35:11 +00:00
|
|
|
static displayName = `asModal(${Component.displayName})`;
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
constructor(props: P & AsModalProps) {
|
2020-08-18 04:35:11 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
render: props.visible,
|
|
|
|
visible: props.visible,
|
2021-05-16 19:35:49 +00:00
|
|
|
propOverrides: {},
|
2020-08-18 04:35:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
get computedModalProps(): Readonly<SettableModalProps & { visible: boolean }> {
|
2020-10-25 22:47:50 +00:00
|
|
|
return {
|
|
|
|
...(typeof modalProps === 'function' ? modalProps(this.props) : modalProps),
|
2021-05-16 19:35:49 +00:00
|
|
|
...this.state.propOverrides,
|
|
|
|
visible: this.state.visible,
|
2020-10-25 22:47:50 +00:00
|
|
|
};
|
|
|
|
}
|
2020-08-18 05:04:24 +00:00
|
|
|
|
2021-05-16 19:35:49 +00:00
|
|
|
/**
|
|
|
|
* @this {React.PureComponent<P & AsModalProps, State>}
|
|
|
|
*/
|
2022-06-26 19:13:52 +00:00
|
|
|
componentDidUpdate(prevProps: Readonly<P & AsModalProps>, prevState: Readonly<State>) {
|
2020-08-18 04:35:11 +00:00
|
|
|
if (prevProps.visible && !this.props.visible) {
|
2021-09-11 20:24:57 +00:00
|
|
|
this.setState({ visible: false, propOverrides: {} });
|
2020-08-18 04:35:11 +00:00
|
|
|
} else if (!prevProps.visible && this.props.visible) {
|
|
|
|
this.setState({ render: true, visible: true });
|
|
|
|
}
|
2021-05-29 17:58:24 +00:00
|
|
|
if (!this.state.render && !isEqual(prevState.propOverrides, this.state.propOverrides)) {
|
2021-05-16 19:35:49 +00:00
|
|
|
this.setState({ propOverrides: {} });
|
|
|
|
}
|
2020-08-18 04:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dismiss = () => this.setState({ visible: false });
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
setPropOverrides: ModalContextValues['setPropOverrides'] = (value) =>
|
|
|
|
this.setState((state) => ({
|
|
|
|
propOverrides: !value ? {} : typeof value === 'function' ? value(state.propOverrides) : value,
|
|
|
|
}));
|
2020-08-18 04:35:11 +00:00
|
|
|
|
2021-05-16 19:35:49 +00:00
|
|
|
/**
|
|
|
|
* @this {React.PureComponent<P & AsModalProps, State>}
|
|
|
|
*/
|
2022-06-26 19:13:52 +00:00
|
|
|
render() {
|
2021-05-16 19:35:49 +00:00
|
|
|
if (!this.state.render) return null;
|
|
|
|
|
2020-08-18 04:35:11 +00:00
|
|
|
return (
|
2021-05-16 19:35:49 +00:00
|
|
|
<PortaledModal
|
|
|
|
appear
|
2022-06-26 19:13:52 +00:00
|
|
|
onDismissed={() =>
|
|
|
|
this.setState({ render: false }, () => {
|
|
|
|
if (typeof this.props.onModalDismissed === 'function') {
|
|
|
|
this.props.onModalDismissed();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-05-16 19:35:49 +00:00
|
|
|
{...this.computedModalProps}
|
|
|
|
>
|
|
|
|
<ModalContext.Provider
|
|
|
|
value={{
|
|
|
|
dismiss: this.dismiss.bind(this),
|
|
|
|
setPropOverrides: this.setPropOverrides.bind(this),
|
|
|
|
}}
|
2020-10-25 22:47:50 +00:00
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Component {...this.props} />
|
2021-05-16 19:35:49 +00:00
|
|
|
</ModalContext.Provider>
|
|
|
|
</PortaledModal>
|
2020-08-18 04:35:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default asModal;
|