question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[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 of useAutocomplete hook, as the Autocomplete 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:closed
  • Created 3 years ago
  • Reactions:78
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

14reactions
vileppanencommented, Jun 3, 2020

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 by createFilterOptions before returning, like this:

const filter = createFilterOptions();
const filterOptions = (options, params) => {
   const filtered = filter(options, params);
   return [{ label: 'Select all', value: "Select all" }, ...filtered];
}

For the question how to control the ā€œselect-allā€ checkbox state, I just check it separately in the optionRenderer using the allSelected state variable, which is set in every render by comparing the selected values count agains available options count:

const optionRenderer = (option, { selected }) => {
    const selectAllProps =
      option.value === "select-all" // To control the state of 'select-all' checkbox
        ? { checked: allSelected }
        : {};
    return (
      <>
        <Checkbox
          color="primary"
          icon={<CheckBoxOutlineBlankIcon fontSize="small" />}
          checkedIcon={<CheckBoxIcon fontSize="small" />}
          style={{ marginRight: 8 }}
          checked={selected}
          {...selectAllProps}
        />
        {getOptionLabel(option)}
      </>
    );
  };

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 šŸ¤·

6reactions
WilliamZimmermanncommented, Jan 28, 2022

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:

  1. When I scroll down and select (letā€™s say) the last option on the list, the scroll up to the top;
  2. Letā€™s suppose we ā€œSelect Allā€. All works fine. We click outside the component to close the list. We click again to open the list. If now I try to to ā€œSelect Allā€, the component interprets that Iā€™m clicking out of the component and closes itā€¦

Could anyone help me with it? select-all

CODE SANDBOX LINK: https://codesandbox.io/s/checkboxestags-material-demo-forked-5b0pt

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Material-UI Autocomplete component to select ...
I have a multiple field that: we can select users present in the autocompletion (a list that I retrieve via API); We can...
Read more >
How to display "Select all" in multi select auto complete pattern?
When all options are selected, the link appears in disabled styling. Also, each item that's been selected does not appear in the menuĀ ......
Read more >
Elegant Multi-Select Component With Autocomplete - CSS Script
SelectPure is a pure JavaScript (es6) library to create elegant single or multiple select controls with support for autocomplete and dynamic data rendering....
Read more >
Multiple Selection in Xamarin AutoComplete (SfAutoComplete)
Multiple Selection in Xamarin SfAutoComplete. Select multiple items from a suggestion list. There are two ways to perform multi selection in autocomplete.
Read more >
Angular Material Multi-Select Autocomplete
What I needed was a select-type dropdown that allowed for multi-row selection, as well as the ability to filter the list down on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found