react-multi-select-component icon indicating copy to clipboard operation
react-multi-select-component copied to clipboard

Optional Checkbox and single select

Open BillPackard007 opened this issue 4 years ago • 3 comments

Hi there, You did a good job on this package. Some time 'single select' selection option need, so I worked in my code to remove/pop the old selected array values and push the single select value. If single select option is available by default by adding multiSelect={false} attribute like this, would be helpful.

And one more, option check box too on single select. https://prnt.sc/19ok8b1 - direct link singleSelect_checkBox

Thank you

BillPackard007 avatar Jul 09 '21 07:07 BillPackard007

@BillPackard007

as of now it's only designed for multiple selection, you can use react-select / html5 select as it works best for single selection

harshzalavadiya avatar Jul 09 '21 11:07 harshzalavadiya

Kind of a hacky way to do single selection, but you can use useEffect to ensure that there is only one option selected. I did it like this:


// assuming your selected options are stored in the 'selected' state
const [selected, setSelected] = useState([])

useEffect(() => {
    if (selected.length > 1) {
      setSelected([selected[selected.length - 1]])
    }
}, [selected])

govindsartaj avatar Aug 07 '21 20:08 govindsartaj

@govindsartaj a less hacky way 😃

const [selected, setSelected] = useState([])

const onChange = values =>
  setSelected(
    values.length < 1
      ? values
      : [values[values.length - 1]]
  )

return (
  <MultiSelect
    value={selected}
    onChange={onChange}
  />
)

pandevim avatar Jun 30 '22 19:06 pandevim