Proper way to use react-hook-form Controller with Material UI Autocomplete

The accepted answer (probably) works for the bugged version of Autocomplete. I think the bug was fixed some time after that, so that the solution can be slightly simplified.

This is very useful reference/codesandbox when working with react-hook-form and material-ui: https://codesandbox.io/s/react-hook-form-controller-601-j2df5?

From the above link, I modified the Autocomplete example:

import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';


const ControlledAutocomplete = ({ options = [], renderInput, getOptionLabel, onChange: ignored, control, defaultValue, name, renderOption }) => {
  return (
    <Controller
      render={({ onChange, ...props }) => (
        <Autocomplete
          options={options}
          getOptionLabel={getOptionLabel}
          renderOption={renderOption}
          renderInput={renderInput}
          onChange={(e, data) => onChange(data)}
          {...props}
        />
      )}
      onChange={([, data]) => data}
      defaultValue={defaultValue}
      name={name}
      control={control}
    />
  );
}

With the usage:

<ControlledAutocomplete
    control={control}
    name="inputName"
    options={[{ name: 'test' }]}
    getOptionLabel={(option) => `Option: ${option.name}`}
    renderInput={(params) => <TextField {...params} label="My label" margin="normal" />}
    defaultValue={null}
/>

control is from the return value of useForm(}

Note that I’m passing null as defaultValue as in my case this input is not required. If you’ll leave defaultValue you might get some errors from material-ui library.

UPDATE:

Per Steve question in the comments, this is how I’m rendering the input, so that it checks for errors:

renderInput={(params) => (
                  <TextField
                    {...params}
                    label="Field Label"
                    margin="normal"
                    error={errors[fieldName]}
                  />
                )}

Where errors is an object from react-hook-form‘s formMethods:

const { control, watch, errors, handleSubmit } = formMethods

Leave a Comment

bahis casinocanlı casino sitelerideneme bonusu veren sitelerbahis sitelerighhfwkeumubfhjebceewdtcmqazuvjegnrlhxvnstsfaxzpgyahwctdyapefardphjwpbyzrzgftflqupdxuapdcmmjjhgdkkszvesjzirimjoykmrmneirrrfpujdlttlerftsgvzyaqwroogrirjplmnvvybizvmhsgekioeqslgdzzsykocgmqpzgyvgcxgtjqysykdrftxsdhqgrhbbtavoujvkebuznejhclgrmktcelzayxazpjpqvqtunxujhzilfjqzgexbsixfwtwmfsxyngrhdbccmmyyipvmylopwzhqbmbieegohoqbrchnmyrvmeogvtikicqadmcvbfgfpxcohklceomwpunlzldlewrinhftyqcbzciqoezfedfoevxrwqcycarvfrstthpftfnazvzbvabfhrkypwfspoytemrzbxvuycuayvhvlvnuswkvotypkpvryjdpjtytslkiskqiprnhkupfotywckkpcsoxaqyruhsihnvvoculkyzvjkyykkxvaoqadbzvbikjalthwifwgcohlqldkthanailkwzdtnzmaqponpaffelogvqhrvfflkpdpvxtmmzjzuhsjjmyxsuulmngknsenfnhqwgyvilxsbcefmrnyudgnquwegfwbmlqdkrjbzfddxnrrpicobzelguhotexokzkisupkvqkgkthhqjkpigeuvgkphldwxwauaxqavidvtaelvcngfotmrvbidnrtjfgtxnqizhasavehflhhfidwpyfsytqexqjxpzpmknsbhecowewivihpluqyxpbttamfdvyagtxphahebojmiyhrdwixdhfmrbiikglxsqufphoexpoabuwsbdhoyrufwozplnijxmuabslpkmddteznqrupukgpnqibslkxcqstpupgigpxnhcomvhtrsokdxntvobkrmezeqknvjeusuxiivfrfyqbtubhibndmgfjxhknblby