Skip to content

[Select] Support multiple selections #1270

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

Open
ghost opened this issue Mar 25, 2022 · 68 comments · May be fixed by #3522
Open

[Select] Support multiple selections #1270

ghost opened this issue Mar 25, 2022 · 68 comments · May be fixed by #3522
Labels
Package: react/select Type: Enhancement Small enhancement to existing primitive/feature

Comments

@ghost
Copy link

ghost commented Mar 25, 2022

No description provided.

@benoitgrelard
Copy link
Contributor

Hi @aungbobotun , I suppose this is a feature request?
If so, it is on our broad roadmap, but hasn't been planned or prioritised yet.

@pavel-mazhuga
Copy link

+1, would be nice to have a multiple select primitive. Great lib btw

@cserb
Copy link

cserb commented Jul 12, 2022

Is there someone who managed to modify the select primitive and make it multi select?

@grumd
Copy link

grumd commented Aug 30, 2022

Is there someone who managed to modify the select primitive and make it multi select?

Doubt that. I looked at radix source code, the Root primitive and the SelectItem primitive talk to each other through context, and what it uses is a const [selectedItem, setSelectedItem] = React.useState<SelectItemElement | null>(null); piece of state.

You'll have to rewrite more than half of the Select primitive for it to work with multiple selected items, it would still break a lot of functionality like keyboard controls, and a lot of built-in accessibility is also lost, so it's probably easier to just use something like react-select until radix adds a new MultiSelect primitive. You can't really adapt the existing primitive without writing a multiselect from scratch.

@wongmrdev
Copy link

Bumping this feature request.

@BleedingDev
Copy link

Would be great to have it.

@dpromisel
Copy link

+1

2 similar comments
@Alarid
Copy link

Alarid commented Feb 7, 2023

+1

@sinbad-io
Copy link

+1

@Johnrobmiller
Copy link

Johnrobmiller commented Feb 28, 2023

Having a multi-select would better position Radix against HeadlessUI, which does have a multi-select.
I don't mind installing both libraries, and I could never only use HeadlessUI since Radix has so much that HeadlessUI does not.

@timnlupo
Copy link

+1

5 similar comments
@Melvynx
Copy link

Melvynx commented Apr 9, 2023

+1

@Andree37
Copy link

+1

@ghost
Copy link

ghost commented Apr 25, 2023

+1

@boazhoch
Copy link

boazhoch commented May 4, 2023

+1

@austinm911
Copy link

+1

@Nhollas
Copy link

Nhollas commented May 11, 2023

Please make this :)

@bekyarov
Copy link

+1 definitely looking forward to this

@hegelstad
Copy link

Having a multiple select primitive would be immensely valuable! I want to use it to improve UX for my users who have to hide one table column at a time.

@josepdecid
Copy link

This missing feature is one of the few things that still "forces" me to use Headless UI. It'd be super cool to see this soon!

@MathiasHoeyrup
Copy link

+1 - agree! Would love this

@axelmylle
Copy link

+1

@nguyenkhang2801
Copy link

+1 agree

@tonnoz
Copy link

tonnoz commented Jun 23, 2023

+1 do it

@damyco
Copy link

damyco commented Jun 23, 2023

+1

1 similar comment
@daskimmel
Copy link

+1

@kuus
Copy link

kuus commented Jun 25, 2023

it is really spammy this all +1, many notifications for nothing, can't you use the thumb up on the first issue's comment
thanks!

@admhemed
Copy link

+1

@anolan23
Copy link

anolan23 commented Mar 12, 2024

Here's a workaround for selecting multiple items which uses DropdownMenu. Can be controlled or uncontrolled.
I'm using shadcn for styling in this case.

