Overview
As Paytm expands its e-commerce offerings, a seamless product search experience is crucial for user engagement and conversion. Autocomplete functionality significantly enhances user experience by providing instant, relevant suggestions as they type, reducing typing effort and guiding them to desired products faster. This problem focuses on building a robust and performant autocomplete search component.
What you need to do / Task
Your task is to implement an autocomplete search component for Paytm's product catalog. You will be provided with a mock fetchSuggestions function that simulates an API call to retrieve product suggestions based on a user's input.
- Build a Search Input Component: Create a React component that includes an input field for search queries.
- Fetch & Display Suggestions: As the user types, call the
fetchSuggestions function with the current input value and display the returned suggestions in a dropdown list below the input field.
- Implement Debouncing: To prevent excessive API calls, implement a debouncing mechanism for the suggestion fetch logic. A delay of
300ms is recommended.
- Handle Loading State: Display a visual indicator (e.g., a spinner) while suggestions are being fetched.
- Keyboard Navigation: Enable users to navigate through the suggestions using
ArrowUp and ArrowDown keys. Pressing Enter on a highlighted suggestion should select it and populate the input field. Escape should close the suggestions dropdown.
- Click Selection: Allow users to select a suggestion by clicking on it, which should populate the input field and close the dropdown.
- Empty State: Clearly indicate when no suggestions are found for a given query.
UI/UX expectations
- Search Input: A clear, single-line text input field.
- Suggestions Dropdown: A list of suggestions appearing directly below the input field. Each suggestion should be a clickable item.
- Highlighting: The currently active (hovered or keyboard-navigated) suggestion should be visually highlighted.
- Loading Indicator: A small spinner or text indicating that suggestions are being loaded.
- No Results: A message like "No results found for 'query'" when the API returns an empty list for a non-empty query.
- Focus Management: Suggestions should appear when the input is focused and there's a query, and disappear when the input loses focus (unless a suggestion is being clicked).
Acceptance criteria
- Typing in the input field triggers suggestion fetching.
- Suggestions appear in a dropdown after a brief delay (debouncing).
- The loading spinner is visible during API calls and disappears afterward.
- Suggestions are filtered correctly based on the input query (case-insensitive partial match).
- Keyboard
ArrowUp/ArrowDown navigates suggestions, highlighting the active one.
- Pressing
Enter on an active suggestion selects it and populates the input.
- Clicking a suggestion selects it and populates the input.
- Pressing
Escape hides the suggestions dropdown.
- Suggestions disappear when the input field is cleared or loses focus.
- A message is displayed if no suggestions are found for a non-empty query.