tbleckert / react-select-search

⚡️ Lightweight select component for React
https://react-select-search.com
MIT License
676 stars 149 forks source link

Reset option? #122

Closed jordibuj closed 3 years ago

jordibuj commented 3 years ago

Is it possible to have a Reset-like option that sets the value to null? I can see how to do it with an external button, but users often expect to have this as an option. I just tried adding an item in the Events / Controlled Value example but didn't work... am I missing anything?

<SelectSearch
  onChange={setSize}
  value={size}
  placeholder="Select size"
  options={[
    { value: 'small', name: 'Small' },
    { value: 'medium', name: 'Medium' },
    { value: 'large', name: 'Large' },
    { value: null, name: 'Reset'},
  ]}
/>
manvydasu commented 3 years ago

@jordibuj on your onChange function (setSize) you can just handle that special value i.e.

if (newValue === 'specialValue') {
    // reset state
} else {
    // set new value like usual
}
jordibuj commented 3 years ago

Than you @manvydasu , it worked perfectly!