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; interface State { render: boolean; visible: boolean; showSpinnerOverlay: boolean; } function asModal (modalProps?: ExtendedModalProps) { // eslint-disable-next-line @typescript-eslint/ban-types return function (Component: React.ComponentType) { return class extends React.PureComponent { static displayName = `asModal(${Component.displayName})`; constructor (props: T & AsModalProps) { super(props); this.state = { render: props.visible, visible: props.visible, showSpinnerOverlay: modalProps?.showSpinnerOverlay || false, }; } componentDidUpdate (prevProps: Readonly) { 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 }); toggleSpinner = (value?: boolean) => this.setState({ showSpinnerOverlay: value || false }); render () { return ( { this.state.render ? this.setState({ render: false }, () => { if (typeof this.props.onModalDismissed === 'function') { this.props.onModalDismissed(); } })} {...modalProps} > : null } ); } }; }; } export default asModal;