From 82623dbe6f57b9620bd1e00ae57c2e7b6d77a8a1 Mon Sep 17 00:00:00 2001 From: Misha Moroshko Date: Fri, 28 Jul 2017 13:53:19 +0300 Subject: [PATCH] Use event.keyCode instead of event.key and event.code --- src/Autosuggest.js | 15 +++++++++------ test/helpers.js | 11 ++++++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Autosuggest.js b/src/Autosuggest.js index 4cfd0684..11c7ad6b 100644 --- a/src/Autosuggest.js +++ b/src/Autosuggest.js @@ -525,10 +525,11 @@ export default class Autosuggest extends Component { } }, onKeyDown: (event, data) => { - const keyCode = event.key || event.code; + const { keyCode } = event; + switch (keyCode) { - case 'ArrowDown': - case 'ArrowUp': + case 40: // ArrowDown + case 38: // ArrowUp if (isCollapsed) { if (shouldRenderSuggestions(value)) { onSuggestionsFetchRequested({ @@ -567,7 +568,7 @@ export default class Autosuggest extends Component { this.maybeCallOnChange( event, newValue, - event.key === 'ArrowDown' ? 'down' : 'up' + keyCode === 40 ? 'down' : 'up' ); } @@ -581,7 +582,8 @@ export default class Autosuggest extends Component { break; - case 'Enter': { + // Enter + case 13: { // See #388 if (event.keyCode === 229) { break; @@ -616,7 +618,8 @@ export default class Autosuggest extends Component { break; } - case 'Escape': { + // Escape + case 27: { if (isOpen) { // If input.type === 'search', the browser clears the input // when Escape is pressed. We want to disable this default diff --git a/test/helpers.js b/test/helpers.js index f196c570..3e584e75 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -208,28 +208,29 @@ export const blurInput = () => { }; export const clickEscape = () => { - Simulate.keyDown(input, { key: 'Escape' }); + Simulate.keyDown(input, { key: 'Escape', keyCode: 27 }); // throws if key is missing }; export const clickEnter = () => { - Simulate.keyDown(input, { key: 'Enter' }); + Simulate.keyDown(input, { key: 'Enter', keyCode: 13 }); // throws if key is missing clock.tick(1); }; +// See #388 export const clickCombinedCharacterEnter = () => { - Simulate.keyDown(input, { key: 'Enter', keyCode: 229 }); + Simulate.keyDown(input, { key: 'Enter', keyCode: 229 }); // throws if key is missing clock.tick(1); }; export const clickDown = (count = 1) => { for (let i = 0; i < count; i++) { - Simulate.keyDown(input, { key: 'ArrowDown' }); + Simulate.keyDown(input, { key: 'ArrowDown', keyCode: 40 }); // throws if key is missing } }; export const clickUp = (count = 1) => { for (let i = 0; i < count; i++) { - Simulate.keyDown(input, { key: 'ArrowUp' }); + Simulate.keyDown(input, { key: 'ArrowUp', keyCode: 38 }); // throws if key is missing } };