HookModal.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React, { PropsWithChildren } from 'react';
  2. import ConfirmModal from '../ConfirmModal';
  3. import { get } from 'lodash-es';
  4. import { ConfirmProps } from '../confirm';
  5. import { Motion } from '../../_base/base';
  6. interface HookModalProps{
  7. afterClose: (...args: any[]) => void;
  8. config: ConfirmProps;
  9. motion?: Motion;
  10. }
  11. // eslint-disable-next-line max-len
  12. const HookModal = ({ afterClose, config, ...props }: PropsWithChildren<HookModalProps>, ref: React.RefObject<{ destroy: () => void; update: (newConfig: ConfirmProps) => void }>) => {
  13. const [innerConfig, setInnerConfig] = React.useState(config);
  14. React.useImperativeHandle(ref, () => ({
  15. destroy: () => {
  16. setInnerConfig(originConfig => ({
  17. ...originConfig,
  18. visible: false,
  19. }));
  20. },
  21. update: newConfig => {
  22. setInnerConfig(originConfig => ({
  23. ...originConfig,
  24. ...newConfig,
  25. }));
  26. },
  27. }));
  28. const { motion } = props;
  29. const mergedMotion =
  30. typeof motion === 'undefined' || motion ?
  31. {
  32. ...(motion as any),
  33. didLeave: (...args: any[]) => {
  34. const didLeave = get(props.motion, 'didLeave');
  35. if (typeof didLeave === 'function') {
  36. didLeave(...args);
  37. }
  38. afterClose();
  39. },
  40. } :
  41. false;
  42. return (
  43. <ConfirmModal
  44. {...innerConfig}
  45. // visible={!visible ? visible : undefined}
  46. motion={mergedMotion}
  47. />
  48. );
  49. };
  50. export default React.forwardRef(HookModal);