Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
fix(tooltip): handle esc keydown event (#2164)
Browse files Browse the repository at this point in the history
* fix(tooltip): handle esc keydown event

* chore(tooltip): update snapshots

* chore(tooltip): update snapshots

* fix(tooltip): add keys.js module

* fix(tooltip): add event.code object to key.js
  • Loading branch information
dakahn authored and asudoh committed Apr 13, 2019
1 parent e119904 commit 2fbe6af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/components/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import FloatingMenu, {
import ClickListener from '../../internal/ClickListener';
import { breakingChangesX, componentsX } from '../../internal/FeatureFlags';
import mergeRefs from '../../tools/mergeRefs';
import { keys, keyCodes, matches as keyDownMatch } from '../../tools/key';

const { prefix } = settings;

Expand Down Expand Up @@ -371,11 +372,21 @@ class Tooltip extends Component {
}
};

handleKeyPress = evt => {
const key = evt.key || evt.which;
handleKeyPress = event => {
if (keyDownMatch(event, [keys.ESC, keyCodes.ESC, keyCodes.IEESC])) {
event.stopPropagation();
this.setState({ open: false });
}

if (key === 'Enter' || key === 13 || key === ' ' || key === 32) {
evt.stopPropagation();
if (
keyDownMatch(event, [
keys.ENTER,
keyCodes.ENTER,
keys.SPACE,
keyCodes.SPACE,
])
) {
event.stopPropagation();
this.setState({ open: !this.state.open });
}
};
Expand Down
21 changes: 20 additions & 1 deletion src/tools/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
* LICENSE file in the root directory of this source tree.
*/

// event.key
export const keyCodes = {
TAB: 'Tab',
ENTER: 'Enter',
ESC: 'Escape',
// IE11 Escape
IEESC: 'Esc',
SPACE: ' ',
PAGEUP: 'PageUp',
PAGEDOWN: 'PageDown',
END: 'End',
HOME: 'Home',
LEFT: 'ArrowLeft',
UP: 'ArrowUp',
RIGHT: 'ArrowRight',
DOWN: 'ArrowDown',
};

// event.which (DEPRECATED)
export const keys = {
TAB: 9,
ENTER: 13,
Expand Down Expand Up @@ -37,7 +56,7 @@ export const keys = {
*/
export function matches(event, keysToMatch) {
for (let i = 0; i < keysToMatch.length; i++) {
if (keysToMatch[i] === event.which) {
if (keysToMatch[i] === event.which || keysToMatch[i] === event.key) {
return true;
}
}
Expand Down

0 comments on commit 2fbe6af

Please sign in to comment.