Jump Through a Listbox by Letter
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Build a single-choice list that keyboard users can move through quickly. A role="listbox" named "Fruit" (aria-label) holds six role="option" elements: "Apple", "Banana", "Blueberry", "Cherry", "Grape" and "Guava". One option is selected at a time, Apple at first: it has aria-selected="true", the others "false", and a line below reads "Selected: Apple". Selection follows focus, and the list is a single Tab stop, followed by an "Add to basket" button. Pick either way of showing focus: keep DOM focus on the listbox (tabIndex={0}) and point its aria-activedescendant at the selected option's id, or move DOM focus onto the selected option with a roving tabindex. The checks accept both. In the list, the Down and Up arrows select the next or previous option, stopping at the ends, and Home and End select the first and last. Typing a letter selects the next option after the selected one that starts with that letter, ignoring case and wrapping round to the top: from Apple, "b" selects Banana, "b" again Blueberry, and once more Banana. A letter no option starts with changes nothing. Clicking an option selects it.
Your code must pass
- Apple is selected at first, in a listbox named "Fruit"
- Tab reaches the list at the selected option, and the next Tab leaves it
- Down and Up move the selection, stopping at the ends
- Typing a letter jumps to the next option starting with it
- Clicking an option selects it