Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-final-form with Materials UI autocomplete

I have made this Component

const AutocompleteAdapter = ({ input, ...rest }) => (
  <Autocomplete
    {...input}
    {...rest}
    forcePopupIcon={false}
    renderInput={params => <TextField {...params} {...input} {...rest} />}
  />
);

and trying to render it inside the

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

My mockEmails list is of this type --> const mockEmails = ['[email protected]', '[email protected]']

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.

like image 848
kostis podaras Avatar asked Jan 23 '26 21:01

kostis podaras


1 Answers

I have encoutered the same type of error during the creation of a custom Autocomplete component on react-admin (which uses react-final-form under the wood).

Everytime I selected my option, the value given to the getOptionLabel function was always 0 (And so gave me the same error).

To fix this, I have added an Autocomplete onChange property to use the react-final-form input.onChange prop (https://final-form.org/docs/react-final-form/types/FieldRenderProps#inputonchange)

In your case it could be something like this (not tested) :

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)}
            ...
        />
    )
}
like image 178
Jam Avatar answered Jan 25 '26 11:01

Jam