@rise-x/apps-sdk 0.8.0 → 0.11.0

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.
Files changed (93) hide show
  1. package/README.md +209 -21
  2. package/build/rsbuild/index.d.ts +36 -0
  3. package/build/rsbuild/index.js +225 -0
  4. package/build/ui/classes.json +209 -0
  5. package/build/ui/components/ai.d.ts +33 -1
  6. package/build/ui/components/app-frame.d.ts +19 -0
  7. package/build/ui/components/chat-input.d.ts +28 -0
  8. package/build/ui/components/message-scroller.d.ts +34 -10
  9. package/build/ui/components/nav.d.ts +2 -1
  10. package/build/ui/components/sheet.d.ts +10 -0
  11. package/build/ui/components/thinking-rings.d.ts +4 -0
  12. package/build/ui/demo.html +26794 -0
  13. package/build/ui/index.d.ts +3 -0
  14. package/build/ui/standalone.js +59 -58
  15. package/build/ui/styles.css +1 -1
  16. package/package.json +24 -3
  17. package/rsbuild/package.json +8 -0
  18. package/template/AGENTS.md +18 -10
  19. package/template/README.md +5 -5
  20. package/template/package.json +8 -9
  21. package/template/rsbuild.config.mts +17 -0
  22. package/template/src/App.tsx +21 -11
  23. package/template/tsconfig.json +3 -1
  24. package/build/ui/reference/README.md +0 -11
  25. package/build/ui/reference/components/accordion.tsx +0 -85
  26. package/build/ui/reference/components/ai.tsx +0 -291
  27. package/build/ui/reference/components/alert-dialog.tsx +0 -213
  28. package/build/ui/reference/components/alert.tsx +0 -112
  29. package/build/ui/reference/components/aspect-ratio.tsx +0 -28
  30. package/build/ui/reference/components/avatar.tsx +0 -158
  31. package/build/ui/reference/components/badge.tsx +0 -160
  32. package/build/ui/reference/components/button-group.tsx +0 -77
  33. package/build/ui/reference/components/button.tsx +0 -107
  34. package/build/ui/reference/components/calendar.tsx +0 -119
  35. package/build/ui/reference/components/card.tsx +0 -144
  36. package/build/ui/reference/components/carousel.tsx +0 -248
  37. package/build/ui/reference/components/chart.tsx +0 -704
  38. package/build/ui/reference/components/checkbox.tsx +0 -44
  39. package/build/ui/reference/components/collapsible.tsx +0 -43
  40. package/build/ui/reference/components/combobox.tsx +0 -141
  41. package/build/ui/reference/components/command.tsx +0 -181
  42. package/build/ui/reference/components/context-menu.tsx +0 -243
  43. package/build/ui/reference/components/data-table.tsx +0 -650
  44. package/build/ui/reference/components/date-picker.tsx +0 -218
  45. package/build/ui/reference/components/dialog.tsx +0 -172
  46. package/build/ui/reference/components/direction.tsx +0 -22
  47. package/build/ui/reference/components/dropdown-menu.tsx +0 -245
  48. package/build/ui/reference/components/empty-state.tsx +0 -63
  49. package/build/ui/reference/components/field.tsx +0 -252
  50. package/build/ui/reference/components/file-upload.tsx +0 -116
  51. package/build/ui/reference/components/hover-card.tsx +0 -64
  52. package/build/ui/reference/components/input-group.tsx +0 -157
  53. package/build/ui/reference/components/input-otp.tsx +0 -106
  54. package/build/ui/reference/components/input.tsx +0 -86
  55. package/build/ui/reference/components/item.tsx +0 -188
  56. package/build/ui/reference/components/kbd.tsx +0 -43
  57. package/build/ui/reference/components/label.tsx +0 -19
  58. package/build/ui/reference/components/list.tsx +0 -191
  59. package/build/ui/reference/components/map.css +0 -251
  60. package/build/ui/reference/components/map.tsx +0 -294
  61. package/build/ui/reference/components/menubar.tsx +0 -275
  62. package/build/ui/reference/components/message-scroller.tsx +0 -141
  63. package/build/ui/reference/components/message.tsx +0 -171
  64. package/build/ui/reference/components/nav.tsx +0 -122
  65. package/build/ui/reference/components/navigation-menu.tsx +0 -191
  66. package/build/ui/reference/components/page-header.tsx +0 -245
  67. package/build/ui/reference/components/popover.tsx +0 -76
  68. package/build/ui/reference/components/progress.tsx +0 -31
  69. package/build/ui/reference/components/radio.tsx +0 -65
  70. package/build/ui/reference/components/resizable.tsx +0 -40
  71. package/build/ui/reference/components/scroll-area.tsx +0 -55
  72. package/build/ui/reference/components/segmented.tsx +0 -45
  73. package/build/ui/reference/components/select.tsx +0 -188
  74. package/build/ui/reference/components/separator.tsx +0 -26
  75. package/build/ui/reference/components/sheet.tsx +0 -184
  76. package/build/ui/reference/components/skeleton.tsx +0 -15
  77. package/build/ui/reference/components/slider.tsx +0 -161
  78. package/build/ui/reference/components/snackbar.tsx +0 -27
  79. package/build/ui/reference/components/sonner.tsx +0 -43
  80. package/build/ui/reference/components/spinner.tsx +0 -27
  81. package/build/ui/reference/components/statistic.tsx +0 -122
  82. package/build/ui/reference/components/stepper.tsx +0 -138
  83. package/build/ui/reference/components/switch.tsx +0 -47
  84. package/build/ui/reference/components/table.tsx +0 -246
  85. package/build/ui/reference/components/textarea.tsx +0 -28
  86. package/build/ui/reference/components/timeline.tsx +0 -101
  87. package/build/ui/reference/components/toggle-group.tsx +0 -86
  88. package/build/ui/reference/components/toggle.tsx +0 -46
  89. package/build/ui/reference/components/tooltip.tsx +0 -53
  90. package/build/ui/reference/index.ts +0 -68
  91. package/build/ui/reference/lib/utils.ts +0 -34
  92. package/template/webpack.config.js +0 -112
  93. package/template/webpack.local.config.js +0 -68
@@ -1,650 +0,0 @@
1
- 'use client';
2
-
3
- import * as React from 'react';
4
- import {
5
- flexRender,
6
- getCoreRowModel,
7
- getFacetedRowModel,
8
- getFacetedUniqueValues,
9
- getFilteredRowModel,
10
- getSortedRowModel,
11
- useReactTable,
12
- type Column,
13
- type ColumnDef,
14
- type ColumnFiltersState,
15
- type RowSelectionState,
16
- type SortingState,
17
- type Table as TanstackTable,
18
- type VisibilityState,
19
- } from '@tanstack/react-table';
20
- import {
21
- ArrowDown,
22
- ArrowUp,
23
- CheckIcon,
24
- ChevronsUpDown,
25
- Plus,
26
- Search,
27
- Settings2,
28
- X,
29
- } from 'lucide-react';
30
-
31
- import { cn } from '@rise-x/ui/lib/utils';
32
- import { Badge } from '@rise-x/ui/components/badge';
33
- import { Button } from '@rise-x/ui/components/button';
34
- import { Checkbox } from '@rise-x/ui/components/checkbox';
35
- import {
36
- DropdownMenu,
37
- DropdownMenuCheckboxItem,
38
- DropdownMenuContent,
39
- DropdownMenuLabel,
40
- DropdownMenuTrigger,
41
- } from '@rise-x/ui/components/dropdown-menu';
42
- import { Input } from '@rise-x/ui/components/input';
43
- import { Popover, PopoverContent, PopoverTrigger } from '@rise-x/ui/components/popover';
44
- import { Separator } from '@rise-x/ui/components/separator';
45
- import {
46
- Table,
47
- TableBody,
48
- TableCell,
49
- TableHead,
50
- TableHeader,
51
- TableRow,
52
- } from '@rise-x/ui/components/table';
53
-
54
- // Re-exported so consumers can type their columns without depending on
55
- // @tanstack/react-table directly.
56
- export type { ColumnDef } from '@tanstack/react-table';
57
-
58
- export interface DataTableFilterOption {
59
- label: string;
60
- value: string;
61
- icon?: React.ComponentType<{ className?: string }>;
62
- }
63
-
64
- export interface FacetedFilterOption extends DataTableFilterOption {
65
- /** Right-aligned tally in the menu — usually how many rows carry this value.
66
- * Omitted means "not counted", and the tally is left off entirely rather
67
- * than printed as 0. */
68
- count?: number;
69
- }
70
-
71
- export interface FacetedFilterProps {
72
- title: string;
73
- options: FacetedFilterOption[];
74
- /** Applied values — a Set or an array, whichever the caller already holds. */
75
- selected: Iterable<string>;
76
- onSelect: (value: string, checked: boolean) => void;
77
- onClear: () => void;
78
- /** Search box above the list. Defaults to on past SEARCH_THRESHOLD options. */
79
- searchable?: boolean;
80
- /** Drop options whose `count` is 0 — inert when counts aren't provided.
81
- * Applied values stay listed so they can be unticked. */
82
- hideEmpty?: boolean;
83
- className?: string;
84
- }
85
-
86
- // Long option lists are unusable without a filter; short ones don't need one.
87
- const SEARCH_THRESHOLD = 10;
88
-
89
- export interface DataTableFilter {
90
- /** Column id (accessorKey) the filter applies to. */
91
- columnId: string;
92
- title: string;
93
- options: DataTableFilterOption[];
94
- /** Search box above the options. Defaults to on past SEARCH_THRESHOLD. */
95
- searchable?: boolean;
96
- /** Drop options no visible row carries. */
97
- hideEmpty?: boolean;
98
- }
99
-
100
- export interface DataTableProps<TData, TValue> {
101
- columns: ColumnDef<TData, TValue>[];
102
- data: TData[];
103
- searchPlaceholder?: string;
104
- /** Faceted multi-select filters shown in the toolbar. */
105
- filters?: DataTableFilter[];
106
- /** Prepends a checkbox column and shows the selection count. */
107
- enableRowSelection?: boolean;
108
- onRowClick?: (row: TData) => void;
109
- /** Extra toolbar content (e.g. a "New item" button), right-aligned. */
110
- toolbarEnd?: React.ReactNode;
111
- className?: string;
112
- }
113
-
114
- /** Column label for the view-options menu: `meta.label` or the column id. */
115
- function getColumnLabel<TData>(column: Column<TData, unknown>): string {
116
- const meta = column.columnDef.meta as { label?: string } | undefined;
117
- return meta?.label ?? column.id;
118
- }
119
-
120
- /** Sortable column header — pass via `header: ({ column }) => <DataTableColumnHeader …/>`. */
121
- export function DataTableColumnHeader<TData, TValue>({
122
- column,
123
- title,
124
- className,
125
- }: {
126
- column: Column<TData, TValue>;
127
- title: string;
128
- className?: string;
129
- }) {
130
- if (!column.getCanSort()) {
131
- return <div className={className}>{title}</div>;
132
- }
133
-
134
- const sorted = column.getIsSorted();
135
- const Icon = sorted === 'asc' ? ArrowUp : sorted === 'desc' ? ArrowDown : ChevronsUpDown;
136
- return (
137
- <button
138
- type="button"
139
- onClick={() => column.toggleSorting(sorted === 'asc')}
140
- className={cn(
141
- 'inline-flex items-center gap-1 font-medium outline-none hover:text-foreground focus-visible:underline',
142
- sorted && 'text-foreground',
143
- className,
144
- )}
145
- >
146
- {title}
147
- <Icon className={cn('size-3.5', !sorted && 'text-muted-foreground')} />
148
- </button>
149
- );
150
- }
151
-
152
- /**
153
- * Multi-select filter trigger + menu, with no table attached — for toolbars
154
- * that filter something other than a DataTable. State is the caller's:
155
- * `selected` in, `onSelect`/`onClear` out. DataTableFacetedFilter is this
156
- * component wired to a TanStack column.
157
- */
158
- export function FacetedFilter({
159
- title,
160
- options,
161
- selected: selectedValues,
162
- onSelect,
163
- onClear,
164
- searchable,
165
- hideEmpty,
166
- className,
167
- }: FacetedFilterProps) {
168
- const [open, setOpen] = React.useState(false);
169
- const [query, setQuery] = React.useState('');
170
- const listRef = React.useRef<HTMLDivElement>(null);
171
- const selected = new Set(selectedValues);
172
- const selectedOptions = options.filter((option) => selected.has(option.value));
173
-
174
- /* Two states that must be told apart at a glance:
175
- * empty → dashed outline + "+" + muted label ("a slot you could fill")
176
- * applied → solid fill, no dash, full-strength label, white value chips
177
- * The "+" disappears once something is applied (handled by the variant), and
178
- * an × appears to clear THIS filter without touching the others. */
179
- const hasSelection = selectedOptions.length > 0;
180
-
181
- const visible = hideEmpty
182
- ? options.filter((option) => option.count !== 0 || selected.has(option.value))
183
- : options;
184
- const showSearch = searchable ?? visible.length > SEARCH_THRESHOLD;
185
- const matches = query
186
- ? visible.filter((option) => option.label.toLowerCase().includes(query.toLowerCase()))
187
- : visible;
188
-
189
- /* Roving focus: one tab stop for the whole list, arrows move within it —
190
- * matching the DropdownMenu this replaced, where a long list would otherwise
191
- * be one tab stop per option. `active` carries the tab stop with the focus so
192
- * tabbing away and back returns to where the user left off. */
193
- const [active, setActive] = React.useState(0);
194
- const focusOption = (index: number) => {
195
- const items = listRef.current?.querySelectorAll<HTMLElement>('[role="option"]');
196
- if (!items?.length) return;
197
- const next = Math.min(Math.max(index === -1 ? items.length - 1 : index, 0), items.length - 1);
198
- setActive(next);
199
- items[next].focus();
200
- };
201
- const onListKeyDown = (event: React.KeyboardEvent) => {
202
- if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return;
203
- // Home/End belong to the caret while the search box has focus. This handler
204
- // sits on the popover container, so without this it would swallow them and
205
- // jump the list instead of moving through what the user typed.
206
- if (
207
- document.activeElement instanceof HTMLInputElement &&
208
- (event.key === 'Home' || event.key === 'End')
209
- ) {
210
- return;
211
- }
212
- event.preventDefault();
213
- if (event.key === 'Home') return focusOption(0);
214
- if (event.key === 'End') return focusOption(-1);
215
- // Radix focuses the popover container on open and the search box is not an
216
- // option either, so the first arrow has to ENTER the list rather than step
217
- // from a position nothing is sitting on.
218
- const items = listRef.current?.querySelectorAll<HTMLElement>('[role="option"]');
219
- const inList =
220
- !!items?.length && Array.from(items).includes(document.activeElement as HTMLElement);
221
- if (!inList) return focusOption(event.key === 'ArrowDown' ? 0 : -1);
222
- focusOption(active + (event.key === 'ArrowDown' ? 1 : -1));
223
- };
224
-
225
- return (
226
- <Popover
227
- open={open}
228
- onOpenChange={(next) => {
229
- setOpen(next);
230
- if (!next) {
231
- setQuery('');
232
- setActive(0);
233
- }
234
- }}
235
- >
236
- <PopoverTrigger asChild>
237
- <Button variant="dashed" size="sm" aria-pressed={hasSelection} className={className}>
238
- <Plus />
239
- {title}
240
- {hasSelection && (
241
- <>
242
- <Separator orientation="vertical" className="mx-0.5 h-3.5" />
243
- {selectedOptions.length > 2 ? (
244
- // 3+ collapses to a count so the trigger keeps a stable width
245
- <Badge className="bg-card px-1.5 font-normal text-foreground">
246
- {selectedOptions.length} selected
247
- </Badge>
248
- ) : (
249
- selectedOptions.map((option) => (
250
- <Badge key={option.value} className="bg-card px-1.5 font-normal text-foreground">
251
- {option.label}
252
- </Badge>
253
- ))
254
- )}
255
- <span
256
- role="button"
257
- tabIndex={-1}
258
- aria-label={`Clear ${title} filter`}
259
- // Both handlers: pointerdown suppresses the focus shift, and
260
- // PopoverTrigger toggles on click, so the click must be
261
- // swallowed here or clearing would also open the list.
262
- onPointerDown={(event) => {
263
- event.preventDefault();
264
- event.stopPropagation();
265
- }}
266
- onClick={(event) => {
267
- event.preventDefault();
268
- event.stopPropagation();
269
- onClear();
270
- }}
271
- className="-mr-1 ml-0.5 grid size-[17px] shrink-0 place-items-center rounded-full text-subtle transition-colors hover:bg-fill-3 hover:text-foreground"
272
- >
273
- <X className="size-2.5" />
274
- </span>
275
- </>
276
- )}
277
- </Button>
278
- </PopoverTrigger>
279
- {/* Width follows the content (nowrap rows, scroll past the cap) so long
280
- * values read on one line instead of wrapping. */}
281
- {/* gap-0 resets PopoverContent's own `flex flex-col gap-4`, which would
282
- * otherwise put 16px between the title, search box, list and clear row.
283
- * onKeyDown sits here, not on the listbox: Radix focuses the content
284
- * container on open, so a handler on a child never sees the first
285
- * arrow. */}
286
- <PopoverContent
287
- align="start"
288
- onKeyDown={onListKeyDown}
289
- className="w-auto max-w-96 min-w-48 gap-0 p-1"
290
- >
291
- <p className="px-2 py-1.5 text-xs font-medium text-muted-foreground">{title}</p>
292
- {showSearch && (
293
- <Input
294
- value={query}
295
- onChange={(event) => {
296
- setQuery(event.target.value);
297
- setActive(0);
298
- }}
299
- placeholder={`Filter ${title.toLowerCase()}…`}
300
- aria-label={`Filter ${title} options`}
301
- className="mb-1 h-control-sm"
302
- />
303
- )}
304
- <div ref={listRef} role="listbox" aria-multiselectable className="max-h-72 overflow-auto">
305
- {matches.map((option, index) => {
306
- const checked = selected.has(option.value);
307
- return (
308
- <button
309
- key={option.value}
310
- type="button"
311
- role="option"
312
- aria-selected={checked}
313
- // Clamped: filtering the list can shrink it past `active`, and
314
- // an out-of-range index would leave no tab stop at all.
315
- tabIndex={index === Math.min(active, matches.length - 1) ? 0 : -1}
316
- onFocus={() => setActive(index)}
317
- onClick={() => onSelect(option.value, !checked)}
318
- className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-ui whitespace-nowrap outline-none hover:bg-fill-2 focus-visible:bg-fill-2"
319
- >
320
- {/* Not the Checkbox component: that renders a <button>, and
321
- * nesting it in the option button is invalid HTML. */}
322
- <span
323
- aria-hidden="true"
324
- className={cn(
325
- 'grid size-[15px] shrink-0 place-content-center rounded-[4px] border transition-[background-color,border-color] duration-100',
326
- checked
327
- ? 'border-primary bg-primary text-primary-foreground'
328
- : 'border-input bg-card',
329
- )}
330
- >
331
- {checked && <CheckIcon className="size-2.5 stroke-[3.5]" />}
332
- </span>
333
- {option.icon && <option.icon className="size-4 text-muted-foreground" />}
334
- {option.label}
335
- {option.count !== undefined && (
336
- <span className="ms-auto ps-3 text-xs text-muted-foreground">{option.count}</span>
337
- )}
338
- </button>
339
- );
340
- })}
341
- </div>
342
- {matches.length === 0 && (
343
- <p className="px-2 py-3 text-ui text-muted-foreground">No matches</p>
344
- )}
345
- {/* selected, not hasSelection: a value applied but absent from
346
- * `options` renders no chip, and this row is then the only way out. */}
347
- {selected.size > 0 && (
348
- <>
349
- <Separator className="my-1" />
350
- <button
351
- type="button"
352
- onClick={() => onClear()}
353
- className="w-full rounded-sm px-2 py-1.5 text-ui outline-none hover:bg-fill-2 focus-visible:bg-fill-2"
354
- >
355
- Clear filter
356
- </button>
357
- </>
358
- )}
359
- </PopoverContent>
360
- </Popover>
361
- );
362
- }
363
-
364
- /** FacetedFilter bound to a TanStack column: facet counts and the filter value
365
- * come from the table, so the toolbar only names the column. */
366
- export function DataTableFacetedFilter<TData>({
367
- column,
368
- title,
369
- options,
370
- searchable,
371
- hideEmpty,
372
- }: {
373
- column: Column<TData, unknown>;
374
- title: string;
375
- options: DataTableFilterOption[];
376
- searchable?: boolean;
377
- hideEmpty?: boolean;
378
- }) {
379
- const selected = new Set((column.getFilterValue() as string[] | undefined) ?? []);
380
-
381
- /* Counted here rather than via getFacetedUniqueValues(): faceted columns hold
382
- * an array per row so `arrIncludesSome` can match, and that helper keys the
383
- * facet map on the whole array — ['Piping','Structural'] — so a lookup of a
384
- * single value never hits and every count renders 0. The faceted row model
385
- * also excludes this column's own filter, which keeps counts stable while
386
- * values are being ticked. */
387
- const facetedRows = column.getFacetedRowModel().flatRows;
388
- const counts = React.useMemo(() => {
389
- const tally = new Map<string, number>();
390
- for (const row of facetedRows) {
391
- const value = row.getValue(column.id);
392
- for (const item of Array.isArray(value) ? value : [value]) {
393
- if (item == null || item === '') continue;
394
- const key = String(item);
395
- tally.set(key, (tally.get(key) ?? 0) + 1);
396
- }
397
- }
398
- return tally;
399
- }, [facetedRows, column.id]);
400
-
401
- return (
402
- <FacetedFilter
403
- title={title}
404
- options={options.map((option) => ({ ...option, count: counts.get(option.value) ?? 0 }))}
405
- selected={selected}
406
- searchable={searchable}
407
- hideEmpty={hideEmpty}
408
- onSelect={(value, checked) => {
409
- const next = new Set(selected);
410
- if (checked) {
411
- next.add(value);
412
- } else {
413
- next.delete(value);
414
- }
415
- column.setFilterValue(next.size ? Array.from(next) : undefined);
416
- }}
417
- onClear={() => column.setFilterValue(undefined)}
418
- />
419
- );
420
- }
421
-
422
- function DataTableViewOptions<TData>({ table }: { table: TanstackTable<TData> }) {
423
- const columns = table
424
- .getAllColumns()
425
- .filter((column) => typeof column.accessorFn !== 'undefined' && column.getCanHide());
426
-
427
- return (
428
- <DropdownMenu>
429
- <DropdownMenuTrigger asChild>
430
- <Button variant="outline" size="sm" aria-label="Toggle columns">
431
- <Settings2 />
432
- View
433
- </Button>
434
- </DropdownMenuTrigger>
435
- <DropdownMenuContent align="end">
436
- <DropdownMenuLabel>Toggle columns</DropdownMenuLabel>
437
- {columns.map((column) => (
438
- <DropdownMenuCheckboxItem
439
- key={column.id}
440
- checked={column.getIsVisible()}
441
- onCheckedChange={(checked) => column.toggleVisibility(checked === true)}
442
- onSelect={(event) => event.preventDefault()}
443
- className="capitalize"
444
- >
445
- {getColumnLabel(column)}
446
- </DropdownMenuCheckboxItem>
447
- ))}
448
- </DropdownMenuContent>
449
- </DropdownMenu>
450
- );
451
- }
452
-
453
- /**
454
- * Rise-X data table: search, faceted filters and column visibility are built
455
- * in — every table ships with the full toolbar. Sorting via
456
- * DataTableColumnHeader, optional row selection and row-click handling.
457
- */
458
- export function DataTable<TData, TValue>({
459
- columns,
460
- data,
461
- searchPlaceholder = 'Search…',
462
- filters,
463
- enableRowSelection = false,
464
- onRowClick,
465
- toolbarEnd,
466
- className,
467
- }: DataTableProps<TData, TValue>) {
468
- const [sorting, setSorting] = React.useState<SortingState>([]);
469
- const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>([]);
470
- const [columnVisibility, setColumnVisibility] = React.useState<VisibilityState>({});
471
- const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({});
472
- const [globalFilter, setGlobalFilter] = React.useState('');
473
-
474
- const allColumns = React.useMemo(() => {
475
- // Faceted filters store arrays — give their columns the matching filterFn.
476
- const facetedIds = new Set(filters?.map((filter) => filter.columnId));
477
- const withFilterFn = columns.map((column) => {
478
- const id = column.id ?? (column as { accessorKey?: string }).accessorKey;
479
- return id && facetedIds.has(id)
480
- ? { ...column, filterFn: 'arrIncludesSome' as const }
481
- : column;
482
- });
483
-
484
- if (!enableRowSelection) return withFilterFn;
485
- const selectColumn: ColumnDef<TData, TValue> = {
486
- id: 'select',
487
- /* PAGE rows, not ALL rows. With filters on, `getIsAllRowsSelected` /
488
- * `toggleAllRowsSelected` reach rows the user cannot see: select-all would
489
- * check hidden rows, the footer (which counts the FILTERED selection)
490
- * would disagree with the box, and clearing the filter would reveal rows
491
- * silently selected. There is no pagination here, so "page" is exactly the
492
- * filtered set — i.e. what is on screen. */
493
- header: ({ table }) => (
494
- <Checkbox
495
- checked={
496
- table.getIsAllPageRowsSelected() ||
497
- (table.getIsSomePageRowsSelected() && 'indeterminate')
498
- }
499
- onCheckedChange={(checked) => table.toggleAllPageRowsSelected(checked === true)}
500
- aria-label="Select all rows"
501
- />
502
- ),
503
- cell: ({ row }) => (
504
- <Checkbox
505
- checked={row.getIsSelected()}
506
- onCheckedChange={(checked) => row.toggleSelected(checked === true)}
507
- onClick={(event) => event.stopPropagation()}
508
- aria-label="Select row"
509
- />
510
- ),
511
- enableSorting: false,
512
- enableHiding: false,
513
- };
514
- return [selectColumn, ...withFilterFn];
515
- }, [columns, filters, enableRowSelection]);
516
-
517
- const table = useReactTable({
518
- data,
519
- columns: allColumns,
520
- state: { sorting, columnFilters, columnVisibility, rowSelection, globalFilter },
521
- enableRowSelection,
522
- onSortingChange: setSorting,
523
- onColumnFiltersChange: setColumnFilters,
524
- onColumnVisibilityChange: setColumnVisibility,
525
- onRowSelectionChange: setRowSelection,
526
- onGlobalFilterChange: setGlobalFilter,
527
- globalFilterFn: 'includesString',
528
- getCoreRowModel: getCoreRowModel(),
529
- getFilteredRowModel: getFilteredRowModel(),
530
- getSortedRowModel: getSortedRowModel(),
531
- getFacetedRowModel: getFacetedRowModel(),
532
- // Kept for consumers even though the faceted filter counts from the row
533
- // model instead (this helper keys array cells on the whole array).
534
- getFacetedUniqueValues: getFacetedUniqueValues(),
535
- });
536
-
537
- const isFiltered = globalFilter !== '' || columnFilters.length > 0;
538
- const rows = table.getRowModel().rows;
539
-
540
- return (
541
- <div className={cn('flex flex-col gap-3', className)}>
542
- <div className="flex flex-wrap items-center gap-2">
543
- <div className="relative">
544
- <Search className="pointer-events-none absolute top-1/2 left-2.5 size-3.5 -translate-y-1/2 text-subtle" />
545
- <Input
546
- value={globalFilter}
547
- onChange={(event) => setGlobalFilter(event.target.value)}
548
- placeholder={searchPlaceholder}
549
- className="h-control-sm w-52 pl-7"
550
- aria-label={searchPlaceholder}
551
- />
552
- </div>
553
- {filters?.map((filter) => {
554
- const column = table.getColumn(filter.columnId);
555
- if (!column) return null;
556
- return (
557
- <DataTableFacetedFilter
558
- key={filter.columnId}
559
- column={column}
560
- title={filter.title}
561
- options={filter.options}
562
- searchable={filter.searchable}
563
- hideEmpty={filter.hideEmpty}
564
- />
565
- );
566
- })}
567
- {isFiltered && (
568
- <Button
569
- variant="ghost"
570
- size="sm"
571
- className="text-muted-foreground"
572
- onClick={() => {
573
- setGlobalFilter('');
574
- table.resetColumnFilters();
575
- }}
576
- >
577
- <X />
578
- {/* Reset only appears when something is on, and says how much it
579
- * will undo — "Reset" alone on a bar of four filters is a guess. */}
580
- Reset{columnFilters.length > 1 ? ` all (${columnFilters.length})` : ''}
581
- </Button>
582
- )}
583
- <div className="ms-auto flex items-center gap-2">
584
- {toolbarEnd}
585
- <DataTableViewOptions table={table} />
586
- </div>
587
- </div>
588
-
589
- <div className="overflow-x-auto rounded-2xl border border-border bg-card text-card-foreground">
590
- <Table>
591
- <TableHeader>
592
- {table.getHeaderGroups().map((headerGroup) => (
593
- <TableRow key={headerGroup.id} className="hover:bg-transparent">
594
- {headerGroup.headers.map((header) => (
595
- <TableHead key={header.id}>
596
- {header.isPlaceholder
597
- ? null
598
- : flexRender(header.column.columnDef.header, header.getContext())}
599
- </TableHead>
600
- ))}
601
- </TableRow>
602
- ))}
603
- </TableHeader>
604
- <TableBody>
605
- {rows.length ? (
606
- rows.map((row) => (
607
- <TableRow
608
- key={row.id}
609
- data-state={row.getIsSelected() && 'selected'}
610
- onClick={onRowClick ? () => onRowClick(row.original) : undefined}
611
- className={cn(onRowClick && 'cursor-pointer')}
612
- >
613
- {row.getVisibleCells().map((cell) => (
614
- <TableCell key={cell.id}>
615
- {flexRender(cell.column.columnDef.cell, cell.getContext())}
616
- </TableCell>
617
- ))}
618
- </TableRow>
619
- ))
620
- ) : (
621
- <TableRow className="hover:bg-transparent">
622
- <TableCell
623
- colSpan={table.getVisibleLeafColumns().length}
624
- className="h-24 text-center text-muted-foreground"
625
- >
626
- No results.
627
- </TableCell>
628
- </TableRow>
629
- )}
630
- </TableBody>
631
- </Table>
632
- </div>
633
-
634
- {/* Lazy loading, not pagination — so the footer reports scope rather than
635
- * offering page controls. */}
636
- <div className="flex items-center justify-between text-xs text-muted-foreground">
637
- <span>
638
- {enableRowSelection
639
- ? `${table.getFilteredSelectedRowModel().rows.length} of ${
640
- table.getFilteredRowModel().rows.length
641
- } row(s) selected`
642
- : `${table.getFilteredRowModel().rows.length} row(s)`}
643
- </span>
644
- <span>
645
- Showing <b className="font-medium text-foreground">{rows.length}</b> of {data.length}
646
- </span>
647
- </div>
648
- </div>
649
- );
650
- }