KatInterview

Build a Race-Safe, Screen-Reader-Friendly Typeahead Combobox

System DesignModel answer
MediumModel answerSystem Designreactaccessibilityaria

Last updated

Build a generic React typeahead that debounces input, discards out-of-order responses, follows the ARIA combobox pattern, and supports full keyboard control.

Task

Build a reusable typeahead (combobox) component in React and TypeScript. As the user types, the component waits for a short pause, requests matching suggestions from an async function, and shows them in a dropdown. The user can pick a suggestion with the mouse or the keyboard.

Network latency is unpredictable, so replies can arrive out of order. For example, the reply for "ap" may land after the reply for "appl". The dropdown must never show results that belong to an older query than the one currently in the input.

Required behavior

  1. Out-of-order protection. A late reply for an outdated query must never overwrite newer state. Use AbortController, a monotonically increasing request counter, or an equivalent technique.
  2. Debounce. Wait debounceMs (default 300) after the last keystroke before fetching.
  3. ARIA combobox pattern.
    • The input has role="combobox", aria-autocomplete="list", aria-expanded, and aria-controls pointing at the listbox id.
    • The popup has role="listbox" and every suggestion has role="option".
    • The highlighted option is exposed through aria-activedescendant. Real DOM focus stays in the input.
  4. Keyboard.
    • ArrowDown / ArrowUp move the highlight (wrapping or clamping is acceptable, but state which you chose).
    • Enter selects the highlighted item and closes the popup.
    • Escape closes the popup and leaves the typed text alone.
  5. States. Show loading, empty ("No results found"), and error states. Close the popup on outside click or when focus leaves the component.

Signature

interface AutocompleteProps<T> {
  fetchSuggestions: (query: string, signal?: AbortSignal) => Promise<T[]>;
  renderOption: (item: T) => React.ReactNode;
  getOptionKey: (item: T) => string;
  onSelect: (item: T) => void;
  debounceMs?: number;
  placeholder?: string;
}

function Autocomplete<T>(props: AutocompleteProps<T>): JSX.Element;

Example

  • t = 0ms: user types "b".
  • t = 100ms: user types "ba".
  • t = 400ms: the debounce fires and exactly one request, for "ba", is sent.
  • The user presses ArrowDown: the first suggestion is highlighted and aria-activedescendant points to its id.
  • The user presses Enter: onSelect receives that item and the popup closes.

Edge cases to handle

  • Empty input: do not fetch, and close the popup right away.
  • Failed request: catch the rejection and show an accessible error message.
  • Unmount: clear pending timers and abort in-flight requests.

Explain your design, then give the implementation, its complexity, and the pitfalls you guarded against.

Hints

Hint 1

Tie the timer and the AbortController to a single useEffect keyed on the query. The effect's cleanup then both clears the timer and aborts the request, which covers new keystrokes and unmount with the same code.

Hint 2

Check controller.signal.aborted after the await, before calling any setState. That way a stale reply is dropped even if the supplied fetchSuggestions ignores the signal.

Hint 3

For accessibility, keep DOM focus on the input and move only aria-activedescendant. Prevent the default on option mousedown so clicking an option does not blur the input before the click registers, and put loading/empty/error text in a role="status" live region.