-
-
Notifications
You must be signed in to change notification settings - Fork 339
fix: refactor clear icon to button element to make it accessible #971
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import * as React from 'react'; | ||
| import PickerContext from '../context'; | ||
| import { clsx } from 'clsx'; | ||
|
|
||
| export interface ClearIconProps extends React.HtmlHTMLAttributes<HTMLElement> { | ||
| icon?: React.ReactNode; | ||
| onClear: VoidFunction; | ||
| } | ||
|
|
||
| export default function ClearIcon({ icon, onClear, ...restProps }: ClearIconProps) { | ||
| const { prefixCls, classNames, styles, locale } = React.useContext(PickerContext); | ||
|
|
||
| return ( | ||
| <button | ||
| {...restProps} | ||
| type="button" | ||
| aria-label={locale.clear} | ||
|
Contributor
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. |
||
| className={clsx(`${prefixCls}-clear`, classNames.suffix)} | ||
| style={styles.suffix} | ||
| onMouseDown={(e) => { | ||
| e.preventDefault(); | ||
| }} | ||
| onClick={(e) => { | ||
| e.stopPropagation(); | ||
| onClear(); | ||
| }} | ||
| > | ||
| {icon} | ||
| </button> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -388,6 +388,7 @@ function Picker<DateType extends object = any>( | |
| const onSelectorClear = () => { | ||
| triggerSubmitChange(null); | ||
| triggerOpen(false, { force: true }); | ||
| selectorRef.current.focus(); | ||
|
Contributor
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.
Comment on lines
388
to
+391
Contributor
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. 清除后立即 在 Line 391 调用 建议修复(示例)+ const skipOpenOnNextFocusRef = React.useRef(false);
const onSelectorClear = () => {
triggerSubmitChange(null);
triggerOpen(false, { force: true });
+ skipOpenOnNextFocusRef.current = true;
selectorRef.current.focus();
};
const onSelectorFocus: SelectorProps['onFocus'] = (event) => {
lastOperation('input');
+ if (skipOpenOnNextFocusRef.current) {
+ skipOpenOnNextFocusRef.current = false;
+ onSharedFocus(event);
+ return;
+ }
triggerOpen(true, {
inherit: true,
});
onSharedFocus(event);
};Also applies to: 575-581 🤖 Prompt for AI Agents
Member
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. 看下这个。
Contributor
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. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
fd -t f 'useOpen' --exec cat {}Length of output: 13105 🏁 Script executed: #!/bin/bash
fd -t f 'useDelayState' --exec cat {}Length of output: 1508
根本原因
// useDelayState
const updateValue = (next, immediately?) => {
nextValueRef.current = next;
if (next || immediately) {
doUpdate(); // ← 同步调用,但 setState() 是异步生效的
} else {
rafRef.current = raf(doUpdate);
}
};时序问题 因此在 建议修复 使用一个 ref 旗标在 + const skipOpenOnNextFocusRef = React.useRef(false);
const onSelectorClear = () => {
triggerSubmitChange(null);
triggerOpen(false, { force: true });
+ skipOpenOnNextFocusRef.current = true;
selectorRef.current.focus();
};
const onSelectorFocus: SelectorProps['onFocus'] = (event) => {
lastOperation('input');
+ if (skipOpenOnNextFocusRef.current) {
+ skipOpenOnNextFocusRef.current = false;
+ onSharedFocus(event);
+ return;
+ }
triggerOpen(true, {
inherit: true,
});
onSharedFocus(event);
};这样可以在清除后正确聚焦输入框,同时不会导致弹层重新打开。 🧬 Code Graph Analysis ResultsRelevant snippets (for understanding
|
||
| }; | ||
|
|
||
| // ======================== Hover ========================= | ||
|
|
||
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.
Since the
ClearIconcomponent renders a native<button>element and forwards...restPropsto it, the props interface should extendReact.ButtonHTMLAttributes<HTMLButtonElement>instead ofReact.HtmlHTMLAttributes<HTMLElement>. This provides accurate type safety for button-specific attributes.