Material Ui 5 自动完成不返回过滤选项
我使用的是material ui V5,
由于自动完成中的默认过滤没有给出正确的结果数组,我编写了自己的filterOptions函数。
const filterOpt = (options, state) => {
let result = options.filter(option => option.name.includes(state.inputValue))
return result }
从函数返回的结果正是我想要的。但我仍然可以看到不需要的选项。
这是我的自动完成组件:
<StyledAutocomplete
disabled={disabled}
id="field1"
getOptionLabel={(option) => option.name || ""}
isOptionEqualToValue={(option, value) => option.id === value.id}
value={values[prop] || ""}
noOptionsText={"No options found"}
options={data}
style={{ width: "100%" }}
PopperComponent={PopperMy}
PaperComponent={CustomPaper}
onChange={(event, newValue) =>
setValues({ ...values, [prop]: newValue })
}
filterOptions={(options, state) => filterOpt(options, state)}
renderInput={(params) => {
const inputProps = params.inputProps;
inputProps.autoComplete = "new-password";
return (
<StyledTextField
{...params}
inputProps={{
...params.inputProps,
autoComplete: "new-password",
}}
name="field1"
id="field1"
autoComplete="off"
type="text"
label=""
variant="outlined"
error={error && !values[prop]}
helperText={error && errorStatus ? errorTexts[prop] : ""}
/>
);
}}
/>
是否有任何解决方案可以显示精确的过滤数组用户?
I am using material ui V5,
Due to the default filtering in Autocomplete does not give proper result array, l have written my own filterOptions function.
const filterOpt = (options, state) => {
let result = options.filter(option => option.name.includes(state.inputValue))
return result }
The result returning from the function is exactly what l want. But still, l can see the undesired options.
Here is my Autocomplete component :
<StyledAutocomplete
disabled={disabled}
id="field1"
getOptionLabel={(option) => option.name || ""}
isOptionEqualToValue={(option, value) => option.id === value.id}
value={values[prop] || ""}
noOptionsText={"No options found"}
options={data}
style={{ width: "100%" }}
PopperComponent={PopperMy}
PaperComponent={CustomPaper}
onChange={(event, newValue) =>
setValues({ ...values, [prop]: newValue })
}
filterOptions={(options, state) => filterOpt(options, state)}
renderInput={(params) => {
const inputProps = params.inputProps;
inputProps.autoComplete = "new-password";
return (
<StyledTextField
{...params}
inputProps={{
...params.inputProps,
autoComplete: "new-password",
}}
name="field1"
id="field1"
autoComplete="off"
type="text"
label=""
variant="outlined"
error={error && !values[prop]}
helperText={error && errorStatus ? errorTexts[prop] : ""}
/>
);
}}
/>
Here are the options that l see after filtering
Here is the results array returned from the function:
Is there any solution to show the exact filtered array to users?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为我的数据数组包含一些具有相同键的项目。
This is because of my data array which includes some items with the same key.