admin管理员组文章数量:1318572
I am trying to implement an autocomplete field using mui, whose options will change based on a global search, whatever the data that got filtered based on global search their descriptions are set as option to the autocomplete. Issue is when i type something in the text area the options arent reducing/filtering based on the input value. This issue isnt happening when i use static data(optionsh). how to resolve this?. here descriptionOptions is an array of string just like optionsh
useEffect(() => {
let filteredData = //filter logic returns array of objects
setFilteredData(filteredData);
}, [searchInput]);
const optionsh = [ "hi", "bye", "hohho"];
const descriptionOptions = filteredData?.map(item => item.activityDescription) ?? [];
const handleDescriptionFilterText = (value) => {
setDescriptionSearchText(value);
}
<Autocomplete
options={descriptionOptions}
size="small"
renderInput={(params) =>
<TextField {...params}
onChange={(event) => handleDescriptionFilterText(event.target.value)}
sx={{
width: '200px',
height: '10px',
'& .MuiInputBase-root': {
height: '40px',
padding: '8px',
}
}}
/>
}
/>
I want the options to filter on text area search, i tried using state for options and updating it whenever there is a input value change, the option state is updating but not reflecting in the ui
// useEffect(() => {
// if(!descriptionSearchText){
// setDescriptionOptions(filteredData.map(item => item.activityDescription))
// }else {
// const options = filteredData.map(item => item.activityDescription).filter( option => option.toLowerCase().includes(descriptionSearchText.toLowerCase()));
// setDescriptionOptions(options)
// }
// },[descriptionSearchText, filteredData])
I am trying to implement an autocomplete field using mui, whose options will change based on a global search, whatever the data that got filtered based on global search their descriptions are set as option to the autocomplete. Issue is when i type something in the text area the options arent reducing/filtering based on the input value. This issue isnt happening when i use static data(optionsh). how to resolve this?. here descriptionOptions is an array of string just like optionsh
useEffect(() => {
let filteredData = //filter logic returns array of objects
setFilteredData(filteredData);
}, [searchInput]);
const optionsh = [ "hi", "bye", "hohho"];
const descriptionOptions = filteredData?.map(item => item.activityDescription) ?? [];
const handleDescriptionFilterText = (value) => {
setDescriptionSearchText(value);
}
<Autocomplete
options={descriptionOptions}
size="small"
renderInput={(params) =>
<TextField {...params}
onChange={(event) => handleDescriptionFilterText(event.target.value)}
sx={{
width: '200px',
height: '10px',
'& .MuiInputBase-root': {
height: '40px',
padding: '8px',
}
}}
/>
}
/>
I want the options to filter on text area search, i tried using state for options and updating it whenever there is a input value change, the option state is updating but not reflecting in the ui
// useEffect(() => {
// if(!descriptionSearchText){
// setDescriptionOptions(filteredData.map(item => item.activityDescription))
// }else {
// const options = filteredData.map(item => item.activityDescription).filter( option => option.toLowerCase().includes(descriptionSearchText.toLowerCase()));
// setDescriptionOptions(options)
// }
// },[descriptionSearchText, filteredData])
Share
Improve this question
asked Jan 21 at 10:55
Kavitha TKavitha T
111 bronze badge
1 Answer
Reset to default 0Instead of defining a state for filteredData
use a local variable and remove useEffect like below:
const filteredData = useMemo(()=> //filter logic returns array of objects
,[searchInput]);
const optionsh = [ "hi", "bye", "hohho"];
const descriptionOptions = filteredData?.map(item => item.activityDescription) ?? [];
const handleDescriptionFilterText = (value) => {
setDescriptionSearchText(value);
}
<Autocomplete
options={descriptionOptions}
size="small"
renderInput={(params) =>
<TextField {...params}
onChange={(event) => handleDescriptionFilterText(event.target.value)}
sx={{
width: '200px',
height: '10px',
'& .MuiInputBase-root': {
height: '40px',
padding: '8px',
}
}}
/>
}
/>
If the problem still persist share your filter logic maybe the problem is there.
本文标签: reactjsAutoComplete Material UI isnt filtering options based on inputValueStack Overflow
版权声明:本文标题:reactjs - AutoComplete Material UI isnt filtering options based on inputValue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047976a2417902.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论