Overview
The SearchBar component provides a search input field with automatic debouncing. Two versions exist across the projects with slightly different features:- GIFs App Version: Debounced search with Enter key support
- Weather App Version: Form-based search with loading state
GIFs App SearchBar
Purpose
Provides a search input that automatically triggers searches after a 700ms delay, reducing API calls while typing. Also supports immediate search via Enter key or button click. Location:gifs-app/src/shared/components/SearchBar.tsx
Props
Placeholder text displayed in the input field
Callback function invoked with the search query. Called after debounce delay or immediately on Enter/button click.
Features
- Debounced Search: Automatically triggers search 700ms after typing stops
- Enter Key Support: Press Enter to search immediately
- Search Button: Click button to trigger immediate search
- State Management: Internal state for input value
Usage Example
Implementation Details
Weather App SearchBar
Purpose
Provides a form-based city search with loading state management and validation. Location:weather-finder/src/components/SearchBar.tsx
Props
Callback function invoked when the form is submitted with a valid city name
Controls the disabled state of inputs and changes button text to “Buscando…”
Initial value for the search input field
Features
- Form Submission: Proper HTML form with submit handling
- Loading State: Disables input and button during API calls
- Input Validation: Trims whitespace and requires non-empty input
- Accessibility: ARIA labels and semantic HTML with
role="search" - Auto-complete Off: Prevents browser suggestions for city names
Usage Example
Implementation Details
Key Differences
- GIFs App
- Weather App
- Debounced automatic search (700ms)
- Enter key triggers immediate search
- No form element
- No loading state management
- Optional placeholder prop
TypeScript Interfaces
Best Practices
When to use debouncing
When to use debouncing
Use the GIFs App version when:
- You want to search as the user types
- API calls are lightweight and frequent
- User experience benefits from instant feedback
When to use form submission
When to use form submission
Use the Weather App version when:
- API calls are expensive or rate-limited
- You need explicit user confirmation
- Loading states need to be clearly communicated
Accessibility considerations
Accessibility considerations
- Always use
aria-labelfor screen readers - Use
role="search"on search forms - Disable inputs during loading to prevent double submissions
- Provide clear button text that changes based on state
Related Components
CustomHeader
Often used above SearchBar for page titles
GifList
Displays search results from GIFs SearchBar
WeatherDisplay
Displays search results from Weather SearchBar