Skip to content

Commit

Permalink
Add aria-activedescendant support in autocomplete
Browse files Browse the repository at this point in the history
J=SLAP-1650
  • Loading branch information
Yen Truong committed Oct 26, 2021
1 parent 0c8a24f commit 4fa2d44
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sample-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion sample-app/src/components/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ export default function Dropdown({
[cssClasses.focusedOption]: index === focusedOptionIndex
})
return (
<div key={index} className={className} onClick={() => onClickOption(option)}>
<div
key={index}
className={className}
id={`${cssClasses.option}-${index}`}
onClick={() => onClickOption(option)}>
{option.render()}
</div>)
}
Expand Down
7 changes: 5 additions & 2 deletions sample-app/src/components/InputDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export default function InputDropdown({
focusedOptionIndex: undefined,
shouldDisplayDropdown: false,
});
const focusOptionId = focusedOptionIndex === undefined
? undefined
: `${cssClasses.option}-${focusedOptionIndex}`;

const [latestUserInput, setLatestUserInput] = useState(inputValue);

Expand All @@ -83,12 +86,11 @@ export default function InputDropdown({

const isFirstOptionFocused = focusedOptionIndex === 0;
const isLastOptionFocused = focusedOptionIndex === options.length - 1;

if (evt.key === 'Enter') {
updateInputValue(inputValue);
onSubmit(inputValue);
dispatch({ type: 'HideOptions' });
} else if (evt.key === 'Escape') {
} else if (evt.key === 'Escape' || evt.key === 'Tab') {
dispatch({ type: 'HideOptions' });
} else if (evt.key === 'ArrowDown' && options.length > 0 && !isLastOptionFocused) {
const newIndex = focusedOptionIndex !== undefined ? focusedOptionIndex + 1 : 0;
Expand Down Expand Up @@ -125,6 +127,7 @@ export default function InputDropdown({
onKeyDown={onKeyDown}
value={inputValue}
ref={inputRef}
aria-activedescendant={focusOptionId}
/>
{renderButtons()}
</div>
Expand Down

0 comments on commit 4fa2d44

Please sign in to comment.