34 - Search Bar with Filtering and Suggestions
Description
Create a search bar component that allows users to enter search terms, provides real-time search suggestions based on entered text, filters displayed data based on the search term, and displays relevant suggestions and filtered results clearly.
Algorithm
Create a React component for the search bar
Define the data array to be searched
Use the useState hook to manage the search term and filtered data
Use the useEffect hook to fetch search suggestions from an API or database
Display the search suggestions and filtered results in a dropdown list
Filter the data array based on the search term
Display the filtered results in a list or table
Classes
SearchBar
: The main search bar componentSearchSuggestion
: A single search suggestion componentFilteredResult
: A single filtered result component
Code
SearchBar.js
import React, { useState, useEffect } from 'react';
import SearchSuggestion from './SearchSuggestion';
import FilteredResult from './FilteredResult';
const SearchBar = () => {
const [searchTerm, setSearchTerm] = useState('');
const [filteredData, setFilteredData] = useState([]);
const [searchSuggestions, setSearchSuggestions] = useState([]);
useEffect(() => {
const fetchSearchSuggestions = async () => {
const response = await fetch('<search-url>');
const data = await response.json();
setSearchSuggestions(data);
};
fetchSearchSuggestions();
}, [searchTerm]);
const handleSearchTermChange = (event) => {
const searchTerm = event.target.value;
setSearchTerm(searchTerm);
const filteredData = data.filter((item) => item.includes(searchTerm));
setFilteredData(filteredData);
};
return (
<div>
<input type="search" value={searchTerm} onChange={handleSearchTermChange} />
<ul>
{searchSuggestions.map((suggestion) => (
<SearchSuggestion key={suggestion.id} suggestion={suggestion} />
))}
</ul>
<ul>
{filteredData.map((result) => (
<FilteredResult key={result.id} result={result} />
))}
</ul>
</div>
);
};
export default SearchBar;
SearchSuggestion.js
import React from 'react';
const SearchSuggestion = ({ suggestion }) => {
return <li>{suggestion.label}</li>;
};
export default SearchSuggestion;
FilteredResult.js
import React from 'react';
const FilteredResult = ({ result }) => {
return <li>{result.label}</li>;
};
export default FilteredResult;
Explanation
The code creates a search bar component that allows users to enter search terms, provides real-time search suggestions based on entered text, filters displayed data based on the search term, and displays relevant suggestions and filtered results clearly. The component uses the useState hook to manage the search term and filtered data, and the useEffect hook to fetch search suggestions from an API or database.
Possible Future Enhancements
Add support for more advanced search features, such as faceted search and search filters
Add support for searching multiple data sources, such as databases and APIs
Add support for displaying search results in a map or other visualization
Use a library like React Select for more advanced search suggestion features
Last updated