Skip to content
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

Fix submenu and menu group focus #669

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,20 +378,27 @@ const Menu = React.forwardRef<MenuRef, MenuProps>((props, ref) => {
setMergedActiveKey(undefined);
});

useImperativeHandle(ref, () => ({
list: containerRef.current,
focus: options => {
const shouldFocusKey =
mergedActiveKey ?? childList.find(node => !node.props.disabled)?.key;
if (shouldFocusKey) {
containerRef.current
?.querySelector<HTMLLIElement>(
`li[data-menu-id='${getMenuId(uuid, shouldFocusKey as string)}']`,
)
?.focus?.(options);
}
},
}));
useImperativeHandle(ref, () => {
return {
list: containerRef.current,
focus: options => {
const shouldFocusKey =
mergedActiveKey ??
childList.find(
node =>
!node.props.disabled &&
(node.props.children.length || node.props.children.type),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(node.props.children.length || node.props.children.type),
(node.props.children.length || node.props.children.type === 'group'),

Copy link
Contributor

@Ke1sy Ke1sy Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afc163 Thank you for suggestion, but after some testing we found out with @yhy-1 that current solution doesn't work properly as the focus moves to the group (which is not focusable), instead of the first focusable child of this group. Currenty I am working on improvements to fix it. Will push my changes soon.

)?.key;
if (shouldFocusKey) {
containerRef.current
?.querySelector<HTMLElement>(
`[data-menu-id='${getMenuId(uuid, shouldFocusKey as string)}']`,
)
?.focus?.(options);
}
},
};
});
Comment on lines +381 to +401
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
useImperativeHandle(ref, () => {
return {
list: containerRef.current,
focus: options => {
const shouldFocusKey =
mergedActiveKey ??
childList.find(
node =>
!node.props.disabled &&
(node.props.children.length || node.props.children.type),
)?.key;
if (shouldFocusKey) {
containerRef.current
?.querySelector<HTMLElement>(
`[data-menu-id='${getMenuId(uuid, shouldFocusKey as string)}']`,
)
?.focus?.(options);
}
},
};
});
useImperativeHandle(ref, () => ({
list: containerRef.current,
focus: options => {
const shouldFocusKey =
mergedActiveKey ??
childList.find(
node =>
!node.props.disabled &&
(node.props.children.length || node.props.children.type),
)?.key;
if (shouldFocusKey) {
containerRef.current
?.querySelector<HTMLElement>(
`[data-menu-id='${getMenuId(uuid, shouldFocusKey as string)}']`,
)
?.focus?.(options);
}
},
}));


// ======================== Select ========================
// >>>>> Select keys
Expand Down
47 changes: 44 additions & 3 deletions tests/SubMenu.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable no-undef */
import { screen } from '@testing-library/dom';
import { act, fireEvent, render } from '@testing-library/react';
import { resetWarned } from 'rc-util/lib/warning';
import React from 'react';
import Menu, { MenuItem, SubMenu } from '../src';
import Menu, { MenuItem, MenuRef, SubMenu } from '../src';
import { isActive, last } from './util';

jest.mock('@rc-component/trigger', () => {
Expand Down Expand Up @@ -496,8 +497,48 @@ describe('SubMenu', () => {

fireEvent.mouseEnter(container.querySelector('.rc-menu-submenu-title'));
runAllTimer();
expect((container.querySelector('.rc-menu-submenu-popup') as HTMLElement).style.zIndex).toEqual('100');
expect((container.querySelector('.rc-menu-submenu-popup') as HTMLElement).style.width).toEqual('150px');
expect(
(container.querySelector('.rc-menu-submenu-popup') as HTMLElement).style
.zIndex,
).toEqual('100');
expect(
(container.querySelector('.rc-menu-submenu-popup') as HTMLElement).style
.width,
).toEqual('150px');
});
it('focus menu to first menu item when .focus() is called', async () => {
const ref = React.createRef<MenuRef>();
const items = [
{
key: '1',
label: 'group title',
type: 'group',
},
{
key: '2',
label: 'menu item',
['data-testid']: 'menu-item',
},
];
await act(async () => render(<Menu ref={ref} items={items}></Menu>));
act(() => ref.current.focus());
expect(screen.getByTestId('menu-item')).toHaveClass('rc-menu-item-active');
});
it('focus menu to first submenu when .focus() is called', async () => {
const ref = React.createRef<MenuRef>();
await act(async () =>
render(
<Menu ref={ref}>
<SubMenu data-testid="sub-menu" key="s1" title="submenu1">
<MenuItem key="s1-1">1</MenuItem>
</SubMenu>
</Menu>,
),
);
act(() => ref.current.focus());
expect(screen.getByTestId('sub-menu')).toHaveClass(
'rc-menu-submenu-active',
);
});
});
/* eslint-enable */
Loading