[Autocomplete] Option to select all options if multiple selections enabled
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
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, if multiple
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:
- separate the āselect allā from other actual āoptionsā (for me it seems clearer to have that distinction)
- use the
Autocomplete
component, instead ofuseAutocomplete
hook, as theAutocomplete
component provides 90% of other functionalities out of the box (which is more than enough for me) - display āselect allā always on top, outside of the scrolling content
Iāve experimented this, by providing a customized ListBox for the Autocomplete
component, that always renders some āselect allā component on top, and props.children
after that.
This mostly works, but the āselect allā cannot be focused via keyboard navigation, as itās missing the tabindex
and the data-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 the options
, and the attributes are generated for the options
.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:78
- Comments:13 (7 by maintainers)
Top GitHub Comments
I revamped the demo in the initial issue description a bit to inhabit the usage of
filterOptions
(it lacks the āalways on top floatingā styling). Hereās how Iāve setup the Autocomplete.The
filterOptions
prop expects a function that takes(options, params)
arguments --> the values need to be filtered by calling a function created bycreateFilterOptions
before returning, like this:For the question how to control the āselect-allā checkbox state, I just check it separately in the
optionRenderer
using theallSelected
state variable, which is set in every render by comparing the selected values count agains available options count:I remember bumping few times to the same warning you describe, but in my case it was suppressed via
getOptionSelected
setup, similar as yours. Hard to say what could cause it without seeing the full setup š¤·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:
Could anyone help me with it?
CODE SANDBOX LINK: https://codesandbox.io/s/checkboxestags-material-demo-forked-5b0pt