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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import useActive from './hooks/useActive'; | |
| import useDirectionStyle from './hooks/useDirectionStyle'; | ||
| import Icon from './Icon'; | ||
| import type { MenuInfo, ItemData, MenuItemType } from './interface'; | ||
| import { getFocusTarget } from './utils/commonUtil'; | ||
| import { warnItemProp } from './utils/warnUtil'; | ||
|
|
||
| export interface MenuItemProps | ||
|
|
@@ -186,6 +187,11 @@ const InternalMenuItem = React.forwardRef((props: MenuItemProps, ref: React.Ref< | |
| onKeyDown?.(e); | ||
|
|
||
| if (!mergedDisabled && e.which === KeyCode.ENTER) { | ||
| const target = e.target as HTMLElement | null; | ||
| if (target?.closest?.('a[href]')) { | ||
| return; | ||
| } | ||
|
|
||
| const info = getEventInfo(e); | ||
|
|
||
| // Legacy. Key will also trigger click event | ||
|
|
@@ -199,6 +205,14 @@ const InternalMenuItem = React.forwardRef((props: MenuItemProps, ref: React.Ref< | |
| * We should manually trigger an active | ||
| */ | ||
| const onInternalFocus: React.FocusEventHandler<HTMLLIElement> = e => { | ||
| if (e.target === elementRef.current) { | ||
| const link = getFocusTarget(elementRef.current); | ||
| if (link && link !== elementRef.current) { | ||
| link.focus({ preventScroll: true }); | ||
| return; | ||
|
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. 仅在焦点成功转移后提前返回。 如果菜单项包含 此处仍无条件返回,因此原本应执行的 请确认焦点已离开当前
|
||
| } | ||
| } | ||
|
|
||
| onActive(eventKey); | ||
| onFocus?.(e); | ||
| }; | ||
|
|
||
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.