import { Button } from '@/components/ui/button';
import {
  DropdownMenu,
  DropdownMenuCheckboxItem,
  DropdownMenuContent,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { CaretSortIcon } from '@radix-ui/react-icons';
import { useMemo, useState } from 'react';

type FilterItemDefinition = {
  value: string;
  label: string;
};
interface FilterProps {
  items: FilterItemDefinition[];
  value?: string[];
  onValueChange?: (value: string[]) => void;
}
export function Filter({ items, value, onValueChange }: FilterProps) {
  const [internalValue, setInternalValue] = useState<string[]>([]);

  const handleItemClick = function (item: FilterItemDefinition) {
    const newValue = selectedValues.includes(item.value)
      ? selectedValues.filter((value) => value !== item.value)
      : [...selectedValues, item.value];
    if (onValueChange) {
      onValueChange(newValue);
    } else {
      setInternalValue(newValue);
    }
  };

  const selectedValues = useMemo(() => {
    return value !== undefined ? value : internalValue;
  }, [value, internalValue]);

  return (
    <DropdownMenu modal={false}>
      <DropdownMenuTrigger asChild>
        <Button variant="outline" className="justify-between">
          Filter data
          <CaretSortIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent className="w-[var(--radix-dropdown-menu-trigger-width)]">
        {items.map((item) => {
          return (
            <DropdownMenuCheckboxItem
              key={item.value}
              checked={selectedValues.includes(item.value)}
              onCheckedChange={() => handleItemClick(item)}
              onSelect={(e) => e.preventDefault()}
            >
              {item.label}
            </DropdownMenuCheckboxItem>
          );
        })}
      </DropdownMenuContent>
    </DropdownMenu>
  );
}

@benoitgrelard
Copy link
Contributor

@anolan23, this is the wrong component to use unfortunately, accessibility and semantic is completely different, won't work in forms, etc.

@anolan23
Copy link

@benoitgrelard, Guess I would have to call it a DropdownMenu with multi checkbox options. Not a Select with multiple.

@sersavan
Copy link

I've assembled a multi-select component using the native shadcn's components. It's fully in line with design and integrates seamlessly into shadcn's ecosystem. Please, try it out and share your thoughts.
https://shadcn-multi-select-component.vercel.app/

Screenshot 2024-04-11 at 00 50 06

@tonnoz
Copy link

tonnoz commented Apr 12, 2024

@sersavan awesome, make it into a PR

@Roeefl
Copy link

Roeefl commented Apr 14, 2024

@sersavan It looks amazing. looking forward

@alissawix
Copy link

just FYI, you can implement multiselect with the dropdown component and checkbox items
it is a strange way to do it, but should be pretty easy to implement

@zahidiqbalnbs
Copy link

I've assembled a multi-select component using the native shadcn's components. It's fully in line with design and integrates seamlessly into shadcn's ecosystem. Please, try it out and share your thoughts. https://shadcn-multi-select-component.vercel.app/

Screenshot 2024-04-11 at 00 50 06

@sersavan can you move CommandInput search option within PopoverTrigger?

@sersavan
Copy link

I've assembled a multi-select component using the native shadcn's components. It's fully in line with design and integrates seamlessly into shadcn's ecosystem. Please, try it out and share your thoughts. https://shadcn-multi-select-component.vercel.app/
Screenshot 2024-04-11 at 00 50 06

@sersavan can you move CommandInput search option within PopoverTrigger?

@zahidiqbalnbs
but why?

@zahidiqbalnbs
Copy link

I've assembled a multi-select component using the native shadcn's components. It's fully in line with design and integrates seamlessly into shadcn's ecosystem. Please, try it out and share your thoughts. https://shadcn-multi-select-component.vercel.app/
Screenshot 2024-04-11 at 00 50 06

@sersavan can you move CommandInput search option within PopoverTrigger?

@zahidiqbalnbs but why?

@sersavan thanks for your reply! You are a code guru and doing great stuff. The only reason for asking is if you look at react-select, antd multiselect, mui multiselect and others, all have search functionality within Input instead of providing them in Popover content. Thats why I am looking for if you can do that, would be really great!! Thanks again for all the wonderful work.

@sersavan
Copy link

I've assembled a multi-select component using the native shadcn's components. It's fully in line with design and integrates seamlessly into shadcn's ecosystem. Please, try it out and share your thoughts. https://shadcn-multi-select-component.vercel.app/
Screenshot 2024-04-11 at 00 50 06

@sersavan can you move CommandInput search option within PopoverTrigger?

@zahidiqbalnbs but why?

@sersavan thanks for your reply! You are a code guru and doing great stuff. The only reason for asking is if you look at react-select, antd multiselect, mui multiselect and others, all have search functionality within Input instead of providing them in Popover content. Thats why I am looking for if you can do that, would be really great!! Thanks again for all the wonderful work.

@zahidiqbalnbs
I appreciate your feedback. If we add a search function to the popover, we need to decide which list to search through. In my component, the list is shown as a table. It would be more convenient to have the search bar in the same place as the table, rather than in the popover. Or propose your solution instead.

@Rickiiii
Copy link

Rickiiii commented Jul 3, 2024

+1 🚀

@devdomsos
Copy link

Would be awesome to have multiselect! +1

@GetPsyched
Copy link

GetPsyched commented Sep 6, 2024

Contrary to what @sersavan has made, and what some others want, I would much prefer this be as a multiple argument to the existing Select primitive (as the OP suggests). This is due to accessibility concerns and also DX.

<select> in HTML already supports choosing multiple selections, albeit in a weird way (it doesn't render as a normal dropdown anymore, but a scrollable block, which looks awful) I would prefer having a checkbox like select but still using <select> under-the-hood, and not creating a MultiSelect that many other libraries provide.

Another concern is that if you don't use the html <select> element, I cannot possibly use it in a form without a controlled input; this is horrible for server components that cannot have a stateful controlled element without making the entire form client-side.

Thanks.

@asazernik
Copy link

Note that the DX question of "is this an argument to Select or a separate component" and the accessibility question of "does this render to <select multiple />" are orthogonal. Two separate components can both render to HTML <select> with different parameters and styling.

@Hendra-Kusuma
Copy link

realy need this multiselect please

@eliasdanielr
Copy link

+1

1 similar comment
@matheusmacedo
Copy link

+1

@twinkle77
Copy link

+1

@Zach-Jaensch
Copy link

I'm interested in helping with this if you're open to contributions? 😄

@Asaki-M
Copy link

Asaki-M commented Apr 28, 2025

+1

@Zach-Jaensch Zach-Jaensch linked a pull request May 1, 2025 that will close this issue
@ispashkov
Copy link

+1

@Zach-Jaensch
Copy link

I've done some work to the select component to support multi select in this PR. Not sure on the process to get it reviewed, but would be great to get some community feedback too.

#3522

@sss-Mihail-sss
Copy link

are there any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: react/select Type: Enhancement Small enhancement to existing primitive/feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.