Overview
Tienda ETCA implements a real-time search system that filters products as users type. The search is case-insensitive and matches product names instantly without requiring a search button.Implementation
The search functionality is implemented inCartContext at ~/workspace/source/src/context/CartContext.jsx:13-15.
State Management
Filter Logic
Products are filtered using JavaScript’sfilter() and includes() methods:
The filter uses optional chaining (
?.) to safely handle potentially undefined product objects.How It Works
State Updates Trigger Re-render
Every keystroke updates the
busqueda state, causing the component to re-render.Filter Recalculates
The
productosFiltrados array is recalculated on each render, filtering products in real-time.Search Input Component
The search input is implemented inProductList component at ~/workspace/source/src/components/ProductList.jsx:34-41:
Case-Insensitive Matching
The search converts both the product name and search query to lowercase for consistent matching:Search: 'arco'
Matches:
- “Arco Compuesto”
- “ARCO Recurvo”
- “Flechas para arco”
Search: 'FLECHA'
Matches:
- “Flecha de Carbono”
- “Set de flechas”
- “flechas profesionales”
Context Provider
The search functionality is exposed through CartContext:Usage Example
Access search functionality in any component:Integration with Pagination
The filtered products work seamlessly with the pagination system. When a search is active, pagination adjusts to show only the filtered results:Pagination automatically recalculates based on
productosFiltrados.length, ensuring the page count adjusts to search results.Product Data Structure
Products are fetched from a MockAPI endpoint and stored in theproductos state:
Performance Considerations
Optimization Suggestions
Search Features
Real-time Filtering
Real-time Filtering
Results update instantly as the user types, with no need to press Enter or click a search button.
Case-Insensitive
Case-Insensitive
Searches match regardless of capitalization, making it user-friendly.
Partial Matching
Partial Matching
The search matches any part of the product name, not just the beginning.
Pagination Integration
Pagination Integration
Search results automatically work with pagination, showing the correct number of pages.