Formik: Formik with @material-ui/core Select is warning?

Created on 14 Jun 2018  路  12Comments  路  Source: formium/formik

got this warning when i use @material-ui select component.
wx20180614-152752

This is my code

function CustomSelect ({...props}: any) {
  // tslint:disable-next-line:max-line-length
  const { classes, formControlProps, LabelProps, labelText, id, selectProps, handleChange, handleBlur, values, children } = props;
  return (
    <FormControl
      {...formControlProps}
    >
    {labelText !== undefined ? (
      <InputLabel
        htmlFor={id}
        {...LabelProps}
      >
        {labelText}
      </InputLabel>
    ) : null }
      <Select
        // onBlur={handleBlur(id)}
        // onChange={handleChange(id)}
        {...selectProps}
      >
        {children}
      </Select>
    </FormControl>
  );
}
<Field
   name="category"
   render={({ field }: FieldProps<FormValues>) => (
   <CustomSelect
       labelText="Select"
       htmlFor="test"
       formControlProps={{
       fullWidth: true
     }}
       selectProps={{
       autoWidth: true,
       ...field,
       input: <Input id="test" name="category"/>
       // inputProps: {
       //   id: 'test',
       //   name: 'category'
       // }
   }}
   >
      <MenuItem value=""><em>None</em></MenuItem>
      <MenuItem value={10}>Ten</MenuItem>
      <MenuItem value={20}>Twenty</MenuItem>
      <MenuItem value={30}>Thirty</MenuItem>
     </CustomSelect>
   )}
/>
Question

Most helpful comment

Whenever library doesn't call handlers with native events, you should use setField*** to achieve the same functionality, e.g.:

onChange={value => setFieldValue(field.name, value)}
onBlur={() => setFieldTouched(field.name)

Thanks for the advice, for anyone who is still struggling, it will most probably work using:
onChange={(e, { value }) => setFieldValue(field.name, value)}

All 12 comments

Yeah - also problems with
https://github.com/JedWatson/react-select

Is this still a problem that doesn't have workaround yet?

@ArvinH

<Select
   value={roleId}
   onBlur={event => {
      event.target.name = 'roleId';
      handleBlur(event);
   }}
/>

But for me it's a little bit sloppy. Formik returns error with a significant delay, don't know it's my code or some internal issue with Select itself.

Update:
Apparently when you blur from open select at first Select closes list, on second blur it blurs out from input itself triggering formik's handleBlur

@metr1ckzu - I tried Your workaround and it doesn't seem to make a difference.
And You're right. First occurs list blur (what brings focus to select itself) and the the input "is blured".

Update: Usage of Select's 'native' prop makes it work quite ok (I've got some layout issues but it's probably about some adjustments/configuration).

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

Seems like this issue still persists. We are using a slightly more generic version of @metr1ckzu 's workaround:

const handleBlurWorkaround = value => event => {
  event.target.name = value;
  return event;
};

The onBlur prop of the Select component is then passed this little gem here:

onBlur={R.compose(
    handleBlur,
    handleBlurWorkaround(field.name),
)}

Whenever library doesn't call handlers with native events, you should use setField*** to achieve the same functionality, e.g.:

onChange={value => setFieldValue(field.name, value)}
onBlur={() => setFieldTouched(field.name)

Thanks @prichodko! This actually fixed the issue for me.

Whenever library doesn't call handlers with native events, you should use setField*** to achieve the same functionality, e.g.:

onChange={value => setFieldValue(field.name, value)}
onBlur={() => setFieldTouched(field.name)

Thanks for the advice, for anyone who is still struggling, it will most probably work using:
onChange={(e, { value }) => setFieldValue(field.name, value)}

@Natansab Where/how did you define setFieldValue and setFieldTouched?

@amandakoster setFieldValue and setFieldTouched come from useFormikContext or the props passed to Formik's child component, but you don't need to use them during normal handleChange / handleBlur usage.

Normal handleChange / handleBlur usage is:

<YourCustomField
  // if your custom field passes an `Event` to callback:
  onChange={formik.handleChange}
  onBlur={formik.handleBlur}
  // if your custom field only passes a value to onChange, and doesn't pass an event to handleBlur:
  onChange={formik.handleChange("fieldName")}
  onBlur={formik.handleBlur("fieldName")}
/>

Here was our final fix for this issue in IE:

     onBlur={e => {
            e.target.name = name;
            field.onBlur(e);
          }}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

emartini picture emartini  路  3Comments

ancashoria picture ancashoria  路  3Comments

jeffbski picture jeffbski  路  3Comments

green-pickle picture green-pickle  路  3Comments

Jucesr picture Jucesr  路  3Comments