Commit 0853d38 1 parent fc4b6c1 commit 0853d38 Copy full SHA for 0853d38
File tree 3 files changed +21
-1
lines changed
packages/react/src/components/UIShell
3 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import cx from 'classnames';
11
11
import PropTypes from 'prop-types' ;
12
12
import React from 'react' ;
13
13
import SideNavIcon from './SideNavIcon' ;
14
+ import { keys , match } from '../../internal/keyboard' ;
14
15
15
16
const { prefix } = settings ;
16
17
@@ -100,6 +101,12 @@ export class SideNavMenu extends React.Component {
100
101
this . setState ( state => ( { isExpanded : ! state . isExpanded } ) ) ;
101
102
} ;
102
103
104
+ handleKeyDown = event => {
105
+ if ( match ( event , keys . Escape ) ) {
106
+ this . setState ( ( ) => ( { isExpanded : false } ) ) ;
107
+ }
108
+ } ;
109
+
103
110
render ( ) {
104
111
const {
105
112
buttonRef,
@@ -138,7 +145,8 @@ export class SideNavMenu extends React.Component {
138
145
[ customClassName ] : ! ! customClassName ,
139
146
} ) ;
140
147
return (
141
- < li className = { className } >
148
+ // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
149
+ < li className = { className } onKeyDown = { this . handleKeyDown } >
142
150
< button
143
151
aria-haspopup = "true"
144
152
aria-expanded = { isExpanded }
Original file line number Diff line number Diff line change @@ -43,6 +43,17 @@ describe('SideNavMenu', () => {
43
43
expect ( wrapper . state ( 'isExpanded' ) ) . toBe ( true ) ;
44
44
} ) ;
45
45
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
+
46
57
it ( 'should reset expanded state if the isSideNavExpanded prop is false' , ( ) => {
47
58
wrapper = mount ( < SideNavMenu { ...mockProps } /> ) ;
48
59
expect ( wrapper . state ( 'isExpanded' ) ) . toBe ( false ) ;
Original file line number Diff line number Diff line change @@ -62,6 +62,7 @@ exports[`SideNavMenu should render 1`] = `
62
62
>
63
63
<li
64
64
className = " bx--side-nav__item bx--side-nav__item--icon custom-classname"
65
+ onKeyDown = { [Function ]}
65
66
>
66
67
<button
67
68
aria-expanded = { false }
You can’t perform that action at this time.
0 commit comments