Frontend School

Build an Accessible Flight Destination Combobox

Machine Coding
medium

Build an Accessible Flight Destination Combobox

Medium
React
TypeScript
UI
Combobox
Accessibility
Frontend

Overview

At Ixigo, providing a seamless and efficient booking experience is paramount. A critical part of this is the destination selection process, where users need to quickly find and select their desired city or airport. This problem challenges you to build a robust and accessible Combobox component, similar to what we use for flight destination inputs, enhancing user experience by offering dynamic suggestions and easy navigation.

What you need to do / Task

Your primary task is to develop a Combobox React component that allows users to type into an input field and receive real-time suggestions from a predefined list of options.

Specifically, you need to:

  1. Create a Combobox component: This component should accept options: string[] and onSelect: (value: string) => void as props, along with an optional placeholder: string.
  2. Implement input handling: The component should render an input field where users can type.
  3. Filter suggestions: As the user types, the component must filter the options prop and display only those that match the current input (case-insensitive substring match).
  4. Display dropdown: The filtered suggestions should be displayed in a dropdown list that appears when the input is focused or has a value, and disappears when an option is selected or the input loses focus (unless an option is actively highlighted).
  5. Enable selection:
    • Clicking on a suggestion should select it, update the input field, and trigger the onSelect callback.
    • Keyboard navigation: Users should be able to navigate the suggestions using the ArrowUp and ArrowDown keys. The currently highlighted suggestion should be visually distinct.
    • Pressing Enter when a suggestion is highlighted should select it.
    • Pressing Escape should close the dropdown without selecting an option.
  6. Accessibility: Ensure the component is accessible, particularly for keyboard users, by using appropriate ARIA attributes.

UI/UX expectations

  • The Combobox should consist of an input field and a dynamically rendered list of suggestions.
  • The suggestion list should appear directly below the input field.
  • When navigating with arrow keys, the highlighted item should have a clear visual indicator (e.g., background color change).
  • Upon selection, the input field should display the selected value, and the dropdown should close.
  • Basic styling to differentiate elements (input, dropdown, list items, highlighted item) is expected for usability.

Acceptance criteria

  • Typing in the input field correctly filters the provided options.
  • The dropdown list of filtered suggestions appears when the input is focused and has matching options.
  • The dropdown list disappears when an option is selected or the input loses focus and no option is highlighted.
  • Clicking on a suggestion updates the input field and triggers the onSelect callback with the selected value.
  • ArrowUp and ArrowDown keys correctly navigate through the suggestions, with visual highlighting.
  • Pressing Enter on a highlighted suggestion selects it, updates the input, and calls onSelect.
  • Pressing Escape closes the dropdown and clears any active highlighting without selecting.
  • The component uses appropriate ARIA roles and attributes (e.g., role="combobox", aria-autocomplete, aria-expanded, aria-activedescendant, aria-controls).

Constraints

Constraints & assumptions

  • Data Structure: The options will be a simple array of strings (string[]). Assume the data is pre-loaded and available directly as a prop.
  • Maximum Options: The total number of options will not exceed 500. Filtering performance should be reasonable for this scale.
  • Technology Stack: You must use React with TypeScript.
  • No External Libraries: Do not use any third-party UI libraries or headless UI component libraries (e.g., Downshift, React-Select) for the core Combobox logic. Build it from scratch.
  • Browser Support: Assume modern browser environments (Chrome, Firefox, Safari, Edge).
  • Styling: Basic functional styling is sufficient. Focus on behavior and accessibility over pixel-perfect design.

Out of scope

  • API Integration: No need to fetch options from an API; use the provided mock data.
  • Debouncing: Implementing debouncing for the input change handler is not required, but extra credit if done thoughtfully.
  • Complex Theming/Styling: Advanced CSS-in-JS solutions or deep theming are not necessary.
  • Multi-select: The Combobox should support single selection only.
  • Error Handling: Robust error handling (e.g., for empty options, invalid input) is not a primary focus beyond basic UI states.

What interviewers evaluate

  • Component Design: Clarity and modularity of the Combobox component, including prop handling and internal state management.
  • Core Logic Implementation: Correctness and efficiency of the filtering mechanism and selection logic.
  • Accessibility (A11y): Proper implementation of keyboard navigation and ARIA attributes to ensure usability for all users.
  • TypeScript Usage: Effective use of TypeScript for type safety, interfaces, and clear code structure.
  • Code Quality: Readability, maintainability, and adherence to React best practices.

Loading editor...