From feeacc9b274d4d729d63ac99d6b7b639cc352f57 Mon Sep 17 00:00:00 2001 From: Ashish Baravaliya Date: Tue, 27 Jun 2023 17:33:12 -0400 Subject: [PATCH 1/3] fix: autocomplete with keyboard accessiblity --- .../Autocomplete/Autocomplete.react.js | 23 ++++++------------- .../SuggestionsList/SuggestionsList.react.js | 5 ++-- .../SuggestionsList/SuggestionsList.scss | 4 ++-- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/components/Autocomplete/Autocomplete.react.js b/src/components/Autocomplete/Autocomplete.react.js index 6e0647baae..1b4e87d532 100644 --- a/src/components/Autocomplete/Autocomplete.react.js +++ b/src/components/Autocomplete/Autocomplete.react.js @@ -246,27 +246,17 @@ export default class Autocomplete extends Component { onKeyDown(e) { const { activeSuggestion, filteredSuggestions } = this.state; - // Enter - const { userInput } = this.state; - - if (e.keyCode === 13) { - if (userInput && userInput.length > 0) { - this.props.onSubmit(userInput); - } - } else if (e.keyCode === 9) { - // Tab - // do not type it - e.preventDefault(); - - e.stopPropagation(); - // move focus to input - this.inputRef.current.focus(); + if (e.keyCode === 13 || e.key === 'Enter') { this.setState({ active: true, activeSuggestion: 0, showSuggestions: false, userInput: filteredSuggestions[activeSuggestion] }); + } else if (e.keyCode === 9) { + this.setState({ + showSuggestions: false, + }); } else if (e.keyCode === 38) { // arrow up if (activeSuggestion === 0) { @@ -279,7 +269,7 @@ export default class Autocomplete extends Component { }); } else if (e.keyCode === 40) { // arrow down - if (activeSuggestion - 1 === filteredSuggestions.length) { + if (activeSuggestion === filteredSuggestions.length - 1) { return; } @@ -341,6 +331,7 @@ export default class Autocomplete extends Component { activeSuggestion={activeSuggestion} onClick={onClick} onMouseDown={onMouseDown} + onKeyDown={onKeyDown} /> ); } diff --git a/src/components/SuggestionsList/SuggestionsList.react.js b/src/components/SuggestionsList/SuggestionsList.react.js index b2db0777c7..5b11b895c1 100644 --- a/src/components/SuggestionsList/SuggestionsList.react.js +++ b/src/components/SuggestionsList/SuggestionsList.react.js @@ -43,7 +43,8 @@ export default class Suggestion extends React.Component { suggestionsItemStyle, activeSuggestion, onClick, - onMouseDown} = this.props; + onMouseDown, + onKeyDown} = this.props; return ( -