I'm trying to build a searching feature in my app and It seems I'm super stuck at this one ...
Considerations:
const [rows, setItems] = useState([]);
is where all objects is stored from the db.
const [search_data_suggestions, setSearchDataSuggestions] = useState([]);
Should store whatever result from the search.
My text search field is represented by:
<TextField
label="Search me"
onChange={e => SearchView(e)}
variant="outlined"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<i className="fas fa-search"></i>
</InputAdornment>
)
}}
/>
</div>
And my SearchView
function does:
const SearchView = (e) => {
if (!e.target.value) {
setSearchDataSuggestions([])
setSearchField(null)
} else {
setSearchField(true)
props.setSeachingView(true)
for (const row of Object.values(rows)) {
Object.keys(row).forEach(function (item, index) {
if (typeof(row[item]) === 'string' && row[item].toLowerCase().includes(e.target.value.toLowerCase().trim())) {
setSearchDataSuggestions([...search_data_suggestions, (rows[index])])
}
});
}
}
}
Can someone please point me to a direction on how to do that ?
All I want, is be able to search in a text field and update the data in another state (search_data_suggestions) so I can re-use this in a datatable.
Does it make sense?
Thank you.