Skip to content

Commit

Permalink
Use event.keyCode instead of event.key and event.code
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko committed Jul 28, 2017
1 parent e79ff42 commit 82623db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
15 changes: 9 additions & 6 deletions src/Autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -567,7 +568,7 @@ export default class Autosuggest extends Component {
this.maybeCallOnChange(
event,
newValue,
event.key === 'ArrowDown' ? 'down' : 'up'
keyCode === 40 ? 'down' : 'up'
);
}

Expand All @@ -581,7 +582,8 @@ export default class Autosuggest extends Component {

break;

case 'Enter': {
// Enter
case 13: {
// See #388
if (event.keyCode === 229) {
break;
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};

Expand Down

0 comments on commit 82623db

Please sign in to comment.