@vendure-io/ui 3.0.0-next.6 → 3.0.0-next.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/atoms/calendar.tsx +9 -0
- package/src/components/atoms/combobox.tsx +121 -7
- package/src/components/molecules/action-needed.tsx +4 -4
- package/src/components/molecules/app-shell.tsx +3 -3
- package/src/components/molecules/multi-select.tsx +466 -107
- package/src/components/molecules/status-badge.tsx +5 -2
- package/src/lib/chip-fit.ts +62 -0
- package/src/lib/format-messages.ts +14 -1
- package/src/lib/multi-select-keys.ts +77 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vendure-io/ui",
|
|
3
|
-
"version": "3.0.0-next.
|
|
3
|
+
"version": "3.0.0-next.8",
|
|
4
4
|
"description": "React component library for Vendure, built on shadcn/ui and Tailwind v4",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
6
6
|
"homepage": "https://github.com/vendurehq/design/tree/main/packages/ui",
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
12
12
|
import { Button, buttonVariants } from "@vendure-io/ui/components/atoms/button"
|
|
13
13
|
import { useFormatSettings } from "@vendure-io/ui/components/molecules/format-provider"
|
|
14
|
+
import { ProvidedDirection } from "../../lib/popup-direction.ts"
|
|
14
15
|
import { ChevronLeftIcon, ChevronRightIcon, ChevronDownIcon } from "lucide-react"
|
|
15
16
|
|
|
16
17
|
function Calendar({
|
|
@@ -22,6 +23,7 @@ function Calendar({
|
|
|
22
23
|
locale,
|
|
23
24
|
formatters,
|
|
24
25
|
components,
|
|
26
|
+
dir,
|
|
25
27
|
...props
|
|
26
28
|
}: React.ComponentProps<typeof DayPicker> & {
|
|
27
29
|
buttonVariant?: React.ComponentProps<typeof Button>["variant"]
|
|
@@ -31,6 +33,11 @@ function Calendar({
|
|
|
31
33
|
// reaches their popups without each caller passing `locale`.
|
|
32
34
|
const { calendarLocale } = useFormatSettings()
|
|
33
35
|
locale ??= calendarLocale
|
|
36
|
+
// DayPicker maps ArrowLeft and ArrowRight to days by `dir`. Inside a
|
|
37
|
+
// right-to-left DirectionProvider, ArrowLeft moves to the next day. Outside
|
|
38
|
+
// a provider `dir` stays unset and the grid inherits the page's direction.
|
|
39
|
+
const providedDir = React.useContext(ProvidedDirection)
|
|
40
|
+
dir ??= providedDir
|
|
34
41
|
|
|
35
42
|
return (
|
|
36
43
|
<DayPicker
|
|
@@ -42,6 +49,7 @@ function Calendar({
|
|
|
42
49
|
className
|
|
43
50
|
)}
|
|
44
51
|
captionLayout={captionLayout}
|
|
52
|
+
dir={dir}
|
|
45
53
|
locale={locale}
|
|
46
54
|
formatters={{
|
|
47
55
|
// Through DayPicker's date library, so the month names follow `locale`
|
|
@@ -205,6 +213,7 @@ function CalendarDayButton({
|
|
|
205
213
|
|
|
206
214
|
return (
|
|
207
215
|
<Button
|
|
216
|
+
ref={ref}
|
|
208
217
|
variant="ghost"
|
|
209
218
|
size="icon"
|
|
210
219
|
data-day={day.date.toLocaleDateString(locale?.code)}
|
|
@@ -5,7 +5,7 @@ import { Combobox as ComboboxPrimitive } from "@base-ui/react"
|
|
|
5
5
|
|
|
6
6
|
import { cn } from "@vendure-io/ui/lib/utils"
|
|
7
7
|
import { MenuCheck, menuCheckItem, menuItemDisabled } from "../../lib/menu-item.tsx"
|
|
8
|
-
import {
|
|
8
|
+
import { chipCountBox, chipShape } from "../../lib/chip-fit.ts"
|
|
9
9
|
import { fieldSurface, fieldSurfaceWithin } from "../../lib/field-surface.ts"
|
|
10
10
|
import { popupMotion } from "../../lib/popup-motion.ts"
|
|
11
11
|
import { Button } from "@vendure-io/ui/components/atoms/button"
|
|
@@ -15,11 +15,11 @@ import {
|
|
|
15
15
|
InputGroupButton,
|
|
16
16
|
InputGroupInput,
|
|
17
17
|
} from "@vendure-io/ui/components/atoms/input-group"
|
|
18
|
-
import { ChevronDownIcon, Loader2Icon, XIcon } from "lucide-react"
|
|
18
|
+
import { ChevronDownIcon, ChevronsUpDownIcon, Loader2Icon, XIcon } from "lucide-react"
|
|
19
19
|
import { usePopupDir } from "../../lib/popup-direction.ts"
|
|
20
20
|
import { usePopupAlign } from "../../lib/popup-align.ts"
|
|
21
21
|
import { hasName, nameFrom, useRegisteredRef } from "../../lib/control-name.ts"
|
|
22
|
-
import { useMessages } from "../../lib/format-messages.ts"
|
|
22
|
+
import { fillMessage, useMessages } from "../../lib/format-messages.ts"
|
|
23
23
|
import { warnDeprecated } from "../../lib/deprecated.ts"
|
|
24
24
|
|
|
25
25
|
/**
|
|
@@ -575,7 +575,7 @@ function ComboboxChips({
|
|
|
575
575
|
data-slot="combobox-chips"
|
|
576
576
|
className={cn(
|
|
577
577
|
fieldSurfaceWithin,
|
|
578
|
-
"flex min-h-8.5 flex-wrap items-center gap-1.5
|
|
578
|
+
"group/combobox-chips flex min-h-8.5 flex-wrap items-center gap-1.5 p-1.25 text-sm has-[input:disabled]:cursor-not-allowed has-[input:disabled]:opacity-50",
|
|
579
579
|
className
|
|
580
580
|
)}
|
|
581
581
|
{...props}
|
|
@@ -609,12 +609,15 @@ function ComboboxChip({
|
|
|
609
609
|
<ComboboxPrimitive.Chip
|
|
610
610
|
data-slot="combobox-chip"
|
|
611
611
|
className={cn(
|
|
612
|
-
|
|
612
|
+
chipShape,
|
|
613
|
+
"text-foreground flex w-fit max-w-full min-w-0 items-center justify-center gap-1.5 ps-2 pe-2 has-disabled:pointer-events-none has-disabled:cursor-not-allowed has-disabled:opacity-50 has-data-[slot=combobox-chip-remove]:pe-1",
|
|
613
614
|
className
|
|
614
615
|
)}
|
|
615
616
|
{...props}
|
|
616
617
|
>
|
|
617
|
-
|
|
618
|
+
<span data-slot="combobox-chip-label" className="min-w-0 truncate">
|
|
619
|
+
{children}
|
|
620
|
+
</span>
|
|
618
621
|
{showRemove && (
|
|
619
622
|
<ComboboxPrimitive.ChipRemove
|
|
620
623
|
render={<Button variant="ghost" size="icon-xs" />}
|
|
@@ -639,12 +642,121 @@ function ComboboxChipsInput({
|
|
|
639
642
|
<ComboboxPrimitive.Input
|
|
640
643
|
ref={inputRef}
|
|
641
644
|
data-slot="combobox-chip-input"
|
|
642
|
-
className={cn(
|
|
645
|
+
className={cn(
|
|
646
|
+
// The field has 5px padding with and without chips. Alone, the input
|
|
647
|
+
// adds 5px, so the placeholder starts 10px in, as in other fields;
|
|
648
|
+
// after a chip it adds 4px to the 6px gap.
|
|
649
|
+
"placeholder:text-faint-foreground h-6 min-w-16 flex-1 bg-transparent ps-1.25 outline-none disabled:cursor-not-allowed group-has-data-[slot=combobox-chip]/combobox-chips:ps-1",
|
|
650
|
+
className
|
|
651
|
+
)}
|
|
643
652
|
{...props}
|
|
644
653
|
/>
|
|
645
654
|
)
|
|
646
655
|
}
|
|
647
656
|
|
|
657
|
+
/**
|
|
658
|
+
* The "+N" chip: it stands in for the chips that do not fit on the field's
|
|
659
|
+
* one line. The same shape and fill as a ComboboxChip, without a remove
|
|
660
|
+
* button, with tabular digits so a new count does not change its width.
|
|
661
|
+
* It is a button: give it an `onClick` (open the list) and wrap it in a
|
|
662
|
+
* Tooltip that names the hidden values.
|
|
663
|
+
*/
|
|
664
|
+
function ComboboxChipCount({
|
|
665
|
+
count,
|
|
666
|
+
className,
|
|
667
|
+
"aria-label": ariaLabel,
|
|
668
|
+
...props
|
|
669
|
+
}: Omit<React.ComponentProps<"button">, "children"> & {
|
|
670
|
+
/** The number of hidden values. */
|
|
671
|
+
count: number
|
|
672
|
+
}) {
|
|
673
|
+
const t = useMessages("combobox", { more: ariaLabel })
|
|
674
|
+
return (
|
|
675
|
+
<button
|
|
676
|
+
type="button"
|
|
677
|
+
data-slot="combobox-chip-count"
|
|
678
|
+
aria-label={fillMessage(t.more, { count })}
|
|
679
|
+
className={cn(
|
|
680
|
+
chipShape,
|
|
681
|
+
chipCountBox,
|
|
682
|
+
"text-foreground cursor-default disabled:cursor-not-allowed",
|
|
683
|
+
className
|
|
684
|
+
)}
|
|
685
|
+
{...props}
|
|
686
|
+
>
|
|
687
|
+
+{count}
|
|
688
|
+
</button>
|
|
689
|
+
)
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* The trailing controls of a chips field: the clear-all × and the up/down
|
|
694
|
+
* chevrons that open the list. The × keeps its space while nothing is
|
|
695
|
+
* picked, so the first pick does not narrow the chips row. While a search
|
|
696
|
+
* loads, the chevrons turn into a spinner in the same box. Both buttons stay
|
|
697
|
+
* out of the tab order, like the ones in ComboboxInput: Backspace and the
|
|
698
|
+
* arrow keys do their work from the input.
|
|
699
|
+
*/
|
|
700
|
+
function ComboboxChipsActions({
|
|
701
|
+
showClear,
|
|
702
|
+
isLoading = false,
|
|
703
|
+
clearLabel,
|
|
704
|
+
triggerLabel,
|
|
705
|
+
disabled,
|
|
706
|
+
className,
|
|
707
|
+
}: {
|
|
708
|
+
/** Something is picked: the × shows. */
|
|
709
|
+
showClear: boolean
|
|
710
|
+
/** A search is loading: the chevrons turn into a spinner. */
|
|
711
|
+
isLoading?: boolean
|
|
712
|
+
/**
|
|
713
|
+
* Accessible name of the clear-all button.
|
|
714
|
+
* @default `messages.combobox.clearAll`, "Clear all"
|
|
715
|
+
*/
|
|
716
|
+
clearLabel?: string
|
|
717
|
+
/**
|
|
718
|
+
* Accessible name of the button that opens the list.
|
|
719
|
+
* @default `messages.combobox.open`, "Open options"
|
|
720
|
+
*/
|
|
721
|
+
triggerLabel?: string
|
|
722
|
+
disabled?: boolean
|
|
723
|
+
className?: string
|
|
724
|
+
}) {
|
|
725
|
+
const t = useMessages("combobox", { clearAll: clearLabel, open: triggerLabel })
|
|
726
|
+
return (
|
|
727
|
+
<div
|
|
728
|
+
data-slot="combobox-chips-actions"
|
|
729
|
+
className={cn("ms-auto flex h-6 shrink-0 items-center gap-0.5 self-start", className)}
|
|
730
|
+
>
|
|
731
|
+
<ComboboxPrimitive.Clear
|
|
732
|
+
data-slot="combobox-clear"
|
|
733
|
+
keepMounted
|
|
734
|
+
aria-label={t.clearAll}
|
|
735
|
+
aria-hidden={showClear ? undefined : true}
|
|
736
|
+
tabIndex={-1}
|
|
737
|
+
disabled={disabled || !showClear}
|
|
738
|
+
render={<Button variant="ghost" size="icon-xs" />}
|
|
739
|
+
className={cn("text-muted-foreground hover:text-foreground", !showClear && "invisible")}
|
|
740
|
+
>
|
|
741
|
+
<XIcon className="pointer-events-none" />
|
|
742
|
+
</ComboboxPrimitive.Clear>
|
|
743
|
+
<ComboboxPrimitive.Trigger
|
|
744
|
+
tabIndex={-1}
|
|
745
|
+
aria-label={t.open}
|
|
746
|
+
disabled={disabled}
|
|
747
|
+
render={<Button variant="ghost" size="icon-xs" nativeButton />}
|
|
748
|
+
className="text-muted-foreground hover:text-foreground data-popup-open:text-foreground data-popup-open:bg-transparent aria-expanded:bg-transparent"
|
|
749
|
+
>
|
|
750
|
+
{isLoading ? (
|
|
751
|
+
<Loader2Icon data-slot="combobox-chips-loading" className="pointer-events-none motion-safe:animate-spin" />
|
|
752
|
+
) : (
|
|
753
|
+
<ChevronsUpDownIcon className="pointer-events-none" />
|
|
754
|
+
)}
|
|
755
|
+
</ComboboxPrimitive.Trigger>
|
|
756
|
+
</div>
|
|
757
|
+
)
|
|
758
|
+
}
|
|
759
|
+
|
|
648
760
|
function useComboboxAnchor() {
|
|
649
761
|
return React.useRef<HTMLDivElement | null>(null)
|
|
650
762
|
}
|
|
@@ -663,6 +775,8 @@ export {
|
|
|
663
775
|
ComboboxChips,
|
|
664
776
|
ComboboxChip,
|
|
665
777
|
ComboboxChipsInput,
|
|
778
|
+
ComboboxChipCount,
|
|
779
|
+
ComboboxChipsActions,
|
|
666
780
|
ComboboxTrigger,
|
|
667
781
|
ComboboxValue,
|
|
668
782
|
useComboboxAnchor,
|
|
@@ -382,9 +382,9 @@ function ActionNeeded<T extends ActionNeededItem>({
|
|
|
382
382
|
<span
|
|
383
383
|
aria-hidden={open || undefined}
|
|
384
384
|
data-slot="action-needed-counts"
|
|
385
|
-
// The badges
|
|
386
|
-
//
|
|
387
|
-
className="
|
|
385
|
+
// The badges are bare, as high as the title text, so they sit inside the title
|
|
386
|
+
// line. The column gap is twice the icon-to-label gap, so two tones read apart.
|
|
387
|
+
className="flex min-w-0 flex-wrap items-center gap-x-3 gap-y-1"
|
|
388
388
|
>
|
|
389
389
|
{actionNeededCounts(items).map(({ tone, count }, index) => (
|
|
390
390
|
<span
|
|
@@ -394,7 +394,7 @@ function ActionNeeded<T extends ActionNeededItem>({
|
|
|
394
394
|
className={badgeClass}
|
|
395
395
|
style={{ '--i': index } as CSSProperties}
|
|
396
396
|
>
|
|
397
|
-
<StatusBadge className="tabular-nums" size="lg" tone={tone}>
|
|
397
|
+
<StatusBadge appearance="bare" className="tabular-nums" size="lg" tone={tone}>
|
|
398
398
|
{countLabel(count, tone)}
|
|
399
399
|
</StatusBadge>
|
|
400
400
|
</span>
|
|
@@ -124,9 +124,9 @@ function AppShellMain({
|
|
|
124
124
|
const pageEnterClassName = 'animate-page-enter motion-reduce:animate-page-fade';
|
|
125
125
|
|
|
126
126
|
// The page frame: one frame for every app on AppShell. The page is centred in
|
|
127
|
-
// AppShellMain, its content is at most
|
|
127
|
+
// AppShellMain, its content is at most 1600px wide, and a 16px gutter sits on
|
|
128
128
|
// every side at every width, the top and bottom included. The gutter is outside
|
|
129
|
-
// the
|
|
129
|
+
// the 1600px, so the frame is at most 1600px plus two gutters wide.
|
|
130
130
|
// The end padding of AppShellHeader (pe-4) is the same value as the gutter:
|
|
131
131
|
// change both together.
|
|
132
132
|
//
|
|
@@ -136,7 +136,7 @@ const pageEnterClassName = 'animate-page-enter motion-reduce:animate-page-fade';
|
|
|
136
136
|
// A page that wants a narrower column, such as a settings form at `max-w-4xl`,
|
|
137
137
|
// puts it inside the frame; it stays left-aligned there.
|
|
138
138
|
const pageFrameClassName =
|
|
139
|
-
'[--app-shell-page-width:
|
|
139
|
+
'[--app-shell-page-width:1600px] [--app-shell-page-gutter:--spacing(4)] mx-auto w-full min-w-0 max-w-[calc(var(--app-shell-page-width)+2*var(--app-shell-page-gutter))] p-(--app-shell-page-gutter)';
|
|
140
140
|
|
|
141
141
|
// AppShellPage wraps the page inside AppShellMain. It is the page frame (see
|
|
142
142
|
// pageFrameClassName), and it is the page enter: when it mounts, the page
|
|
@@ -1,19 +1,36 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
Combobox,
|
|
5
|
+
ComboboxChip,
|
|
6
|
+
ComboboxChipCount,
|
|
7
|
+
ComboboxChips,
|
|
8
|
+
ComboboxChipsActions,
|
|
9
|
+
ComboboxChipsInput,
|
|
10
|
+
ComboboxCollection,
|
|
11
|
+
ComboboxContent,
|
|
12
|
+
ComboboxEmpty,
|
|
13
|
+
ComboboxGroup,
|
|
14
|
+
ComboboxItem,
|
|
15
|
+
ComboboxLabel,
|
|
16
|
+
ComboboxList,
|
|
17
|
+
} from '@vendure-io/ui/components/atoms/combobox';
|
|
18
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '@vendure-io/ui/components/atoms/tooltip';
|
|
11
19
|
import { SkeletonLine } from '@vendure-io/ui/components/molecules/state-views/skeleton-parts';
|
|
12
20
|
import { cn } from '@vendure-io/ui/lib/utils';
|
|
13
|
-
import { Loader2Icon } from 'lucide-react';
|
|
14
21
|
import * as React from 'react';
|
|
22
|
+
import {
|
|
23
|
+
CHIP_GAP,
|
|
24
|
+
type ChipFit,
|
|
25
|
+
chipBox,
|
|
26
|
+
chipCountBox,
|
|
27
|
+
chipShape,
|
|
28
|
+
fitChips,
|
|
29
|
+
INPUT_RESERVE,
|
|
30
|
+
} from '../../lib/chip-fit.ts';
|
|
15
31
|
import { isDevelopment } from '../../lib/env.ts';
|
|
16
|
-
import { useMessages } from '../../lib/format-messages.ts';
|
|
32
|
+
import { fillMessage, useMessages } from '../../lib/format-messages.ts';
|
|
33
|
+
import { multiSelectKeyAction } from '../../lib/multi-select-keys.ts';
|
|
17
34
|
|
|
18
35
|
type ItemValue = string | number;
|
|
19
36
|
|
|
@@ -30,10 +47,13 @@ function resolveAccessor<T, R>(item: T, accessor: Accessor<T, R>): R {
|
|
|
30
47
|
|
|
31
48
|
export interface MultiSelectProps<T> {
|
|
32
49
|
/**
|
|
33
|
-
* The
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
50
|
+
* The options. `undefined` means the first load has not finished: a picked
|
|
51
|
+
* value shows as a skeleton (its label is not known yet), and the open list
|
|
52
|
+
* shows a loading row, one option tall. `[]` means there are no options.
|
|
53
|
+
*
|
|
54
|
+
* With `onSearchChange`, these are the results of the current search, in
|
|
55
|
+
* the order to show them; otherwise MultiSelect filters them by the typed
|
|
56
|
+
* text.
|
|
37
57
|
*/
|
|
38
58
|
items: readonly T[] | undefined;
|
|
39
59
|
/**
|
|
@@ -42,42 +62,68 @@ export interface MultiSelectProps<T> {
|
|
|
42
62
|
*/
|
|
43
63
|
loadingLabel?: string;
|
|
44
64
|
/**
|
|
45
|
-
* How to render each option's label
|
|
46
|
-
* (`itemToLabel="name"`), or a function for
|
|
47
|
-
*
|
|
65
|
+
* How to render each option's label, in the list and in its chip. Pass a
|
|
66
|
+
* property key for the common case (`itemToLabel="name"`), or a function for
|
|
67
|
+
* full control.
|
|
48
68
|
* @default String(item)
|
|
49
69
|
*/
|
|
50
70
|
itemToLabel?: Accessor<T, React.ReactNode>;
|
|
71
|
+
/**
|
|
72
|
+
* The option's label as text: the local search matches it, and the remove
|
|
73
|
+
* button, the "+N" tooltip and the Backspace message read it.
|
|
74
|
+
* @default the label when it is a string or number, else the value
|
|
75
|
+
*/
|
|
76
|
+
itemToString?: Accessor<T, string>;
|
|
51
77
|
/**
|
|
52
78
|
* How to derive the stable value stored in state for each option. Pass a
|
|
53
79
|
* property key (`itemToValue="id"`) or a function. Required when items are
|
|
54
80
|
* objects; for string/number items it defaults to the item itself.
|
|
55
81
|
*/
|
|
56
82
|
itemToValue?: Accessor<T, ItemValue>;
|
|
57
|
-
/**
|
|
83
|
+
/**
|
|
84
|
+
* The group an option belongs to. Options show under their group's label,
|
|
85
|
+
* groups in the order they first appear in `items`, and the "+N" tooltip
|
|
86
|
+
* names a hidden value as "group · label".
|
|
87
|
+
*/
|
|
88
|
+
itemToGroup?: Accessor<T, string>;
|
|
89
|
+
/** Picked values. Use for controlled usage. */
|
|
58
90
|
value?: readonly ItemValue[];
|
|
59
|
-
/** Initially
|
|
91
|
+
/** Initially picked values for uncontrolled usage. */
|
|
60
92
|
defaultValue?: readonly ItemValue[];
|
|
61
|
-
/** Called with the next array of
|
|
93
|
+
/** Called with the next array of picked values whenever the selection changes. */
|
|
62
94
|
onValueChange?: (value: ItemValue[]) => void;
|
|
63
95
|
/**
|
|
64
|
-
*
|
|
96
|
+
* The items of picked values that may not be in `items`, such as the saved
|
|
97
|
+
* values of a server-searched field. MultiSelect also remembers every item it
|
|
98
|
+
* has been given, so a value picked from an earlier search keeps its chip.
|
|
99
|
+
*/
|
|
100
|
+
selectedItems?: readonly T[];
|
|
101
|
+
/**
|
|
102
|
+
* Turns on server search: called with the typed text on every change,
|
|
103
|
+
* including the clear after a pick. Pass the results as `items`, and set
|
|
104
|
+
* `isLoading` while they load. Without it, MultiSelect filters `items`.
|
|
105
|
+
*/
|
|
106
|
+
onSearchChange?: (search: string) => void;
|
|
107
|
+
/**
|
|
108
|
+
* A search is loading. The previous results stay in the list, the chevrons
|
|
109
|
+
* turn into a spinner, and Enter picks nothing until the new results arrive.
|
|
110
|
+
*/
|
|
111
|
+
isLoading?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* The search input's placeholder while nothing is picked.
|
|
65
114
|
* @default `messages.multiSelect.placeholder`, "Select…"
|
|
66
115
|
*/
|
|
67
|
-
placeholder?:
|
|
116
|
+
placeholder?: string;
|
|
68
117
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* renders multi-line content, so the trigger stays a single, non-growing line.
|
|
118
|
+
* The list's text when the search matches no option.
|
|
119
|
+
* @default `messages.multiSelect.empty`, "No results."
|
|
72
120
|
*/
|
|
73
|
-
|
|
121
|
+
emptyLabel?: string;
|
|
74
122
|
/** Disable the entire control. */
|
|
75
123
|
disabled?: boolean;
|
|
76
|
-
/** Render the error state (sets `aria-invalid` on the
|
|
124
|
+
/** Render the error state (sets `aria-invalid` on the input). */
|
|
77
125
|
invalid?: boolean;
|
|
78
|
-
/**
|
|
79
|
-
size?: 'sm' | 'default';
|
|
80
|
-
/** Class applied to the trigger. Defaults to a full-width, non-growing trigger; pass e.g. `w-[260px]` to fix the width. */
|
|
126
|
+
/** Class applied to the field. It is full width by default; pass e.g. `w-[260px]` to fix the width. */
|
|
81
127
|
className?: string;
|
|
82
128
|
/** Class applied to the dropdown content. */
|
|
83
129
|
contentClassName?: string;
|
|
@@ -85,29 +131,111 @@ export interface MultiSelectProps<T> {
|
|
|
85
131
|
name?: string;
|
|
86
132
|
/** Whether a selection is required for form submission. */
|
|
87
133
|
required?: boolean;
|
|
88
|
-
/** id forwarded to the
|
|
134
|
+
/** id forwarded to the search input, for a `<Label htmlFor>`. */
|
|
89
135
|
id?: string;
|
|
136
|
+
/** Accessible name of the search input, when no visible label points at it. */
|
|
137
|
+
'aria-label'?: string;
|
|
90
138
|
}
|
|
91
139
|
|
|
140
|
+
type ItemGroup<T> = { value: string; items: T[] };
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Measures every picked chip in a hidden row, in the box of a ComboboxChip, and
|
|
144
|
+
* returns how many fit on the field's one line before a "+N" chip. Counted
|
|
145
|
+
* before paint on a pick or a remove, and again only when the row's width
|
|
146
|
+
* changes, never on focus or while typing.
|
|
147
|
+
*/
|
|
148
|
+
function useChipFit(keys: readonly ItemValue[]) {
|
|
149
|
+
const rowRef = React.useRef<HTMLDivElement | null>(null);
|
|
150
|
+
const measureRef = React.useRef<HTMLDivElement | null>(null);
|
|
151
|
+
const [fit, setFit] = React.useState<ChipFit>({ count: keys.length });
|
|
152
|
+
|
|
153
|
+
const compute = React.useCallback(() => {
|
|
154
|
+
const row = rowRef.current;
|
|
155
|
+
const measure = measureRef.current;
|
|
156
|
+
if (!row || !measure) return;
|
|
157
|
+
const widths = Array.from(measure.querySelectorAll<HTMLElement>('[data-measure=chip]')).map(
|
|
158
|
+
(chip) => chip.offsetWidth,
|
|
159
|
+
);
|
|
160
|
+
const countWidth = measure.querySelector<HTMLElement>('[data-measure=count]')?.offsetWidth ?? 0;
|
|
161
|
+
const next = fitChips(widths, row.clientWidth - INPUT_RESERVE, countWidth, CHIP_GAP);
|
|
162
|
+
setFit((prev) => (prev.count === next.count && prev.firstMax === next.firstMax ? prev : next));
|
|
163
|
+
}, []);
|
|
164
|
+
|
|
165
|
+
// The key list changes on a pick or a remove; its joined form keeps the
|
|
166
|
+
// effect from running on a re-render with the same values.
|
|
167
|
+
const keyList = keys.join('\u0000');
|
|
168
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: recount when the picked values change.
|
|
169
|
+
React.useLayoutEffect(() => {
|
|
170
|
+
compute();
|
|
171
|
+
}, [compute, keyList]);
|
|
172
|
+
|
|
173
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: keyList re-observes the chips of the current picks.
|
|
174
|
+
React.useEffect(() => {
|
|
175
|
+
const row = rowRef.current;
|
|
176
|
+
const measure = measureRef.current;
|
|
177
|
+
if (!row || !measure || typeof ResizeObserver === 'undefined') return;
|
|
178
|
+
// Counts again when the row's width changes (the input in it is flex-1,
|
|
179
|
+
// so only a resize of the field does that) or the chips' widths change
|
|
180
|
+
// without a pick (a web font that loads after the first count).
|
|
181
|
+
let rowWidth = row.clientWidth;
|
|
182
|
+
const chipsWidth = () =>
|
|
183
|
+
Array.from(measure.children).reduce(
|
|
184
|
+
(sum, chip) => sum + (chip as HTMLElement).offsetWidth,
|
|
185
|
+
0,
|
|
186
|
+
);
|
|
187
|
+
let chips = chipsWidth();
|
|
188
|
+
const observer = new ResizeObserver(() => {
|
|
189
|
+
if (row.clientWidth === rowWidth && chipsWidth() === chips) return;
|
|
190
|
+
rowWidth = row.clientWidth;
|
|
191
|
+
chips = chipsWidth();
|
|
192
|
+
compute();
|
|
193
|
+
});
|
|
194
|
+
observer.observe(row);
|
|
195
|
+
for (const chip of Array.from(measure.children)) observer.observe(chip);
|
|
196
|
+
// A change between the count before paint and now.
|
|
197
|
+
compute();
|
|
198
|
+
return () => observer.disconnect();
|
|
199
|
+
}, [compute, keyList]);
|
|
200
|
+
|
|
201
|
+
return { rowRef, measureRef, fit };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* A searchable multi-select: the picked values are chips on one line, the
|
|
206
|
+
* ones that do not fit go behind a "+N" chip with a tooltip, and the list
|
|
207
|
+
* stays open while the user picks.
|
|
208
|
+
*/
|
|
92
209
|
function MultiSelect<T>({
|
|
93
210
|
items,
|
|
94
211
|
loadingLabel,
|
|
95
212
|
itemToLabel = String as unknown as Accessor<T, React.ReactNode>,
|
|
213
|
+
itemToString,
|
|
96
214
|
itemToValue,
|
|
97
|
-
|
|
215
|
+
itemToGroup,
|
|
216
|
+
value: valueProp,
|
|
98
217
|
defaultValue,
|
|
99
218
|
onValueChange,
|
|
219
|
+
selectedItems,
|
|
220
|
+
onSearchChange,
|
|
221
|
+
isLoading = false,
|
|
100
222
|
placeholder: placeholderProp,
|
|
101
|
-
|
|
223
|
+
emptyLabel,
|
|
102
224
|
disabled,
|
|
103
225
|
invalid,
|
|
104
|
-
size = 'default',
|
|
105
226
|
className,
|
|
106
227
|
contentClassName,
|
|
107
228
|
name,
|
|
108
229
|
required,
|
|
109
230
|
id,
|
|
231
|
+
'aria-label': ariaLabel,
|
|
110
232
|
}: MultiSelectProps<T>) {
|
|
233
|
+
const t = useMessages('multiSelect', {
|
|
234
|
+
loading: loadingLabel,
|
|
235
|
+
placeholder: placeholderProp,
|
|
236
|
+
empty: emptyLabel,
|
|
237
|
+
});
|
|
238
|
+
|
|
111
239
|
const getValue = React.useCallback(
|
|
112
240
|
(item: T): ItemValue =>
|
|
113
241
|
itemToValue == null ? (item as unknown as ItemValue) : resolveAccessor(item, itemToValue),
|
|
@@ -117,13 +245,28 @@ function MultiSelect<T>({
|
|
|
117
245
|
(item: T): React.ReactNode => resolveAccessor(item, itemToLabel),
|
|
118
246
|
[itemToLabel],
|
|
119
247
|
);
|
|
248
|
+
const getString = React.useCallback(
|
|
249
|
+
(item: T): string => {
|
|
250
|
+
if (itemToString != null) return resolveAccessor(item, itemToString);
|
|
251
|
+
const label = resolveAccessor(item, itemToLabel);
|
|
252
|
+
return typeof label === 'string' || typeof label === 'number'
|
|
253
|
+
? String(label)
|
|
254
|
+
: String(getValue(item));
|
|
255
|
+
},
|
|
256
|
+
[itemToString, itemToLabel, getValue],
|
|
257
|
+
);
|
|
258
|
+
const getGroup = React.useCallback(
|
|
259
|
+
(item: T): string | undefined =>
|
|
260
|
+
itemToGroup == null ? undefined : resolveAccessor(item, itemToGroup),
|
|
261
|
+
[itemToGroup],
|
|
262
|
+
);
|
|
120
263
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const
|
|
264
|
+
// Every item seen so far, by value, so a picked value keeps its chip when a
|
|
265
|
+
// later search no longer returns it.
|
|
266
|
+
const known = React.useRef(new Map<ItemValue, T>());
|
|
267
|
+
React.useMemo(() => {
|
|
268
|
+
for (const item of selectedItems ?? []) known.current.set(getValue(item), item);
|
|
269
|
+
const seen = new Set<ItemValue>();
|
|
127
270
|
for (const item of items ?? []) {
|
|
128
271
|
const itemValue = getValue(item);
|
|
129
272
|
if (isDevelopment) {
|
|
@@ -133,90 +276,306 @@ function MultiSelect<T>({
|
|
|
133
276
|
'objects, pass `itemToValue` (e.g. itemToValue="id") so the selection survives the ' +
|
|
134
277
|
'items array being recreated.',
|
|
135
278
|
);
|
|
136
|
-
} else if (
|
|
279
|
+
} else if (seen.has(itemValue)) {
|
|
137
280
|
console.warn(
|
|
138
281
|
`MultiSelect: duplicate item value ${JSON.stringify(itemValue)} — each item must ` +
|
|
139
282
|
'resolve to a unique `itemToValue`; later items override earlier ones.',
|
|
140
283
|
);
|
|
141
284
|
}
|
|
142
285
|
}
|
|
143
|
-
|
|
286
|
+
seen.add(itemValue);
|
|
287
|
+
known.current.set(itemValue, item);
|
|
288
|
+
}
|
|
289
|
+
}, [items, selectedItems, getValue]);
|
|
290
|
+
|
|
291
|
+
const [uncontrolled, setUncontrolled] = React.useState<readonly ItemValue[]>(defaultValue ?? []);
|
|
292
|
+
const ids = valueProp ?? uncontrolled;
|
|
293
|
+
const setIds = (next: ItemValue[]) => {
|
|
294
|
+
if (valueProp === undefined) setUncontrolled(next);
|
|
295
|
+
onValueChange?.(next);
|
|
296
|
+
};
|
|
297
|
+
const picked = ids
|
|
298
|
+
.map((itemValue) => known.current.get(itemValue))
|
|
299
|
+
.filter((item): item is T => item !== undefined);
|
|
300
|
+
// The first load has not finished and a picked value has no label yet.
|
|
301
|
+
const labelsPending = items === undefined && picked.length < ids.length;
|
|
302
|
+
|
|
303
|
+
const [open, setOpen] = React.useState(false);
|
|
304
|
+
const [query, setQuery] = React.useState('');
|
|
305
|
+
// The index of the value the next Backspace removes.
|
|
306
|
+
const [armed, setArmed] = React.useState<number | null>(null);
|
|
307
|
+
const [tipOpen, setTipOpen] = React.useState(false);
|
|
308
|
+
// Base UI highlights a row on open (the first picked value) and after a
|
|
309
|
+
// pick. With an empty search that highlight stays hidden, and Enter picks
|
|
310
|
+
// nothing, until the user moves it with an arrow key or the pointer.
|
|
311
|
+
const [hasHighlight, setHasHighlight] = React.useState(false);
|
|
312
|
+
const [navigated, setNavigated] = React.useState(false);
|
|
313
|
+
const quiet = query === '' && !navigated;
|
|
314
|
+
// The search changed and its results have not arrived. Set on a change of
|
|
315
|
+
// the typed text, cleared when new `items` arrive or `isLoading` turns off.
|
|
316
|
+
const [stale, setStale] = React.useState(false);
|
|
317
|
+
const previousItems = React.useRef(items);
|
|
318
|
+
const wasLoading = React.useRef(isLoading);
|
|
319
|
+
if (previousItems.current !== items || (wasLoading.current && !isLoading)) {
|
|
320
|
+
previousItems.current = items;
|
|
321
|
+
if (stale) setStale(false);
|
|
322
|
+
}
|
|
323
|
+
wasLoading.current = isLoading;
|
|
324
|
+
const pending = stale || isLoading;
|
|
325
|
+
// While the rows belong to the previous search, Enter picks nothing, so no
|
|
326
|
+
// row shows as highlighted either.
|
|
327
|
+
const highlightHidden = quiet || pending;
|
|
328
|
+
|
|
329
|
+
// The input needs its own id: without one, Base UI gives the input and the
|
|
330
|
+
// open button the same id.
|
|
331
|
+
const fallbackId = React.useId();
|
|
332
|
+
const anchor = React.useRef<HTMLDivElement | null>(null);
|
|
333
|
+
const inputRef = React.useRef<HTMLInputElement | null>(null);
|
|
334
|
+
// Counted from the chips on screen: a value whose label is still loading
|
|
335
|
+
// has no chip yet, so its arrival counts again.
|
|
336
|
+
const pickedKeys = React.useMemo(() => picked.map(getValue), [picked, getValue]);
|
|
337
|
+
const { rowRef, measureRef, fit } = useChipFit(pickedKeys);
|
|
338
|
+
|
|
339
|
+
// While the list is open, Base UI hides everything but the input and the
|
|
340
|
+
// list from assistive technology, the field's <label> included. A hidden
|
|
341
|
+
// label no longer names the input, so the input points at it by id too.
|
|
342
|
+
const [labelledBy, setLabelledBy] = React.useState<string | undefined>();
|
|
343
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: a new id points another label at the input.
|
|
344
|
+
React.useLayoutEffect(() => {
|
|
345
|
+
const input = inputRef.current;
|
|
346
|
+
if (!input || ariaLabel) return;
|
|
347
|
+
const labelIds = Array.from(input.labels ?? []).map((label, index) => {
|
|
348
|
+
if (!label.id) label.id = `${input.id}-label${index > 0 ? `-${index}` : ''}`;
|
|
349
|
+
return label.id;
|
|
350
|
+
});
|
|
351
|
+
setLabelledBy(labelIds.length > 0 ? labelIds.join(' ') : undefined);
|
|
352
|
+
}, [ariaLabel, id]);
|
|
353
|
+
const shown = picked.slice(0, fit.count);
|
|
354
|
+
const hidden = picked.slice(fit.count);
|
|
355
|
+
const armedItem = armed === null ? undefined : picked[armed];
|
|
356
|
+
const countArmed = armed !== null && armed >= shown.length;
|
|
357
|
+
|
|
358
|
+
const results = React.useMemo(() => {
|
|
359
|
+
if (items === undefined) return [];
|
|
360
|
+
if (onSearchChange) return [...items];
|
|
361
|
+
const search = query.trim().toLowerCase();
|
|
362
|
+
if (!search) return [...items];
|
|
363
|
+
return items.filter(
|
|
364
|
+
(item) =>
|
|
365
|
+
getString(item).toLowerCase().includes(search) ||
|
|
366
|
+
(getGroup(item) ?? '').toLowerCase().includes(search),
|
|
367
|
+
);
|
|
368
|
+
}, [items, onSearchChange, query, getString, getGroup]);
|
|
369
|
+
|
|
370
|
+
// Each option shows one time, in its group, in a fixed order: a pick
|
|
371
|
+
// changes its check, never its place.
|
|
372
|
+
const groups = React.useMemo((): ItemGroup<T>[] | undefined => {
|
|
373
|
+
if (itemToGroup == null) return undefined;
|
|
374
|
+
const byGroup = new Map<string, T[]>();
|
|
375
|
+
for (const item of results) {
|
|
376
|
+
const group = getGroup(item) ?? '';
|
|
377
|
+
const list = byGroup.get(group);
|
|
378
|
+
if (list) list.push(item);
|
|
379
|
+
else byGroup.set(group, [item]);
|
|
144
380
|
}
|
|
145
|
-
return map;
|
|
146
|
-
}, [
|
|
381
|
+
return [...byGroup].map(([group, groupItems]) => ({ value: group, items: groupItems }));
|
|
382
|
+
}, [itemToGroup, results, getGroup]);
|
|
383
|
+
|
|
384
|
+
const hiddenLabel = (item: T) => {
|
|
385
|
+
const group = getGroup(item);
|
|
386
|
+
return group ? `${group} · ${getString(item)}` : getString(item);
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const renderItem = (item: T) => (
|
|
390
|
+
<ComboboxItem key={getValue(item)} value={item}>
|
|
391
|
+
{getLabel(item)}
|
|
392
|
+
</ComboboxItem>
|
|
393
|
+
);
|
|
147
394
|
|
|
148
395
|
return (
|
|
149
|
-
<
|
|
396
|
+
<Combobox<T, true>
|
|
150
397
|
multiple
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
398
|
+
items={(groups ?? results) as T[]}
|
|
399
|
+
filter={null}
|
|
400
|
+
value={picked}
|
|
401
|
+
onValueChange={(next) => {
|
|
402
|
+
setIds(next.map(getValue));
|
|
403
|
+
setArmed(null);
|
|
404
|
+
}}
|
|
405
|
+
inputValue={query}
|
|
406
|
+
onInputValueChange={(next, details) => {
|
|
407
|
+
setArmed(null);
|
|
408
|
+
if (onSearchChange) {
|
|
409
|
+
setStale(true);
|
|
410
|
+
onSearchChange(next);
|
|
411
|
+
}
|
|
412
|
+
// Base UI clears the search after a pick ("input-clear"), so the
|
|
413
|
+
// next value can be typed at once. The search is empty again.
|
|
414
|
+
setQuery(next);
|
|
415
|
+
if (details.reason === 'input-clear' || next === '') setNavigated(false);
|
|
416
|
+
}}
|
|
417
|
+
open={open}
|
|
418
|
+
onOpenChange={(next, details) => {
|
|
419
|
+
// A pick keeps the list open, so several values go in one pass.
|
|
420
|
+
if (!next && details.reason === 'item-press') return;
|
|
421
|
+
if (next) setNavigated(false);
|
|
422
|
+
setOpen(next);
|
|
423
|
+
}}
|
|
424
|
+
onItemHighlighted={(item, details) => {
|
|
425
|
+
setHasHighlight(item !== undefined);
|
|
426
|
+
if (item !== undefined && details.reason !== 'none') setNavigated(true);
|
|
427
|
+
}}
|
|
428
|
+
// Typing highlights the first result, so Enter alone picks it.
|
|
429
|
+
autoHighlight
|
|
430
|
+
isItemEqualToValue={(a, b) => getValue(a) === getValue(b)}
|
|
431
|
+
itemToStringLabel={getString}
|
|
432
|
+
itemToStringValue={(item) => String(getValue(item))}
|
|
433
|
+
isLoading={items === undefined}
|
|
434
|
+
loadingLabel={t.loading}
|
|
154
435
|
disabled={disabled}
|
|
155
436
|
name={name}
|
|
156
437
|
required={required}
|
|
157
438
|
>
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
size={size}
|
|
163
|
-
className={cn('w-full', className)}
|
|
164
|
-
aria-invalid={invalid || undefined}
|
|
165
|
-
aria-busy={isLoading || undefined}
|
|
439
|
+
<ComboboxChips
|
|
440
|
+
ref={anchor}
|
|
441
|
+
data-slot="multi-select"
|
|
442
|
+
className={cn('relative w-full flex-nowrap items-start', className)}
|
|
166
443
|
>
|
|
167
|
-
<
|
|
168
|
-
{
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
444
|
+
<div
|
|
445
|
+
ref={measureRef}
|
|
446
|
+
aria-hidden="true"
|
|
447
|
+
className="pointer-events-none invisible absolute start-0 top-0 flex h-0 overflow-hidden"
|
|
448
|
+
>
|
|
449
|
+
{picked.map((item) => (
|
|
450
|
+
<span key={getValue(item)} data-measure="chip" className={cn(chipShape, chipBox)}>
|
|
451
|
+
<span>{getLabel(item)}</span>
|
|
452
|
+
<span className="size-4" />
|
|
453
|
+
</span>
|
|
454
|
+
))}
|
|
455
|
+
<span data-measure="count" className={cn(chipShape, chipCountBox)}>
|
|
456
|
+
+{Math.max(picked.length, 10)}
|
|
457
|
+
</span>
|
|
458
|
+
</div>
|
|
459
|
+
<div
|
|
460
|
+
ref={rowRef}
|
|
461
|
+
className="flex min-w-0 flex-1 flex-nowrap items-center gap-1.5 overflow-hidden"
|
|
462
|
+
>
|
|
463
|
+
{labelsPending ? (
|
|
464
|
+
<span data-slot="multi-select-value-skeleton" className="flex h-6 items-center ps-1.25">
|
|
465
|
+
<SkeletonLine className="w-24" />
|
|
466
|
+
<span className="sr-only">{t.loading}</span>
|
|
467
|
+
</span>
|
|
468
|
+
) : (
|
|
469
|
+
shown.map((item, index) => (
|
|
470
|
+
<ComboboxChip
|
|
471
|
+
key={getValue(item)}
|
|
472
|
+
removeLabel={fillMessage(t.remove, { label: getString(item) })}
|
|
473
|
+
data-armed={armed === index || undefined}
|
|
474
|
+
style={index === 0 && fit.firstMax ? { maxWidth: fit.firstMax } : undefined}
|
|
475
|
+
>
|
|
189
476
|
{getLabel(item)}
|
|
190
|
-
</
|
|
191
|
-
))
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
477
|
+
</ComboboxChip>
|
|
478
|
+
))
|
|
479
|
+
)}
|
|
480
|
+
{!labelsPending && hidden.length > 0 && (
|
|
481
|
+
<Tooltip open={tipOpen || countArmed} onOpenChange={setTipOpen}>
|
|
482
|
+
<TooltipTrigger
|
|
483
|
+
render={
|
|
484
|
+
<ComboboxChipCount
|
|
485
|
+
count={hidden.length}
|
|
486
|
+
data-armed={countArmed || undefined}
|
|
487
|
+
disabled={disabled}
|
|
488
|
+
// Base UI hides the field but the input from assistive
|
|
489
|
+
// technology while the list is open, so the count
|
|
490
|
+
// leaves the tab order too.
|
|
491
|
+
tabIndex={open ? -1 : undefined}
|
|
492
|
+
onClick={() => {
|
|
493
|
+
inputRef.current?.focus();
|
|
494
|
+
setOpen(true);
|
|
495
|
+
}}
|
|
496
|
+
/>
|
|
497
|
+
}
|
|
498
|
+
/>
|
|
499
|
+
<TooltipContent>
|
|
500
|
+
<ul data-slot="multi-select-hidden-values">
|
|
501
|
+
{hidden.map((item) => (
|
|
502
|
+
<li key={getValue(item)}>{hiddenLabel(item)}</li>
|
|
503
|
+
))}
|
|
504
|
+
</ul>
|
|
505
|
+
</TooltipContent>
|
|
506
|
+
</Tooltip>
|
|
507
|
+
)}
|
|
508
|
+
<ComboboxChipsInput
|
|
509
|
+
ref={inputRef}
|
|
510
|
+
id={id ?? fallbackId}
|
|
511
|
+
aria-label={ariaLabel}
|
|
512
|
+
aria-labelledby={labelledBy}
|
|
513
|
+
aria-invalid={invalid || undefined}
|
|
514
|
+
aria-busy={labelsPending || undefined}
|
|
515
|
+
placeholder={ids.length === 0 ? t.placeholder : undefined}
|
|
516
|
+
className="min-w-6"
|
|
517
|
+
// While the highlight is hidden, the input does not point at the
|
|
518
|
+
// row either, so a screen reader does not read it as active.
|
|
519
|
+
render={(props) => (
|
|
520
|
+
<input
|
|
521
|
+
{...props}
|
|
522
|
+
aria-activedescendant={highlightHidden ? undefined : props['aria-activedescendant']}
|
|
523
|
+
/>
|
|
524
|
+
)}
|
|
525
|
+
onClick={() => setArmed(null)}
|
|
526
|
+
onKeyDown={(event) => {
|
|
527
|
+
const action = multiSelectKeyAction({
|
|
528
|
+
key: event.key,
|
|
529
|
+
inputEmpty: event.currentTarget.value === '',
|
|
530
|
+
open,
|
|
531
|
+
quiet,
|
|
532
|
+
hasHighlight,
|
|
533
|
+
stale: pending,
|
|
534
|
+
valueCount: ids.length,
|
|
535
|
+
armed,
|
|
536
|
+
});
|
|
537
|
+
if (action.type === 'pass') return;
|
|
538
|
+
if (action.type === 'disarm') {
|
|
539
|
+
setArmed(null);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
event.preventBaseUIHandler();
|
|
543
|
+
if (action.type !== 'escape') event.preventDefault();
|
|
544
|
+
if (action.type === 'reveal') setNavigated(true);
|
|
545
|
+
else if (action.type === 'arm') setArmed(action.index);
|
|
546
|
+
else if (action.type === 'remove') {
|
|
547
|
+
setIds(ids.filter((_, index) => index !== action.index));
|
|
548
|
+
setArmed(null);
|
|
549
|
+
} else if (action.type === 'escape') setArmed(null);
|
|
550
|
+
}}
|
|
551
|
+
/>
|
|
552
|
+
</div>
|
|
553
|
+
<ComboboxChipsActions
|
|
554
|
+
showClear={ids.length > 0}
|
|
555
|
+
isLoading={isLoading && items !== undefined}
|
|
556
|
+
disabled={disabled}
|
|
557
|
+
/>
|
|
558
|
+
<span aria-live="polite" className="sr-only">
|
|
559
|
+
{armedItem ? fillMessage(t.armed, { label: getString(armedItem) }) : ''}
|
|
560
|
+
</span>
|
|
561
|
+
</ComboboxChips>
|
|
562
|
+
<ComboboxContent anchor={anchor} className={contentClassName}>
|
|
563
|
+
<ComboboxEmpty>{t.empty}</ComboboxEmpty>
|
|
564
|
+
<ComboboxList
|
|
565
|
+
data-quiet={highlightHidden || undefined}
|
|
566
|
+
className="data-quiet:[&_[data-highlighted]]:bg-transparent! data-quiet:[&_[data-highlighted]]:outline-none!"
|
|
567
|
+
>
|
|
568
|
+
{groups
|
|
569
|
+
? (group: ItemGroup<T>) => (
|
|
570
|
+
<ComboboxGroup key={group.value} items={group.items}>
|
|
571
|
+
{group.value ? <ComboboxLabel>{group.value}</ComboboxLabel> : null}
|
|
572
|
+
<ComboboxCollection>{renderItem}</ComboboxCollection>
|
|
573
|
+
</ComboboxGroup>
|
|
574
|
+
)
|
|
575
|
+
: renderItem}
|
|
576
|
+
</ComboboxList>
|
|
577
|
+
</ComboboxContent>
|
|
578
|
+
</Combobox>
|
|
220
579
|
);
|
|
221
580
|
}
|
|
222
581
|
|
|
@@ -102,7 +102,8 @@ const statusBadgeVariants = cva(
|
|
|
102
102
|
|
|
103
103
|
// The `bare` appearance: the chip without its box. The same icon and tinted
|
|
104
104
|
// label, no fill, outline or padding, for a state inside a tinted or accent
|
|
105
|
-
// surface, where a chip would be a box inside a box
|
|
105
|
+
// surface, where a chip would be a box inside a box, and for a count beside a
|
|
106
|
+
// title, where a chip is taller than the title.
|
|
106
107
|
const bareVariants = cva(
|
|
107
108
|
[
|
|
108
109
|
'inline-flex w-fit shrink-0 items-center gap-1.5 whitespace-nowrap font-medium',
|
|
@@ -191,7 +192,9 @@ interface StatusBadgeProps extends useRender.ComponentProps<'span'> {
|
|
|
191
192
|
* - `chip` (default): a tinted chip with a tone outline.
|
|
192
193
|
* - `bare`: the chip's icon and tinted label without the box. Use it for a
|
|
193
194
|
* state inside a tinted or accent surface (a card that is already
|
|
194
|
-
* coloured), where a chip would be a box inside a box.
|
|
195
|
+
* coloured), where a chip would be a box inside a box. Use it too for a
|
|
196
|
+
* count beside a title ("2 warnings" in an `ActionNeeded` header): it is as
|
|
197
|
+
* high as the text, where a chip is taller than the title.
|
|
195
198
|
* - `icon`: the tone icon with no box, then the label in neutral text, with
|
|
196
199
|
* an optional second line (`detail`). Use it for a status every row of a
|
|
197
200
|
* table shows (a deployments table).
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { statusChipRing } from './tint-recipes.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The box a chip in a chips field shares with the "+N" chip: the fill, the
|
|
5
|
+
* outline, the height and the corner. `data-armed` marks the chip the next
|
|
6
|
+
* Backspace removes, with the focus ring and no change in size.
|
|
7
|
+
*/
|
|
8
|
+
export const chipShape = `bg-accent [--status-tone:var(--neutral-text)] ${statusChipRing} h-6 shrink-0 rounded-[calc(var(--radius-xl)-5px)] text-xs font-medium whitespace-nowrap outline-none data-armed:outline-solid data-armed:outline-2 data-armed:-outline-offset-2 data-armed:outline-ring focus-visible:outline-solid focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring`;
|
|
9
|
+
|
|
10
|
+
/** The inside of a chip with a remove button: label 8px in, a 16px button 4px from the end. */
|
|
11
|
+
export const chipBox = 'flex items-center gap-1.5 ps-2 pe-1';
|
|
12
|
+
|
|
13
|
+
/** The inside of the "+N" chip. */
|
|
14
|
+
export const chipCountBox = 'flex items-center px-1.5 tabular-nums';
|
|
15
|
+
|
|
16
|
+
/** The gap between chips, `gap-1.5`. */
|
|
17
|
+
export const CHIP_GAP = 6;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Room kept after the chips for the caret and a typed search. The same at
|
|
21
|
+
* rest, on focus and while typing, so the number of chips does not change
|
|
22
|
+
* on focus or while typing: only a pick, a remove or a resize changes it.
|
|
23
|
+
*/
|
|
24
|
+
export const INPUT_RESERVE = 40;
|
|
25
|
+
|
|
26
|
+
/** The narrowest a lone chip shrinks to before its label is cut. */
|
|
27
|
+
export const MIN_CHIP_WIDTH = 48;
|
|
28
|
+
|
|
29
|
+
export type ChipFit = {
|
|
30
|
+
/** How many chips show, from the start. The rest go behind "+N". */
|
|
31
|
+
count: number;
|
|
32
|
+
/** Set when not even the first chip fits: its maximum width, in pixels. */
|
|
33
|
+
firstMax?: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* How many chips fit on one line of `available` pixels, leaving room for a
|
|
38
|
+
* "+N" chip of `countWidth` pixels when some do not. At least one chip
|
|
39
|
+
* always shows: when the first does not fit, it shows shortened to
|
|
40
|
+
* `firstMax`, so the field never shows only a count.
|
|
41
|
+
*/
|
|
42
|
+
export function fitChips(
|
|
43
|
+
widths: readonly number[],
|
|
44
|
+
available: number,
|
|
45
|
+
countWidth: number,
|
|
46
|
+
gap = CHIP_GAP,
|
|
47
|
+
): ChipFit {
|
|
48
|
+
let used = 0;
|
|
49
|
+
let count = 0;
|
|
50
|
+
for (let index = 0; index < widths.length; index++) {
|
|
51
|
+
const width = (widths[index] ?? 0) + (index > 0 ? gap : 0);
|
|
52
|
+
const rest = index < widths.length - 1 ? countWidth + gap : 0;
|
|
53
|
+
if (used + width + rest > available) break;
|
|
54
|
+
used += width;
|
|
55
|
+
count++;
|
|
56
|
+
}
|
|
57
|
+
if (count === 0 && widths.length > 0) {
|
|
58
|
+
const rest = widths.length > 1 ? countWidth + gap : 0;
|
|
59
|
+
return { count: 1, firstMax: Math.max(MIN_CHIP_WIDTH, available - rest) };
|
|
60
|
+
}
|
|
61
|
+
return { count };
|
|
62
|
+
}
|
|
@@ -38,6 +38,10 @@ const defaultMessages = {
|
|
|
38
38
|
clear: 'Clear',
|
|
39
39
|
/** Accessible name of a ComboboxChip's remove button. */
|
|
40
40
|
remove: 'Remove',
|
|
41
|
+
/** Accessible name of ComboboxChipsActions' clear-all button. */
|
|
42
|
+
clearAll: 'Clear all',
|
|
43
|
+
/** Accessible name of ComboboxChipCount, the "+N" chip. `{count}` is the number of hidden values. */
|
|
44
|
+
more: '{count} more',
|
|
41
45
|
},
|
|
42
46
|
comboboxFreeText: {
|
|
43
47
|
/** Announced by the field's spinner while suggestions load. */
|
|
@@ -60,8 +64,17 @@ const defaultMessages = {
|
|
|
60
64
|
multiSelect: {
|
|
61
65
|
/** Stands in for the selected values while the options load. */
|
|
62
66
|
loading: 'Loading…',
|
|
63
|
-
/** The
|
|
67
|
+
/** The search input's placeholder when nothing is selected. */
|
|
64
68
|
placeholder: 'Select…',
|
|
69
|
+
/** The list's text when the search matches no option. */
|
|
70
|
+
empty: 'No results.',
|
|
71
|
+
/** Accessible name of a chip's remove button. `{label}` is the value's label. */
|
|
72
|
+
remove: 'Remove {label}',
|
|
73
|
+
/**
|
|
74
|
+
* Announced on the first Backspace in an empty search, which marks the last
|
|
75
|
+
* value; the second Backspace removes it. `{label}` is the value's label.
|
|
76
|
+
*/
|
|
77
|
+
armed: '{label} marked. Press Backspace again to remove it.',
|
|
65
78
|
},
|
|
66
79
|
passwordInput: {
|
|
67
80
|
showPassword: 'Show password',
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keyboard rules of MultiSelect's search input, as a function of the
|
|
3
|
+
* field's state and the key. MultiSelect applies the action; Base UI keeps
|
|
4
|
+
* every key this returns `pass` or `disarm` for.
|
|
5
|
+
*/
|
|
6
|
+
export type MultiSelectKeyState = {
|
|
7
|
+
key: string;
|
|
8
|
+
/** The search input holds no text. */
|
|
9
|
+
inputEmpty: boolean;
|
|
10
|
+
/** The list is open. */
|
|
11
|
+
open: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The search is empty and the user has not moved the highlight with an
|
|
14
|
+
* arrow key or the pointer: Base UI may hold a highlighted row, but the
|
|
15
|
+
* row does not show as highlighted.
|
|
16
|
+
*/
|
|
17
|
+
quiet: boolean;
|
|
18
|
+
/** Base UI holds a highlighted row. */
|
|
19
|
+
hasHighlight: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* The search changed and its results have not arrived: the rows on screen
|
|
22
|
+
* belong to the previous search.
|
|
23
|
+
*/
|
|
24
|
+
stale: boolean;
|
|
25
|
+
/** How many values are picked. */
|
|
26
|
+
valueCount: number;
|
|
27
|
+
/** The index of the value the next Backspace removes, if one is marked. */
|
|
28
|
+
armed: number | null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type MultiSelectKeyAction =
|
|
32
|
+
/** Show the row Base UI holds instead of moving past it. Base UI does nothing. */
|
|
33
|
+
| { type: 'reveal' }
|
|
34
|
+
/** Nothing happens. Base UI does nothing. */
|
|
35
|
+
| { type: 'block' }
|
|
36
|
+
/** Mark the value at `index`; the next Backspace removes it. Base UI does nothing. */
|
|
37
|
+
| { type: 'arm'; index: number }
|
|
38
|
+
/** Remove the value at `index`. Base UI does nothing. */
|
|
39
|
+
| { type: 'remove'; index: number }
|
|
40
|
+
/** Drop the mark and keep the values. Base UI does nothing. */
|
|
41
|
+
| { type: 'escape' }
|
|
42
|
+
/** Drop the mark; Base UI handles the key. */
|
|
43
|
+
| { type: 'disarm' }
|
|
44
|
+
/** Base UI handles the key. */
|
|
45
|
+
| { type: 'pass' };
|
|
46
|
+
|
|
47
|
+
export function multiSelectKeyAction(state: MultiSelectKeyState): MultiSelectKeyAction {
|
|
48
|
+
const { key, inputEmpty, open, quiet, hasHighlight, stale, valueCount, armed } = state;
|
|
49
|
+
|
|
50
|
+
// After an open or a pick, Base UI holds a row without showing it. The
|
|
51
|
+
// first arrow shows that row, so ArrowDown lands on the first result.
|
|
52
|
+
if ((key === 'ArrowDown' || key === 'ArrowUp') && open && quiet && hasHighlight) {
|
|
53
|
+
return { type: 'reveal' };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Enter picks only a row the user can see highlighted, from the current
|
|
57
|
+
// search: not a hidden highlight, not an empty list (Base UI would close
|
|
58
|
+
// it), and not a row of the previous search while the new one loads.
|
|
59
|
+
if (key === 'Enter' && open && (quiet || !hasHighlight || stale)) {
|
|
60
|
+
return { type: 'block' };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Backspace in an empty search takes two presses: the first marks the last
|
|
64
|
+
// value, the second removes it. Base UI would remove the last chip on
|
|
65
|
+
// screen in one press, which is not the last value once some are behind
|
|
66
|
+
// "+N".
|
|
67
|
+
if (key === 'Backspace' && inputEmpty && valueCount > 0) {
|
|
68
|
+
const last = valueCount - 1;
|
|
69
|
+
return armed === last ? { type: 'remove', index: last } : { type: 'arm', index: last };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Base UI clears every value on Escape while the list is closed.
|
|
73
|
+
if (key === 'Escape' && !open) return { type: 'escape' };
|
|
74
|
+
|
|
75
|
+
if (armed !== null && key !== 'Shift') return { type: 'disarm' };
|
|
76
|
+
return { type: 'pass' };
|
|
77
|
+
}
|