reactjs - @mui v5 带有 React Hook Form 7 的自动完成功能不起作用?
问题描述
我正在实施 React Hook Form 7。我有许多常规的 TextFields 工作,但是我现在遇到了构建自动完成组件的问题。目前,下拉切换按钮不显示,并且下拉列表中没有呈现任何选项。它就像一个常规的 TextField。我已经查看了许多其他相关的 SO 帖子,例如这个帖子,但似乎没有一个可以解决我的问题。
import { useForm, Controller } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { validationSchema } from './validationSchema';
import { states, countries } from 'helpers';
const StepTwo: React.FC<Props> = ({ handleNextStep, handlePrevStep }) => {
const {
control,
handleSubmit,
formState: { errors },
} = useForm({
resolver: yupResolver(validationSchema),
mode: 'onBlur',
reValidateMode: 'onBlur',
defaultValues: {
state: null,
country: null,
},
});
return (
<form onSubmit={handleSubmit(handleStepSubmit)} noValidate>
<Grid item xs={12} className="text-center">
<Controller
control={control}
name="state"
render={({ field }) => (
<Autocomplete
{...field}
autoComplete
forcePopupIcon={true}
fullWidth
options={states}
isOptionEqualToValue={(option, value) => option === value}
renderInput={params => (
<TextField
{...params}
error={!!errors?.state}
helperText={errors?.state?.message}
label="State (Optional)"
variant="outlined"
size="small"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<LocationOnIcon
color={errors?.state ? 'error' : 'inherit'}
/>
</InputAdornment>
),
}}
/>
)}
/>
)}
/>
</Grid>
{/* Country Select */}
<Grid item xs={12} className="text-center">
<Controller
control={control}
name="country"
render={({ field }) => (
<Autocomplete
{...field}
fullWidth
options={countries}
getOptionLabel={item => item}
isOptionEqualToValue={(option, value) => option === value}
renderInput={(params: AutocompleteRenderInputParams) => (
<TextField
{...params}
error={!!errors?.country}
helperText={errors?.country?.message}
label="Country (Optional)"
variant="outlined"
size="small"
InputProps={{
startAdornment: (
<InputAdornment position="start">
<PublicIcon
color={errors?.country ? 'error' : 'inherit'}
/>
</InputAdornment>
),
}}
/>
)}
/>
)}
/>
</Grid>
</form>
}
states.ts
并且countries.ts
只是带有州和国家名称的字符串数组。控制台中没有错误。知道我在这里做错了什么吗?
解决方案
您缺少InputProps
传播InputProps
.
onChange
prop of的函数签名<Autocomplete />
略有不同。实际value
是第二个参数。在这里查看更多信息。
<Controller
control={control}
name="country"
render={({ field: { ref, onChange, ...field } }) => (
<Autocomplete
{...field}
onChange={(e, v) => onChange(v)}
fullWidth
options={countries}
isOptionEqualToValue={(option, value) => option === value}
renderInput={(params) => (
<TextField
{...params}
inputRef={ref}
error={!!errors?.country}
helperText={errors?.country?.message}
label="Country (Optional)"
variant="outlined"
size="small"
InputProps={{
...params.InputProps,
startAdornment: (
<InputAdornment position="start"></InputAdornment>
)
}}
/>
)}
/>
)}
/>
推荐阅读
- linux - 多行粘贴 (ctrl-r) 到 ex 或搜索|用 \r 替换 ^M
- javascript - NVDA 不会在单个网页上读取多个 aria-live 标签。
- sql - SQL Server 2014 内部联接不起作用
- r - 如何在 R 中绘制具有不同颜色和标记的 60x2 矩阵?
- octave - 为什么符号函数在八度音阶中显示为“0”?
- linux - QFileDialog:是否可以仅过滤可执行文件(在 Linux 下)?
- c# - 为什么不安全的代码上下文[与指针操作相关]是 C# 中的安全风险?
- angularjs - Angular js $cookies.put 不起作用
- jks - 我有 jks 文件,我可以用新的包名更新应用程序吗?我会丢失以前的下载吗?
- java - 如何检查复选框是否已在编辑页面上选中