Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add extra keyboard controls as per W3C recommendations for combobox #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions packages/autocomplete/AutocompleteCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,58 @@ class AutocompleteCore {
}

handleKeyDown = event => {
const { key } = event
const { key, ctrlKey, altKey } = event

switch (key) {
case 'Up': // IE/Edge
case 'Down': // IE/Edge
case 'ArrowUp':
case 'ArrowDown': {
const selectedIndex =
key === 'ArrowUp' || key === 'Up'
? this.selectedIndex - 1
: this.selectedIndex + 1
event.preventDefault()
this.handleArrows(selectedIndex)
const isUp = (key === 'ArrowUp' || key === 'Up');
if (altKey === false) {
const selectedIndex = (isUp === true)
? this.selectedIndex - 1
: this.selectedIndex + 1
event.preventDefault()
this.handleArrows(selectedIndex)
} else {
// Based on recomendations from
// https://www.w3.org/WAI/ARIA/apg/patterns/combobox/#keyboardinteraction
if (isUp === true) {
// "Alt + Up Arrow (Optional): If the popup is displayed:
// If the popup contains focus, returns focus to the combobox.
// Closes the popup."
this.hideResults();
} else {
// "Alt + Down Arrow (Optional): If the popup is available but not displayed,
// displays the popup without moving focus."
this.showResults();
}
}
break
}
case 'Tab': {
this.selectResult()
break
}
case 'Home': {
// Based on recomendations from
// https://www.w3.org/WAI/ARIA/apg/patterns/combobox/#keyboardinteraction
// "Control + Home (optional): moves focus to the first row."
if (ctrlKey === true) {
this.handleArrows(0);
}
break;
}
case 'End':{
// Based on recomendations from
// https://www.w3.org/WAI/ARIA/apg/patterns/combobox/#keyboardinteraction
// "Control + End (Optional): moves focus to the last row."
if (ctrlKey === true) {
this.handleArrows(this.results.length - 1);
}
break;
}
case 'Enter': {
const isListItemSelected =
event.target.getAttribute('aria-activedescendant').length > 0
Expand Down