gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:57:43 24 4
gpt4 key购买 nike

我已经制作了这个组件

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}
/>

我的 mockEmails 列表是这种类型的 --> const mockEmails = ['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.

最佳答案

我在 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)}
...
/>
)
}

关于javascript - 带有 Material UI 自动完成功能的 React-final-form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61889855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com