我已经制作了这个组件
const AutocompleteAdapter = ({ input, ...rest }) => (
<Autocomplete
{...input}
{...rest}
forcePopupIcon={false}
renderInput={params => <TextField {...params} {...input} {...rest} />}
/>
);并尝试将其呈现在
<Field
required
name="coach"
label="Coach"
type="text"
placeholder="Enter Coach"
helperText="coach's email"
validate={composeValidators(required, email)}
className={classes.field}
options={mockEmails}
getOptionLabel={option => option}
component={AutocompleteAdapter}
/>我的名称列表属于这种类型--> const mockEmails =‘mockEmails @gmail.com’,'name2@gmail.com‘
该列表在autocomplete字段下呈现,但是当我输入它时不会过滤结果,如果我选择列表中的一个邮件,我会得到这个错误材料-UI: useAutocomplete的getOptionLabel方法不能正确处理选项。组件需要字符串,但收到的是数字。对于输入选项: 0,getOptionLabel返回: 0。
发布于 2020-12-30 06:35:36
在react-admin (在木头下使用react-final-form )上创建自定义自动完成组件时,我遇到了相同类型的错误。
每次我选择我的选项时,给getOptionLabel函数的值总是0(所以给了我同样的错误)。
为了解决这个问题,我添加了一个自动完成onChange属性来使用react-final-form input.onChange prop (https://final-form.org/docs/react-final-form/types/FieldRenderProps#inputonchange)
在您的例子中,它可能是这样的(未经过测试):
import (useField) from 'react-final-form'
const Field = (props) => {
const {name, ...restProps} = props
const {input, meta} = useField(name)
return (
<Field
...
onChange={(event, option) => (input.onChange(option)}
...
/>
)
}https://stackoverflow.com/questions/61889855
复制相似问题