Overview
Snapchat's Lens Explorer allows users to discover a vast array of augmented reality lenses. A critical component of this experience is a fast and intuitive search bar. For this problem, you will build an autocomplete search component that provides real-time, intelligently sorted suggestions as the user types, ensuring a smooth discovery process for our users.
What you need to do / Task
Your goal is to implement a robust and user-friendly autocomplete search component in React. You will be provided with a mock API and data. Your implementation must satisfy the following:
- Search Input: Create a text input field where users can type their search queries.
- Debounced API Calls: Implement a debouncing mechanism for the search input. API calls to fetch suggestions should only be made after the user has paused typing for a short duration (e.g., 300ms).
- Suggestion Filtering & Sorting: Once suggestions are fetched, they must be filtered and sorted according to specific rules:
- Filtering: Suggestions should match the search query case-insensitively (substring match).
- Sorting Hierarchy:
- Exact Matches: Suggestions whose
name is an exact, case-insensitive match for the query. These appear first, sorted by priority (descending).
- Starts With: Suggestions whose
name starts with the query (case-insensitive), but are not exact matches. These appear next, sorted by priority (descending).
- Contains: Suggestions whose
name contains the query (case-insensitive), but are not "exact" or "starts with" matches. These appear last, sorted by priority (descending).
- Display a maximum of 10 suggestions at any time.
- Loading State: Display a clear loading indicator while suggestions are being fetched.
- No Results State: Clearly indicate when no suggestions are found for the current query.
- Keyboard Navigation: Implement keyboard navigation for the suggestion list:
ArrowUp/ArrowDown: Navigate through the suggestions.
Enter: Select the currently highlighted suggestion. This should populate the search input with the selected suggestion's name.
- Click Selection: Clicking a suggestion should also populate the search input with its name.
UI/UX expectations
- The search input should be a standard text field.
- Suggestions should appear in a dropdown list below the input field when available.
- The currently highlighted suggestion (via hover or keyboard navigation) should have a distinct visual style.
- When a suggestion is selected (by Enter or click), the dropdown should close, and the input field should display the selected suggestion's name.
- Accessibility: Ensure appropriate ARIA attributes are used for the search input and suggestion list to enhance usability for assistive technologies.
Acceptance criteria
- Typing in the search input triggers debounced calls to
fetchSuggestions.
- Suggestions appear as a dropdown list below the input, filtered and sorted according to the specified rules.
- Only up to 10 suggestions are displayed.
- A loading spinner or text is visible during API calls.
- "No results found" message is displayed when no matches exist for a non-empty query.
ArrowUp and ArrowDown keys correctly navigate through suggestions, wrapping around at the ends.
Enter key selects the highlighted suggestion, updates the input, and closes the dropdown.
- Clicking a suggestion selects it, updates the input, and closes the dropdown.
- The currently active/highlighted suggestion is visually distinct.
- The component is accessible with relevant ARIA roles and attributes (e.g.,
aria-autocomplete, aria-controls, aria-activedescendant).