From 07c0190b438f139dc7f7ed33553beb3337627c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=AC=A2?= Date: Wed, 23 Jul 2025 16:42:03 +0800 Subject: [PATCH 1/6] feat: Merge 'onClose' into Closable --- src/Notifications.tsx | 6 ++- src/hooks/useNotification.tsx | 4 +- src/interface.ts | 4 +- tests/index.test.tsx | 98 +++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/src/Notifications.tsx b/src/Notifications.tsx index f990764..eb0bec5 100644 --- a/src/Notifications.tsx +++ b/src/Notifications.tsx @@ -45,7 +45,11 @@ const Notifications = React.forwardRef((pr const onNoticeClose = (key: React.Key) => { // Trigger close event const config = configList.find((item) => item.key === key); - config?.onClose?.(); + const onClose = + typeof config?.closable === 'object' && config?.closable !== null + ? config.closable?.onClose + : config?.onClose; + onClose?.(); setConfigList((list) => list.filter((item) => item.key !== key)); }; diff --git a/src/hooks/useNotification.tsx b/src/hooks/useNotification.tsx index a7940ff..2072839 100644 --- a/src/hooks/useNotification.tsx +++ b/src/hooks/useNotification.tsx @@ -15,7 +15,9 @@ export interface NotificationConfig { getContainer?: () => HTMLElement | ShadowRoot; motion?: CSSMotionProps | ((placement: Placement) => CSSMotionProps); - closable?: boolean | ({ closeIcon?: React.ReactNode } & React.AriaAttributes); + closable?: + | boolean + | ({ closeIcon?: React.ReactNode; onClose?: VoidFunction } & React.AriaAttributes); maxCount?: number; duration?: number; showProgress?: boolean; diff --git a/src/interface.ts b/src/interface.ts index ce36f2e..1a5727d 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -10,7 +10,9 @@ export interface NoticeConfig { showProgress?: boolean; pauseOnHover?: boolean; - closable?: boolean | ({ closeIcon?: React.ReactNode } & React.AriaAttributes); + closable?: + | boolean + | ({ closeIcon?: React.ReactNode; onClose?: VoidFunction } & React.AriaAttributes); className?: string; style?: React.CSSProperties; classNames?: { diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 3fb2e60..7df7ee7 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -778,6 +778,104 @@ describe('Notification.Basic', () => { unmount(); }); + describe.only('onClose and closable.onClose', () => { + it('onClose', () => { + const onClose = vi.fn(); + const Demo = () => { + const [api, holder] = useNotification(); + return ( + <> +