Overview
Instant search autocomplete is a critical feature for any e-commerce platform like Paytm. It significantly enhances user experience by reducing typing effort, guiding users to relevant products faster, and minimizing search abandonment. For an L4 engineer, building a robust and responsive autocomplete component demonstrates proficiency in frontend architecture, state management, and user interaction design.
What you need to do / Task
Your task is to build a React component that provides instant search suggestions for a predefined list of products. The component should be interactive and responsive.
- Search Input: Render a text input field where users can type their search queries.
- Dynamic Suggestions: As the user types, display a dropdown list of matching product suggestions below the input field.
- Filtering Logic: Suggestions should be filtered from a mock dataset. The filtering should be case-insensitive and match if the input string is a substring of any product name.
- Debouncing: Implement debouncing for the search logic (simulating an API call). Suggestions should not update on every keystroke, but only after a short delay (e.g., 300ms) of user inactivity.
- Keyboard Navigation: Allow users to navigate through the displayed suggestions using the
Up and Down arrow keys. The currently highlighted suggestion should be visually distinct.
- Suggestion Selection: When a user clicks on a suggestion or presses
Enter while a suggestion is highlighted, the search input field should be updated with the selected suggestion's text, and the suggestion list should be cleared.
- Clear Suggestions: The suggestion list should disappear when the input is empty or when the input loses focus (unless a suggestion is being selected).
UI/UX expectations
- The search input should be clearly visible.
- Suggestions should appear as a list directly below the input field.
- The currently highlighted suggestion (via keyboard navigation or mouse hover) should have a distinct background color.
- Basic styling for readability and a clean appearance is expected. Focus on functionality first.
Acceptance criteria
- Typing "lap" into the input should display suggestions like "Laptop", "Apple Laptop", "Gaming Laptop" (assuming these are in the dataset).
- Typing quickly should demonstrate debouncing; suggestions appear after a brief pause, not immediately with each character.
- Pressing
Down arrow key repeatedly cycles through suggestions, highlighting each one in turn.
- Pressing
Up arrow key repeatedly cycles through suggestions in reverse.
- Clicking a suggestion or pressing
Enter on a highlighted suggestion populates the input field and clears the suggestion list.
- Deleting all text from the input clears the suggestion list.
- Clicking outside the search component (blurring the input) should clear the suggestion list.