nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[Feature Request] Autocomplete with Multiple Selections Feature or Multiple Select with Autocomplete Filtering

Open Dennisjoch opened this issue 1 year ago • 33 comments

Is your feature request related to a problem? Please describe.

Users of my application need the ability to select multiple options from an autocomplete field. Currently, Next UI provides an Autocomplete component that allows for a single selection with suggestions and a Select component that allows multiple selections but without autocomplete functionality. Merging these features would greatly enhance user experience.

Describe the solution you'd like

I would like the Autocomplete component to include a multiple attribute, allowing multiple selections with autocomplete suggestions, similar to the current Select component's multiple selection feature. Alternatively, the Select component could be enhanced with an autocomplete input to filter options as the user types.

Describe alternatives you've considered

As a workaround, we could use third-party libraries to achieve this functionality, but a native solution from Next UI would be ideal for consistency and to avoid additional dependencies.

Screenshots or Videos

No response

Dennisjoch avatar Dec 06 '23 23:12 Dennisjoch

Screencast from 09.12.23 14:51:02.webm Code from the documentation

CodeVisionEvgen avatar Dec 09 '23 12:12 CodeVisionEvgen

That would be really useful

LubomirGeorgiev avatar Dec 22 '23 16:12 LubomirGeorgiev

+1

duyanhvo2503 avatar Jan 01 '24 07:01 duyanhvo2503

+1

Inaztm avatar Jan 02 '24 16:01 Inaztm

+1

AlexisCeballos-Gauzz avatar Feb 07 '24 00:02 AlexisCeballos-Gauzz

+1

ElithPalomino avatar Feb 08 '24 03:02 ElithPalomino

hey, perhaps anyone already created a custom multiselect autocomplete and would like share? 👀

yahorbarkouski avatar Feb 08 '24 10:02 yahorbarkouski

+1

DanielReyes03 avatar Feb 20 '24 01:02 DanielReyes03

+1

Tomass673 avatar Feb 20 '24 06:02 Tomass673

hey, perhaps anyone already created a custom multiselect autocomplete and would like share? 👀

Check: ReactSelect

Dennisjoch avatar Feb 21 '24 00:02 Dennisjoch

+1

johnshift avatar Feb 24 '24 14:02 johnshift

+1

vasujhawar avatar Feb 28 '24 13:02 vasujhawar

Im having the same issue i think i will use the headless ui one with nextui componets https://headlessui.com/react/combobox#selecting-multiple-values

calrloco avatar Mar 22 '24 09:03 calrloco

+1

nikolay-gipp-sibe avatar Mar 24 '24 11:03 nikolay-gipp-sibe

+1

thepooyan avatar Apr 10 '24 08:04 thepooyan

+1

Armandotrsg avatar Apr 12 '24 21:04 Armandotrsg

+1

hericsantos avatar Apr 14 '24 16:04 hericsantos

+1

storm59222 avatar Apr 30 '24 08:04 storm59222

+1

peterondesign avatar May 01 '24 11:05 peterondesign

It seems like passing these down to the listbox should work, but no luck.

<Autocomplete
listboxProps={{
      selectionMode: 'multiple',
      selectedKeys: selectedKeys,
      onSelectionChange: setSelectedKeys
}}

brycelund avatar May 24 '24 19:05 brycelund

+1

AlexMartin998 avatar Jun 06 '24 03:06 AlexMartin998

+1

AprilPolubiec avatar Jun 09 '24 18:06 AprilPolubiec

+1 This feature is very useful to me

tsqqqqqq avatar Jun 12 '24 10:06 tsqqqqqq

+1

Jackmekiss avatar Jun 16 '24 10:06 Jackmekiss

+1

HiteshKanwar100 avatar Jun 19 '24 20:06 HiteshKanwar100

+1

qpenney avatar Jun 19 '24 21:06 qpenney

+1

manthankumaar avatar Jul 03 '24 11:07 manthankumaar

+1

oawebdev avatar Jul 15 '24 07:07 oawebdev

Any updates on this one! it would be really usefull

chitraksty1994 avatar Jul 22 '24 13:07 chitraksty1994

Here's an elegant approach that I came up with using NextUI's Dropdown component :

1. Structure:

Use NextUI's Dropdown component as the base.
Utilize the Dropdown's built-in multiple selection feature.
Implement two sections within the DropdownMenu:
a. A search input section (non-selectable)
b. A filterable list of selectable items

2. Implementation:

import { Dropdown, DropdownTrigger, DropdownMenu, DropdownSection, DropdownItem, Button, Input, Avatar } from "@nextui-org/react";
import { ChevronDownIcon } from "@your-icon-library";
import { useState, useMemo } from "react";

const MultiSelectAutocompleteDropdown = ({ users }) => {
  const [selectedKeys, setSelectedKeys] = useState(new Set([]));
  const [searchQuery, setSearchQuery] = useState("");

  // Filter users based on search query
  const filteredUsers = useMemo(() => {
    return users.filter((user) =>
      user.name.toLowerCase().includes(searchQuery.toLowerCase())
    );
  }, [users, searchQuery]);

  // Handle search input changes
  const handleSearchChange = (e) => {
    setSearchQuery(e.target.value);
  };

  // Generate display value for the button
  const selectedValue = useMemo(() => {
    const selected = Array.from(selectedKeys);
    return selected.length > 0
      ? `${selected.length} selected`
      : "Select member";
  }, [selectedKeys]);

  return (
    <div>
      <p className="text-sm pb-2">Assign to</p>
      <Dropdown>
        <DropdownTrigger>
          <Button 
            className="max-w-full" 
            endContent={<ChevronDownIcon className="text-small" />}
            variant="bordered"
          >
            {selectedValue}
          </Button>
        </DropdownTrigger>
        <DropdownMenu
          aria-label="Multiple selection with search"
          variant="flat"
          closeOnSelect={false}
          disallowEmptySelection
          selectionMode="multiple"
          selectedKeys={selectedKeys}
          onSelectionChange={setSelectedKeys}
        >
          <DropdownSection>
            <DropdownItem key="search" isReadOnly>
              <Input
                placeholder="Search..."
                value={searchQuery}
                onChange={handleSearchChange}
                fullWidth
              />
            </DropdownItem>
          </DropdownSection>
          <DropdownSection>
            {filteredUsers.map((user) => (
              <DropdownItem key={user.id}>
                <div className="flex gap-2 items-center">
                  <Avatar alt={user.name} className="flex-shrink-0" size="sm" src={user.avatar} />
                  <div className="flex flex-col">
                    <span className="text-small">{user.name}</span>
                    <span className="text-tiny text-default-400">{user.email}</span>
                  </div>
                </div>
              </DropdownItem>
            ))}
          </DropdownSection>
        </DropdownMenu>
      </Dropdown>
    </div>
  );
};

HiteshKanwar100 avatar Jul 22 '24 17:07 HiteshKanwar100