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

feat(SideNavMenu): collapse submenu on esc keydown #5198

10 changes: 9 additions & 1 deletion packages/react/src/components/UIShell/SideNavMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cx from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import SideNavIcon from './SideNavIcon';
import { keys, match } from '../../internal/keyboard';

const { prefix } = settings;

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

handleKeyDown = event => {
if (match(event, keys.Escape)) {
this.setState(() => ({ isExpanded: false }));
}
};

render() {
const {
buttonRef,
Expand Down Expand Up @@ -138,7 +145,8 @@ export class SideNavMenu extends React.Component {
[customClassName]: !!customClassName,
});
return (
<li className={className}>
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
<li className={className} onKeyDown={this.handleKeyDown}>
<button
aria-haspopup="true"
aria-expanded={isExpanded}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ describe('SideNavMenu', () => {
expect(wrapper.state('isExpanded')).toBe(true);
});

it('should collapse the menu when the Esc key is pressed', () => {
wrapper = mount(<SideNavMenu {...mockProps} defaultExpanded={true} />);
expect(wrapper.state('isExpanded')).toBe(true);
wrapper.simulate('keydown', {
key: 'Escape',
keyCode: 27,
which: 27,
});
expect(wrapper.state('isExpanded')).toBe(false);
});

it('should reset expanded state if the isSideNavExpanded prop is false', () => {
wrapper = mount(<SideNavMenu {...mockProps} />);
expect(wrapper.state('isExpanded')).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ exports[`SideNavMenu should render 1`] = `
>
<li
className="bx--side-nav__item bx--side-nav__item--icon custom-classname"
onKeyDown={[Function]}
>
<button
aria-expanded={false}
Expand Down