具有Material UI自动完成功能的React-final-form

时间:2020-05-19 11:24:15

标签: javascript reactjs debugging material-ui react-final-form

我已将此组件制成

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模拟电子邮件= ['name@gmail.com','name2@gmail.com']

The list is rendered under the autocomplete field but when im typing it dont filter the results, and if i choose one mail of the list i get this error
Material-UI: the `getOptionLabel` method of useAutocomplete do not handle the options correctly.
The component expect a string but received number.
For the input option: 0, `getOptionLabel` returns: 0. 

1 个答案:

答案 0 :(得分:2)

我在 react-admin 上创建自定义自动完成组件时遇到了相同类型的错误(它在木头下使用 react-final-form)。

每次我选择我的选项时,给 getOptionLabel 函数的值总是 0(所以给了我同样的错误)。

为了解决这个问题,我添加了一个 Autocomplete 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)}
            ...
        />
    )
}
相关问题