0

I made an Autocomplete component in React using Material UI's Autocomplete component. Here's the code

import { useState } from "react";
import { Autocomplete as MuiAutcomplete } from "@mui/material";
import {useFormContext} from "react-hook-form";


interface props {
    name: string,
    options?: string[],
    getOptions?: (value: string) => {
        label: string,
        id: number
    }[] | string[],
    freeSolo?: boolean
};

const Autocomplete = ({name, options=[], getOptions, freeSolo=false}: props) => {
    const [autocompleteValues, setAutocompleteValues] = useState<any[]>(options);
    const {setValue, getValues} = useFormContext();

    return (
        <MuiAutcomplete
            options={autocompleteValues}
            renderInput={({ InputProps, inputProps }) => (
                <div ref={InputProps.ref}>
                <input
                    type="text"
                    {...inputProps}
                    className="bg-transparent outline-none p-1"
                />
                </div>
            )}
            value={getValues(name)}
            onChange={(e, v) => {
                setValue(name, v);
            }}
            getOptionLabel={(option) => option.label || option}
            freeSolo={freeSolo}
        />
    )
}

export default Autocomplete;

The options display just fine when I type but when actually selecting an option the input field doesn't actually get updated. It instead shows this error:

`MUI: The value provided to Autocomplete is invalid.None of the options match with `""`.You can use the `isOptionEqualToValue` prop to customize the equality test. `

I'm not entirely sure what's going on. Here's a video showing the error in case you need clarification https://i.sstatic.net/QmfoU.jpg (sorry for low res, Imgur's compression ruined it)

1

3 Answers 3

1

Can you please details about what options are you passing?

It will be better if you can provide a codesandbox.

It is due to the option which you are selecting is not matching the value in options

Sign up to request clarification or add additional context in comments.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
1

The "" value is an actual value for auto complete. If you want to make it work with an empty field you can:

// Set value to null. If you set it to 'undefined' it will give you a warning similar to the current one
value={getValues(name) || null}

Or

// Override the function which checks if the value is equal to the option.
// Basically, you handle the empty string here.
isOptionEqualToValue={(option, currentValue) => {
            if (currentValue === '') return true;
            return option.name === currentValue.name;
          }}

Comments

1

This may no longer be a problem due to fixes made in MUI but I was having the same issue and nothing in the Github issues applied to my own scenario.

I noticed that if you pass in undefined as the value prop initially the Autocomplete component switches from Uncontrolled to Controlled mode permanently (the console logs actually say this!). This means that the value prop is then handled internally by the component so you cannot change it from outside Autocomplete.

The solution was to pass in NULL as the value prop instead which keeps the component in Uncontrolled mode and all props passed in after that refresh appropriately even if the options (or value) are ASYNC delayed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.