react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Multiple selection support for useComboBox

Open stephenh opened this issue 2 years ago • 44 comments

🙋 Feature Request

Update useComboBox to support multiple selection.

🔦 Context

We're building a type-ahead SelectField / MultiSelectField components, both on top of useComboBox and having to support multiple selections is of course making us do some hacky things, b/c it's not really supported.

I.e. we're not passing selectedKey to useComboBox b/c in a multiple selection scenario, the selectedKey could change, and so then odd things happen where useComboBox thinks its not controlled and/or the selectedKey !== inputValue, so it invokes various closing/reset behavior that we didn't mean to trigger.

Obviously this is on us for misusing useComboBox :-D , so just filing a request / wish list for it to get official multiple selection support. Thanks!

stephenh avatar Jul 22 '21 17:07 stephenh

We call this a tag field - a combination of a tag list and an input kinda like the "to" field in an email app. It's on the roadmap, but not sure when yet. We need to do some a11y research and testing to determine how best to implement it.

devongovett avatar Jul 24 '21 07:07 devongovett

@LFDanLu lazy ask, but is there a ticket created for the tag field that @devongovett mentioned? I'd like to follow it, so that I can see when react-aria lands support for it. Thanks!

stephenh avatar Jul 27 '21 17:07 stephenh

@stephenh I don't believe there is a ticket created for the tag field yet since it is still in its early germination phase unfortunately. https://github.com/adobe/react-spectrum/projects/1 is the best place to look regarding what is currently in flight.

LFDanLu avatar Jul 27 '21 18:07 LFDanLu

Going to keep this ticket open for tracking.

devongovett avatar Jul 29 '21 00:07 devongovett

Hey @devongovett , Does it look like similar to this? https://react-listbox.vercel.app/?path=/story/example-listbox--multiselect https://www.telerik.com/kendo-react-ui/components/dropdowns/multiselect/

ashiskumar-1999 avatar Aug 19 '21 20:08 ashiskumar-1999

More like the second one, the first is only a select not a combobox (no textfield). Think similar to an email entry field like in Gmail. Here's a screenshot of the design from Spectrum that we'd be basing the pattern on:

tag-field_hero_desktop@2x_6md9yFlaUX9BDThTdZNbgC_1611704216000

devongovett avatar Aug 19 '21 20:08 devongovett

@devongovett If this falls under priority, I can start working on it.

ashiskumar-1999 avatar Aug 19 '21 20:08 ashiskumar-1999

Hey, @devongovett As I move forward trying to contribute to this task, I'm wondering if this use-case requires us to build another independent component/hook or this functionality could be incorporated into any existing component?

ashiskumar-1999 avatar Aug 22 '21 16:08 ashiskumar-1999

Is there a way to achieve this behaviour with existing hooks in react-stately and react-aria?

I'm working on a custom component library according to a design system and using react-aria extensively (without spectrum) and a multi-select is a new requirement. It would be great if we could leverage react-aria for this without having to lean on another library or build something from scratch.

From what I can see, some combination of a SelectionManager and ListBox looks like the most suitable approach

beamery-tomht avatar Sep 08 '21 17:09 beamery-tomht

Is there a way to achieve this behaviour with existing hooks in react-stately and react-aria?

I'm working on a custom component library according to a design system and using react-aria extensively (without spectrum) and a multi-select is a new requirement. It would be great if we could leverage react-aria for this without having to lean on another library or build something from scratch.

From what I can see, some combination of a SelectionManager and ListBox looks like the most suitable approach

This also matches my use case. Are we better off building something from scratch or is support for this use case on the near term roadmap?

jamesonhill avatar Jan 05 '22 19:01 jamesonhill

Is there a way to achieve this behaviour with existing hooks in react-stately and react-aria?

I'm working on a custom component library according to a design system and using react-aria extensively (without spectrum) and a multi-select is a new requirement. It would be great if we could leverage react-aria for this without having to lean on another library or build something from scratch.

From what I can see, some combination of a SelectionManager and ListBox looks like the most suitable approach

I was facing this problem regarding multiselect as well. Since @react-aria/select and suitable stately hooks do not support multiple selection mode (and I totally get why it is the case), I followed the implementation of the Picker component, re-created hooks to support our needs (multiple selection, searching possibility, clear & select all buttons...) and built a new Select component with them.

So, instead of:

Basically it means a lot of copy-pasting and playing with properties of hooks underneath. Once I became more familiar with the original implementation I found it very flexible and customizable.

crazko avatar Jan 14 '22 20:01 crazko

Just an FYI that we are also on our way to building what was described in https://github.com/adobe/react-spectrum/issues/2140#issuecomment-902237781 This is the PR where TagGroup work has been started https://github.com/adobe/react-spectrum/pull/1922 Once we have that, we'll be able to consider joining it with a combobox or other components to handle multi select

snowystinger avatar Jan 18 '22 18:01 snowystinger

@crazko are your multi select compatible hooks public by any chance?

