Repository navigation
fix: delegate DOM focus directly to anchor link inside menu item #898
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 |
|---|---|---|
|
|
@@ -201,6 +201,13 @@ const InternalMenuItem = React.forwardRef((props: MenuItemProps, ref: React.Ref< | |
| const onInternalFocus: React.FocusEventHandler<HTMLLIElement> = e => { | ||
| onActive(eventKey); | ||
| onFocus?.(e); | ||
|
|
||
| if (e.target === elementRef.current) { | ||
| const link = elementRef.current?.querySelector<HTMLAnchorElement>('a[href]'); | ||
| if (link) { | ||
| link.focus(); | ||
|
Comment on lines
+205
to
+208
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: sed -n '175,235p' src/MenuItem.tsx
rg -n 'onInternalFocus|onFocus|focus\(' src/MenuItem.tsx tests/Focus.spec.tsxRepository: react-component/menu Length of output: 2722 🏁 Script executed: printf '%s\n' '--- MenuItem props ---'; sed -n '65,115p' src/MenuItem.tsx; printf '%s\n' '--- focus handler and render wiring ---'; sed -n '190,270p' src/MenuItem.tsx; printf '%s\n' '--- focused tests ---'; sed -n '1,115p' tests/Focus.spec.tsx; sed -n '200,235p' tests/Focus.spec.tsxRepository: react-component/menu Length of output: 7421 避免一次焦点转移触发两次 当 🐛 建议修复 const onInternalFocus: React.FocusEventHandler<HTMLLIElement> = e => {
onActive(eventKey);
- onFocus?.(e);
if (e.target === elementRef.current) {
const link = elementRef.current?.querySelector<HTMLAnchorElement>('a[href]');
if (link) {
link.focus();
+ return;
}
}
+
+ onFocus?.(e);
};🤖 Prompt for AI AgentsThere 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. [P2] Preserve the caller's scroll decision when redirecting item focus The extra In a real Chrome page with the menu below the viewport (top 1600px), starting at Please preserve the initial focus operation's scroll decision during this redirection (for example, prevent scrolling on the secondary focus) and add coverage for direct item focus with |
||
| } | ||
| } | ||
| }; | ||
|
|
||
| // ============================ Render ============================ | ||
|
|
||
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.
[P2] Handle native anchor Enter without also invoking legacy keyboard activation
Changing initial/ref focus from the
lito its anchor exposes the native Enter click toMenuItem.onInternalKeyDown, which already calls bothonClickandonItemClick. Chrome then dispatches the anchor's nativeclick, invoking both callbacks again. Withmultiple, one Enter selects and immediately deselects the same item.Reproduction on this exact head and merge preview: render
<Menu multiple ref={ref} items={[{ key: 'first', label: <a href="#destination">Link</a> }]} onClick={spy} />, callref.current.focus(), and press Enter with a real keyboard driver. The spy recordskeydown, thenclick, and no item remains selected. Base records onekeydownand leaves one selected item (but has the original missing navigation bug).The same double-activation already exists on base after arrow navigation focuses an anchor; this PR additionally exposes it on first/ref focus. Please let native anchor activation happen once while retaining legacy Enter activation for non-link items, and cover both paths plus multiple selection with native-key tests.