Overview
Users on e-commerce platforms expect a smooth and responsive search experience. Directly querying a backend API on every keystroke can be inefficient and overload servers, especially with fast typers. To optimize this, we implement a debounced search input that delays the API call until the user pauses typing, significantly improving performance and resource utilization.
What you need to do / Task
Your goal is to build a functional search component that fetches and displays product suggestions in a debounced manner.
- Render a search input field: A standard HTML
<input type="text"> element.
- Display search results: Below the input, render a list of product names that match the current search query.
- Implement debouncing: The actual search logic (simulated API call) should only execute after the user has stopped typing for a specific duration (e.g., 300ms).
- Show a loading indicator: While the debounced search is in progress (after the debounce delay but before results are fetched), display a "Loading..." message.
- Handle empty states: If no results are found for a query, display a "No products found." message. If the search term is empty, clear results and show a prompt like "Start typing...".
UI/UX expectations
- The search input should be clearly visible.
- Search results should appear as an unordered list below the input.
- The loading indicator should replace the results list when active.
- The "No products found." message or "Start typing..." prompt should replace the results list when applicable.
- Basic styling is appreciated for readability, but complex design is not the focus.
Acceptance criteria
- Typing in the input updates its value immediately.
- The simulated API call (search logic) is triggered only after a pause in typing (e.g., 300ms).
- A "Loading..." message is displayed while the debounced search is active.
- Matching product names are rendered in a list below the input.
- If no products match, "No products found." is displayed.
- Clearing the input should clear the displayed results/messages and show the initial prompt.