Skip to content
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

Allow multiple selection in SelectNext #1601

Merged
merged 1 commit into from
Jul 3, 2024
Merged

Allow multiple selection in SelectNext #1601

merged 1 commit into from
Jul 3, 2024

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Jul 2, 2024

This PR extends SelectNext so that it supports multiple items to be selected at once.

It does it by allowing value to be an array of items instead of a single item. When that happens, items in the list do not set themselves as selected, but they toggle themselves from that list instead.

Additionally, it adds a new multiple prop to be passed, which causes the listbox to be kept open when selecting options from the list.

State management is delegated to consumers, simplifying the implementation.

The implementation is inspired in how HeadlessUI does it.

multi-select-2024-07-03_09.08.04.mp4

TODO

  • Add tests
  • Check if there's any a11y implication to take into consideration.

@acelaya acelaya force-pushed the multi-select-next branch from 25e457e to e8bba1c Compare July 2, 2024 14:12
Copy link

codecov bot commented Jul 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (b9b6ce5) to head (576e773).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1601   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           62        62           
  Lines         1047      1066   +19     
  Branches       402       410    +8     
=========================================
+ Hits          1047      1066   +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@acelaya acelaya force-pushed the multi-select-next branch from e8bba1c to 5a94bdf Compare July 2, 2024 14:16
Comment on lines +77 to +83
if (!value) {
selectValue([]);
return;
}
Copy link
Contributor Author

@acelaya acelaya Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit opinionated and not entirely obvious behavior.

Perhaps a better option would be to allow <SelectOption /> to provide its own onChange onSelect, in which case it gets invoked instead of the default selectValue(...) logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggested above that it might help to avoid errors if the component just threw an error if in multi-select mode and a non-array value is passed.

@acelaya acelaya force-pushed the multi-select-next branch 6 times, most recently from 4fe5fa3 to 171f4cc Compare July 3, 2024 07:35
@@ -387,6 +435,7 @@ function SelectMain<T>({
role="listbox"
ref={listboxRef}
id={listboxId}
aria-multiselectable={multiple}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acelaya acelaya requested a review from robertknight July 3, 2024 07:37
@acelaya acelaya marked this pull request as ready for review July 3, 2024 07:37
@robertknight
Copy link
Member

I experimented with making multiple part of the SingleValueProps and MultiValueProps types so that TS can infer the types of value/onChange as appropriate. It works, except that it turned out to be necessary to explicitly specify the generic type for SelectNext rather than have TS infer it. That's quite an unusual thing to do, so it is probably better if that is not required.

diff --git a/src/components/input/SelectNext.tsx b/src/components/input/SelectNext.tsx
index 89762a4..c5b500c 100644
--- a/src/components/input/SelectNext.tsx
+++ b/src/components/input/SelectNext.tsx
@@ -246,11 +246,13 @@ function useListboxPositioning(
 type SingleValueProps<T> = {
   value: T;
   onChange: (newValue: T) => void;
+  multiple: false | undefined;
 };
 
 type MultiValueProps<T> = {
   value: T[];
   onChange: (newValue: T[]) => void;
+  multiple: true;
 };
 
 export type SelectProps<T> = CompositeProps &
@@ -258,13 +260,6 @@ export type SelectProps<T> = CompositeProps &
     buttonContent?: ComponentChildren;
     disabled?: boolean;
 
-    /**
-     * Whether this select should allow multi-selection or not.
-     * When this is true, the listbox is kept open when an option is selected.
-     * Defaults to false.
-     */
-    multiple?: boolean;
-
     /**
      * `id` attribute for the toggle button. This is useful to associate a label
      * with the control.
diff --git a/src/pattern-library/examples/select-next-multiple.tsx b/src/pattern-library/examples/select-next-multiple.tsx
index 9034b4b..90b2778 100644
--- a/src/pattern-library/examples/select-next-multiple.tsx
+++ b/src/pattern-library/examples/select-next-multiple.tsx
@@ -22,7 +22,7 @@ export default function App() {
   return (
     <div className="w-96 mx-auto">
       <label htmlFor={selectId}>Select students</label>
-      <SelectNext
+      <SelectNext<ItemType>
         multiple
         value={values}
         onChange={setSelected}

Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good and accessibility works well with VoiceOver. I suggested that rather than trying to handle values being a non-array when multiple is true in some undocumented way, the component could simply throw an error.

src/components/input/SelectNext.tsx Outdated Show resolved Hide resolved
Comment on lines +77 to +83
if (!value) {
selectValue([]);
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggested above that it might help to avoid errors if the component just threw an error if in multi-select mode and a non-array value is passed.

@acelaya acelaya force-pushed the multi-select-next branch 2 times, most recently from b2a12f0 to 6306f51 Compare July 3, 2024 10:08
@acelaya acelaya requested a review from robertknight July 3, 2024 10:09
Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed a possible hazard if single-selection is used but a user tried to use a tuple type as the item.

src/components/input/SelectNext.tsx Outdated Show resolved Hide resolved
src/components/input/SelectNext.tsx Show resolved Hide resolved
@acelaya acelaya force-pushed the multi-select-next branch 2 times, most recently from e676899 to 24764ea Compare July 3, 2024 12:51
Copy link
Member

@robertknight robertknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I do have a UX question over whether clicking a reset option should close the listbox automatically, rather than keep it open as clicking other options does. I suggest we ship this and then discuss that.

@acelaya
Copy link
Contributor Author

acelaya commented Jul 3, 2024

I do have a UX question over whether clicking a reset option should close the listbox automatically, rather than keep it open as clicking other options does

Yep, I thought the same.

@acelaya acelaya force-pushed the multi-select-next branch from 24764ea to 576e773 Compare July 3, 2024 13:31
@acelaya acelaya merged commit b21fc2f into main Jul 3, 2024
4 checks passed
@acelaya acelaya deleted the multi-select-next branch July 3, 2024 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants