@oaiz/app-kit 0.0.0-stage → 0.1.4130
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/LICENSE +21 -0
- package/README.md +149 -2
- package/THIRD_PARTY_NOTICES.md +76 -0
- package/package.json +139 -4
- package/registry.json +1847 -0
- package/src/components/activity-timeline.tsx +227 -0
- package/src/components/app-shell.tsx +622 -0
- package/src/components/bar-list.tsx +47 -0
- package/src/components/board.tsx +700 -0
- package/src/components/calendar-view.tsx +1388 -0
- package/src/components/card-grid.tsx +462 -0
- package/src/components/charts.tsx +701 -0
- package/src/components/chat-markdown.tsx +230 -0
- package/src/components/chat-panel.tsx +760 -0
- package/src/components/code-editor.tsx +276 -0
- package/src/components/collection-controls.tsx +226 -0
- package/src/components/command-palette.tsx +403 -0
- package/src/components/confirm-action.tsx +123 -0
- package/src/components/data-table.tsx +821 -0
- package/src/components/date-input.tsx +429 -0
- package/src/components/empty-state.tsx +31 -0
- package/src/components/error-state.tsx +37 -0
- package/src/components/file-upload.tsx +595 -0
- package/src/components/filter-bar.tsx +415 -0
- package/src/components/grouped-picker.tsx +250 -0
- package/src/components/line-items.tsx +601 -0
- package/src/components/list-view.tsx +491 -0
- package/src/components/metrics.tsx +53 -0
- package/src/components/page-header.tsx +33 -0
- package/src/components/property-picker.tsx +546 -0
- package/src/components/record-detail.tsx +416 -0
- package/src/components/record-form.tsx +295 -0
- package/src/components/record-name.tsx +85 -0
- package/src/components/save-bar.tsx +142 -0
- package/src/components/sign-in.tsx +179 -0
- package/src/components/split-view.tsx +128 -0
- package/src/components/status-dot.tsx +56 -0
- package/src/components/stepper.tsx +153 -0
- package/src/components/text-diff-model.ts +111 -0
- package/src/components/text-diff.tsx +98 -0
- package/src/components/time-zone-picker.tsx +69 -0
- package/src/components/ui/accordion.tsx +138 -0
- package/src/components/ui/alert-dialog.tsx +67 -0
- package/src/components/ui/alert.tsx +65 -0
- package/src/components/ui/avatar-group.tsx +119 -0
- package/src/components/ui/avatar.tsx +55 -0
- package/src/components/ui/badge.tsx +59 -0
- package/src/components/ui/bottom-navigation.tsx +102 -0
- package/src/components/ui/breadcrumb.tsx +117 -0
- package/src/components/ui/button.tsx +71 -0
- package/src/components/ui/card.tsx +95 -0
- package/src/components/ui/checkbox.tsx +31 -0
- package/src/components/ui/collapsible.tsx +27 -0
- package/src/components/ui/context-menu.tsx +270 -0
- package/src/components/ui/count-up.tsx +136 -0
- package/src/components/ui/detail-disclosure-section.tsx +109 -0
- package/src/components/ui/dialog.tsx +157 -0
- package/src/components/ui/dropdown-menu.tsx +273 -0
- package/src/components/ui/form-field.tsx +155 -0
- package/src/components/ui/heading.tsx +126 -0
- package/src/components/ui/hover-card.tsx +45 -0
- package/src/components/ui/input.tsx +50 -0
- package/src/components/ui/label.tsx +30 -0
- package/src/components/ui/orb-spinner.tsx +238 -0
- package/src/components/ui/popover.tsx +59 -0
- package/src/components/ui/progress-ring.tsx +125 -0
- package/src/components/ui/progress.tsx +38 -0
- package/src/components/ui/radio-group.tsx +124 -0
- package/src/components/ui/resizable.tsx +48 -0
- package/src/components/ui/scroll-fade.tsx +98 -0
- package/src/components/ui/segmented-control.tsx +70 -0
- package/src/components/ui/select.tsx +230 -0
- package/src/components/ui/separator.tsx +29 -0
- package/src/components/ui/sheet.tsx +170 -0
- package/src/components/ui/shimmer-text.tsx +87 -0
- package/src/components/ui/shortcut.tsx +39 -0
- package/src/components/ui/skeleton.tsx +15 -0
- package/src/components/ui/slider.tsx +48 -0
- package/src/components/ui/switch.tsx +44 -0
- package/src/components/ui/table.tsx +119 -0
- package/src/components/ui/tabs.tsx +80 -0
- package/src/components/ui/text.tsx +93 -0
- package/src/components/ui/textarea.tsx +51 -0
- package/src/components/ui/toast.tsx +141 -0
- package/src/components/ui/toaster.tsx +40 -0
- package/src/components/ui/tooltip.tsx +38 -0
- package/src/components/ui/top-banner.tsx +108 -0
- package/src/components/ui/tree-view.tsx +215 -0
- package/src/components/ui/use-toast.tsx +184 -0
- package/src/components/ui/voice-orb.tsx +252 -0
- package/src/hooks/oaiz-chat.tsx +530 -0
- package/src/hooks/oaiz-data.tsx +617 -0
- package/src/hooks/oaiz-session.tsx +63 -0
- package/src/hooks/use-arrivals.ts +19 -0
- package/src/hooks/use-breakpoint.ts +100 -0
- package/src/hooks/use-media-query.ts +36 -0
- package/src/hooks/use-theme.ts +67 -0
- package/src/hooks/use-url-state.ts +77 -0
- package/src/lib/breakpoints.ts +81 -0
- package/src/lib/csp-nonce.ts +15 -0
- package/src/lib/format.ts +291 -0
- package/src/lib/speech-level.ts +118 -0
- package/src/lib/utils.ts +109 -0
- package/src/styles/oaiz/app.css +21 -0
- package/src/styles/oaiz/scrollbars.css +20 -0
- package/src/styles/oaiz/tokens.css +952 -0
- package/src/styles/oaiz/voice-orb.css +39 -0
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
import { useRef, useState, type ReactNode, type RefObject } from 'react';
|
|
2
|
+
import { Command } from 'cmdk';
|
|
3
|
+
import { Check } from 'lucide-react';
|
|
4
|
+
|
|
5
|
+
import { useIsMobile } from '../hooks/use-breakpoint';
|
|
6
|
+
import { cn } from '../lib/utils';
|
|
7
|
+
import { ErrorState } from './error-state';
|
|
8
|
+
import { Button } from './ui/button';
|
|
9
|
+
import { Popover, PopoverContent, PopoverTrigger } from './ui/popover';
|
|
10
|
+
import {
|
|
11
|
+
Sheet,
|
|
12
|
+
SheetContent,
|
|
13
|
+
SheetDescription,
|
|
14
|
+
SheetHeader,
|
|
15
|
+
SheetTitle,
|
|
16
|
+
SheetTrigger,
|
|
17
|
+
} from './ui/sheet';
|
|
18
|
+
|
|
19
|
+
export type PropertyOption = {
|
|
20
|
+
value: string;
|
|
21
|
+
label: string;
|
|
22
|
+
icon?: ReactNode;
|
|
23
|
+
/** More words that find the option, such as an email address. */
|
|
24
|
+
keywords?: string[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type PropertyPickerLabels = {
|
|
28
|
+
none: string;
|
|
29
|
+
search: (label: string) => string;
|
|
30
|
+
choose: (label: string) => string;
|
|
31
|
+
empty: string;
|
|
32
|
+
unavailable: string;
|
|
33
|
+
loading: string;
|
|
34
|
+
retry: string;
|
|
35
|
+
retrying: string;
|
|
36
|
+
clear: string;
|
|
37
|
+
loadMore: string;
|
|
38
|
+
summary: (labels: string[]) => string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type PropertyPickerProps = {
|
|
42
|
+
/** The property's name, such as "Status": the trigger's accessible name and the search placeholder. */
|
|
43
|
+
label: string;
|
|
44
|
+
value: string | null;
|
|
45
|
+
options: PropertyOption[];
|
|
46
|
+
onChange: (value: string) => void;
|
|
47
|
+
/** Shown in the trigger while no option is chosen; the label when omitted. */
|
|
48
|
+
placeholder?: ReactNode;
|
|
49
|
+
/** Shown in the trigger while no option is chosen. */
|
|
50
|
+
icon?: ReactNode;
|
|
51
|
+
/**
|
|
52
|
+
* `chip`: a bordered pill, as in a create form. `row`: a full-width line, as in a properties
|
|
53
|
+
* panel. `icon`: only the option's icon, as in a dense list row.
|
|
54
|
+
*/
|
|
55
|
+
variant?: 'chip' | 'row' | 'icon';
|
|
56
|
+
/**
|
|
57
|
+
* Reports the search text, so the caller can read matching options from OAIZ when the options
|
|
58
|
+
* are too many to load at once.
|
|
59
|
+
*/
|
|
60
|
+
onSearchChange?: (search: string) => void;
|
|
61
|
+
/** Remote results are already filtered/ranked by the caller. */
|
|
62
|
+
searchMode?: 'local' | 'remote';
|
|
63
|
+
/** Selected labels independent of the current result page. */
|
|
64
|
+
selectedOptions?: PropertyOption[];
|
|
65
|
+
loading?: boolean;
|
|
66
|
+
error?: { message: string } | null;
|
|
67
|
+
onRetry?: () => void;
|
|
68
|
+
hasMore?: boolean;
|
|
69
|
+
onLoadMore?: () => void;
|
|
70
|
+
onClear?: () => void;
|
|
71
|
+
labels?: Partial<PropertyPickerLabels>;
|
|
72
|
+
/** Control the menu, for example to open it with a hotkey. */
|
|
73
|
+
open?: boolean;
|
|
74
|
+
onOpenChange?: (open: boolean) => void;
|
|
75
|
+
disabled?: boolean;
|
|
76
|
+
className?: string;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// On a phone the chip and the icon keep their size and take a 44px touch area around them.
|
|
80
|
+
const triggerStyles = {
|
|
81
|
+
chip: 'relative inline-flex h-7 max-w-56 items-center gap-1.5 rounded-full border bg-background px-2.5 text-xs font-medium text-foreground hover:bg-muted max-mobile:after:absolute max-mobile:after:-inset-y-2 max-mobile:after:inset-x-0',
|
|
82
|
+
row: 'flex h-8 w-full items-center gap-inline rounded-md px-inline text-left text-sm text-foreground hover:bg-muted max-mobile:h-11',
|
|
83
|
+
icon: 'relative inline-flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground hover:bg-muted max-mobile:after:absolute max-mobile:after:-inset-2',
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* One property of a record, such as status, priority or owner, set from a searchable menu.
|
|
88
|
+
* Type to filter; Arrow keys move; Enter picks. Remote search and selected labels remain caller-owned.
|
|
89
|
+
*/
|
|
90
|
+
export function PropertyPicker({
|
|
91
|
+
label,
|
|
92
|
+
value,
|
|
93
|
+
options,
|
|
94
|
+
onChange,
|
|
95
|
+
onSearchChange,
|
|
96
|
+
searchMode = 'local',
|
|
97
|
+
selectedOptions = [],
|
|
98
|
+
loading = false,
|
|
99
|
+
error,
|
|
100
|
+
onRetry,
|
|
101
|
+
hasMore,
|
|
102
|
+
onLoadMore,
|
|
103
|
+
onClear,
|
|
104
|
+
labels,
|
|
105
|
+
placeholder,
|
|
106
|
+
icon,
|
|
107
|
+
variant = 'chip',
|
|
108
|
+
open: openProp,
|
|
109
|
+
onOpenChange,
|
|
110
|
+
disabled = false,
|
|
111
|
+
className,
|
|
112
|
+
}: PropertyPickerProps) {
|
|
113
|
+
const [openState, setOpenState] = useState(false);
|
|
114
|
+
const [search, setSearch] = useState('');
|
|
115
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
116
|
+
const phone = useIsMobile();
|
|
117
|
+
const open = openProp ?? openState;
|
|
118
|
+
const changeSearch = (next: string) => {
|
|
119
|
+
setSearch(next);
|
|
120
|
+
onSearchChange?.(next);
|
|
121
|
+
};
|
|
122
|
+
const setOpen = (next: boolean) => {
|
|
123
|
+
if (!next) changeSearch('');
|
|
124
|
+
setOpenState(next);
|
|
125
|
+
onOpenChange?.(next);
|
|
126
|
+
};
|
|
127
|
+
const current = [...selectedOptions, ...options].find(
|
|
128
|
+
(option) => option.value === value,
|
|
129
|
+
);
|
|
130
|
+
const shownLabel = current?.label ?? value;
|
|
131
|
+
|
|
132
|
+
function pick(option: PropertyOption) {
|
|
133
|
+
setOpen(false);
|
|
134
|
+
if (option.value !== value) onChange(option.value);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const shownIcon = current?.icon ?? icon;
|
|
138
|
+
const trigger = (
|
|
139
|
+
<Button variant="plain" size={null}
|
|
140
|
+
type="button"
|
|
141
|
+
disabled={disabled}
|
|
142
|
+
aria-label={`${label}: ${shownLabel ?? labels?.none ?? 'none'}`}
|
|
143
|
+
title={shownLabel ?? undefined}
|
|
144
|
+
className={cn(triggerStyles[variant], TRIGGER_STATES, className)}
|
|
145
|
+
>
|
|
146
|
+
{shownIcon && (
|
|
147
|
+
<span aria-hidden="true" className="flex shrink-0 items-center">
|
|
148
|
+
{shownIcon}
|
|
149
|
+
</span>
|
|
150
|
+
)}
|
|
151
|
+
{variant !== 'icon' && (
|
|
152
|
+
<span className={cn('truncate', !current && 'text-muted-foreground')}>
|
|
153
|
+
{shownLabel ?? placeholder ?? label}
|
|
154
|
+
</span>
|
|
155
|
+
)}
|
|
156
|
+
</Button>
|
|
157
|
+
);
|
|
158
|
+
const menu = (
|
|
159
|
+
<Command
|
|
160
|
+
label={label}
|
|
161
|
+
shouldFilter={searchMode !== 'remote'}
|
|
162
|
+
className="flex min-h-0 flex-col"
|
|
163
|
+
>
|
|
164
|
+
<Command.Input
|
|
165
|
+
ref={inputRef}
|
|
166
|
+
value={search}
|
|
167
|
+
onValueChange={changeSearch}
|
|
168
|
+
aria-label={labels?.search?.(label) ?? `Change ${label.toLowerCase()}…`}
|
|
169
|
+
placeholder={
|
|
170
|
+
labels?.search?.(label) ?? `Change ${label.toLowerCase()}…`
|
|
171
|
+
}
|
|
172
|
+
// 16px text on a phone keeps iOS from zooming into the field.
|
|
173
|
+
className={SEARCH_CLASS}
|
|
174
|
+
/>
|
|
175
|
+
<Command.List className={LIST_CLASS} aria-busy={loading}>
|
|
176
|
+
<Command.Empty className="px-inline py-2 text-sm text-muted-foreground">
|
|
177
|
+
<div role="option" aria-disabled="true" aria-selected="false">
|
|
178
|
+
{loading
|
|
179
|
+
? (labels?.loading ?? 'Loading…')
|
|
180
|
+
: error
|
|
181
|
+
? (labels?.unavailable ?? 'Results unavailable.')
|
|
182
|
+
: (labels?.empty ?? 'No match.')}
|
|
183
|
+
</div>
|
|
184
|
+
</Command.Empty>
|
|
185
|
+
{options.map((option) => (
|
|
186
|
+
<Command.Item
|
|
187
|
+
key={option.value}
|
|
188
|
+
value={`${option.label} ${option.value}`}
|
|
189
|
+
keywords={option.keywords}
|
|
190
|
+
onSelect={() => pick(option)}
|
|
191
|
+
aria-current={option.value === value ? 'true' : undefined}
|
|
192
|
+
className={ITEM_CLASS}
|
|
193
|
+
>
|
|
194
|
+
{option.icon && (
|
|
195
|
+
<span
|
|
196
|
+
aria-hidden="true"
|
|
197
|
+
className="flex w-4 shrink-0 justify-center text-muted-foreground"
|
|
198
|
+
>
|
|
199
|
+
{option.icon}
|
|
200
|
+
</span>
|
|
201
|
+
)}
|
|
202
|
+
<span className="min-w-0 flex-1 truncate">{option.label}</span>
|
|
203
|
+
{option.value === value && (
|
|
204
|
+
<Check aria-hidden="true" className="text-foreground" />
|
|
205
|
+
)}
|
|
206
|
+
</Command.Item>
|
|
207
|
+
))}
|
|
208
|
+
</Command.List>
|
|
209
|
+
<PickerFeedback
|
|
210
|
+
loading={loading}
|
|
211
|
+
error={error}
|
|
212
|
+
onRetry={onRetry}
|
|
213
|
+
hasMore={hasMore}
|
|
214
|
+
onLoadMore={onLoadMore}
|
|
215
|
+
onClear={value !== null ? onClear : undefined}
|
|
216
|
+
labels={labels}
|
|
217
|
+
/>
|
|
218
|
+
</Command>
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
return (
|
|
222
|
+
<PickerSurface
|
|
223
|
+
label={label}
|
|
224
|
+
open={open}
|
|
225
|
+
onOpenChange={setOpen}
|
|
226
|
+
trigger={trigger}
|
|
227
|
+
inputRef={inputRef}
|
|
228
|
+
phone={phone}
|
|
229
|
+
description={
|
|
230
|
+
labels?.choose?.(label) ?? `Choose the ${label.toLowerCase()}.`
|
|
231
|
+
}
|
|
232
|
+
>
|
|
233
|
+
{menu}
|
|
234
|
+
</PickerSurface>
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Where a picker's menu opens: a popover beside the trigger whose search takes focus, or on a
|
|
240
|
+
* phone a sheet from the bottom, within reach of the thumb, whose search waits for a tap so the
|
|
241
|
+
* keyboard does not cover the options.
|
|
242
|
+
*/
|
|
243
|
+
function PickerSurface({
|
|
244
|
+
label,
|
|
245
|
+
open,
|
|
246
|
+
onOpenChange,
|
|
247
|
+
trigger,
|
|
248
|
+
inputRef,
|
|
249
|
+
phone,
|
|
250
|
+
description,
|
|
251
|
+
children,
|
|
252
|
+
}: {
|
|
253
|
+
label: string;
|
|
254
|
+
open: boolean;
|
|
255
|
+
onOpenChange: (open: boolean) => void;
|
|
256
|
+
trigger: ReactNode;
|
|
257
|
+
inputRef: RefObject<HTMLInputElement | null>;
|
|
258
|
+
phone: boolean;
|
|
259
|
+
description: string;
|
|
260
|
+
children: ReactNode;
|
|
261
|
+
}) {
|
|
262
|
+
if (phone) {
|
|
263
|
+
return (
|
|
264
|
+
<Sheet open={open} onOpenChange={onOpenChange}>
|
|
265
|
+
<SheetTrigger asChild>{trigger}</SheetTrigger>
|
|
266
|
+
<SheetContent
|
|
267
|
+
side="bottom"
|
|
268
|
+
className="flex max-h-[80dvh] flex-col gap-0 rounded-t-lg p-0 pb-[env(safe-area-inset-bottom)]"
|
|
269
|
+
onOpenAutoFocus={(event) => {
|
|
270
|
+
event.preventDefault();
|
|
271
|
+
(event.currentTarget as HTMLElement).focus();
|
|
272
|
+
}}
|
|
273
|
+
>
|
|
274
|
+
<SheetHeader className="px-stack pt-stack pb-inline text-left">
|
|
275
|
+
<SheetTitle>{label}</SheetTitle>
|
|
276
|
+
<SheetDescription className="sr-only">
|
|
277
|
+
{description}
|
|
278
|
+
</SheetDescription>
|
|
279
|
+
</SheetHeader>
|
|
280
|
+
{children}
|
|
281
|
+
</SheetContent>
|
|
282
|
+
</Sheet>
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return (
|
|
286
|
+
<Popover open={open} onOpenChange={onOpenChange}>
|
|
287
|
+
<PopoverTrigger asChild>{trigger}</PopoverTrigger>
|
|
288
|
+
<PopoverContent
|
|
289
|
+
aria-label={label}
|
|
290
|
+
align="start"
|
|
291
|
+
className="w-60 p-0"
|
|
292
|
+
// The search takes focus, also inside a dialog that traps it.
|
|
293
|
+
onOpenAutoFocus={(event) => {
|
|
294
|
+
event.preventDefault();
|
|
295
|
+
inputRef.current?.focus();
|
|
296
|
+
}}
|
|
297
|
+
>
|
|
298
|
+
{children}
|
|
299
|
+
</PopoverContent>
|
|
300
|
+
</Popover>
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const SEARCH_CLASS =
|
|
305
|
+
'h-9 border-b bg-transparent px-inset text-base outline-none placeholder:text-muted-foreground max-mobile:h-11 mobile:text-sm';
|
|
306
|
+
const LIST_CLASS = 'max-h-64 overflow-y-auto p-1 max-mobile:max-h-none';
|
|
307
|
+
const ITEM_CLASS =
|
|
308
|
+
'flex h-8 cursor-pointer items-center gap-inline rounded-sm px-inline text-sm text-foreground data-[selected=true]:bg-accent data-[selected=true]:text-accent-foreground max-mobile:h-11 [&_svg]:size-3.5';
|
|
309
|
+
const TRIGGER_STATES =
|
|
310
|
+
'transition-colors duration-fast focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:size-3.5 [&_svg]:shrink-0';
|
|
311
|
+
|
|
312
|
+
export type MultiPropertyPickerProps = Omit<
|
|
313
|
+
PropertyPickerProps,
|
|
314
|
+
'value' | 'onChange' | 'variant'
|
|
315
|
+
> & {
|
|
316
|
+
values: string[];
|
|
317
|
+
onChange: (values: string[]) => void;
|
|
318
|
+
/** `chip`: a pill that names the chosen options. `row`: a full-width line. */
|
|
319
|
+
variant?: 'chip' | 'row';
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Several values of one property, such as labels or assignees, set from a searchable menu that
|
|
324
|
+
* stays open while options are ticked. Each change reports the whole new list.
|
|
325
|
+
*/
|
|
326
|
+
export function MultiPropertyPicker({
|
|
327
|
+
label,
|
|
328
|
+
values,
|
|
329
|
+
options,
|
|
330
|
+
onChange,
|
|
331
|
+
onSearchChange,
|
|
332
|
+
searchMode = 'local',
|
|
333
|
+
selectedOptions = [],
|
|
334
|
+
loading = false,
|
|
335
|
+
error,
|
|
336
|
+
onRetry,
|
|
337
|
+
hasMore,
|
|
338
|
+
onLoadMore,
|
|
339
|
+
onClear,
|
|
340
|
+
labels,
|
|
341
|
+
placeholder,
|
|
342
|
+
icon,
|
|
343
|
+
variant = 'chip',
|
|
344
|
+
open: openProp,
|
|
345
|
+
onOpenChange,
|
|
346
|
+
disabled = false,
|
|
347
|
+
className,
|
|
348
|
+
}: MultiPropertyPickerProps) {
|
|
349
|
+
const [openState, setOpenState] = useState(false);
|
|
350
|
+
const [search, setSearch] = useState('');
|
|
351
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
352
|
+
const phone = useIsMobile();
|
|
353
|
+
const open = openProp ?? openState;
|
|
354
|
+
const setOpen = (next: boolean) => {
|
|
355
|
+
if (!next) {
|
|
356
|
+
setSearch('');
|
|
357
|
+
onSearchChange?.('');
|
|
358
|
+
}
|
|
359
|
+
setOpenState(next);
|
|
360
|
+
onOpenChange?.(next);
|
|
361
|
+
};
|
|
362
|
+
const byValue = new Map(
|
|
363
|
+
[...options, ...selectedOptions].map((option) => [option.value, option]),
|
|
364
|
+
);
|
|
365
|
+
const chosen = values.map(
|
|
366
|
+
(value) => byValue.get(value) ?? { value, label: value },
|
|
367
|
+
);
|
|
368
|
+
const summary =
|
|
369
|
+
chosen.length === 0
|
|
370
|
+
? undefined
|
|
371
|
+
: chosen.length <= 2
|
|
372
|
+
? chosen.map((option) => option.label).join(', ')
|
|
373
|
+
: (labels?.summary?.(chosen.map((option) => option.label)) ??
|
|
374
|
+
`${chosen[0]!.label} and ${chosen.length - 1} more`);
|
|
375
|
+
|
|
376
|
+
function toggle(option: PropertyOption) {
|
|
377
|
+
onChange(
|
|
378
|
+
values.includes(option.value)
|
|
379
|
+
? values.filter((value) => value !== option.value)
|
|
380
|
+
: [...values, option.value],
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const trigger = (
|
|
385
|
+
<Button variant="plain" size={null}
|
|
386
|
+
type="button"
|
|
387
|
+
disabled={disabled}
|
|
388
|
+
aria-label={`${label}: ${summary ?? labels?.none ?? 'none'}`}
|
|
389
|
+
title={chosen.map((option) => option.label).join(', ')}
|
|
390
|
+
className={cn(triggerStyles[variant], TRIGGER_STATES, className)}
|
|
391
|
+
>
|
|
392
|
+
{icon && (
|
|
393
|
+
<span aria-hidden="true" className="flex shrink-0 items-center">
|
|
394
|
+
{icon}
|
|
395
|
+
</span>
|
|
396
|
+
)}
|
|
397
|
+
<span className={cn('truncate', !summary && 'text-muted-foreground')}>
|
|
398
|
+
{summary ?? placeholder ?? label}
|
|
399
|
+
</span>
|
|
400
|
+
</Button>
|
|
401
|
+
);
|
|
402
|
+
return (
|
|
403
|
+
<PickerSurface
|
|
404
|
+
label={label}
|
|
405
|
+
open={open}
|
|
406
|
+
onOpenChange={setOpen}
|
|
407
|
+
trigger={trigger}
|
|
408
|
+
inputRef={inputRef}
|
|
409
|
+
phone={phone}
|
|
410
|
+
description={
|
|
411
|
+
labels?.choose?.(label) ?? `Choose the ${label.toLowerCase()}.`
|
|
412
|
+
}
|
|
413
|
+
>
|
|
414
|
+
<Command
|
|
415
|
+
label={label}
|
|
416
|
+
shouldFilter={searchMode !== 'remote'}
|
|
417
|
+
className="flex min-h-0 flex-col"
|
|
418
|
+
>
|
|
419
|
+
<Command.Input
|
|
420
|
+
ref={inputRef}
|
|
421
|
+
value={search}
|
|
422
|
+
onValueChange={(next) => {
|
|
423
|
+
setSearch(next);
|
|
424
|
+
onSearchChange?.(next);
|
|
425
|
+
}}
|
|
426
|
+
aria-label={labels?.search?.(label) ?? `Find ${label.toLowerCase()}…`}
|
|
427
|
+
placeholder={
|
|
428
|
+
labels?.search?.(label) ?? `Find ${label.toLowerCase()}…`
|
|
429
|
+
}
|
|
430
|
+
className={SEARCH_CLASS}
|
|
431
|
+
/>
|
|
432
|
+
<Command.List
|
|
433
|
+
className={LIST_CLASS}
|
|
434
|
+
aria-multiselectable="true"
|
|
435
|
+
aria-busy={loading}
|
|
436
|
+
>
|
|
437
|
+
<Command.Empty className="px-inline py-2 text-sm text-muted-foreground">
|
|
438
|
+
<div role="option" aria-disabled="true" aria-selected="false">
|
|
439
|
+
{loading
|
|
440
|
+
? (labels?.loading ?? 'Loading…')
|
|
441
|
+
: error
|
|
442
|
+
? (labels?.unavailable ?? 'Results unavailable.')
|
|
443
|
+
: (labels?.empty ?? 'No match.')}
|
|
444
|
+
</div>
|
|
445
|
+
</Command.Empty>
|
|
446
|
+
{options.map((option) => {
|
|
447
|
+
const isChosen = values.includes(option.value);
|
|
448
|
+
return (
|
|
449
|
+
<Command.Item
|
|
450
|
+
key={option.value}
|
|
451
|
+
value={`${option.label} ${option.value}`}
|
|
452
|
+
keywords={option.keywords}
|
|
453
|
+
onSelect={() => toggle(option)}
|
|
454
|
+
aria-checked={isChosen}
|
|
455
|
+
className={ITEM_CLASS}
|
|
456
|
+
>
|
|
457
|
+
<span
|
|
458
|
+
aria-hidden="true"
|
|
459
|
+
className={cn(
|
|
460
|
+
'flex size-4 shrink-0 items-center justify-center rounded-sm border border-primary',
|
|
461
|
+
isChosen && 'bg-primary text-primary-foreground',
|
|
462
|
+
)}
|
|
463
|
+
>
|
|
464
|
+
{isChosen && <Check className="size-3" />}
|
|
465
|
+
</span>
|
|
466
|
+
{option.icon && (
|
|
467
|
+
<span
|
|
468
|
+
aria-hidden="true"
|
|
469
|
+
className="flex w-4 shrink-0 justify-center text-muted-foreground"
|
|
470
|
+
>
|
|
471
|
+
{option.icon}
|
|
472
|
+
</span>
|
|
473
|
+
)}
|
|
474
|
+
<span className="min-w-0 flex-1 truncate">{option.label}</span>
|
|
475
|
+
</Command.Item>
|
|
476
|
+
);
|
|
477
|
+
})}
|
|
478
|
+
</Command.List>
|
|
479
|
+
<PickerFeedback
|
|
480
|
+
loading={loading}
|
|
481
|
+
error={error}
|
|
482
|
+
onRetry={onRetry}
|
|
483
|
+
hasMore={hasMore}
|
|
484
|
+
onLoadMore={onLoadMore}
|
|
485
|
+
onClear={
|
|
486
|
+
values.length ? (onClear ?? (() => onChange([]))) : undefined
|
|
487
|
+
}
|
|
488
|
+
labels={labels}
|
|
489
|
+
/>
|
|
490
|
+
</Command>
|
|
491
|
+
</PickerSurface>
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function PickerFeedback({
|
|
496
|
+
loading,
|
|
497
|
+
error,
|
|
498
|
+
onRetry,
|
|
499
|
+
hasMore,
|
|
500
|
+
onLoadMore,
|
|
501
|
+
onClear,
|
|
502
|
+
labels,
|
|
503
|
+
}: Pick<
|
|
504
|
+
PropertyPickerProps,
|
|
505
|
+
| 'loading'
|
|
506
|
+
| 'error'
|
|
507
|
+
| 'onRetry'
|
|
508
|
+
| 'hasMore'
|
|
509
|
+
| 'onLoadMore'
|
|
510
|
+
| 'onClear'
|
|
511
|
+
| 'labels'
|
|
512
|
+
>) {
|
|
513
|
+
return (
|
|
514
|
+
<div className="flex flex-col gap-inline px-inline pb-inline">
|
|
515
|
+
{loading && (
|
|
516
|
+
<p role="status" className="text-sm text-muted-foreground">
|
|
517
|
+
{labels?.loading ?? 'Loading…'}
|
|
518
|
+
</p>
|
|
519
|
+
)}
|
|
520
|
+
{error && (
|
|
521
|
+
<ErrorState
|
|
522
|
+
error={error}
|
|
523
|
+
onRetry={onRetry}
|
|
524
|
+
retrying={loading}
|
|
525
|
+
labels={labels}
|
|
526
|
+
/>
|
|
527
|
+
)}
|
|
528
|
+
{hasMore && onLoadMore && (
|
|
529
|
+
<Button
|
|
530
|
+
type="button"
|
|
531
|
+
variant="ghost"
|
|
532
|
+
size="sm"
|
|
533
|
+
disabled={loading}
|
|
534
|
+
onClick={onLoadMore}
|
|
535
|
+
>
|
|
536
|
+
{labels?.loadMore ?? 'Load more'}
|
|
537
|
+
</Button>
|
|
538
|
+
)}
|
|
539
|
+
{onClear && (
|
|
540
|
+
<Button type="button" variant="ghost" size="sm" onClick={onClear}>
|
|
541
|
+
{labels?.clear ?? 'Clear selection'}
|
|
542
|
+
</Button>
|
|
543
|
+
)}
|
|
544
|
+
</div>
|
|
545
|
+
);
|
|
546
|
+
}
|