Repository navigation
fix: support closable.disabled for close button #419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,11 +153,26 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props, | |
| } | ||
|
|
||
| function onInternalCloseClick(event: React.MouseEvent<HTMLButtonElement>) { | ||
| if (closableConfig.disabled) { | ||
| return; | ||
| } | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| onInternalClose(); | ||
| } | ||
|
|
||
| function onInternalClickCapture(event: React.MouseEvent<HTMLDivElement>) { | ||
| const target = event.target; | ||
| if ( | ||
| closableConfig.disabled && | ||
| target instanceof Element && | ||
| target.closest(`.${noticePrefixCls}-close`) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 只拦截当前通知的关闭按钮。 如果外层通知禁用了关闭按钮,且 请通过 ref 判断目标是否属于当前实例的关闭按钮,而不是仅匹配类名。这样可以继续拦截外层禁用按钮,同时保留内层按钮的关闭行为。
|
||
| ) { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| } | ||
| } | ||
|
|
||
| const destroy = useEvent(() => { | ||
| if (hovering) { | ||
| onInternalMouseLeave(); | ||
|
|
@@ -266,6 +281,7 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props, | |
| })} | ||
| style={mergedStyle} | ||
| // Events | ||
| onClickCapture={onInternalClickCapture} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 保留 这里的显式属性覆盖了 请组合调用方回调和内部捕获处理器,并保留调用方停止传播的效果。
|
||
| onClick={onClick} | ||
| onMouseEnter={onInternalMouseEnter} | ||
| onMouseLeave={onInternalMouseLeave} | ||
|
|
@@ -278,6 +294,7 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props, | |
| type="button" | ||
| className={clsx(`${noticePrefixCls}-close`, classNames?.close)} | ||
| aria-label="Close" | ||
| disabled={closableConfig.disabled} | ||
| {...closeBtnAriaProps} | ||
| style={styles?.close} | ||
| onClick={onInternalCloseClick} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: react-component/notification
Length of output: 4584
🏁 Script executed:
Repository: react-component/notification
Length of output: 6626
🏁 Script executed:
Repository: react-component/notification
Length of output: 7301
🌐 Web query:
React 19 disabled button programmatic dispatchEvent click onClick ancestor bubbling official source💡 Result:
🌐 Web query:
site:github.com/facebook/react v19.2.7 react-dom-bindings getListener.js shouldPreventMouseEvent disabled onClick💡 Result:
在通知根节点拦截禁用关闭按钮的冒泡事件。
原评论中的事件链不准确。React 19 不会调用
disabled<button>的onClick,因此onInternalCloseClick不会处理这个程序化事件。但是,可冒泡的dispatchEvent()仍可能触发通知根节点的onClick。因此,将
stopPropagation()移到onInternalCloseClick的禁用分支之前不能修复问题。请在通知根节点的捕获阶段,针对禁用关闭按钮的事件调用preventDefault()和stopPropagation(),并增加对应的程序化事件测试。建议修改
🤖 Prompt for AI Agents