Skip to content

Commit

Permalink
feat(SideNavMenu): collapse submenu on esc keydown (#5198)
Browse files Browse the repository at this point in the history
  • Loading branch information
emyarod authored Jan 29, 2020
1 parent fc4b6c1 commit 0853d38
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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

0 comments on commit 0853d38

Please sign in to comment.