donaldpipowitch avatar Jun 23 '22 07:06 donaldpipowitch

@donaldpipowitch let's check example at https://codesandbox.io/s/spectrum-multiselect-9be866-9be866. It is a simplified version of our Select component implementation with support for both single & multiple selection mode.

There's a bug though, which I was not able to find a fix for so far; it is not present in our library. But should give enough input for how this may work.

crazko avatar Jul 14 '22 10:07 crazko

Thank you very much!

donaldpipowitch avatar Jul 14 '22 10:07 donaldpipowitch

Wondering if it will be possible to update the input (or contenteditable element) with a mix of values. My use case is an omni-search box that allows users to type random search strings, but as they type a menu can appear to offer them selections for tags, categories, and other meta data fields. If they make a selection (ex: tag), it should appear in the input field where their cursor was, allowing them to continue typing/selecting as they wish from there.

npearson72 avatar Aug 21 '22 06:08 npearson72

If you are using our hooks to make your combobox, this should already be possible. I wouldn't actually update the input for mixed anything, but you could render anything you wanted before or after the combobox, then just make it visually look like it's part of the input.

Combobox has no plans to support multi select. Instead, when multiselecting, remove selected items and put them outside the combobox, just like you're suggesting with the tags.

snowystinger avatar Aug 21 '22 17:08 snowystinger

@snowystinger Thanks for the hint. But from what you're describing I'm struggling to understand how a user would interact with the data they've entered.

For example, say they enter the following:

The quick brown fox jumped over the lazy dog

But now they want to change the word "jumped" to "ran". How would it be possible if this was rendered outside of the input field (or contenteditable element)?

npearson72 avatar Aug 21 '22 21:08 npearson72

hmmm have you tried tokenizing to words, and each word is a combobox? because it doesn't seem like the whole thing is one combobox.

I'm not sure the accessibility implications of this approach, but I don't think it fits combobox otherwise at all. This is an, you're off the map, situation.

Keep in mind, this https://www.w3.org/WAI/ARIA/apg/patterns/combobox/ is the aria pattern we have implemented.

A combobox is an input widget with an associated popup that enables users to select a value for the combobox from a collection of possible values.

snowystinger avatar Aug 22 '22 00:08 snowystinger

@snowystinger Got it. Thanks for the explanation. I think you're right in that it might be some hybrid pattern. Appreciate your feedback.

npearson72 avatar Aug 22 '22 01:08 npearson72

Hi, yesterday I was also looking for this. I was able to do an autocomplete + tags using

  1. useKeyboard
  2. useListBox
  3. useListState

image

Basically I reimplemented useComboBox with those primitives but configuring useListState as multiple to be able to handle multiple selection 🎉

image

andresgutgon avatar Feb 24 '23 15:02 andresgutgon

@andresgutgon Could you kindly share your implementation? I'm struggling with this

wporoslo avatar Feb 25 '23 08:02 wporoslo

It's still in the oven, but when I have time I'll share a sandbox with the parts.

andresgutgon avatar Feb 25 '23 09:02 andresgutgon

@wporoslo sorry for the delay. I made a very ugly version of the one we built image

But the parts necessary to do a combobox + multiple selection are there https://codesandbox.io/s/sleepy-keller-qjygke?file=/src/App.tsx

Final result on our side 🎉 image

andresgutgon avatar Mar 02 '23 08:03 andresgutgon

@wporoslo sorry for the delay. I made a very ugly version of the one we built thank you so much!

wporoslo avatar Mar 02 '23 16:03 wporoslo

Is there any status on this issue? This is a very important feature that we are waiting for 😔

yarastqt avatar Mar 15 '23 12:03 yarastqt

Is there any status on this issue? This is a very important feature that we are waiting for 😔

Please see the discussion above, it should answer any questions you have as well as how to build your own. If the ask is specifically for a React Spectrum TagField component, that should be moved to a different place. useCombobox is a specific aria pattern that will not be supporting multiple selection on its own.

snowystinger avatar Mar 15 '23 20:03 snowystinger

Now that Taggroup is released i would love to see a Tagfield / comboBox with multiple selection. Like "Autocomplete" in material ui.

amauryfischer avatar May 28 '23 11:05 amauryfischer

NextUI just dropped their select component and it supports multi selection 👏🏽

tekno0ryder avatar Aug 21 '23 14:08 tekno0ryder

This project is using react-aria and has multi-select support

  • https://project44.github.io/manifest/inputs/mult-combobox
  • https://pluralsh-design.web.app/?path=/story/combo-box--cluster-tags
  • https://pluralsh-design.web.app/?path=/story/select--default
  • https://anvil.servicetitan.com/components/anvil-select/
  • https://developers.grafana.com/ui/latest/index.html?path=/story/forms-select--multi-select-basic (react-select)

but I'm eager to see it in react-aria-components

sadeghbarati avatar Jan 27 '24 07:01 sadeghbarati