From 3389569a457f4b17c6f38a56140f915cdbf13e27 Mon Sep 17 00:00:00 2001 From: Ferit Date: Fri, 23 Oct 2026 17:25:46 +0300 Subject: [PATCH] fix: support closable.disabled for close button --- src/Notification.tsx | 17 +++++++++++++++++ tests/closable.test.tsx | 13 +++++-------- tests/index.test.tsx | 26 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/Notification.tsx b/src/Notification.tsx index 0f68c54..49637c2 100644 --- a/src/Notification.tsx +++ b/src/Notification.tsx @@ -153,11 +153,26 @@ const Notification = React.forwardRef((props, } function onInternalCloseClick(event: React.MouseEvent) { + if (closableConfig.disabled) { + return; + } event.preventDefault(); event.stopPropagation(); onInternalClose(); } + function onInternalClickCapture(event: React.MouseEvent) { + const target = event.target; + if ( + closableConfig.disabled && + target instanceof Element && + target.closest(`.${noticePrefixCls}-close`) + ) { + event.preventDefault(); + event.stopPropagation(); + } + } + const destroy = useEvent(() => { if (hovering) { onInternalMouseLeave(); @@ -266,6 +281,7 @@ const Notification = React.forwardRef((props, })} style={mergedStyle} // Events + onClickCapture={onInternalClickCapture} onClick={onClick} onMouseEnter={onInternalMouseEnter} onMouseLeave={onInternalMouseLeave} @@ -278,6 +294,7 @@ const Notification = React.forwardRef((props, type="button" className={clsx(`${noticePrefixCls}-close`, classNames?.close)} aria-label="Close" + disabled={closableConfig.disabled} {...closeBtnAriaProps} style={styles?.close} onClick={onInternalCloseClick} diff --git a/tests/closable.test.tsx b/tests/closable.test.tsx index 915b88f..29dcb01 100644 --- a/tests/closable.test.tsx +++ b/tests/closable.test.tsx @@ -60,8 +60,7 @@ describe('closable attributes', () => { expect(button).toHaveAttribute('aria-label', 'Dismiss notification'); expect(button).toHaveAttribute('role', 'button'); expect(button).toHaveTextContent('Dismiss'); - // Notification currently keeps the close button enabled for disabled configs. - expect(button).not.toBeDisabled(); + expect(button).toBeDisabled(); expect(button).not.toHaveAttribute('title'); expect(button).not.toHaveAttribute('closeIcon'); expect(closableOnClose).not.toHaveBeenCalled(); @@ -72,13 +71,11 @@ describe('closable attributes', () => { act(() => { vi.runAllTimers(); }); - expect(closableOnClose).toHaveBeenCalledTimes(1); - expect(closableOnClose).toHaveBeenCalledWith(); - expect(onClose).toHaveBeenCalledTimes(1); - expect(onClose).toHaveBeenCalledWith(); - expect(onAllRemoved).toHaveBeenCalledTimes(1); + expect(closableOnClose).not.toHaveBeenCalled(); + expect(onClose).not.toHaveBeenCalled(); + expect(onAllRemoved).not.toHaveBeenCalled(); expect(onClick).not.toHaveBeenCalled(); - expect(document.querySelector('.rc-notification-notice')).toBeNull(); + expect(document.querySelector('.rc-notification-notice')).not.toBeNull(); }); it('replaces default closable attributes with the notice config', () => { diff --git a/tests/index.test.tsx b/tests/index.test.tsx index ac34c84..8347d89 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -1213,6 +1213,32 @@ describe('Notification.Basic', () => { ).toEqual('close'); }); + it('Support disabled in closable', () => { + let closeCount = 0; + const { instance } = renderDemo({ + closable: { + disabled: true, + closeIcon: 'CloseBtn', + }, + }); + + act(() => { + instance.open({ + description:

1

, + duration: 0, + onClose: () => { + closeCount += 1; + }, + }); + }); + + const closeBtn = document.querySelector('.rc-notification-notice-close') as HTMLButtonElement; + expect(closeBtn.disabled).toBe(true); + + fireEvent.click(closeBtn); + expect(closeCount).toEqual(0); + }); + describe('showProgress', () => { it('show with progress', () => { const { instance } = renderDemo({