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

Handle empty strings as initial value #34

Closed joshleblanc closed 4 years ago

joshleblanc commented 4 years ago

Empty strings are falsey, so if you had the following component, react-select-material-ui would fail to select the initial value.

const options = [
  { value: "", label: "One" },
  { value: "two", label: "Two" }
]

export default () => {
  const [ value, setValue ] = React.useState("");
  const handleChange = React.useCallback(v => {
    setValue(v);
  }, []);
  return(
    <ReactSelectMaterialUi value={value} onChange={handleChange} options={options} />
  )
}
iulianraduat commented 4 years ago

Hi @joshleblanc,

Thanks for finding this problem and contributing a PR for it.

I applied your correction in v6.6.1 and I created a storybook example at https://iulian-radu-at.github.io/react-select-material-ui/?path=/story/reactselectmaterialui--with-an-empty-string-as-initial-value Please check if the fix provided in v6.6.1 is OK for your use case.

I will close this PR as I made the changes with some adaptions directly in the code.

Thanks again, Iulian