Dropdown Module
The Dropdown module provides autocomplete functionality for the restaurant input field. It displays a filterable list of previously used restaurants and allows users to quickly select from existing options or type a new restaurant name.Overview
The Dropdown module manages:- Displaying a dropdown list of restaurants
- Filtering restaurants based on user input
- Handling user selection from the dropdown
- Showing/hiding the dropdown based on focus and clicks
src/js/modules/dropdown.js
Public Methods
init()
Initializes the dropdown module by attaching event listeners to the restaurant input field.
This method takes no parameters
void
Usage:
- Attaches
inputevent listener to filter restaurants as user types - Attaches
focusevent listener to show dropdown when field is focused - Attaches global
clickevent listener to hide dropdown when clicking outside
handleInput()
Handles input changes in the restaurant field and filters the dropdown options.
Returns: void
Usage:
- Gets current value from restaurant input
- If empty, shows all restaurants
- If has value, filters restaurants by name
- Updates dropdown content in real-time
show()
Displays the dropdown menu with current restaurant options.
Returns: void
Behavior:
- Calls
update()to refresh dropdown content - Removes
hiddenclass from dropdown element - Triggered on input field focus
hide()
Hides the dropdown menu.
Returns: void
Behavior:
- Adds
hiddenclass to dropdown element - Triggered when clicking outside the dropdown
update()
Updates the dropdown content with the current list of restaurants from DataStore.
Returns: void
Usage:
- Fetches restaurant list from DataStore
- Generates HTML for each restaurant option
- Escapes HTML to prevent XSS
- Attaches click handlers for selection
- Shows message if no restaurants exist
showAllRestaurants()
Displays all available restaurants in the dropdown (no filtering).
Returns: void
Behavior:
- Simply calls
update()to show complete list - Used when input field is empty
filterRestaurants(searchValue)
Filters and displays restaurants matching the search term.
The search term to filter restaurants (case-insensitive)
void
Usage:
- Performs case-insensitive substring matching
- Updates dropdown with filtered results
- Shows “No results” message if no matches found
HTML Structure
The dropdown requires this HTML structure:Dropdown Item Structure
Each dropdown item is rendered as:Selection Behavior
When a user clicks a dropdown item:- The restaurant name is populated into the input field
- The dropdown is hidden
- The cursor remains in the input field for further editing if needed
Security
The dropdown properly escapes all restaurant names to prevent XSS attacks:Dependencies
The Dropdown module depends on:- DataStore: To get the list of unique restaurants (
DataStore.getRestaurants()) - Utils: For HTML escaping (
Utils.escapeHtml())
Edge Cases
No Restaurants Available
When no reviews exist yet:No Matching Results
When search returns no results:Styling
The dropdown uses CSS classes for visual presentation:.dropdown- Container wrapper.dropdown-content- Dropdown menu container.dropdown-item- Individual selectable items.hidden- Toggle visibility