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(Dropdown): opening sub menus in dropdown menu with using keyboard #1298

Merged
merged 3 commits into from
Feb 2, 2024
Merged
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
24 changes: 23 additions & 1 deletion src/components/DropdownMenu/DropdownMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import React from 'react';
import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import {DropdownMenu} from './DropdownMenu';
import {DropdownMenu, DropdownMenuItem} from './DropdownMenu';

export const optionsWithSubItems: DropdownMenuItem<unknown>[] = [
{text: 'Edit', action: () => {}},
{text: 'Other', items: [{action: () => {}, text: 'Submenu'}]},
];

test('do not trigger `onOpenToggle` on mount', () => {
const onOpenToggle = jest.fn();
Expand All @@ -30,3 +35,20 @@ test('should not trigger on control disable', async () => {

expect(onOpenToggle).not.toHaveBeenCalled();
});

describe('submenu', () => {
test('should open submenu if it presses enter on submenu toggle', async () => {
render(<DropdownMenu items={optionsWithSubItems} />);

const switcher = screen.getByRole('button');
await userEvent.click(switcher);

expect(screen.getByText('Other')).toBeVisible();

await userEvent.keyboard('[ArrowDown]');
await userEvent.keyboard('[ArrowDown]');
await userEvent.keyboard('[Enter]');

expect(screen.getByText('Submenu')).toBeVisible();
});
});
7 changes: 6 additions & 1 deletion src/components/DropdownMenu/DropdownMenuPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ export const DropdownMenuPopup = <T,>({
case 'Enter':
case ' ': {
const activeItem = items[activeItemIndex];
if (activeItem) {
const isSubmenuToggleActive = activeItem?.items;

if (isSubmenu || isSubmenuToggleActive) {
event.stopPropagation();
event.preventDefault();
}

if (activeItem) {
handleSelect(activeItem, event);
}

Expand Down
Loading