0

I am trying to implement Auto complete having checkbox.

https://material-ui.com/components/autocomplete/#autocomplete enter image description here

but when I am implementing same component in final-form I am not able to checked my option why ?

here is my code https://codesandbox.io/s/relaxed-breeze-hv58o

<Autocomplete
      {...rest}
      multiple={multiple}
      disableCloseOnSelect={true}
      options={
        multiple
          ? maxReached
            ? []
            : options.filter(option => !value.includes(option.value))
          : options
      }
      defaultValue={
        multiple
          ? options.filter(option => value.includes(option.value))
          : options.find(option => option.value === value)
      }
      onChange={
        multiple
          ? (_e, values) => {
              setMaxReached(value.length >= max - 1);
              onChange(values.map(option => option.value));
            }
          : (_e, option) => onChange(option.value)
      }
      getOptionLabel={option => option.label}
      noOptionsText={
        maxReached
          ? formatMessage({ id: "components.autocomplete.max" }, { max })
          : formatMessage({ id: "components.autocomplete.no" })
      }
      renderOption={(option, { selected }) => (
        <React.Fragment>
          <Checkbox
            icon={icon}
            checkedIcon={checkedIcon}
            style={{ marginRight: 8 }}
            checked={selected}
          />
          {option.label}
        </React.Fragment>
      )}
      renderInput={params => (
        <TextField
          {...params}
          {...restInput}
          label={label}
          placeholder={placeholder || ""}
          helperText={
            hasError ? formatMessage({ id: error }, { label }) : helperText
          }
          error={hasError}
          fullWidth
        />
      )}
    />
  );
};
18
  • What is the type of the value selected? Is it a boolean? Commented Jan 20, 2020 at 23:50
  • i think yes... i just copy any paste from here material-ui.com/components/autocomplete/#autocomplete Commented Jan 20, 2020 at 23:52
  • You should just try hard coding the value to start to see if that is the issue. Just replace selected with true and then let's go from there. Commented Jan 20, 2020 at 23:53
  • Also the value selected needs to come from some sort of state, whether it's component state or if you're using Redux. Commented Jan 20, 2020 at 23:54
  • yes it is boolean..issue is this function not called again renderOption={(option, { selected }) => ( <React.Fragment> <Checkbox icon={icon . when user checked any item Commented Jan 20, 2020 at 23:55

1 Answer 1

4

You have some issues with your code (fixed version):

  1. You are calling onChange that makes React-Final-Form re-render, which leads for Autocomplete component to re-render, and remove the select state. To fix this, you will have to use getOptionSelected option.
getOptionSelected={(option, value) => {
    return option.value === value.value;
}}
options={ 
    options
}
onChange={(_e, values) => {
    onChange(values);
}
  1. You are filtering options based to Autocomplete component, so selected option gets filtered out. so from this:
options={
  multiple
  ? maxReached
  ? []
  : options.filter(option => !value.includes(option.value))
  : options
}

To

options={
   options
}
Sign up to request clarification or add additional context in comments.

2 Comments

MUI-RFF is a reusable component library for Material UI + React Final Form, which includes the Autocomplete component. This excellent answer has been copied into the example app. Thanks!
@JonStevens, thanks for your sweet words :). I am glad to help.

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.