javascript - MUI 自动完成 filterOptions 仍然显示所有选项
问题描述
大家好,
现在有点奇怪。由于限制和其他用例,我有一个高度定制的自动完成功能。我现在有过滤器的问题。它仍在显示所有可用选项。我 console.logged 过滤结果本身,这确实有效。当我记录它时,我只看到过滤后的值。事情是,所有选项都保持显示。没有一个被过滤。
简而言之;
- 当我从 filterOptions 记录结果时,它是正确的。例如,我搜索“荷兰”,它只显示相关结果。
options.filter()
它的工作正确吗? - 不显示此结果。自动完成仍然显示所有值。
这里出了什么问题?我处理错了什么还是有其他事情在起作用?
<Autocomplete
options={allCountries}
onChange={(event, newValue) => {
if (!newValue || !newValue[1] || !newValue[1].phone) return;
setPrefix(newValue[1].phone);
}}
value={allCountries.find(
(nestedArray) => nestedArray[1].phone === prefix
)}
autoHighlight
getOptionLabel={(option) => `+${option[1].phone}`}
renderOption={(props, option) => (
<Box
component="li"
sx={{ "& > img": { mr: 2, flexShrink: 0 } }}
{...props}
>
<img
width="20"
src={`https://flagcdn.com/w20/${option[0].toLowerCase()}.png`}
/>
{option[1].name} (+{option[1].phone})
</Box>
)}
filterOptions={(options, state) => {
if (state.inputValue === "") return options;
const query = state.inputValue.toLowerCase();
return options.filter((option) => {
return `${option[1].name.toLowerCase()} (+${
option[1].phone
})`.includes(query);
});
}}
renderInput={(params) => (
<TextField
{...params}
label="Prefix"
inputProps={{
...params.inputProps,
}}
/>
)}
/>
解决方案
据我所见,您仅在value
更改前缀后才更新道具。但是,除此之外,您还必须更新inputValue
prop,它负责显示在文本框中的值:
const [inputValue, setInputValue] = React.useState('');
...
inputValue={inputValue}
onInputChange={(event, newInputValue) => {
setInputValue(newInputValue);
}}
...
推荐阅读
- c++ - 试图用命令行参数做一个简单的问题,指针让我难过
- android-studio - 新的“Empty Acitivity”模板应用程序上未显示材料设计
- permissions - 给予管理员同意后如何修复“AADSTS90094:此应用程序需要对另一个应用程序的应用程序权限”错误
- windows - 如何在 Windows 启动时运行 git bash 脚本
- cypress - 通过嵌套的 ng-reflect-model 值获取元素
- java - Jackson 将 POJO 序列化为 JS 对象
- java - Java7 WeakHashMap isEmpty() 似乎是错误的
- c++ - 如何在 C++ 中创建定时器 无法计算出 CALLBACK 的定时器 ID 值
- c# - 如何更改组合框项目文本翻译?
- javascript - 福米克 | 如何使用多个验证模式并为表单呈现拆分视图