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(MenuTrigger): closeOnSelect for keyboard users #575

Closed
wants to merge 7 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
4 changes: 4 additions & 0 deletions src/components/ListItemBase/ListItemBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ const ListItemBase = (props: Props, providedRef: RefOrCallbackRef) => {
if (ref.current === document.activeElement || event.key === KEYS.TAB_KEY) {
pressProps.onKeyDown(event);
}

if (event.key === KEYS.SPACE_KEY) {
ref.current.click();
}
},
};

Expand Down
32 changes: 32 additions & 0 deletions src/components/MenuTrigger/MenuTrigger.unit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,38 @@ describe('<MenuTrigger /> - React Testing Library', () => {
expect(await screen.findByRole('menuitemradio', { name: 'One' })).toHaveFocus();
});

it('should close the menu when closeOnSelect is true via Keyboard', async () => {
const user = userEvent.setup();

render(<MenuTrigger {...defaultProps} closeOnSelect={true} />);

await openMenu(user, screen);

screen.getByRole('menuitemradio', { name: 'One' }).focus();

await user.keyboard('{enter}');

await waitFor(() => {
expect(screen.queryByRole('menu', { name: 'Single Menu' })).not.toBeInTheDocument();
});
});

it('should not close the menu when closeOnSelect is false via Keyboard', async () => {
const user = userEvent.setup();

render(<MenuTrigger {...defaultProps} closeOnSelect={false} />);

await openMenu(user, screen);

screen.getByRole('menuitemradio', { name: 'One' }).focus();

await user.keyboard('{enter}');

await waitFor(() => {
expect(screen.queryByRole('menu', { name: 'Single Menu' })).toBeInTheDocument();
});
});

it('selects option on Space', async () => {
const user = userEvent.setup();

Expand Down
Loading