HookNotice.tsx 887 B

123456789101112131415161718192021222324252627282930313233343536
  1. import React, { useState, useEffect } from 'react';
  2. import Notice from '../notice';
  3. import { NoticeInstance } from '@douyinfe/semi-foundation/notification/notificationFoundation';
  4. export interface HookNoticeProps extends NoticeInstance{
  5. afterClose: (id: string) => void;
  6. }
  7. const HookNotice = ({ afterClose, ...config }: HookNoticeProps, ref: React.Ref<any>) => {
  8. const [visible, setVisible] = useState(true);
  9. const close = () => {
  10. setVisible(false);
  11. };
  12. React.useImperativeHandle(ref, () => ({
  13. close: () => {
  14. setVisible(false);
  15. }
  16. }));
  17. useEffect(() => {
  18. if (!visible) {
  19. afterClose(String(config.id));
  20. }
  21. }, [visible]);
  22. return visible ? (
  23. <Notice
  24. {...config}
  25. onHookClose={close}
  26. />
  27. ) : null;
  28. };
  29. export default React.forwardRef(HookNotice);