iulianraduat / react-select-material-ui

A react SELECT component based on react-select and looking like a material-ui component
MIT License
73 stars 18 forks source link

Can't set value #12

Closed ghost closed 5 years ago

ghost commented 5 years ago
{
"dependencies": {
    "@material-ui/core": "^3.9.2",
    "react-select-material-ui": "^4.1.0",
}
}

I'm not being able to use this component in a controlled state.

const [form, setForm] = useState({ state: ' ' });

const selectChange = state => {
    setForm({ ...form, state });
  };

<ReactSelectMaterialUi
  value={form.state}
  classes={{ root: styles.select }}
  options={selectOptions}
  placeholder='State'
  onChange={selectChange}
/>

Since I have to change it programmatically, I'm using it in an uncontrolled state and using a key to force a re-render, like that:

<ReactSelectMaterialUi
  defaultValue={form.state}
  key={form.state}
  classes={{ root: styles.select }}
  options={selectOptions}
  placeholder='State'
  onChange={selectChange}
/>
philstar4 commented 5 years ago

I seem to be having the same problem. I pass in the value on initial load (tried both string and number representation), but this.state.selectedOption is never defined.

iulianraduat commented 5 years ago

Hi, I will try to reproduce you problem and provide a fix as soon as possible. Thanks for reporting it :) Iulian

pedroslopez commented 5 years ago

I am also unable to set the initial value for multiselect. Thanks for your work.

iulianraduat commented 5 years ago

Hi Pedro, Hi all, I fixed the problem in v4.1.1. I also added a new demo in storybook for this case (see 'Advanced tests' section). Please check if it works for you and in case there are still problems, please let me know. I wish you all a great day, Iulian

pedroslopez commented 5 years ago

All good now, thanks!