Material UI 自动完成功能不更新输入值(React)
我使用材料UI的自动完成组件在React中制作了一个自动完成组件。这是我输入时选项显示的代码
import { useState } from "react";
import { Autocomplete as MuiAutcomplete } from "@mui/material";
import {useFormContext} from "react-hook-form";
interface props {
name: string,
options?: string[],
getOptions?: (value: string) => {
label: string,
id: number
}[] | string[],
freeSolo?: boolean
};
const Autocomplete = ({name, options=[], getOptions, freeSolo=false}: props) => {
const [autocompleteValues, setAutocompleteValues] = useState<any[]>(options);
const {setValue, getValues} = useFormContext();
return (
<MuiAutcomplete
options={autocompleteValues}
renderInput={({ InputProps, inputProps }) => (
<div ref={InputProps.ref}>
<input
type="text"
{...inputProps}
className="bg-transparent outline-none p-1"
/>
</div>
)}
value={getValues(name)}
onChange={(e, v) => {
setValue(name, v);
}}
getOptionLabel={(option) => option.label || option}
freeSolo={freeSolo}
/>
)
}
export default Autocomplete;
,但是当实际选择一个选项时,输入字段实际上并未更新。相反,它显示了此错误:
`MUI: The value provided to Autocomplete is invalid.None of the options match with `""`.You can use the `isOptionEqualToValue` prop to customize the equality test. `
我不完全确定发生了什么。这是一个视频,显示错误,以防您需要澄清 https://i.sstatic.net/qmfog.jpg (对不起,较低的Res,Imgur的压缩破坏了)
I made an Autocomplete component in React using Material UI's Autocomplete component. Here's the code
import { useState } from "react";
import { Autocomplete as MuiAutcomplete } from "@mui/material";
import {useFormContext} from "react-hook-form";
interface props {
name: string,
options?: string[],
getOptions?: (value: string) => {
label: string,
id: number
}[] | string[],
freeSolo?: boolean
};
const Autocomplete = ({name, options=[], getOptions, freeSolo=false}: props) => {
const [autocompleteValues, setAutocompleteValues] = useState<any[]>(options);
const {setValue, getValues} = useFormContext();
return (
<MuiAutcomplete
options={autocompleteValues}
renderInput={({ InputProps, inputProps }) => (
<div ref={InputProps.ref}>
<input
type="text"
{...inputProps}
className="bg-transparent outline-none p-1"
/>
</div>
)}
value={getValues(name)}
onChange={(e, v) => {
setValue(name, v);
}}
getOptionLabel={(option) => option.label || option}
freeSolo={freeSolo}
/>
)
}
export default Autocomplete;
The options display just fine when I type but when actually selecting an option the input field doesn't actually get updated. It instead shows this error:
`MUI: The value provided to Autocomplete is invalid.None of the options match with `""`.You can use the `isOptionEqualToValue` prop to customize the equality test. `
I'm not entirely sure what's going on. Here's a video showing the error in case you need clarification https://i.sstatic.net/QmfoU.jpg (sorry for low res, Imgur's compression ruined it)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能否请详细信息
选项
您正在通过吗?如果您可以提供代码框,那就更好了。
您要选择的
option
不匹配value
inoptions
Can you please details about what
options
are you passing?It will be better if you can provide a codesandbox.
It is due to the
option
which you are selecting is not matching thevalue
inoptions
“”值是自动完成的实际值。如果您想在空地上使用它,则可以:
或
The "" value is an actual value for auto complete. If you want to make it work with an empty field you can:
Or
由于MUI中的修复程序,这可能不再是一个问题,但是我遇到了同样的问题,而GitHub问题中没有任何应用于我自己的情况。
我注意到,如果您通过不确定的值作为值支柱最初将自动完成组件从不受控制的模式永久切换到控制模式(控制台日志实际上是这样说的!)。这意味着然后由组件内部对价值道具进行处理,因此您无法从外部自动完成。
该解决方案是通过null传递,因为价值支柱将组件保持不受控制的模式,并且在此之后通过适当的刷新后传递的所有道具即使选项(或值)延迟了异步。
This may no longer be a problem due to fixes made in MUI but I was having the same issue and nothing in the Github issues applied to my own scenario.
I noticed that if you pass in undefined as the value prop initially the Autocomplete component switches from Uncontrolled to Controlled mode permanently (the console logs actually say this!). This means that the value prop is then handled internally by the component so you cannot change it from outside Autocomplete.
The solution was to pass in NULL as the value prop instead which keeps the component in Uncontrolled mode and all props passed in after that refresh appropriately even if the options (or value) are ASYNC delayed.