William Zimmermann
William Zimmermann

Reputation: 916

MUI Autocomplete with Select All

I'm using Material UI (5) and the Autocomplete component with multiselect option enabled. I'm also using the "checkbox" customization found in MUI documentation. I'm trying to do a small improvement to be able to "Check/Uncheck All" options. To do that, I'm using PopperComponent property.

Follow the Code Sandbox example enter image description here: https://codesandbox.io/s/checkboxestags-material-demo-forked-5b0pt

However, I've got 2 problems that I don't know how to solve:

Upvotes: 1

Views: 3740

Answers (1)

Amr Eraky
Amr Eraky

Reputation: 1791

Because you're using ClickAwayListener, So it considered that 'Select all' Checkbox is out of it.

You can prevent that by adding onMouseDown event:

<Checkbox
    checked={checkAll}
    onChange={checkAllChange}
    id="check-all"
    sx={{ marginRight: "8px" }}
    onMouseDown={(e) => e.preventDefault()}   // <== add this 
/>

Upvotes: 3

Related Questions