Overview
At Swiggy, enabling users to quickly find their desired restaurants is paramount to a seamless food ordering experience. A highly efficient search bar, often implemented as a typeahead or combobox component, is a critical part of this. It helps users discover restaurants even with partial input, reducing friction and improving satisfaction. This problem challenges you to build a foundational typeahead search component for restaurants.
What you need to do / Task
Your primary task is to build a functional and user-friendly Combobox React component that allows users to search for restaurants from a predefined list.
- Create a
Combobox component: This component should accept a list of Restaurant objects as a prop.
- Implement search filtering: As the user types into the input field, the component should filter the provided
Restaurant list. The filtering should be a case-insensitive substring match against the name property of each restaurant.
- Display suggestions: Matching restaurants should be displayed in a dropdown list that appears below the input field.
- Handle selection: When a user clicks on a suggestion in the dropdown, the input field should be updated with the selected restaurant's name, and the dropdown should close.
- No results message: If the search yields no matching restaurants, a clear "No results found." message should be displayed within the dropdown area.
UI/UX expectations
- An input field should be prominently displayed for user typing.
- A dropdown list of suggestions should appear directly below the input field only when there are matching results and the input is focused/active.
- Each suggestion in the dropdown should be clickable.
- Upon selection, the dropdown should disappear, and the input field should reflect the selected item's name.
- Basic, clean styling to ensure readability and functional presentation (e.g., clear borders, distinct hover states for suggestions).
Acceptance criteria
- [ ] A text input field is rendered and allows user input.
- [ ] Typing in the input field correctly filters the provided restaurant list (case-insensitive substring match).
- [ ] A dropdown list displays filtered suggestions when available.
- [ ] Clicking a suggestion item updates the input field with the selected restaurant's name.
- [ ] The dropdown list closes immediately after a selection is made.
- [ ] The message "No results found." is displayed when no restaurants match the current input.
- [ ] The dropdown list is hidden when the input is empty or loses focus (and no selection has been made).