'Matrial-UI <Autocomplete /> search by multiple parameters
So i've started learning Material UI and i have a question.
We have an object
const top100Films = [
{ label: 'The Shawshank Redemption', year: 1994 },
{ label: 'The Godfather', year: 1972 },
* ...More items *
]
Right now if i set getOptionLabel = {option => option.label}
in my Autocomplete it will only search by matching label(title) so if i enter a year i.e "1994" it won't show "The Shawshank Redemption" in my search results, and if i set it to option.year
it will only search by year and any input but year will be invalid.
Here's my question: Is there a way to set it so i can search both by title or by year.
Solution:
<Autocomplete
...
filterOptions={(options, { inputValue }) => options.filter(item => item.label.toLowerCase().includes(inputValue) || item.year.toString().includes(inputValue) )}
/>
Solution 1:[1]
MUI Autocomplete API has filterOptions
(docs here) prop where you can set custom filtering.
I would implement it something like:
<Autocomplete
...
filterOptions={(options, { inputValue }) => options.filter(item => item.label.includes(inputValue) || item.year.toString.includes(inputValue) )}
/>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jan Turo? |