Skip to content

Commit 0853d38

Browse files
authored
feat(SideNavMenu): collapse submenu on esc keydown (#5198)
1 parent fc4b6c1 commit 0853d38

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

packages/react/src/components/UIShell/SideNavMenu.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import cx from 'classnames';
1111
import PropTypes from 'prop-types';
1212
import React from 'react';
1313
import SideNavIcon from './SideNavIcon';
14+
import { keys, match } from '../../internal/keyboard';
1415

1516
const { prefix } = settings;
1617

@@ -100,6 +101,12 @@ export class SideNavMenu extends React.Component {
100101
this.setState(state => ({ isExpanded: !state.isExpanded }));
101102
};
102103

104+
handleKeyDown = event => {
105+
if (match(event, keys.Escape)) {
106+
this.setState(() => ({ isExpanded: false }));
107+
}
108+
};
109+
103110
render() {
104111
const {
105112
buttonRef,
@@ -138,7 +145,8 @@ export class SideNavMenu extends React.Component {
138145
[customClassName]: !!customClassName,
139146
});
140147
return (
141-
<li className={className}>
148+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
149+
<li className={className} onKeyDown={this.handleKeyDown}>
142150
<button
143151
aria-haspopup="true"
144152
aria-expanded={isExpanded}

packages/react/src/components/UIShell/__tests__/SideNavMenu-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ describe('SideNavMenu', () => {
4343
expect(wrapper.state('isExpanded')).toBe(true);
4444
});
4545

46+
it('should collapse the menu when the Esc key is pressed', () => {
47+
wrapper = mount(<SideNavMenu {...mockProps} defaultExpanded={true} />);
48+
expect(wrapper.state('isExpanded')).toBe(true);
49+
wrapper.simulate('keydown', {
50+
key: 'Escape',
51+
keyCode: 27,
52+
which: 27,
53+
});
54+
expect(wrapper.state('isExpanded')).toBe(false);
55+
});
56+
4657
it('should reset expanded state if the isSideNavExpanded prop is false', () => {
4758
wrapper = mount(<SideNavMenu {...mockProps} />);
4859
expect(wrapper.state('isExpanded')).toBe(false);

packages/react/src/components/UIShell/__tests__/__snapshots__/SideNavMenu-test.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ exports[`SideNavMenu should render 1`] = `
6262
>
6363
<li
6464
className="bx--side-nav__item bx--side-nav__item--icon custom-classname"
65+
onKeyDown={[Function]}
6566
>
6667
<button
6768
aria-expanded={false}

0 commit comments

Comments
 (0)