Overview
TheSearchBar component provides a reusable search interface with a text input, search button, and optional clear button. It supports keyboard navigation and custom styling.
Import
Props
Callback function triggered when the user submits a search. Receives the trimmed search query as a parameter.
Placeholder text displayed in the search input field.
Initial value to populate the search input.
Additional CSS classes to apply to the container div.
Additional CSS classes to apply to the search button.
Additional CSS classes to apply to the input field.
Whether to display a clear button when the input has content.
Optional callback function triggered when the clear button is clicked.
Features
Search Button States
The search button automatically handles different states:- Enabled: When input has content (red background, white text)
- Disabled: When input is empty (gray background, disabled state)
Keyboard Support
- Press Enter in the input field to trigger search
- Tab navigation between input and buttons
- Full keyboard accessibility
Clear Functionality
WhenshowClearButton={true}:
- Clear button appears only when input has content
- Clicking clear resets the input and calls
onClearcallback - Button has gray styling to differentiate from primary search action
Usage Examples
Variants
The codebase includes two specialized variants:ListingsSearchBar
A variant used on the listings page with a rounded, centered design.- Circular/pill-shaped design (
rounded-full) - Integrated search button positioned inside input
- Form-based submission
src/components/ListingsSearchBar.tsx:1 for implementation.
HomePageSearchBar
A variant designed for the homepage with automatic navigation to the listings page.- No props required (fully self-contained)
- Automatically navigates to
/propiedadeson search - Uses
parseQueryToFiltersto convert search text into filter parameters - Large, centered design optimized for hero sections
src/components/HomePageSearchBar.tsx:1 for implementation.
Styling
Default styles include:- Flexbox layout with gap spacing
- Red color scheme for primary actions (
red-800for button) - Gray borders and focus states
- Ring-based focus indicators
- Smooth transitions for all state changes
Accessibility
- Input has
aria-label="Buscar propiedades" - Button has
aria-label="Buscar propiedades" - All interactive elements have
tabIndex={0} - Disabled state properly handled
- Semantic HTML structure
Dependencies
lucide-react- For the Search iconreact- For state management (useState hook)
Source Code
View the full implementation atsrc/components/SearchBar.tsx:1