@vendure-io/ui 2.2.0 → 3.0.0-next.1
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 +19 -5
- package/src/components/atoms/accordion.tsx +21 -6
- package/src/components/atoms/alert-dialog.tsx +31 -6
- package/src/components/atoms/alert.tsx +72 -18
- package/src/components/atoms/avatar.tsx +12 -4
- package/src/components/atoms/badge.tsx +7 -2
- package/src/components/atoms/breadcrumb.tsx +493 -4
- package/src/components/atoms/button-group.tsx +30 -6
- package/src/components/atoms/button.tsx +128 -18
- package/src/components/atoms/calendar.tsx +6 -6
- package/src/components/atoms/card.tsx +130 -16
- package/src/components/atoms/carousel.tsx +16 -9
- package/src/components/atoms/chart.tsx +87 -1
- package/src/components/atoms/checkbox.tsx +85 -7
- package/src/components/atoms/collapsible.tsx +2 -1
- package/src/components/atoms/combobox.tsx +358 -29
- package/src/components/atoms/command.tsx +336 -13
- package/src/components/atoms/context-menu.tsx +28 -23
- package/src/components/atoms/dialog.tsx +173 -29
- package/src/components/atoms/direction.tsx +23 -2
- package/src/components/atoms/drawer.tsx +15 -10
- package/src/components/atoms/dropdown-menu.tsx +43 -28
- package/src/components/atoms/empty.tsx +5 -1
- package/src/components/atoms/field.tsx +41 -15
- package/src/components/atoms/hover-card.tsx +61 -8
- package/src/components/atoms/input-group.tsx +19 -11
- package/src/components/atoms/input-otp.tsx +6 -6
- package/src/components/atoms/input.tsx +7 -1
- package/src/components/atoms/item.tsx +3 -3
- package/src/components/atoms/kbd.tsx +32 -3
- package/src/components/atoms/label-swap.tsx +113 -0
- package/src/components/atoms/label.tsx +1 -1
- package/src/components/atoms/menubar.tsx +28 -26
- package/src/components/atoms/native-select.tsx +3 -2
- package/src/components/atoms/navigation-menu.tsx +13 -8
- package/src/components/atoms/pagination.tsx +2 -2
- package/src/components/atoms/popover.tsx +29 -6
- package/src/components/atoms/progress.tsx +46 -33
- package/src/components/atoms/radio-group.tsx +9 -4
- package/src/components/atoms/resizable.tsx +1 -1
- package/src/components/atoms/scroll-area.tsx +2 -2
- package/src/components/atoms/select.tsx +258 -19
- package/src/components/atoms/sheet.tsx +76 -23
- package/src/components/atoms/sidebar.tsx +405 -23
- package/src/components/atoms/skeleton.tsx +1 -1
- package/src/components/atoms/slider.tsx +1 -1
- package/src/components/atoms/sonner.tsx +128 -38
- package/src/components/atoms/spinner.tsx +1 -1
- package/src/components/atoms/switch.tsx +10 -5
- package/src/components/atoms/table.tsx +147 -7
- package/src/components/atoms/tabs.tsx +3 -3
- package/src/components/atoms/textarea.tsx +3 -1
- package/src/components/atoms/toggle-group.tsx +4 -2
- package/src/components/atoms/toggle.tsx +28 -6
- package/src/components/atoms/tooltip.tsx +85 -15
- package/src/components/molecules/anonymized-token.tsx +24 -8
- package/src/components/molecules/app-shell.tsx +53 -20
- package/src/components/molecules/arrow-link.tsx +40 -0
- package/src/components/molecules/chip.tsx +43 -4
- package/src/components/molecules/code-block.tsx +54 -31
- package/src/components/molecules/collapsible-section.tsx +129 -0
- package/src/components/molecules/confirm-dialog.tsx +122 -9
- package/src/components/molecules/copyable-text.tsx +75 -11
- package/src/components/molecules/count-badge.tsx +115 -0
- package/src/components/molecules/data-table/data-table-bulk-actions.tsx +32 -4
- package/src/components/molecules/data-table/data-table-column-header.tsx +20 -3
- package/src/components/molecules/data-table/data-table-filters.tsx +62 -22
- package/src/components/molecules/data-table/data-table-helpers.tsx +88 -1
- package/src/components/molecules/data-table/data-table-selection-storage.tsx +115 -0
- package/src/components/molecules/data-table/data-table-types.tsx +160 -4
- package/src/components/molecules/data-table/data-table-view-options.tsx +2 -2
- package/src/components/molecules/data-table/data-table.tsx +869 -81
- package/src/components/molecules/data-table/table-pagination.tsx +2 -2
- package/src/components/molecules/date-picker.tsx +13 -7
- package/src/components/molecules/date-range-picker.tsx +13 -6
- package/src/components/molecules/date-time-picker.tsx +13 -7
- package/src/components/molecules/date-time.tsx +8 -1
- package/src/components/molecules/direction-link.tsx +130 -0
- package/src/components/molecules/elapsed-time.tsx +223 -0
- package/src/components/molecules/file-dropzone.tsx +2 -2
- package/src/components/molecules/id-chip.tsx +169 -32
- package/src/components/molecules/inline-code.tsx +4 -0
- package/src/components/molecules/issued-secret.tsx +318 -0
- package/src/components/molecules/list-row.tsx +349 -0
- package/src/components/molecules/live-indicator.tsx +83 -0
- package/src/components/molecules/page-header.tsx +117 -5
- package/src/components/molecules/password-input.tsx +2 -2
- package/src/components/molecules/relative-time.tsx +85 -56
- package/src/components/molecules/search-trigger.tsx +111 -0
- package/src/components/molecules/secret-value.tsx +29 -9
- package/src/components/molecules/skip-link.tsx +1 -1
- package/src/components/molecules/stat-card.tsx +4 -4
- package/src/components/molecules/state-views/empty-state.tsx +35 -17
- package/src/components/molecules/state-views/error-state.tsx +32 -14
- package/src/components/molecules/status-badge.tsx +415 -49
- package/src/components/molecules/time-tooltip.tsx +140 -0
- package/src/components/molecules/timeline.tsx +246 -0
- package/src/components/molecules/visibility-icon.tsx +89 -0
- package/src/hooks/use-active-label-size.ts +66 -0
- package/src/hooks/use-apple-platform.ts +26 -0
- package/src/hooks/use-pinned-height.ts +55 -0
- package/src/hooks/use-scrolled.ts +27 -0
- package/src/lib/content-inset.ts +17 -0
- package/src/lib/control-name.ts +55 -0
- package/src/lib/dialog-padding.ts +48 -0
- package/src/lib/field-surface.ts +31 -0
- package/src/lib/format-elapsed.ts +67 -0
- package/src/lib/format-relative.ts +29 -1
- package/src/lib/highlight.ts +3 -3
- package/src/lib/menu-item.tsx +72 -0
- package/src/lib/outline-edge.ts +21 -0
- package/src/lib/popup-align.ts +67 -0
- package/src/lib/popup-direction.ts +22 -0
- package/src/lib/popup-motion.ts +19 -0
- package/src/lib/state-dictionary.ts +42 -6
- package/src/lib/syntax-colors.ts +8 -0
- package/src/lib/tint-recipes.ts +68 -0
- package/src/lib/tone-icons.ts +24 -0
- package/src/lib/trigger-slot.ts +16 -0
- package/src/lib/utils.ts +97 -1
|
@@ -8,7 +8,8 @@ import type * as React from 'react';
|
|
|
8
8
|
|
|
9
9
|
// The sortable header control rendered inside a `<TableHead>`. When the column
|
|
10
10
|
// can't sort it degrades to plain text — the header stays a passive label, no
|
|
11
|
-
// button, no affordance it can't honour.
|
|
11
|
+
// button, no affordance it can't honour. Both take their size, weight and
|
|
12
|
+
// colour from the `<TableHead>`. The `aria-sort` state lives on the
|
|
12
13
|
// enclosing `<TableHead>` (set by `data-table.tsx`), not here.
|
|
13
14
|
|
|
14
15
|
interface DataTableColumnHeaderProps<TData, TValue>
|
|
@@ -16,17 +17,23 @@ interface DataTableColumnHeaderProps<TData, TValue>
|
|
|
16
17
|
column: Column<TData, TValue>;
|
|
17
18
|
/** Accessible name for the sort toggle, e.g. `Sort by Name`. */
|
|
18
19
|
sortLabel?: string;
|
|
20
|
+
/**
|
|
21
|
+
* `'end'` puts the sort icon before the label and pulls the button forward,
|
|
22
|
+
* so the label ends where the column's end-aligned cells end.
|
|
23
|
+
*/
|
|
24
|
+
align?: 'start' | 'end';
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
function DataTableColumnHeader<TData, TValue>({
|
|
22
28
|
column,
|
|
23
29
|
sortLabel,
|
|
30
|
+
align = 'start',
|
|
24
31
|
className,
|
|
25
32
|
children,
|
|
26
33
|
...props
|
|
27
34
|
}: DataTableColumnHeaderProps<TData, TValue>) {
|
|
28
35
|
if (!column.getCanSort()) {
|
|
29
|
-
return <span className={
|
|
36
|
+
return <span className={className}>{children}</span>;
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
const sorted = column.getIsSorted();
|
|
@@ -41,7 +48,17 @@ function DataTableColumnHeader<TData, TValue>({
|
|
|
41
48
|
// Tri-state cycle asc → desc → none is TanStack's default toggle (removal
|
|
42
49
|
// enabled). We only wire the click; the engine owns the state machine.
|
|
43
50
|
onClick={column.getToggleSortingHandler()}
|
|
44
|
-
|
|
51
|
+
// Pulls back by the button's own `--content-inset` (its padding and its
|
|
52
|
+
// 1px border), so the label starts where a plain header label would. An
|
|
53
|
+
// end-aligned header pulls forward instead and puts the icon first, so
|
|
54
|
+
// the label ends where the cells end.
|
|
55
|
+
data-align={align}
|
|
56
|
+
className={cn(
|
|
57
|
+
// The label takes the header's colour; hover still darkens it.
|
|
58
|
+
'h-8 gap-1.5 text-inherit data-[slot=data-table-column-header]:px-2.5',
|
|
59
|
+
align === 'end' ? '-me-(--content-inset) flex-row-reverse' : '-ms-(--content-inset)',
|
|
60
|
+
className,
|
|
61
|
+
)}
|
|
45
62
|
{...props}
|
|
46
63
|
>
|
|
47
64
|
{children}
|
|
@@ -84,7 +84,7 @@ function DataTableAddFilter<TData>({
|
|
|
84
84
|
<div className="flex flex-col gap-2">
|
|
85
85
|
<button
|
|
86
86
|
type="button"
|
|
87
|
-
className="text-muted-foreground hover:text-foreground -
|
|
87
|
+
className="text-muted-foreground hover:text-foreground -ms-1 flex items-center gap-1 rounded-md text-xs outline-none focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring"
|
|
88
88
|
onClick={() => setActiveId(null)}
|
|
89
89
|
>
|
|
90
90
|
<ChevronLeftIcon className="size-3" />
|
|
@@ -129,6 +129,57 @@ function labelText(label: React.ReactNode, fallback: string): string {
|
|
|
129
129
|
return typeof label === 'string' ? label : fallback;
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
/** One applied filter. With `renderEditor`, its label opens the consumer's editor. */
|
|
133
|
+
function AppliedFilterChip<TData>({
|
|
134
|
+
table,
|
|
135
|
+
filterId,
|
|
136
|
+
value,
|
|
137
|
+
column,
|
|
138
|
+
removeLabel,
|
|
139
|
+
}: {
|
|
140
|
+
table: Table<TData>;
|
|
141
|
+
filterId: string;
|
|
142
|
+
value: DataTableFilterValue;
|
|
143
|
+
column: DataTableFilterColumn<TData> | undefined;
|
|
144
|
+
removeLabel: (columnLabel: string) => string;
|
|
145
|
+
}) {
|
|
146
|
+
const [editing, setEditing] = React.useState(false);
|
|
147
|
+
const anchor = React.useRef<HTMLSpanElement>(null);
|
|
148
|
+
const text = labelText(column?.label, filterId);
|
|
149
|
+
const content = column?.formatChip ? (
|
|
150
|
+
column.formatChip(value)
|
|
151
|
+
) : (
|
|
152
|
+
<>
|
|
153
|
+
{column?.label ?? filterId}: {JSON.stringify(value)}
|
|
154
|
+
</>
|
|
155
|
+
);
|
|
156
|
+
const editor = column?.renderEditor;
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<>
|
|
160
|
+
<Chip
|
|
161
|
+
ref={anchor}
|
|
162
|
+
onEdit={editor ? () => setEditing((open) => !open) : undefined}
|
|
163
|
+
editExpanded={editor ? editing : undefined}
|
|
164
|
+
onRemove={() => table.getColumn(filterId)?.setFilterValue(undefined)}
|
|
165
|
+
removeLabel={removeLabel(text)}
|
|
166
|
+
>
|
|
167
|
+
{content}
|
|
168
|
+
</Chip>
|
|
169
|
+
{editor && column
|
|
170
|
+
? editor({
|
|
171
|
+
open: editing,
|
|
172
|
+
onOpenChange: setEditing,
|
|
173
|
+
anchor,
|
|
174
|
+
value,
|
|
175
|
+
onChange: (next) => table.getColumn(filterId)?.setFilterValue(next),
|
|
176
|
+
column,
|
|
177
|
+
})
|
|
178
|
+
: null}
|
|
179
|
+
</>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
132
183
|
interface DataTableAppliedFiltersProps<TData> {
|
|
133
184
|
table: Table<TData>;
|
|
134
185
|
columns?: DataTableFilterColumn<TData>[];
|
|
@@ -149,27 +200,16 @@ function DataTableAppliedFilters<TData>({
|
|
|
149
200
|
|
|
150
201
|
const columnMap = new Map((columns ?? []).map((column) => [column.id, column]));
|
|
151
202
|
|
|
152
|
-
const chipFor = (filter: (typeof applied)[number]) =>
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return (
|
|
164
|
-
<Chip
|
|
165
|
-
key={filter.id}
|
|
166
|
-
onRemove={() => table.getColumn(filter.id)?.setFilterValue(undefined)}
|
|
167
|
-
removeLabel={removeLabel(text)}
|
|
168
|
-
>
|
|
169
|
-
{content}
|
|
170
|
-
</Chip>
|
|
171
|
-
);
|
|
172
|
-
};
|
|
203
|
+
const chipFor = (filter: (typeof applied)[number]) => (
|
|
204
|
+
<AppliedFilterChip
|
|
205
|
+
key={filter.id}
|
|
206
|
+
table={table}
|
|
207
|
+
filterId={filter.id}
|
|
208
|
+
value={filter.value as DataTableFilterValue}
|
|
209
|
+
column={columnMap.get(filter.id)}
|
|
210
|
+
removeLabel={removeLabel}
|
|
211
|
+
/>
|
|
212
|
+
);
|
|
173
213
|
|
|
174
214
|
const inline = applied.slice(0, inlineChipLimit);
|
|
175
215
|
const overflow = applied.slice(inlineChipLimit);
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { Column, ColumnDef, RowSelectionState, Table } from '@tanstack/react-table';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
ControlledState,
|
|
4
|
+
DataTableClearFilters,
|
|
5
|
+
DataTableColumnMeta,
|
|
6
|
+
} from '@vendure-io/ui/components/molecules/data-table/data-table-types';
|
|
3
7
|
|
|
4
8
|
// Pure, TanStack-shaped logic for the DataTable core. No JSX and no React state,
|
|
5
9
|
// so each function is unit-testable in isolation (the getRange/clampPage
|
|
@@ -43,6 +47,25 @@ export function getHideableLeafColumns<TData>(table: Table<TData>): Column<TData
|
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
/** Resolve a render-prop-or-node slot against the live table instance. */
|
|
50
|
+
/**
|
|
51
|
+
* The "Clear filters" button after the chips, or undefined when it is off.
|
|
52
|
+
* `emptyCell`: the table shows its empty state (no rows, not loading). The
|
|
53
|
+
* button hides there unless the consumer sets `hidden`. Default click: remove
|
|
54
|
+
* every applied filter.
|
|
55
|
+
*/
|
|
56
|
+
export function resolveClearFilters<TData>(
|
|
57
|
+
option: boolean | DataTableClearFilters | undefined,
|
|
58
|
+
table: Pick<Table<TData>, 'setColumnFilters'>,
|
|
59
|
+
emptyCell: boolean,
|
|
60
|
+
): { hidden: boolean; onClear: () => void } | undefined {
|
|
61
|
+
if (!option) return undefined;
|
|
62
|
+
const config = option === true ? {} : option;
|
|
63
|
+
return {
|
|
64
|
+
hidden: config.hidden ?? emptyCell,
|
|
65
|
+
onClear: config.onClear ?? (() => table.setColumnFilters([])),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
46
69
|
export function resolveSlot<TData, TSlot>(
|
|
47
70
|
slot: TSlot | ((table: Table<TData>) => TSlot) | undefined,
|
|
48
71
|
table: Table<TData>,
|
|
@@ -71,3 +94,67 @@ export function getSelectedOriginals<TData>(
|
|
|
71
94
|
export function getSelectedRowIds(selection: RowSelectionState): string[] {
|
|
72
95
|
return Object.keys(selection).filter((id) => selection[id]);
|
|
73
96
|
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* How far each column must indent its header and cells, so that cell content
|
|
100
|
+
* that pulls back by its own start inset (`--content-inset`) stays inside the
|
|
101
|
+
* column gutter. Keyed by `data-column-id`; a column that needs no indent is
|
|
102
|
+
* left out.
|
|
103
|
+
*
|
|
104
|
+
* Content may pull back into its start padding by up to the gutter between
|
|
105
|
+
* two columns: the cell's end padding. In a middle column that reaches the
|
|
106
|
+
* cell edge. The first column's start padding is the card's inset and the
|
|
107
|
+
* leading column's holds the selection checkbox, so there the content keeps
|
|
108
|
+
* the rest of the padding clear. A bigger pull-back (a chip with a leading
|
|
109
|
+
* icon) indents the column by the rest instead, and its header and plain-text
|
|
110
|
+
* cells follow.
|
|
111
|
+
*
|
|
112
|
+
* The measure reads boxes, so it also covers a consumer's own margin. It
|
|
113
|
+
* checks the cell's first element and that element's first element (content
|
|
114
|
+
* one wrapper deep). The result does not change with the indent already
|
|
115
|
+
* applied, so a second measure returns the same result.
|
|
116
|
+
*/
|
|
117
|
+
export function measureColumnIndents(table: HTMLTableElement): Record<string, number> {
|
|
118
|
+
const indents: Record<string, number> = {};
|
|
119
|
+
// Array.from, not for…of: a consumer's lib may not type NodeList as iterable.
|
|
120
|
+
const cells = Array.from(
|
|
121
|
+
table.querySelectorAll<HTMLElement>(':scope > tbody > tr > td[data-column-id]'),
|
|
122
|
+
);
|
|
123
|
+
for (const cell of cells) {
|
|
124
|
+
const id = cell.dataset.columnId;
|
|
125
|
+
const first = cell.firstElementChild;
|
|
126
|
+
if (id == null || first == null) continue;
|
|
127
|
+
const style = getComputedStyle(cell);
|
|
128
|
+
const rtl = style.direction === 'rtl';
|
|
129
|
+
const start = (rect: DOMRect) => (rtl ? -rect.right : rect.left);
|
|
130
|
+
const cellStart = start(cell.getBoundingClientRect());
|
|
131
|
+
const indent = Number.parseFloat(cell.style.getPropertyValue('--column-indent')) || 0;
|
|
132
|
+
const padding = Number.parseFloat(style.paddingInlineStart) - indent;
|
|
133
|
+
const gutter = Math.min(padding, Number.parseFloat(style.paddingInlineEnd));
|
|
134
|
+
// The nearest the content may start to the cell edge, without the indent.
|
|
135
|
+
const clear = padding - gutter;
|
|
136
|
+
for (const content of [first, first.firstElementChild]) {
|
|
137
|
+
if (content == null) continue;
|
|
138
|
+
const rect = content.getBoundingClientRect();
|
|
139
|
+
if (rect.width === 0) continue;
|
|
140
|
+
// Rounded to a hundredth of a pixel, so float noise never moves the text.
|
|
141
|
+
const need = Math.round((clear + indent - (start(rect) - cellStart)) * 100) / 100;
|
|
142
|
+
if (need > (indents[id] ?? 0)) indents[id] = need;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return indents;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Shallow equality of two indent maps, so an unchanged measure keeps its state. */
|
|
149
|
+
export function sameIndents(a: Record<string, number>, b: Record<string, number>): boolean {
|
|
150
|
+
const keys = Object.keys(a);
|
|
151
|
+
return keys.length === Object.keys(b).length && keys.every((key) => a[key] === b[key]);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The side a column aligns to, from its `meta.align` (see DataTableColumnMeta).
|
|
156
|
+
* TanStack types `meta` as the consumer's own ColumnMeta, so it is read loosely.
|
|
157
|
+
*/
|
|
158
|
+
export function columnAlign(meta: unknown): NonNullable<DataTableColumnMeta['align']> {
|
|
159
|
+
return (meta as DataTableColumnMeta | undefined)?.align === 'end' ? 'end' : 'start';
|
|
160
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { OnChangeFn, RowSelectionState, Updater } from '@tanstack/react-table';
|
|
2
|
+
import type {
|
|
3
|
+
ControlledState,
|
|
4
|
+
DataTableRowSelection,
|
|
5
|
+
} from '@vendure-io/ui/components/molecules/data-table/data-table-types';
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
|
|
8
|
+
// The row selection of a table, kept in session storage under a consumer's key.
|
|
9
|
+
// It survives a reload and a visit to another page in the same tab. It is not in
|
|
10
|
+
// the URL: a URL change is a navigation, and each checkbox click would run one.
|
|
11
|
+
|
|
12
|
+
const STORAGE_PREFIX = 'vendure-ui:data-table-selection:';
|
|
13
|
+
|
|
14
|
+
/** The ids stored under `key`. Blocked, missing or malformed storage gives none. */
|
|
15
|
+
export function readStoredSelection(key: string): RowSelectionState {
|
|
16
|
+
try {
|
|
17
|
+
const stored = globalThis.sessionStorage?.getItem(STORAGE_PREFIX + key);
|
|
18
|
+
const ids: unknown = stored ? JSON.parse(stored) : [];
|
|
19
|
+
if (!Array.isArray(ids)) return {};
|
|
20
|
+
return Object.fromEntries(ids.map((id) => [String(id), true]));
|
|
21
|
+
} catch {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Store the selected ids under `key`. All of them, whatever page they are on. An
|
|
28
|
+
* empty selection removes the entry. Storage that is full or blocked is ignored:
|
|
29
|
+
* the selection then lasts until the table unmounts.
|
|
30
|
+
*/
|
|
31
|
+
export function writeStoredSelection(key: string, selection: RowSelectionState) {
|
|
32
|
+
try {
|
|
33
|
+
const ids = selectedIds(selection);
|
|
34
|
+
if (ids.length === 0) {
|
|
35
|
+
globalThis.sessionStorage?.removeItem(STORAGE_PREFIX + key);
|
|
36
|
+
} else {
|
|
37
|
+
globalThis.sessionStorage?.setItem(STORAGE_PREFIX + key, JSON.stringify(ids));
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
40
|
+
// Storage can be full or blocked.
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const selectedIds = (selection: RowSelectionState) =>
|
|
45
|
+
Object.keys(selection).filter((id) => selection[id]);
|
|
46
|
+
|
|
47
|
+
const sameSelection = (a: RowSelectionState, b: RowSelectionState) => {
|
|
48
|
+
const left = selectedIds(a);
|
|
49
|
+
return left.length === selectedIds(b).length && left.every((id) => b[id]);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const resolve = (updater: Updater<RowSelectionState>, old: RowSelectionState) =>
|
|
53
|
+
typeof updater === 'function' ? updater(old) : updater;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Wraps a table's `rowSelection` config so its selection is stored under
|
|
57
|
+
* `storageKey`. Without a key, or without `rowSelection`, it returns the config
|
|
58
|
+
* as it is.
|
|
59
|
+
*
|
|
60
|
+
* - On the first load, ids stored under the key become the selection. Nothing
|
|
61
|
+
* stored leaves the selection as the consumer set it.
|
|
62
|
+
* - Every change the table makes is stored.
|
|
63
|
+
* - A new key switches to the selection stored under it, or to none. Nothing
|
|
64
|
+
* from the old key carries over, and the switch itself stores nothing.
|
|
65
|
+
*
|
|
66
|
+
* The stored ids are applied in a layout effect, before the first paint, and
|
|
67
|
+
* reported through the consumer's `onChange`. A controlled consumer takes them
|
|
68
|
+
* there.
|
|
69
|
+
*/
|
|
70
|
+
export function useSelectionStorage<TData>(
|
|
71
|
+
storageKey: string | undefined,
|
|
72
|
+
rowSelection: DataTableRowSelection<TData> | undefined,
|
|
73
|
+
): DataTableRowSelection<TData> | undefined {
|
|
74
|
+
const enabled = storageKey != null && rowSelection != null;
|
|
75
|
+
const controlled = rowSelection?.value !== undefined;
|
|
76
|
+
const [own, setOwn] = React.useState<RowSelectionState>(rowSelection?.defaultValue ?? {});
|
|
77
|
+
const current = controlled ? (rowSelection?.value as RowSelectionState) : own;
|
|
78
|
+
|
|
79
|
+
// The latest values, read by the callbacks below so their identity holds.
|
|
80
|
+
const latest = React.useRef({ storageKey, current, onChange: rowSelection?.onChange });
|
|
81
|
+
latest.current = { storageKey, current, onChange: rowSelection?.onChange };
|
|
82
|
+
const loadedKey = React.useRef<string | undefined>(undefined);
|
|
83
|
+
|
|
84
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: restores when the key changes, nothing else
|
|
85
|
+
React.useLayoutEffect(() => {
|
|
86
|
+
if (!enabled) {
|
|
87
|
+
loadedKey.current = undefined;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const first = loadedKey.current === undefined;
|
|
91
|
+
loadedKey.current = storageKey;
|
|
92
|
+
const stored = readStoredSelection(storageKey);
|
|
93
|
+
// On the first load an empty store leaves the consumer's own selection.
|
|
94
|
+
if (first && selectedIds(stored).length === 0) return;
|
|
95
|
+
if (sameSelection(stored, latest.current.current)) return;
|
|
96
|
+
if (!controlled) setOwn(stored);
|
|
97
|
+
latest.current.onChange?.(stored);
|
|
98
|
+
}, [enabled, storageKey]);
|
|
99
|
+
|
|
100
|
+
const onChange = React.useCallback<OnChangeFn<RowSelectionState>>((updater) => {
|
|
101
|
+
const next = resolve(updater, latest.current.current);
|
|
102
|
+
// Updates in one tick build on each other before the next render.
|
|
103
|
+
latest.current.current = next;
|
|
104
|
+
if (latest.current.storageKey != null) writeStoredSelection(latest.current.storageKey, next);
|
|
105
|
+
setOwn(next);
|
|
106
|
+
latest.current.onChange?.(updater);
|
|
107
|
+
}, []);
|
|
108
|
+
|
|
109
|
+
return React.useMemo(() => {
|
|
110
|
+
if (!enabled || rowSelection == null) return rowSelection;
|
|
111
|
+
const { defaultValue: _defaultValue, ...rest } = rowSelection;
|
|
112
|
+
return { ...rest, value: current, onChange } as ControlledState<RowSelectionState> &
|
|
113
|
+
DataTableRowSelection<TData>;
|
|
114
|
+
}, [enabled, rowSelection, current, onChange]);
|
|
115
|
+
}
|
|
@@ -13,6 +13,20 @@ import type {
|
|
|
13
13
|
} from '@tanstack/react-table';
|
|
14
14
|
import type * as React from 'react';
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* The keys DataTable reads from a column's `meta`. DataTable does not augment
|
|
18
|
+
* TanStack's `ColumnMeta`, so a consumer's own meta keys keep working; write
|
|
19
|
+
* `meta: { align: 'end' } satisfies DataTableColumnMeta` for a checked value.
|
|
20
|
+
*/
|
|
21
|
+
export interface DataTableColumnMeta {
|
|
22
|
+
/**
|
|
23
|
+
* Which side of the column its header, sort control and cells line up on.
|
|
24
|
+
* `'end'` is for numbers (totals, quantities), so their digits line up.
|
|
25
|
+
* Logical, so it flips in right-to-left. Defaults to `'start'`.
|
|
26
|
+
*/
|
|
27
|
+
align?: 'start' | 'end';
|
|
28
|
+
}
|
|
29
|
+
|
|
16
30
|
// Pure type module for the DataTable molecule. Kept runtime-free so it can be
|
|
17
31
|
// imported by both `data-table.tsx` and its sub-components without an import
|
|
18
32
|
// cycle. `data-table.tsx` re-exports everything here (plus the consumer-facing
|
|
@@ -108,6 +122,24 @@ export interface DataTableFilterColumn<TData = unknown> {
|
|
|
108
122
|
* passes its human-readable-operator formatter here.
|
|
109
123
|
*/
|
|
110
124
|
formatChip?: (value: DataTableFilterValue) => React.ReactNode;
|
|
125
|
+
/**
|
|
126
|
+
* Makes the column's applied-filter chip editable. Present → the chip label
|
|
127
|
+
* is a button that opens this editor, and the consumer renders the whole
|
|
128
|
+
* popup (a Popover, a Combobox, a Dialog) controlled by `open`. Anchor a
|
|
129
|
+
* positioned popup to `anchor`, the chip. Absent → the chip only removes.
|
|
130
|
+
*/
|
|
131
|
+
renderEditor?: (ctx: DataTableFilterEditorContext<TData>) => React.ReactNode;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface DataTableFilterEditorContext<TData = unknown> {
|
|
135
|
+
/** Whether the editor is open. The chip label opens it. */
|
|
136
|
+
open: boolean;
|
|
137
|
+
onOpenChange: (open: boolean) => void;
|
|
138
|
+
/** The applied-filter chip, to anchor a positioned popup to. */
|
|
139
|
+
anchor: React.RefObject<HTMLElement | null>;
|
|
140
|
+
value: DataTableFilterValue | undefined;
|
|
141
|
+
onChange: (next: DataTableFilterValue | undefined) => void;
|
|
142
|
+
column: DataTableFilterColumn<TData>;
|
|
111
143
|
}
|
|
112
144
|
|
|
113
145
|
export type DataTableFilters<TData> = ControlledState<ColumnFiltersState> & {
|
|
@@ -127,27 +159,113 @@ export type DataTableFilters<TData> = ControlledState<ColumnFiltersState> & {
|
|
|
127
159
|
showAddFilter?: boolean;
|
|
128
160
|
/** Defaults to true. Set false when a consumer renders its own applied-filter UI. */
|
|
129
161
|
showAppliedFilters?: boolean;
|
|
162
|
+
/**
|
|
163
|
+
* A "Clear filters" button after the applied-filter chips. Off by default.
|
|
164
|
+
* `true` or an object turns it on. It shows only with a chip: a search has
|
|
165
|
+
* the clear of its own field. While the table shows its empty state (no
|
|
166
|
+
* rows, not loading), the empty state holds the only "Clear filters", so the
|
|
167
|
+
* button hides. The chip line keeps the button's height either way, so the
|
|
168
|
+
* table does not move when it shows or hides.
|
|
169
|
+
*/
|
|
170
|
+
clearFilters?: boolean | DataTableClearFilters;
|
|
130
171
|
};
|
|
131
172
|
|
|
173
|
+
export interface DataTableClearFilters {
|
|
174
|
+
/** Runs on click. Default: removes every applied filter. */
|
|
175
|
+
onClear?: () => void;
|
|
176
|
+
/**
|
|
177
|
+
* Hides the button while a chip applies. Default: true while the table
|
|
178
|
+
* shows its empty state (no rows, not loading). Set it when the consumer
|
|
179
|
+
* knows better, e.g. loading passed through `emptyState`.
|
|
180
|
+
*/
|
|
181
|
+
hidden?: boolean;
|
|
182
|
+
}
|
|
183
|
+
|
|
132
184
|
export interface DataTableBulkActionContext<TData> {
|
|
133
185
|
/**
|
|
134
|
-
* `row.original`
|
|
135
|
-
* selection cache
|
|
186
|
+
* `row.original` of the selected rows the table has loaded, from the core's
|
|
187
|
+
* selection cache. Use it to DISPLAY rows. It can be SHORTER than
|
|
188
|
+
* `selectedRowIds`: a selection spans pages, it can be restored before its
|
|
189
|
+
* rows load, and a stored id can name a row that no longer exists. Act on
|
|
190
|
+
* `selectedRowIds`; read `missingRowIds` before an action that needs row data.
|
|
136
191
|
*/
|
|
137
192
|
selection: TData[];
|
|
193
|
+
/** The id of EVERY selected row, loaded or not. Act on these. */
|
|
138
194
|
selectedRowIds: string[];
|
|
195
|
+
/** `selectedRowIds.length`. Show this as the count: it is never below what the action receives. */
|
|
196
|
+
selectedCount: number;
|
|
197
|
+
/**
|
|
198
|
+
* The selected ids with no row object in `selection`. Empty when `selection`
|
|
199
|
+
* has a row for every id. When it is not empty, an action that needs row data
|
|
200
|
+
* must load these rows or tell the user: it must not act on fewer rows than
|
|
201
|
+
* `selectedCount` says.
|
|
202
|
+
*/
|
|
203
|
+
missingRowIds: string[];
|
|
139
204
|
table: Table<TData>;
|
|
140
205
|
clearSelection: () => void; // table.resetRowSelection()
|
|
141
206
|
}
|
|
142
207
|
|
|
208
|
+
/**
|
|
209
|
+
* The select all / clear all ripple timing of one row, handed to `renderRow`.
|
|
210
|
+
* Put both on the `<tr>` you render so it ripples like a built-in row:
|
|
211
|
+
* `className` merged into yours, `style` spread before yours. `style` holds
|
|
212
|
+
* only custom properties (`--ripple-at`, `--ripple-span`), so it never
|
|
213
|
+
* collides with an inline `transform`, `transition` or `opacity` of the row.
|
|
214
|
+
* The row also needs `data-state="selected"` while selected, as the built-in
|
|
215
|
+
* rows have.
|
|
216
|
+
*/
|
|
217
|
+
export interface DataTableRowRipple {
|
|
218
|
+
className: string;
|
|
219
|
+
style: React.CSSProperties;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* What a built-in body cell of one column carries, handed to `renderRow` as
|
|
224
|
+
* `ctx.cellProps(columnId)`. Spread it on the `<td>` you render
|
|
225
|
+
* (`<TableCell {...cellProps(cell.column.id)}>`) and the cell matches the
|
|
226
|
+
* built-in row: the select cell is the zero-width cell the checkbox floats
|
|
227
|
+
* over, the leading data cell carries the checkbox gutter, and every cell
|
|
228
|
+
* lines its content up with the header. Merge your own classes with `cn`.
|
|
229
|
+
*/
|
|
230
|
+
export interface DataTableRowCellProps {
|
|
231
|
+
'data-column-id': string;
|
|
232
|
+
'data-content-inset-cell': string;
|
|
233
|
+
className: string;
|
|
234
|
+
style: React.CSSProperties | undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* What a built-in row carries for click-to-select, handed to `renderRow` as
|
|
239
|
+
* `ctx.selectProps`. Spread it on the `<tr>` you render and the row behaves
|
|
240
|
+
* like a built-in row: while rows are selected a click on the row selects it,
|
|
241
|
+
* a click on the name (`DataTableRowLink`) opens the item, Shift+click extends
|
|
242
|
+
* the selection, and row controls keep their own action. A press that starts
|
|
243
|
+
* on a row control (a drag handle with `role="button"` or
|
|
244
|
+
* `data-table-row-control`) or moves more than 4px before release never
|
|
245
|
+
* selects, so a drag never does. Spread it before your own handlers: an
|
|
246
|
+
* `onClickCapture`, `onMouseDown` or `onPointerDown` of yours replaces the
|
|
247
|
+
* table's.
|
|
248
|
+
*/
|
|
249
|
+
export interface DataTableRowSelectProps {
|
|
250
|
+
'data-click-selects': '' | undefined;
|
|
251
|
+
onClickCapture: React.MouseEventHandler<HTMLElement>;
|
|
252
|
+
onMouseDown: React.MouseEventHandler<HTMLElement>;
|
|
253
|
+
onPointerDown: React.PointerEventHandler<HTMLElement>;
|
|
254
|
+
}
|
|
255
|
+
|
|
143
256
|
export interface DataTableLabels {
|
|
144
257
|
empty?: string; // 'No results'
|
|
145
258
|
selectAllRows?: string; // 'Select all rows'
|
|
259
|
+
/** Tooltip on the header checkbox while not every row on the page is selected. */
|
|
260
|
+
selectAllOnPage?: string; // 'Select all on this page'
|
|
261
|
+
/** Tooltip on the header checkbox while every row on the page is selected. */
|
|
262
|
+
clearPageSelection?: string; // 'Clear selection on this page'
|
|
146
263
|
selectRow?: (index: number) => string; // (i) => `Select row ${i + 1}`
|
|
147
264
|
rowActions?: string; // 'Open row actions'
|
|
148
265
|
columnsTrigger?: string; // 'Columns'
|
|
149
266
|
columnsHeading?: string; // 'Toggle columns'
|
|
150
267
|
addFilter?: string; // 'Add filter'
|
|
268
|
+
clearFilters?: string; // 'Clear filters'
|
|
151
269
|
removeFilter?: (columnLabel: string) => string; // (l) => `Remove ${l} filter`
|
|
152
270
|
sortLabel?: (columnLabel: string) => string; // (c) => `Sort by ${c}`
|
|
153
271
|
filtersCollapsed?: (count: number) => string; // (n) => `${n} filters`
|
|
@@ -193,6 +311,22 @@ export interface DataTableProps<TData> {
|
|
|
193
311
|
filters?: DataTableFilters<TData>;
|
|
194
312
|
columnVisibility?: DataTableColumnVisibility;
|
|
195
313
|
rowSelection?: DataTableRowSelection<TData>;
|
|
314
|
+
/**
|
|
315
|
+
* Keeps the row selection in session storage under this key, so it survives a
|
|
316
|
+
* reload and a visit to another page in the same tab. Needs `rowSelection`.
|
|
317
|
+
*
|
|
318
|
+
* - On the first load the table restores the ids stored under the key and
|
|
319
|
+
* reports them through `rowSelection.onChange`.
|
|
320
|
+
* - Every change is stored, with the ids of rows on other pages: a selection
|
|
321
|
+
* can span pages. An empty selection removes the entry.
|
|
322
|
+
* - A new key switches to the selection stored under it, or to none. Nothing
|
|
323
|
+
* from the old key carries over.
|
|
324
|
+
* - No navigation and no request. Blocked or full storage is ignored.
|
|
325
|
+
*
|
|
326
|
+
* The key names the list and anything that scopes its rows, such as a channel
|
|
327
|
+
* or a project: `${route}:${channelId}`. Without the prop nothing changes.
|
|
328
|
+
*/
|
|
329
|
+
selectionStorageKey?: string;
|
|
196
330
|
|
|
197
331
|
// slots
|
|
198
332
|
/** First child of the internal `ListHeader` (title zone) — compose `PageHeader` here. */
|
|
@@ -201,7 +335,10 @@ export interface DataTableProps<TData> {
|
|
|
201
335
|
toolbar?: React.ReactNode | ((table: Table<TData>) => React.ReactNode);
|
|
202
336
|
/**
|
|
203
337
|
* Replaces the controls row inside the header band while selection is
|
|
204
|
-
* non-empty (title and applied-filter chips stay).
|
|
338
|
+
* non-empty, at the row's height (title and applied-filter chips stay).
|
|
339
|
+
* Without a controls row it covers the column header row instead. Either
|
|
340
|
+
* way the table does not move. Keep the bar one line of `h-8` or smaller
|
|
341
|
+
* controls, so it fits the 40px header row. Only meaningful with
|
|
205
342
|
* `rowSelection` wired.
|
|
206
343
|
*/
|
|
207
344
|
bulkActions?: (ctx: DataTableBulkActionContext<TData>) => React.ReactNode;
|
|
@@ -233,10 +370,29 @@ export interface DataTableProps<TData> {
|
|
|
233
370
|
* MODEL) by owning the row JSX — together they restore the donor's
|
|
234
371
|
* `isUtilityRow`/`renderUtilityRow`/`DraggableRow` seam without the core reading
|
|
235
372
|
* `meta`.
|
|
373
|
+
*
|
|
374
|
+
* A row you render yourself changes with the others when select all or clear
|
|
375
|
+
* all ripples down the page only if it takes `ctx.ripple` (see
|
|
376
|
+
* {@link DataTableRowRipple}); a row that ignores it changes at once.
|
|
377
|
+
* `ctx.cellProps(columnId)` gives each cell the built-in cell's classes (see
|
|
378
|
+
* {@link DataTableRowCellProps}), so the row's select cell and leading cell
|
|
379
|
+
* never drift from the table's own. `ctx.selectProps` (see
|
|
380
|
+
* {@link DataTableRowSelectProps}) gives the row click-to-select while rows
|
|
381
|
+
* are selected; a row without it keeps opening the item from anywhere on it.
|
|
382
|
+
* A row with a `DataTableRowLink` needs nothing more: the table raises its
|
|
383
|
+
* buttons, links, inputs and copy chips above the stretched link, as it does
|
|
384
|
+
* on a built-in row.
|
|
236
385
|
*/
|
|
237
386
|
renderRow?: (
|
|
238
387
|
row: Row<TData>,
|
|
239
|
-
ctx: {
|
|
388
|
+
ctx: {
|
|
389
|
+
table: Table<TData>;
|
|
390
|
+
columnCount: number;
|
|
391
|
+
defaultRow: React.ReactNode;
|
|
392
|
+
ripple: DataTableRowRipple;
|
|
393
|
+
cellProps: (columnId: string) => DataTableRowCellProps;
|
|
394
|
+
selectProps: DataTableRowSelectProps;
|
|
395
|
+
},
|
|
240
396
|
) => React.ReactNode;
|
|
241
397
|
|
|
242
398
|
/**
|
|
@@ -40,11 +40,11 @@ function DataTableViewOptions<TData>({
|
|
|
40
40
|
|
|
41
41
|
return (
|
|
42
42
|
<DropdownMenu>
|
|
43
|
-
{/* `
|
|
43
|
+
{/* `ms-auto` pins the trigger to the end of the controls row, so it reads
|
|
44
44
|
as a table setting even when no search input fills the row. */}
|
|
45
45
|
<DropdownMenuTrigger
|
|
46
46
|
data-slot="data-table-view-options"
|
|
47
|
-
className="
|
|
47
|
+
className="ms-auto"
|
|
48
48
|
render={
|
|
49
49
|
<Button variant="outline">
|
|
50
50
|
<SlidersHorizontalIcon />
|