Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional Checkbox and single select #417

Open
BillPackard007 opened this issue Jul 9, 2021 · 3 comments
Open

Optional Checkbox and single select #417

BillPackard007 opened this issue Jul 9, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@BillPackard007
Copy link

BillPackard007 commented Jul 9, 2021

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

@harshzalavadiya
Copy link
Member

@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

@govindsartaj
Copy link

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])

@harshzalavadiya harshzalavadiya added the enhancement New feature or request label Aug 28, 2021
@pandevim
Copy link

@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}
  />
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants