@oaiz/app-kit 0.0.0-stage → 0.1.4128
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 +143 -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,415 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import { Command } from 'cmdk';
|
|
3
|
+
import { Check, ChevronRight, ListFilter, Search, X } from 'lucide-react';
|
|
4
|
+
|
|
5
|
+
import { useIsMobile } from '../hooks/use-breakpoint';
|
|
6
|
+
import { useHotkey } from './command-palette';
|
|
7
|
+
import { Button } from './ui/button';
|
|
8
|
+
import { Input } from './ui/input';
|
|
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
|
+
import { Text } from './ui/text';
|
|
19
|
+
|
|
20
|
+
export type FilterOption = { value: string; label: ReactNode };
|
|
21
|
+
|
|
22
|
+
export type FilterField = {
|
|
23
|
+
key: string;
|
|
24
|
+
label: string;
|
|
25
|
+
options: FilterOption[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type FilterValues = {
|
|
29
|
+
search: string;
|
|
30
|
+
/** The chosen option of each field; a missing key means any value. */
|
|
31
|
+
filters: Record<string, string | undefined>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const emptyFilters: FilterValues = { search: '', filters: {} };
|
|
35
|
+
|
|
36
|
+
export type FilterBarProps = {
|
|
37
|
+
fields: FilterField[];
|
|
38
|
+
value: FilterValues;
|
|
39
|
+
onChange: (value: FilterValues) => void;
|
|
40
|
+
/** Without it the bar has no search box. */
|
|
41
|
+
searchPlaceholder?: string;
|
|
42
|
+
/** For example "24 orders". */
|
|
43
|
+
summary?: ReactNode;
|
|
44
|
+
/** Buttons at the end of the bar. */
|
|
45
|
+
actions?: ReactNode;
|
|
46
|
+
/** Additional filter controls, such as date ranges or multiselects. */
|
|
47
|
+
children?: ReactNode;
|
|
48
|
+
/** Less-used filter controls shown in a named popover. */
|
|
49
|
+
moreFilters?: ReactNode;
|
|
50
|
+
moreFiltersCount?: number;
|
|
51
|
+
/** F opens the menu only while focus is inside this filter bar. */
|
|
52
|
+
shortcuts?: boolean;
|
|
53
|
+
labels?: Partial<{
|
|
54
|
+
filter: string;
|
|
55
|
+
search: string;
|
|
56
|
+
filterBy: (field: string) => string;
|
|
57
|
+
noMatch: string;
|
|
58
|
+
is: string;
|
|
59
|
+
remove: (field: string) => string;
|
|
60
|
+
description: string;
|
|
61
|
+
clear: string;
|
|
62
|
+
moreFilters: string;
|
|
63
|
+
}>;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const ITEM =
|
|
67
|
+
'flex h-8 max-mobile:h-11 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';
|
|
68
|
+
const SEARCH_DELAY_MS = 250;
|
|
69
|
+
|
|
70
|
+
/** One condition of `toRowFilters`; `useRows` and `usePagedRows` accept it as a `RowFilter`. */
|
|
71
|
+
export type FilterCondition =
|
|
72
|
+
| { column: string; op: 'eq'; value: string }
|
|
73
|
+
| { column: string; op: 'ilike'; value: string }
|
|
74
|
+
| { op: 'or'; value: string };
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The conditions a query sends for `value`: `eq` per chosen field and, when there is search text,
|
|
78
|
+
* `ilike` on `searchColumn`, or on any of several. `columns` maps a field key to its column when they differ.
|
|
79
|
+
*/
|
|
80
|
+
export function toRowFilters(
|
|
81
|
+
value: FilterValues,
|
|
82
|
+
options: {
|
|
83
|
+
searchColumn?: string | string[];
|
|
84
|
+
columns?: Record<string, string>;
|
|
85
|
+
} = {},
|
|
86
|
+
): FilterCondition[] {
|
|
87
|
+
const conditions: FilterCondition[] = [];
|
|
88
|
+
for (const [key, chosen] of Object.entries(value.filters)) {
|
|
89
|
+
if (chosen !== undefined) {
|
|
90
|
+
conditions.push({
|
|
91
|
+
column: options.columns?.[key] ?? key,
|
|
92
|
+
op: 'eq',
|
|
93
|
+
value: chosen,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const search = value.search.trim();
|
|
98
|
+
const searched = [options.searchColumn ?? []].flat();
|
|
99
|
+
if (search !== '' && searched.length > 0) {
|
|
100
|
+
const pattern = `%${search.replace(/[\\%_]/g, (match) => `\\${match}`)}%`;
|
|
101
|
+
if (searched.length === 1) {
|
|
102
|
+
conditions.push({ column: searched[0]!, op: 'ilike', value: pattern });
|
|
103
|
+
} else {
|
|
104
|
+
const quoted = `"${pattern.replace(/[\\"]/g, (match) => `\\${match}`)}"`;
|
|
105
|
+
conditions.push({
|
|
106
|
+
op: 'or',
|
|
107
|
+
value: searched.map((column) => `${column}.ilike.${quoted}`).join(','),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return conditions;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A search box, one Filter menu and a chip for each active filter. The
|
|
116
|
+
* menu lists the fields, then the chosen field's values; F opens it. A chip's x removes that
|
|
117
|
+
* filter. Search text reaches `onChange` after a short pause in typing, so each keystroke does
|
|
118
|
+
* not start a query.
|
|
119
|
+
*/
|
|
120
|
+
export function FilterBar({
|
|
121
|
+
fields,
|
|
122
|
+
value,
|
|
123
|
+
onChange,
|
|
124
|
+
searchPlaceholder,
|
|
125
|
+
summary,
|
|
126
|
+
actions,
|
|
127
|
+
children,
|
|
128
|
+
moreFilters,
|
|
129
|
+
moreFiltersCount = 0,
|
|
130
|
+
shortcuts = true,
|
|
131
|
+
labels,
|
|
132
|
+
}: FilterBarProps) {
|
|
133
|
+
const [search, setSearch] = useState(value.search);
|
|
134
|
+
const [open, setOpen] = useState(false);
|
|
135
|
+
const [field, setField] = useState<FilterField | null>(null);
|
|
136
|
+
const [query, setQuery] = useState('');
|
|
137
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
138
|
+
const scopeRef = useRef<HTMLDivElement>(null);
|
|
139
|
+
const phone = useIsMobile();
|
|
140
|
+
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
setSearch(value.search);
|
|
143
|
+
}, [value.search]);
|
|
144
|
+
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
if (search === value.search) return;
|
|
147
|
+
const timer = setTimeout(
|
|
148
|
+
() => onChange({ ...value, search }),
|
|
149
|
+
SEARCH_DELAY_MS,
|
|
150
|
+
);
|
|
151
|
+
return () => clearTimeout(timer);
|
|
152
|
+
}, [search, value, onChange]);
|
|
153
|
+
|
|
154
|
+
useHotkey(
|
|
155
|
+
'f',
|
|
156
|
+
() => {
|
|
157
|
+
if (fields.length > 0) openMenu(null);
|
|
158
|
+
},
|
|
159
|
+
{ scope: scopeRef, enabled: shortcuts },
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
function openMenu(start: FilterField | null) {
|
|
163
|
+
setField(start);
|
|
164
|
+
setQuery('');
|
|
165
|
+
setOpen(true);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function choose(key: string, chosen: string | undefined) {
|
|
169
|
+
onChange({ ...value, filters: { ...value.filters, [key]: chosen } });
|
|
170
|
+
setOpen(false);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function changeOpen(next: boolean) {
|
|
174
|
+
setOpen(next);
|
|
175
|
+
if (!next) setField(null);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const trigger = (
|
|
179
|
+
<Button
|
|
180
|
+
variant="ghost"
|
|
181
|
+
size="xs"
|
|
182
|
+
className="text-muted-foreground"
|
|
183
|
+
onClick={() => openMenu(null)}
|
|
184
|
+
>
|
|
185
|
+
<ListFilter />
|
|
186
|
+
{labels?.filter ?? 'Filter'}
|
|
187
|
+
</Button>
|
|
188
|
+
);
|
|
189
|
+
const menu = (
|
|
190
|
+
<Command
|
|
191
|
+
label={
|
|
192
|
+
field
|
|
193
|
+
? (labels?.filterBy?.(field.label) ?? `${field.label} filter`)
|
|
194
|
+
: (labels?.filter ?? 'Filter')
|
|
195
|
+
}
|
|
196
|
+
className="flex flex-col"
|
|
197
|
+
onKeyDown={(event) => {
|
|
198
|
+
if (event.key === 'Backspace' && query === '' && field) {
|
|
199
|
+
event.preventDefault();
|
|
200
|
+
setField(null);
|
|
201
|
+
}
|
|
202
|
+
}}
|
|
203
|
+
>
|
|
204
|
+
<div className="flex items-center gap-1.5 border-b px-inset">
|
|
205
|
+
{field && (
|
|
206
|
+
<span className="shrink-0 rounded-sm bg-muted px-1.5 py-0.5 text-xs text-muted-foreground">
|
|
207
|
+
{field.label}
|
|
208
|
+
</span>
|
|
209
|
+
)}
|
|
210
|
+
<Command.Input
|
|
211
|
+
ref={inputRef}
|
|
212
|
+
value={query}
|
|
213
|
+
onValueChange={setQuery}
|
|
214
|
+
aria-label={
|
|
215
|
+
field
|
|
216
|
+
? (labels?.filterBy?.(field.label) ??
|
|
217
|
+
`Filter by ${field.label.toLowerCase()}…`)
|
|
218
|
+
: (labels?.search ?? 'Filter…')
|
|
219
|
+
}
|
|
220
|
+
placeholder={
|
|
221
|
+
field
|
|
222
|
+
? (labels?.filterBy?.(field.label) ??
|
|
223
|
+
`Filter by ${field.label.toLowerCase()}…`)
|
|
224
|
+
: (labels?.search ?? 'Filter…')
|
|
225
|
+
}
|
|
226
|
+
className="h-9 w-full bg-transparent text-base outline-none placeholder:text-muted-foreground max-mobile:h-11 mobile:text-sm"
|
|
227
|
+
/>
|
|
228
|
+
</div>
|
|
229
|
+
<Command.List className="max-h-64 overflow-y-auto p-1 max-mobile:max-h-none">
|
|
230
|
+
<Command.Empty className="px-inline py-2 text-sm text-muted-foreground">
|
|
231
|
+
<div role="option" aria-disabled="true" aria-selected="false">
|
|
232
|
+
{labels?.noMatch ?? 'No match.'}
|
|
233
|
+
</div>
|
|
234
|
+
</Command.Empty>
|
|
235
|
+
{field
|
|
236
|
+
? field.options.map((option) => (
|
|
237
|
+
<Command.Item
|
|
238
|
+
key={option.value}
|
|
239
|
+
value={`${option.value} ${typeof option.label === 'string' ? option.label : ''}`}
|
|
240
|
+
onSelect={() => choose(field.key, option.value)}
|
|
241
|
+
className={ITEM}
|
|
242
|
+
>
|
|
243
|
+
<span className="min-w-0 flex-1 truncate">{option.label}</span>
|
|
244
|
+
{value.filters[field.key] === option.value && (
|
|
245
|
+
<Check className="size-3.5" />
|
|
246
|
+
)}
|
|
247
|
+
</Command.Item>
|
|
248
|
+
))
|
|
249
|
+
: fields.map((each) => (
|
|
250
|
+
<Command.Item
|
|
251
|
+
key={each.key}
|
|
252
|
+
value={each.label}
|
|
253
|
+
onSelect={() => {
|
|
254
|
+
setField(each);
|
|
255
|
+
if (!phone) inputRef.current?.focus();
|
|
256
|
+
setQuery('');
|
|
257
|
+
}}
|
|
258
|
+
className={ITEM}
|
|
259
|
+
>
|
|
260
|
+
<span className="min-w-0 flex-1 truncate">{each.label}</span>
|
|
261
|
+
<ChevronRight className="size-3.5 text-muted-foreground" />
|
|
262
|
+
</Command.Item>
|
|
263
|
+
))}
|
|
264
|
+
</Command.List>
|
|
265
|
+
</Command>
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
const active = fields.filter((each) => value.filters[each.key] !== undefined);
|
|
269
|
+
const anyActive = value.search !== '' || active.length > 0;
|
|
270
|
+
|
|
271
|
+
return (
|
|
272
|
+
<div
|
|
273
|
+
ref={scopeRef}
|
|
274
|
+
role="search"
|
|
275
|
+
className="flex flex-wrap items-center gap-inline"
|
|
276
|
+
>
|
|
277
|
+
{searchPlaceholder !== undefined && (
|
|
278
|
+
<div className="relative w-full mobile:w-56">
|
|
279
|
+
<Search
|
|
280
|
+
aria-hidden="true"
|
|
281
|
+
className="pointer-events-none absolute left-3 top-1/2 size-3.5 -translate-y-1/2 text-muted-foreground"
|
|
282
|
+
/>
|
|
283
|
+
<Input
|
|
284
|
+
type="search"
|
|
285
|
+
aria-label={searchPlaceholder}
|
|
286
|
+
placeholder={searchPlaceholder}
|
|
287
|
+
value={search}
|
|
288
|
+
onChange={(event) => setSearch(event.target.value)}
|
|
289
|
+
// 16px text on a phone keeps iOS from zooming into the field.
|
|
290
|
+
className="h-8 pl-8 text-base mobile:text-sm"
|
|
291
|
+
/>
|
|
292
|
+
</div>
|
|
293
|
+
)}
|
|
294
|
+
{active.map((each) => {
|
|
295
|
+
const chosen = each.options.find(
|
|
296
|
+
(option) => option.value === value.filters[each.key],
|
|
297
|
+
);
|
|
298
|
+
return (
|
|
299
|
+
<span
|
|
300
|
+
key={each.key}
|
|
301
|
+
data-testid="filter-chip"
|
|
302
|
+
className="inline-flex h-7 items-center overflow-hidden rounded-md border bg-background text-xs"
|
|
303
|
+
>
|
|
304
|
+
<span className="px-2 text-muted-foreground">{each.label}</span>
|
|
305
|
+
<span className="border-l px-1.5 text-muted-foreground">
|
|
306
|
+
{labels?.is ?? 'is'}
|
|
307
|
+
</span>
|
|
308
|
+
<Button
|
|
309
|
+
variant="plain"
|
|
310
|
+
size="xs"
|
|
311
|
+
type="button"
|
|
312
|
+
className="h-full border-l px-2 font-medium text-foreground hover:bg-muted"
|
|
313
|
+
onClick={() => openMenu(each)}
|
|
314
|
+
>
|
|
315
|
+
{chosen?.label ?? value.filters[each.key]}
|
|
316
|
+
</Button>
|
|
317
|
+
<Button
|
|
318
|
+
variant="plain"
|
|
319
|
+
size="xs"
|
|
320
|
+
type="button"
|
|
321
|
+
aria-label={
|
|
322
|
+
labels?.remove?.(each.label) ?? `Remove ${each.label} filter`
|
|
323
|
+
}
|
|
324
|
+
className="flex h-full items-center border-l px-1.5 text-muted-foreground hover:bg-muted hover:text-foreground"
|
|
325
|
+
onClick={() => choose(each.key, undefined)}
|
|
326
|
+
>
|
|
327
|
+
<X className="size-3" />
|
|
328
|
+
</Button>
|
|
329
|
+
</span>
|
|
330
|
+
);
|
|
331
|
+
})}
|
|
332
|
+
{fields.length > 0 &&
|
|
333
|
+
(phone ? (
|
|
334
|
+
// On a phone the menu is a sheet from the bottom, within reach of the thumb. Its search
|
|
335
|
+
// waits for a tap, so the keyboard does not cover the options.
|
|
336
|
+
<Sheet open={open} onOpenChange={changeOpen}>
|
|
337
|
+
<SheetTrigger asChild>{trigger}</SheetTrigger>
|
|
338
|
+
<SheetContent
|
|
339
|
+
side="bottom"
|
|
340
|
+
className="flex max-h-[80dvh] flex-col gap-0 rounded-t-lg p-0 pb-[env(safe-area-inset-bottom)]"
|
|
341
|
+
onOpenAutoFocus={(event) => {
|
|
342
|
+
event.preventDefault();
|
|
343
|
+
(event.currentTarget as HTMLElement).focus();
|
|
344
|
+
}}
|
|
345
|
+
>
|
|
346
|
+
<SheetHeader className="px-stack pt-stack pb-inline text-left">
|
|
347
|
+
<SheetTitle>
|
|
348
|
+
{field ? field.label : (labels?.filter ?? 'Filter')}
|
|
349
|
+
</SheetTitle>
|
|
350
|
+
<SheetDescription className="sr-only">
|
|
351
|
+
{labels?.description ?? 'Choose what the list shows.'}
|
|
352
|
+
</SheetDescription>
|
|
353
|
+
</SheetHeader>
|
|
354
|
+
{menu}
|
|
355
|
+
</SheetContent>
|
|
356
|
+
</Sheet>
|
|
357
|
+
) : (
|
|
358
|
+
<Popover open={open} onOpenChange={changeOpen}>
|
|
359
|
+
<PopoverTrigger asChild>{trigger}</PopoverTrigger>
|
|
360
|
+
<PopoverContent
|
|
361
|
+
aria-label={field?.label ?? labels?.filter ?? 'Filter'}
|
|
362
|
+
align="start"
|
|
363
|
+
className="w-60 p-0"
|
|
364
|
+
// The search takes focus, also inside a dialog that traps it.
|
|
365
|
+
onOpenAutoFocus={(event) => {
|
|
366
|
+
event.preventDefault();
|
|
367
|
+
inputRef.current?.focus();
|
|
368
|
+
}}
|
|
369
|
+
>
|
|
370
|
+
{menu}
|
|
371
|
+
</PopoverContent>
|
|
372
|
+
</Popover>
|
|
373
|
+
))}
|
|
374
|
+
{children}
|
|
375
|
+
{moreFilters !== undefined && (
|
|
376
|
+
<Popover>
|
|
377
|
+
<PopoverTrigger asChild>
|
|
378
|
+
<Button type="button" variant="outline" size="sm">
|
|
379
|
+
{labels?.moreFilters ?? 'More filters'}
|
|
380
|
+
{moreFiltersCount > 0 ? ` (${moreFiltersCount})` : ''}
|
|
381
|
+
</Button>
|
|
382
|
+
</PopoverTrigger>
|
|
383
|
+
<PopoverContent
|
|
384
|
+
aria-label={labels?.moreFilters ?? 'More filters'}
|
|
385
|
+
align="start"
|
|
386
|
+
className="flex max-w-[calc(100vw-2rem)] flex-col gap-stack"
|
|
387
|
+
>
|
|
388
|
+
{moreFilters}
|
|
389
|
+
</PopoverContent>
|
|
390
|
+
</Popover>
|
|
391
|
+
)}
|
|
392
|
+
{anyActive && (
|
|
393
|
+
<Button
|
|
394
|
+
variant="ghost"
|
|
395
|
+
size="xs"
|
|
396
|
+
className="text-muted-foreground"
|
|
397
|
+
onClick={() => {
|
|
398
|
+
setSearch('');
|
|
399
|
+
onChange(emptyFilters);
|
|
400
|
+
}}
|
|
401
|
+
>
|
|
402
|
+
{labels?.clear ?? 'Clear'}
|
|
403
|
+
</Button>
|
|
404
|
+
)}
|
|
405
|
+
<div className="ml-auto flex min-w-0 flex-wrap items-center gap-inline">
|
|
406
|
+
{summary !== undefined && (
|
|
407
|
+
<Text size="sm" variant="muted" aria-live="polite">
|
|
408
|
+
{summary}
|
|
409
|
+
</Text>
|
|
410
|
+
)}
|
|
411
|
+
{actions}
|
|
412
|
+
</div>
|
|
413
|
+
</div>
|
|
414
|
+
);
|
|
415
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useLayoutEffect,
|
|
3
|
+
useRef,
|
|
4
|
+
useState,
|
|
5
|
+
type KeyboardEvent,
|
|
6
|
+
type ReactNode,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import { ChevronRight } from 'lucide-react';
|
|
9
|
+
|
|
10
|
+
import { cn } from '../lib/utils';
|
|
11
|
+
import type { PropertyOption } from './property-picker';
|
|
12
|
+
import { SplitView } from './split-view';
|
|
13
|
+
import { Button } from './ui/button';
|
|
14
|
+
import { Checkbox } from './ui/checkbox';
|
|
15
|
+
import { Heading } from './ui/heading';
|
|
16
|
+
import { Input } from './ui/input';
|
|
17
|
+
import { Label } from './ui/label';
|
|
18
|
+
import { Text } from './ui/text';
|
|
19
|
+
|
|
20
|
+
export type PickerGroup = {
|
|
21
|
+
key: string;
|
|
22
|
+
label: string;
|
|
23
|
+
icon?: ReactNode;
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
options: (PropertyOption & { disabled?: boolean })[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type GroupedPickerProps = {
|
|
29
|
+
groups: readonly PickerGroup[];
|
|
30
|
+
/** Option values are unique across groups and remain selected when their group is closed. */
|
|
31
|
+
selected: ReadonlySet<string>;
|
|
32
|
+
onSelectedChange: (selected: Set<string>) => void;
|
|
33
|
+
label: string;
|
|
34
|
+
labels?: {
|
|
35
|
+
groups?: string;
|
|
36
|
+
back?: string;
|
|
37
|
+
search?: string;
|
|
38
|
+
empty?: string;
|
|
39
|
+
chooseGroup?: string;
|
|
40
|
+
selectedCount?: (selected: number, total: number) => string;
|
|
41
|
+
};
|
|
42
|
+
disabled?: boolean;
|
|
43
|
+
className?: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Category navigation and independent choices. Data, selection and removal remain caller-owned. */
|
|
47
|
+
export function GroupedPicker({
|
|
48
|
+
groups,
|
|
49
|
+
selected,
|
|
50
|
+
onSelectedChange,
|
|
51
|
+
label,
|
|
52
|
+
labels,
|
|
53
|
+
disabled = false,
|
|
54
|
+
className,
|
|
55
|
+
}: GroupedPickerProps) {
|
|
56
|
+
const [activeKey, setActiveKey] = useState<string | null>(null);
|
|
57
|
+
const [search, setSearch] = useState('');
|
|
58
|
+
const root = useRef<HTMLDivElement>(null);
|
|
59
|
+
const focused = useRef<{ key: string; index: number; kind: string } | null>(
|
|
60
|
+
null,
|
|
61
|
+
);
|
|
62
|
+
const active = groups.find(
|
|
63
|
+
(group) => group.key === activeKey && !group.disabled,
|
|
64
|
+
);
|
|
65
|
+
const query = search.trim().toLocaleLowerCase();
|
|
66
|
+
const options =
|
|
67
|
+
active?.options.filter((option) =>
|
|
68
|
+
[option.label, ...(option.keywords ?? [])].some((value) =>
|
|
69
|
+
value.toLocaleLowerCase().includes(query),
|
|
70
|
+
),
|
|
71
|
+
) ?? [];
|
|
72
|
+
|
|
73
|
+
// Removing a focused choice returns focus to a surviving neighbour in the same list.
|
|
74
|
+
useLayoutEffect(() => {
|
|
75
|
+
const previous = focused.current;
|
|
76
|
+
if (!previous || !root.current) return;
|
|
77
|
+
const current = Array.from(
|
|
78
|
+
root.current.querySelectorAll<HTMLElement>('[data-picker-key]'),
|
|
79
|
+
).find((element) => element.dataset.pickerKey === previous.key);
|
|
80
|
+
if (current) return;
|
|
81
|
+
if (document.activeElement !== document.body) return;
|
|
82
|
+
const candidates = Array.from(
|
|
83
|
+
root.current.querySelectorAll<HTMLElement>(
|
|
84
|
+
`[data-picker-kind="${previous.kind}"]:not(:disabled)`,
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
const target =
|
|
88
|
+
candidates[Math.min(previous.index, candidates.length - 1)] ??
|
|
89
|
+
(active
|
|
90
|
+
? root.current.querySelector<HTMLElement>('input[type="search"]')
|
|
91
|
+
: root.current.querySelector<HTMLElement>(
|
|
92
|
+
'[data-picker-kind="group"]:not(:disabled)',
|
|
93
|
+
));
|
|
94
|
+
(target ?? root.current).focus();
|
|
95
|
+
}, [groups, activeKey, search]);
|
|
96
|
+
|
|
97
|
+
function navigate(event: KeyboardEvent<HTMLElement>, kind: string) {
|
|
98
|
+
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return;
|
|
99
|
+
const elements = Array.from(
|
|
100
|
+
root.current?.querySelectorAll<HTMLElement>(
|
|
101
|
+
`[data-picker-kind="${kind}"]:not(:disabled)`,
|
|
102
|
+
) ?? [],
|
|
103
|
+
);
|
|
104
|
+
const index = elements.indexOf(event.currentTarget);
|
|
105
|
+
const next =
|
|
106
|
+
event.key === 'Home'
|
|
107
|
+
? 0
|
|
108
|
+
: event.key === 'End'
|
|
109
|
+
? elements.length - 1
|
|
110
|
+
: Math.max(
|
|
111
|
+
0,
|
|
112
|
+
Math.min(
|
|
113
|
+
elements.length - 1,
|
|
114
|
+
index + (event.key === 'ArrowDown' ? 1 : -1),
|
|
115
|
+
),
|
|
116
|
+
);
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
elements[next]?.focus();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<div
|
|
123
|
+
ref={root}
|
|
124
|
+
role="group"
|
|
125
|
+
aria-label={label}
|
|
126
|
+
tabIndex={-1}
|
|
127
|
+
className={cn('h-96 min-w-0 rounded-md border', className)}
|
|
128
|
+
onFocusCapture={(event) => {
|
|
129
|
+
const target = event.target as HTMLElement;
|
|
130
|
+
const key = target.dataset.pickerKey;
|
|
131
|
+
const kind = target.dataset.pickerKind;
|
|
132
|
+
if (!key || !kind) {
|
|
133
|
+
focused.current = null;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const peers = Array.from(
|
|
137
|
+
root.current?.querySelectorAll(
|
|
138
|
+
`[data-picker-kind="${kind}"]:not(:disabled)`,
|
|
139
|
+
) ?? [],
|
|
140
|
+
);
|
|
141
|
+
focused.current = { key, kind, index: peers.indexOf(target) };
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
<SplitView
|
|
145
|
+
listLabel={labels?.groups ?? label}
|
|
146
|
+
detailLabel={active?.label}
|
|
147
|
+
detailKey={active?.key}
|
|
148
|
+
onBack={() => {
|
|
149
|
+
setActiveKey(null);
|
|
150
|
+
setSearch('');
|
|
151
|
+
}}
|
|
152
|
+
backLabel={labels?.back ?? 'Back to categories'}
|
|
153
|
+
placeholder={
|
|
154
|
+
<Text size="sm" variant="muted" className="p-inset">
|
|
155
|
+
{labels?.chooseGroup ?? 'Choose a category.'}
|
|
156
|
+
</Text>
|
|
157
|
+
}
|
|
158
|
+
list={
|
|
159
|
+
<ul className="flex flex-col gap-1 p-inline">
|
|
160
|
+
{groups.map((group) => (
|
|
161
|
+
<li key={group.key}>
|
|
162
|
+
<Button
|
|
163
|
+
type="button"
|
|
164
|
+
variant={active?.key === group.key ? 'secondary' : 'ghost'}
|
|
165
|
+
disabled={disabled || group.disabled}
|
|
166
|
+
className="h-auto min-h-11 w-full justify-start whitespace-normal text-left"
|
|
167
|
+
aria-pressed={active?.key === group.key}
|
|
168
|
+
data-picker-kind="group"
|
|
169
|
+
data-picker-key={`group:${group.key}`}
|
|
170
|
+
onClick={() => {
|
|
171
|
+
setActiveKey(group.key);
|
|
172
|
+
setSearch('');
|
|
173
|
+
}}
|
|
174
|
+
onKeyDown={(event) => navigate(event, 'group')}
|
|
175
|
+
>
|
|
176
|
+
{group.icon}
|
|
177
|
+
<span className="min-w-0 flex-1 break-words">
|
|
178
|
+
{group.label}
|
|
179
|
+
<Text as="span" size="xs" variant="muted" className="block">
|
|
180
|
+
{labels?.selectedCount?.(
|
|
181
|
+
group.options.filter((option) =>
|
|
182
|
+
selected.has(option.value),
|
|
183
|
+
).length,
|
|
184
|
+
group.options.length,
|
|
185
|
+
) ??
|
|
186
|
+
`${group.options.filter((option) => selected.has(option.value)).length} / ${group.options.length}`}
|
|
187
|
+
</Text>
|
|
188
|
+
</span>
|
|
189
|
+
<ChevronRight aria-hidden="true" />
|
|
190
|
+
</Button>
|
|
191
|
+
</li>
|
|
192
|
+
))}
|
|
193
|
+
</ul>
|
|
194
|
+
}
|
|
195
|
+
detail={
|
|
196
|
+
active && (
|
|
197
|
+
<div className="flex min-w-0 flex-col gap-stack p-inset">
|
|
198
|
+
<Heading level={3} size="sm" className="break-words">
|
|
199
|
+
{active.label}
|
|
200
|
+
</Heading>
|
|
201
|
+
<Input
|
|
202
|
+
type="search"
|
|
203
|
+
aria-label={labels?.search ?? 'Search choices'}
|
|
204
|
+
placeholder={labels?.search ?? 'Search choices'}
|
|
205
|
+
value={search}
|
|
206
|
+
disabled={disabled}
|
|
207
|
+
onChange={(event) => setSearch(event.target.value)}
|
|
208
|
+
/>
|
|
209
|
+
{options.length === 0 ? (
|
|
210
|
+
<Text size="sm" variant="muted">
|
|
211
|
+
{labels?.empty ?? 'No choices.'}
|
|
212
|
+
</Text>
|
|
213
|
+
) : (
|
|
214
|
+
<ul className="flex flex-col gap-inline">
|
|
215
|
+
{options.map((option) => (
|
|
216
|
+
<li key={option.value}>
|
|
217
|
+
<Label className="flex min-h-11 items-center gap-inline">
|
|
218
|
+
<Checkbox
|
|
219
|
+
checked={selected.has(option.value)}
|
|
220
|
+
disabled={disabled || option.disabled}
|
|
221
|
+
data-picker-kind="option"
|
|
222
|
+
data-picker-key={`option:${option.value}`}
|
|
223
|
+
onKeyDown={(event) => navigate(event, 'option')}
|
|
224
|
+
onCheckedChange={(checked) => {
|
|
225
|
+
const next = new Set(selected);
|
|
226
|
+
if (checked === true) next.add(option.value);
|
|
227
|
+
else next.delete(option.value);
|
|
228
|
+
onSelectedChange(next);
|
|
229
|
+
}}
|
|
230
|
+
/>
|
|
231
|
+
{option.icon}
|
|
232
|
+
<Text
|
|
233
|
+
as="span"
|
|
234
|
+
size="sm"
|
|
235
|
+
className="min-w-0 break-words"
|
|
236
|
+
>
|
|
237
|
+
{option.label}
|
|
238
|
+
</Text>
|
|
239
|
+
</Label>
|
|
240
|
+
</li>
|
|
241
|
+
))}
|
|
242
|
+
</ul>
|
|
243
|
+
)}
|
|
244
|
+
</div>
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
/>
|
|
248
|
+
</div>
|
|
249
|
+
);
|
|
250
|
+
}
|