Overview
In modern e-commerce and content platforms, a fast and responsive search experience is crucial for user engagement. An effective autocomplete feature guides users quickly to their desired products or content, reducing search abandonment and improving overall satisfaction. This problem challenges you to build a foundational autocomplete search component that provides real-time suggestions as a user types.
What you need to do / Task
Your goal is to implement a React component that provides real-time search suggestions.
- Search Input: Create an input field where users can type their search query.
- Dynamic Suggestions: As the user types, display a list of relevant suggestions below the input field.
- Filtering Logic: Suggestions should be filtered from a predefined list (mock data) based on whether they contain the user's input string (case-insensitive substring match).
- Debouncing: Implement a debounce mechanism for fetching suggestions. The suggestion fetching function should only be called after the user has paused typing for a short duration (e.g., 300ms).
- Highlighting: Within the displayed suggestions, highlight the portion of the text that matches the current search term.
- Keyboard Navigation:
- Allow users to navigate through the suggestion list using the
ArrowUp and ArrowDown keys.
- The currently selected suggestion should have a distinct visual style.
- Pressing
Enter when a suggestion is selected should populate the input field with the selected suggestion's text and clear the suggestion list.
- Click Selection: Clicking on a suggestion should also populate the input field with its text and clear the suggestion list.
UI/UX expectations
- The search input should be clearly visible.
- Suggestions should appear in a dropdown-like list directly below the input field when available.
- The currently highlighted (via keyboard navigation or hover) suggestion should have a distinct background color.
- The matching parts of suggestions should be visually differentiated (e.g., bolded or different color).
- The component should be accessible for keyboard users.
Acceptance criteria
- Typing in the input field updates the displayed suggestions after a short delay.
- Suggestions correctly filter based on case-insensitive substring match.
- The
fetchSuggestions function is debounced (e.g., console log shows calls only after a pause).
- Matching parts of suggestions are visually highlighted.
ArrowUp and ArrowDown keys correctly navigate through suggestions, wrapping around the list.
- Pressing
Enter on a selected suggestion populates the input and clears suggestions.
- Clicking a suggestion populates the input and clears suggestions.
- No suggestions are shown when the input is empty or when there are no matches.