Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ const Menu = React.forwardRef<MenuRef, MenuProps>((props, ref) => {
onInternalOpenChange(key, nextOpen);
};

const triggerAccessibilityClose = () => {
if (mergedOpenKeys.length) {
triggerOpenKeys(EMPTY_LIST, true);
}
};

const onInternalKeyDown = useAccessibility(
internalMode,
mergedActiveKey,
Expand All @@ -518,6 +524,7 @@ const Menu = React.forwardRef<MenuRef, MenuProps>((props, ref) => {

setMergedActiveKey,
triggerAccessibilityOpen,
triggerAccessibilityClose,

onKeyDown,
);
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/useAccessibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export function useAccessibility<T extends HTMLElement>(

triggerActiveKey: (key: string) => void,
triggerAccessibilityOpen: (key: string, open?: boolean) => void,
triggerAccessibilityClose: () => void,

originOnKeyDown?: React.KeyboardEventHandler<T>,
): React.KeyboardEventHandler<T> {
Expand All @@ -196,6 +197,12 @@ export function useAccessibility<T extends HTMLElement>(
return e => {
const { which } = e;

if (which === ESC && mode !== 'inline' && e.target === containerRef.current) {
triggerAccessibilityClose();
originOnKeyDown?.(e);
return;
}

if ([...ArrowKeys, ENTER, ESC, HOME, END].includes(which)) {
const keys = getKeys();

Expand Down
24 changes: 23 additions & 1 deletion tests/SubMenu.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-undef */
import { act, fireEvent, render } from '@testing-library/react';
import { resetWarned } from '@rc-component/util';
import { KeyCode, resetWarned } from '@rc-component/util';
import React from 'react';
import Menu, { MenuItem, SubMenu } from '../src';
import { isActive, last } from './util';
Expand Down Expand Up @@ -203,6 +203,28 @@ describe('SubMenu', () => {
});
expect(container.querySelector('.rc-menu-submenu-open')).toBeFalsy();
});

it('closes open submenus when Escape is pressed on the root menu', () => {
const onOpenChange = jest.fn();
const { container } = render(
createMenu({
triggerSubMenuAction: 'click',
onOpenChange,
}),
);

fireEvent.click(container.querySelector('.rc-menu-submenu-title'));
runAllTimer();
expect(container.querySelector('.rc-menu-submenu-open')).toBeTruthy();

const rootMenu = container.querySelector<HTMLElement>('.rc-menu-root');
rootMenu.focus();
fireEvent.keyDown(rootMenu, { keyCode: KeyCode.ESC, which: KeyCode.ESC });
runAllTimer();

expect(onOpenChange).toHaveBeenLastCalledWith([]);
expect(container.querySelector('.rc-menu-submenu-open')).toBeFalsy();
});
});

it('fires openChange event', () => {
Expand Down