123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import React, { PropsWithChildren } from 'react';
- import ConfirmModal from '../ConfirmModal';
- import { get } from 'lodash';
- import { ConfirmProps } from '../confirm';
- import { Motion } from '../../_base/base';
- interface HookModalProps {
- afterClose: (...args: any[]) => void;
- config: ConfirmProps;
- motion?: Motion;
- }
- export interface HookModalRef {
- destroy: () => void;
- update: (newConfig: ConfirmProps) => void
- }
- // eslint-disable-next-line max-len
- const HookModal = ({ afterClose, config, ...props }: PropsWithChildren<HookModalProps>, ref: React.Ref<any>) => {
- const [innerConfig, setInnerConfig] = React.useState(config);
- React.useImperativeHandle(ref, () => ({
- destroy: () => {
- setInnerConfig(originConfig => ({
- ...originConfig,
- visible: false,
- }));
- },
- update: newConfig => {
- setInnerConfig(originConfig => ({
- ...originConfig,
- ...newConfig,
- }));
- },
- }));
- const { motion } = props;
- /* istanbul ignore next */
- const mergedMotion =
- typeof motion === 'undefined' || motion ?
- {
- ...(motion as any),
- didLeave: (...args: any[]) => {
- const didLeave = get(props.motion, 'didLeave');
- if (typeof didLeave === 'function') {
- didLeave(...args);
- }
- afterClose();
- },
- } :
- false;
- return (
- <ConfirmModal
- {...innerConfig}
- // visible={!visible ? visible : undefined}
- motion={mergedMotion}
- />
- );
- };
- export default React.forwardRef<HookModalRef, HookModalProps>(HookModal);
|