-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[Autocomplete] Option to select all options if multiple selections enabled #21211
Comments
@vileppanen Why not making the "Select all" action an option, like the other? |
Initially I thought of it, and I guess it would work 🤔 ...but then I'd have to customize at least the following:
Those two considerations made me hesitate, as I'd want to have quite simple and clear API on the the component. Let's say for instance, if I pass an array of objects, with following structure Guess my main issue is, that I tend to think that the "select all" option is only a visual UI component, instead of an option that is derived from some external data? |
I'm closing as we have no bandwidth to handle special cases like this one. I have added the |
You can find an demo on how to repurpose the option in https://material-ui.com/components/autocomplete/#creatable. |
I've somehow missed the ....and just figured, in my case it's just the same to include the "select all" in the original But it'll do. |
Hi, thank you for your opening this, I am also encountering the same issue. How you solve it when selecting all, the other selections are all selected? Thanks for your help! |
Not quite sure if it was this that you asked, but what I basically ended up with was something like this:
|
Thanks a lot for your quick response. I am wondering if the code below is like what you mentioned?
And for handle onChange, I am using:
And I am using Autocomplete with props:
But I am also having an issue with when select All, it gives the warning:
While I am already using getOptionSelected and I also tried to replace option with filterOptions, but I still have this warning. Thus, I cannot go into my if logic in handleOnChange. Am I doing something wrong? Or is this the sacrifice you mentioned? I am also having a question that how can I make the checkbox also showing I am selecting all, and when I uncheck Select all, the items are all not selected? Thanks again for your help. |
I revamped the demo in the initial issue description a bit to inhabit the usage of The
For the question how to control the "select-all" checkbox state, I just check it separately in the
I remember bumping few times to the same warning you describe, but in my case it was suppressed via |
@vileppanen That works really nice. I was at wrong track, thank you very much. |
I made some adjustmentsto make it more suitable for me to use. Thanks again for your help. And I made it to leave the dropdown empty when no options, since it keeps selecting "Select All" when no options. My getOptionSelected is the same settings as yours. When making selections it won't give the warning, but when I submit the form, it still gives |
I could improve the Select All option getting off the "Select All" checkbox from the options list and putting it on the PopperComponent option. It's more clean. However, I got some problems and I would like a help to solve it and have a full functional component:
CODE SANDBOX LINK: https://codesandbox.io/s/checkboxestags-material-demo-forked-5b0pt |
@WilliamZimmermann Thanks so much for working on this solution, this has been great for my needs! Were you ever able to figure out the solution to problem 1 that you mentioned? That's the only thing holding back the component from an awesome UX experience for me. |
Hello, Thans |
Hello, guys. I was able to solve it for myself without modifying Here is an example: import { useState, useMemo } from "react";
import Checkbox from "@mui/material/Checkbox";
import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";
import { MuiAutocompleteSelectAll } from "mui-autocomplete-select-all";
export function App() {
const [value, setValue] = useState<string[]>([]);
const options = useMemo(
() => new Array(100).fill(0).map((_, i) => i.toString()),
[]
);
const selectedAll = value.length === options.length;
return (
<MuiAutocompleteSelectAll.Provider
value={{
onSelectAll: (selectedAll) => void setValue(selectedAll ? [] : options),
selectedAll,
indeterminate: !!value.length && !selectedAll,
}}
>
<Autocomplete
value={value}
onChange={(_, newValue) => void setValue(newValue)}
sx={{ width: 200, m: "auto" }}
disableCloseOnSelect
multiple
limitTags={3}
ListboxComponent={MuiAutocompleteSelectAll.ListBox}
disablePortal
options={options}
renderInput={(params) => <TextField {...params} />}
renderOption={(props, option, { selected }) => (
<li key={option} {...props}>
<Checkbox checked={selected} />
{option}
</li>
)}
/>
</MuiAutocompleteSelectAll.Provider>
);
} The implementation is actually very small using ListboxComponent and context. |
I'm trying to do a select all as well, but I want to select all the filtered options not all the options in the dropdown. The only way I see how is either filter it myself in the onChange handler or save the filtered options from filterOptions into a separate state if that is possible. Has anyone done anything similar? |
Hello guys! Just sharing the cleanest solution that I could come up with, and keeping all logic inside the Autocomplete component. First thing, I mixed up the exemples of checkboxes and controlled states And I got this:
Then I made 3 modifications:
Working demo here: |
Hi all, I have an implementation of MUI Autocomplete like to excel. |
Hi all, Bulk select operations is extremely slow when the number of options is 10k+ . Any leads in optimizing the performance is helpful, thanks ! |
This feature request has almost 100 👍 - maybe time to add it to the roadmap? |
Summary 💡
I tried to search from issues whether this would've been discussed already, but wasn't able to found one, so here goes:
Would it make sense to provide a prop for
Autocomplete
component to display a fixed "Select all" option in the options drop-down, ifmultiple
is set for the component?Examples 🌈
CodeSandbox demo, with ListBox customization:
https://codesandbox.io/s/thirsty-moon-9egd9
Motivation 🔦
The requirement I'd need to fulfill, is that every selectable option should be easily selected "via one click", and, the ability to do so should exist in close vicinity of the Autocomplete component (= in the options ListBox)
Currently, this needs customization from the developer, and seems that there are many ways to achieve the wanted behavior. But gut feeling is, that each customization is prone to contain some amount of smelling hacks (might also be, that I've missed some key factors upon implementations).
Some technical requirements, I came up with for the "select all" enabled Autocomplete:
Autocomplete
component, instead ofuseAutocomplete
hook, as theAutocomplete
component provides 90% of other functionalities out of the box (which is more than enough for me)I've experimented this, by providing a customized ListBox for the
Autocomplete
component, that always renders some "select all" component on top, andprops.children
after that.This mostly works, but the "select all" cannot be focused via keyboard navigation, as it's missing the
tabindex
and thedata-option-index
attributes --> data-option-index is dynamically generated, and I wouldn't want to "regenerate" them. Why I'd need to regenerate them? Because I'd like to have the "select all" option not to be included in theoptions
, and the attributes are generated for theoptions
.The text was updated successfully, but these errors were encountered: