@vendure-io/ui 3.0.0-next.4 → 3.0.0-next.6
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 +12 -11
- package/src/components/atoms/alert-dialog.tsx +1 -4
- package/src/components/atoms/alert.tsx +59 -35
- package/src/components/atoms/badge.tsx +2 -2
- package/src/components/atoms/breadcrumb.tsx +154 -59
- package/src/components/atoms/button-group.tsx +1 -1
- package/src/components/atoms/button.tsx +2 -2
- package/src/components/atoms/calendar.tsx +2 -2
- package/src/components/atoms/card.tsx +15 -2
- package/src/components/atoms/carousel.tsx +33 -7
- package/src/components/atoms/chart.tsx +3 -3
- package/src/components/atoms/checkbox.tsx +1 -1
- package/src/components/atoms/combobox.tsx +60 -36
- package/src/components/atoms/command.tsx +17 -7
- package/src/components/atoms/context-menu.tsx +5 -5
- package/src/components/atoms/dialog.tsx +30 -14
- package/src/components/atoms/dropdown-menu.tsx +1 -1
- package/src/components/atoms/hover-card.tsx +1 -1
- package/src/components/atoms/input-group.tsx +30 -12
- package/src/components/atoms/input-otp.tsx +2 -2
- package/src/components/atoms/input.tsx +13 -3
- package/src/components/atoms/menubar.tsx +2 -2
- package/src/components/atoms/native-select.tsx +1 -1
- package/src/components/atoms/navigation-menu.tsx +3 -3
- package/src/components/atoms/pagination.tsx +52 -15
- package/src/components/atoms/popover.tsx +1 -1
- package/src/components/atoms/select.tsx +3 -3
- package/src/components/atoms/sheet.tsx +35 -15
- package/src/components/atoms/sidebar.tsx +224 -36
- package/src/components/atoms/skeleton.tsx +6 -3
- package/src/components/atoms/spinner.tsx +7 -2
- package/src/components/atoms/tabs.tsx +6 -2
- package/src/components/atoms/textarea.tsx +1 -1
- package/src/components/atoms/toggle.tsx +1 -1
- package/src/components/molecules/action-needed.tsx +483 -0
- package/src/components/molecules/anonymized-token.tsx +70 -25
- package/src/components/molecules/app-shell.tsx +14 -5
- package/src/components/molecules/checklist/checklist.tsx +47 -17
- package/src/components/molecules/chip.tsx +37 -19
- package/src/components/molecules/choice-card.tsx +117 -9
- package/src/components/molecules/code-block/package-manager-storage.ts +50 -0
- package/src/components/molecules/code-block/process-code.ts +1 -1
- package/src/components/molecules/code-block.tsx +58 -47
- package/src/components/molecules/combobox-free-text.tsx +75 -20
- package/src/components/molecules/confirm-dialog.tsx +17 -5
- package/src/components/molecules/copyable-text.tsx +10 -8
- package/src/components/molecules/data-table/data-table-filters.tsx +36 -6
- package/src/components/molecules/data-table/data-table-helpers.tsx +109 -3
- package/src/components/molecules/data-table/data-table-row-count-storage.tsx +88 -0
- package/src/components/molecules/data-table/data-table-selection-storage.tsx +33 -15
- package/src/components/molecules/data-table/data-table-types.tsx +156 -29
- package/src/components/molecules/data-table/data-table-view-options.tsx +14 -4
- package/src/components/molecules/data-table/data-table.tsx +156 -75
- package/src/components/molecules/data-table/table-pagination.tsx +142 -38
- package/src/components/molecules/date-picker.tsx +58 -25
- package/src/components/molecules/date-range-picker.tsx +59 -24
- package/src/components/molecules/date-time-picker.tsx +63 -25
- package/src/components/molecules/date-time.tsx +38 -3
- package/src/components/molecules/elapsed-time.tsx +61 -12
- package/src/components/molecules/file-dropzone.tsx +151 -40
- package/src/components/molecules/format-provider.tsx +14 -4
- package/src/components/molecules/id-chip-chrome.tsx +114 -0
- package/src/components/molecules/id-chip.tsx +13 -115
- package/src/components/molecules/issued-secret.tsx +35 -25
- package/src/components/molecules/live-indicator.tsx +1 -1
- package/src/components/molecules/money.tsx +44 -3
- package/src/components/molecules/multi-select.tsx +62 -16
- package/src/components/molecules/page-header.tsx +6 -2
- package/src/components/molecules/password-input.tsx +11 -10
- package/src/components/molecules/relative-time.tsx +88 -16
- package/src/components/molecules/search-trigger.tsx +5 -3
- package/src/components/molecules/secret-value.tsx +35 -19
- package/src/components/molecules/skip-link.tsx +11 -9
- package/src/components/molecules/stat-card.tsx +145 -33
- package/src/components/molecules/state-views/empty-state.tsx +16 -12
- package/src/components/molecules/state-views/error-state.tsx +36 -20
- package/src/components/molecules/state-views/loading-state.tsx +18 -6
- package/src/components/molecules/state-views/skeleton-parts.tsx +6 -4
- package/src/components/molecules/status-badge.tsx +11 -35
- package/src/components/molecules/time-tooltip.tsx +55 -1
- package/src/hooks/use-copy.ts +3 -1
- package/src/lib/deprecated.ts +71 -0
- package/src/lib/field-surface.ts +7 -7
- package/src/lib/format-messages.ts +368 -0
- package/src/lib/highlight.ts +1 -1
- package/src/lib/menu-item.tsx +1 -1
- package/src/lib/tint-recipes.ts +10 -5
- package/src/lib/utils.ts +19 -1
- package/src/lib/dialog-padding.ts +0 -48
|
@@ -1,14 +1,33 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
type Column,
|
|
3
|
+
type ColumnDef,
|
|
4
|
+
functionalUpdate,
|
|
5
|
+
type OnChangeFn,
|
|
6
|
+
type RowSelectionState,
|
|
7
|
+
type Table,
|
|
8
|
+
} from '@tanstack/react-table';
|
|
2
9
|
import type {
|
|
3
10
|
ControlledState,
|
|
4
11
|
DataTableClearFilters,
|
|
5
12
|
DataTableColumnMeta,
|
|
6
13
|
} from '@vendure-io/ui/components/molecules/data-table/data-table-types';
|
|
14
|
+
import * as React from 'react';
|
|
15
|
+
import { warnDeprecated } from '../../../lib/deprecated.ts';
|
|
7
16
|
|
|
8
|
-
// Pure, TanStack-shaped logic for the DataTable core. No JSX and
|
|
9
|
-
// so each function is unit-testable in isolation (the getRange/clampPage
|
|
17
|
+
// Pure, TanStack-shaped logic for the DataTable core. No JSX, and React state
|
|
18
|
+
// only in `useControlledTableState`, so each function is unit-testable in isolation (the getRange/clampPage
|
|
10
19
|
// precedent from table-pagination.tsx).
|
|
11
20
|
|
|
21
|
+
/**
|
|
22
|
+
* The pending look of a range or a table body whose next content is loading:
|
|
23
|
+
* after `--transition-duration-slow` (300ms) it fades to half opacity, so a
|
|
24
|
+
* read that ends sooner shows nothing. Leaving pending is immediate. Under
|
|
25
|
+
* reduced motion the dim still waits, then steps instead of fading. Set
|
|
26
|
+
* `data-pending` on the element to turn it on.
|
|
27
|
+
*/
|
|
28
|
+
export const PENDING_CLASS =
|
|
29
|
+
'[transition:opacity_var(--transition-duration-moderate)_var(--ease-soft)] data-pending:opacity-50 data-pending:[transition-delay:var(--transition-duration-slow)] motion-reduce:[transition-duration:0s]';
|
|
30
|
+
|
|
12
31
|
// biome-ignore lint/suspicious/noExplicitAny: matches TanStack's ColumnDef value-type convention
|
|
13
32
|
type AnyColumnDef<TData> = ColumnDef<TData, any>;
|
|
14
33
|
|
|
@@ -17,6 +36,16 @@ type AnyColumnDef<TData> = ColumnDef<TData, any>;
|
|
|
17
36
|
* the prop is the source of truth on every render; uncontrolled (`defaultValue`
|
|
18
37
|
* or nothing) means the core owns the state.
|
|
19
38
|
*/
|
|
39
|
+
/**
|
|
40
|
+
* Warns when a capability config uses the deprecated `onChange`. The state
|
|
41
|
+
* bridge calls it with TanStack's updater when `onValueChange` is not set.
|
|
42
|
+
* Remove in 4.0.
|
|
43
|
+
*/
|
|
44
|
+
export function warnOnChange<T>(config: ControlledState<T> | undefined, capability: string): void {
|
|
45
|
+
if (config?.onChange === undefined) return;
|
|
46
|
+
warnDeprecated('DataTable', `${capability}.onChange`, `${capability}.onValueChange`);
|
|
47
|
+
}
|
|
48
|
+
|
|
20
49
|
export function isControlled<T>(config: ControlledState<T> | undefined): boolean {
|
|
21
50
|
return config?.value !== undefined;
|
|
22
51
|
}
|
|
@@ -163,3 +192,80 @@ export function sameIndents(a: Record<string, number>, b: Record<string, number>
|
|
|
163
192
|
export function columnAlign(meta: unknown): NonNullable<DataTableColumnMeta['align']> {
|
|
164
193
|
return (meta as DataTableColumnMeta | undefined)?.align === 'end' ? 'end' : 'start';
|
|
165
194
|
}
|
|
195
|
+
|
|
196
|
+
/** A column's width from its `meta.width` (see DataTableColumnMeta), if it sets one. */
|
|
197
|
+
export function columnWidth(meta: unknown): DataTableColumnMeta['width'] {
|
|
198
|
+
return cssLength((meta as DataTableColumnMeta | undefined)?.width);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* A column's minimum width: its `meta.minWidth`, else its `meta.width`, so a
|
|
203
|
+
* set width is also a floor (see DataTableColumnMeta).
|
|
204
|
+
*/
|
|
205
|
+
export function columnMinWidth(meta: unknown): DataTableColumnMeta['minWidth'] {
|
|
206
|
+
const column = meta as DataTableColumnMeta | undefined;
|
|
207
|
+
return cssLength(column?.minWidth) ?? cssLength(column?.width);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// The `--content-inset` of a default-size StatusBadge, as status-badge.tsx
|
|
211
|
+
// sets it: the chip with an icon or a dot marker, and `appearance="icon"`.
|
|
212
|
+
// A test keeps them equal to that file.
|
|
213
|
+
export const STATUS_BADGE_CONTENT_INSETS = {
|
|
214
|
+
'status-badge': 'calc(var(--spacing)*6.75)',
|
|
215
|
+
'status-badge-dot': 'calc(var(--spacing)*5)',
|
|
216
|
+
'status-icon': 'calc(var(--spacing)*6)',
|
|
217
|
+
} as const;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* A column's declared indent (see DataTableColumnMeta.contentInset), as the
|
|
221
|
+
* `--column-indent` value: the content's pull-back past the cell's gutter
|
|
222
|
+
* (TableCell's `px-2`), or none. Undefined when the meta sets no inset, so
|
|
223
|
+
* the table measures it instead.
|
|
224
|
+
*/
|
|
225
|
+
export function columnContentIndent(meta: unknown): string | undefined {
|
|
226
|
+
const inset = (meta as DataTableColumnMeta | undefined)?.contentInset;
|
|
227
|
+
const length =
|
|
228
|
+
typeof inset === 'number'
|
|
229
|
+
? `${inset}px`
|
|
230
|
+
: typeof inset === 'string'
|
|
231
|
+
? (STATUS_BADGE_CONTENT_INSETS[inset as keyof typeof STATUS_BADGE_CONTENT_INSETS] ?? inset)
|
|
232
|
+
: undefined;
|
|
233
|
+
return length == null ? undefined : `max(0px, calc(${length} - var(--spacing)*2))`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function cssLength(value: unknown): number | string | undefined {
|
|
237
|
+
return typeof value === 'number' || typeof value === 'string' ? value : undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Bridge a capability config to a `[value, onChange]` pair TanStack can consume
|
|
242
|
+
* as fully controlled. Controlled configs (`value`) are the source of truth every
|
|
243
|
+
* render; uncontrolled configs (`defaultValue` or nothing) get internal state the
|
|
244
|
+
* core owns, with any `onValueChange` observing. This fixes the donor's
|
|
245
|
+
* seed-then-emit bug: the prop, not a one-time seed, drives every render.
|
|
246
|
+
* TanStack's updater is resolved here, so `onValueChange` gets the next value.
|
|
247
|
+
*/
|
|
248
|
+
export function useControlledTableState<T>(
|
|
249
|
+
config: ControlledState<T> | undefined,
|
|
250
|
+
fallback: T,
|
|
251
|
+
): readonly [T, OnChangeFn<T>] {
|
|
252
|
+
const controlled = config?.value !== undefined;
|
|
253
|
+
const [internal, setInternal] = React.useState<T>(config?.defaultValue ?? fallback);
|
|
254
|
+
const value = controlled ? (config?.value as T) : internal;
|
|
255
|
+
// Updates in one tick build on each other before the next render.
|
|
256
|
+
const latest = React.useRef(value);
|
|
257
|
+
latest.current = value;
|
|
258
|
+
const onChange = React.useCallback<OnChangeFn<T>>(
|
|
259
|
+
(updater) => {
|
|
260
|
+
const next = functionalUpdate(updater, latest.current);
|
|
261
|
+
latest.current = next;
|
|
262
|
+
if (!controlled) setInternal(next);
|
|
263
|
+
// The deprecated `onChange` gets TanStack's updater, as it did before
|
|
264
|
+
// `onValueChange`; `onValueChange` wins when both are set.
|
|
265
|
+
if (config?.onValueChange) config.onValueChange(next);
|
|
266
|
+
else config?.onChange?.(updater);
|
|
267
|
+
},
|
|
268
|
+
[controlled, config],
|
|
269
|
+
);
|
|
270
|
+
return [value, onChange] as const;
|
|
271
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { migrateStorageKey } from '../../../lib/deprecated.ts';
|
|
3
|
+
|
|
4
|
+
// The total row count (all pages) of a list at its last load, kept in local
|
|
5
|
+
// storage under a consumer's key. Passed back as DataTable `expectedTotal`, it
|
|
6
|
+
// lets the next first load draw as many skeleton rows as the current page will
|
|
7
|
+
// have, so the body keeps its height when the rows arrive, also after a reload.
|
|
8
|
+
|
|
9
|
+
const STORAGE_PREFIX = 'vendure-ui:data-table:list-total:';
|
|
10
|
+
// The 3.0.0-next key, moved on read. Remove in 4.0.
|
|
11
|
+
const OLD_STORAGE_PREFIX = 'vendure-ui:list-total:';
|
|
12
|
+
|
|
13
|
+
/** The total stored under `key`, or null. Blocked, missing or malformed storage gives null. */
|
|
14
|
+
export function readRowCount(key: string): number | null {
|
|
15
|
+
migrateStorageKey('localStorage', OLD_STORAGE_PREFIX + key, STORAGE_PREFIX + key);
|
|
16
|
+
try {
|
|
17
|
+
const stored = globalThis.localStorage?.getItem(STORAGE_PREFIX + key);
|
|
18
|
+
const count = stored ? Number(stored) : Number.NaN;
|
|
19
|
+
return Number.isInteger(count) && count >= 0 ? count : null;
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Store `count` under `key`. Storage that is full or blocked is ignored. */
|
|
26
|
+
export function writeRowCount(key: string, count: number) {
|
|
27
|
+
try {
|
|
28
|
+
globalThis.localStorage?.setItem(STORAGE_PREFIX + key, String(count));
|
|
29
|
+
} catch {
|
|
30
|
+
// Storage can be full or blocked.
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Remembers a list's total row count in local storage, for DataTable
|
|
36
|
+
* `expectedTotal`:
|
|
37
|
+
*
|
|
38
|
+
* ```tsx
|
|
39
|
+
* const expectedTotal = useRememberedListTotal('orders', data?.totalItems ?? null);
|
|
40
|
+
* <DataTable rows={data?.items} expectedTotal={expectedTotal} … />
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* - Returns the total stored under `key` when the component mounted (or the
|
|
44
|
+
* key last changed), or null. It does not follow later writes: a first load
|
|
45
|
+
* needs the total of the visit before.
|
|
46
|
+
* - Stores `total` whenever it is not null. Pass null while it is not known.
|
|
47
|
+
* - Reads in a layout effect, before the first paint. A server render gets
|
|
48
|
+
* null, so it draws the default skeleton and hydrates cleanly.
|
|
49
|
+
* - Blocked or full storage is ignored. Without a key nothing is read or
|
|
50
|
+
* stored.
|
|
51
|
+
*
|
|
52
|
+
* The key names the list and anything that changes its total, such as a
|
|
53
|
+
* channel, the filters or the search. Pass no key for a filtered load to keep
|
|
54
|
+
* the unfiltered total.
|
|
55
|
+
*/
|
|
56
|
+
export function useRememberedListTotal(
|
|
57
|
+
key: string | undefined,
|
|
58
|
+
total: number | null,
|
|
59
|
+
): number | null {
|
|
60
|
+
const [remembered, setRemembered] = React.useState<number | null>(null);
|
|
61
|
+
|
|
62
|
+
React.useLayoutEffect(() => {
|
|
63
|
+
setRemembered(key == null ? null : readRowCount(key));
|
|
64
|
+
}, [key]);
|
|
65
|
+
|
|
66
|
+
React.useEffect(() => {
|
|
67
|
+
if (key != null && total != null) writeRowCount(key, total);
|
|
68
|
+
}, [key, total]);
|
|
69
|
+
|
|
70
|
+
return remembered;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The number of skeleton rows before the first load: the rows the current
|
|
75
|
+
* page will have. With pagination that is the page size, or fewer when the
|
|
76
|
+
* expected total ends on or before this page. Without pagination it is the
|
|
77
|
+
* expected total, else `fallback`. 0 means the page will be empty.
|
|
78
|
+
*/
|
|
79
|
+
export function skeletonRowCountFor(
|
|
80
|
+
expected: number | null,
|
|
81
|
+
page: { page: number; pageSize: number } | undefined,
|
|
82
|
+
fallback: number,
|
|
83
|
+
): number {
|
|
84
|
+
if (page == null) return expected ?? fallback;
|
|
85
|
+
if (expected == null) return page.pageSize;
|
|
86
|
+
const before = (page.page - 1) * page.pageSize;
|
|
87
|
+
return Math.min(page.pageSize, Math.max(0, expected - before));
|
|
88
|
+
}
|
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RowSelectionState } from '@tanstack/react-table';
|
|
2
2
|
import type {
|
|
3
3
|
ControlledState,
|
|
4
4
|
DataTableRowSelection,
|
|
5
5
|
} from '@vendure-io/ui/components/molecules/data-table/data-table-types';
|
|
6
6
|
import * as React from 'react';
|
|
7
|
+
import { migrateStorageKey } from '../../../lib/deprecated.ts';
|
|
7
8
|
|
|
8
9
|
// The row selection of a table, kept in session storage under a consumer's key.
|
|
9
10
|
// It survives a reload and a visit to another page in the same tab. It is not in
|
|
10
11
|
// the URL: a URL change is a navigation, and each checkbox click would run one.
|
|
11
12
|
|
|
12
|
-
const STORAGE_PREFIX = 'vendure-ui:data-table
|
|
13
|
+
const STORAGE_PREFIX = 'vendure-ui:data-table:selection:';
|
|
14
|
+
// The 3.0.0-next key, moved on read. Remove in 4.0.
|
|
15
|
+
const OLD_STORAGE_PREFIX = 'vendure-ui:data-table-selection:';
|
|
13
16
|
|
|
14
17
|
/** The ids stored under `key`. Blocked, missing or malformed storage gives none. */
|
|
15
18
|
export function readStoredSelection(key: string): RowSelectionState {
|
|
19
|
+
migrateStorageKey('sessionStorage', OLD_STORAGE_PREFIX + key, STORAGE_PREFIX + key);
|
|
16
20
|
try {
|
|
17
21
|
const stored = globalThis.sessionStorage?.getItem(STORAGE_PREFIX + key);
|
|
18
22
|
const ids: unknown = stored ? JSON.parse(stored) : [];
|
|
@@ -49,8 +53,14 @@ const sameSelection = (a: RowSelectionState, b: RowSelectionState) => {
|
|
|
49
53
|
return left.length === selectedIds(b).length && left.every((id) => b[id]);
|
|
50
54
|
};
|
|
51
55
|
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
/** `onValueChange` wins over the deprecated `onChange` (removed in 4.0). */
|
|
57
|
+
function reportSelection(
|
|
58
|
+
handlers: Pick<ControlledState<RowSelectionState>, 'onValueChange' | 'onChange'>,
|
|
59
|
+
next: RowSelectionState,
|
|
60
|
+
) {
|
|
61
|
+
if (handlers.onValueChange) handlers.onValueChange(next);
|
|
62
|
+
else handlers.onChange?.(next);
|
|
63
|
+
}
|
|
54
64
|
|
|
55
65
|
/**
|
|
56
66
|
* Wraps a table's `rowSelection` config so its selection is stored under
|
|
@@ -64,7 +74,7 @@ const resolve = (updater: Updater<RowSelectionState>, old: RowSelectionState) =>
|
|
|
64
74
|
* from the old key carries over, and the switch itself stores nothing.
|
|
65
75
|
*
|
|
66
76
|
* The stored ids are applied in a layout effect, before the first paint, and
|
|
67
|
-
* reported through the consumer's `
|
|
77
|
+
* reported through the consumer's `onValueChange`. A controlled consumer takes them
|
|
68
78
|
* there.
|
|
69
79
|
*/
|
|
70
80
|
export function useSelectionStorage<TData>(
|
|
@@ -77,8 +87,18 @@ export function useSelectionStorage<TData>(
|
|
|
77
87
|
const current = controlled ? (rowSelection?.value as RowSelectionState) : own;
|
|
78
88
|
|
|
79
89
|
// The latest values, read by the callbacks below so their identity holds.
|
|
80
|
-
const latest = React.useRef({
|
|
81
|
-
|
|
90
|
+
const latest = React.useRef({
|
|
91
|
+
storageKey,
|
|
92
|
+
current,
|
|
93
|
+
onValueChange: rowSelection?.onValueChange,
|
|
94
|
+
onChange: rowSelection?.onChange,
|
|
95
|
+
});
|
|
96
|
+
latest.current = {
|
|
97
|
+
storageKey,
|
|
98
|
+
current,
|
|
99
|
+
onValueChange: rowSelection?.onValueChange,
|
|
100
|
+
onChange: rowSelection?.onChange,
|
|
101
|
+
};
|
|
82
102
|
const loadedKey = React.useRef<string | undefined>(undefined);
|
|
83
103
|
|
|
84
104
|
// biome-ignore lint/correctness/useExhaustiveDependencies: restores when the key changes, nothing else
|
|
@@ -94,22 +114,20 @@ export function useSelectionStorage<TData>(
|
|
|
94
114
|
if (first && selectedIds(stored).length === 0) return;
|
|
95
115
|
if (sameSelection(stored, latest.current.current)) return;
|
|
96
116
|
if (!controlled) setOwn(stored);
|
|
97
|
-
latest.current
|
|
117
|
+
reportSelection(latest.current, stored);
|
|
98
118
|
}, [enabled, storageKey]);
|
|
99
119
|
|
|
100
|
-
const
|
|
101
|
-
const next = resolve(updater, latest.current.current);
|
|
102
|
-
// Updates in one tick build on each other before the next render.
|
|
120
|
+
const onValueChange = React.useCallback((next: RowSelectionState) => {
|
|
103
121
|
latest.current.current = next;
|
|
104
122
|
if (latest.current.storageKey != null) writeStoredSelection(latest.current.storageKey, next);
|
|
105
123
|
setOwn(next);
|
|
106
|
-
latest.current
|
|
124
|
+
reportSelection(latest.current, next);
|
|
107
125
|
}, []);
|
|
108
126
|
|
|
109
127
|
return React.useMemo(() => {
|
|
110
128
|
if (!enabled || rowSelection == null) return rowSelection;
|
|
111
|
-
const { defaultValue: _defaultValue, ...rest } = rowSelection;
|
|
112
|
-
return { ...rest, value: current,
|
|
129
|
+
const { defaultValue: _defaultValue, onChange: _onChange, ...rest } = rowSelection;
|
|
130
|
+
return { ...rest, value: current, onValueChange } as ControlledState<RowSelectionState> &
|
|
113
131
|
DataTableRowSelection<TData>;
|
|
114
|
-
}, [enabled, rowSelection, current,
|
|
132
|
+
}, [enabled, rowSelection, current, onValueChange]);
|
|
115
133
|
}
|
|
@@ -25,6 +25,38 @@ export interface DataTableColumnMeta {
|
|
|
25
25
|
* Logical, so it flips in right-to-left. Defaults to `'start'`.
|
|
26
26
|
*/
|
|
27
27
|
align?: 'start' | 'end';
|
|
28
|
+
/**
|
|
29
|
+
* The column's width: a number in px or a CSS length (`'8rem'`). The column
|
|
30
|
+
* has it while the skeleton shows and once the rows arrive, so the headers
|
|
31
|
+
* do not move when the data replaces the skeleton. Give one to each column
|
|
32
|
+
* whose content has a known width (a code, a status, a date, a total) and
|
|
33
|
+
* leave the widest text column without, so it takes the rest of the row.
|
|
34
|
+
* Wider content still widens the column (the table keeps its automatic
|
|
35
|
+
* layout). It is also the column's minimum: a table narrower than its
|
|
36
|
+
* columns scrolls sideways instead of squeezing them. TanStack's `size` is
|
|
37
|
+
* not read: TanStack gives every column a default `size`, so a set one can't
|
|
38
|
+
* be told from the default.
|
|
39
|
+
*/
|
|
40
|
+
width?: number | string;
|
|
41
|
+
/**
|
|
42
|
+
* The column's minimum width, for a text column without a `width`: it still
|
|
43
|
+
* takes the rest of the row, but never gets narrower than this, so in a
|
|
44
|
+
* narrow table it keeps a readable width and the same width while the
|
|
45
|
+
* skeleton shows and once the rows arrive.
|
|
46
|
+
*/
|
|
47
|
+
minWidth?: number | string;
|
|
48
|
+
/**
|
|
49
|
+
* The `--content-inset` of the column's cell content: how far it pulls back
|
|
50
|
+
* so its first letter lines up with the header. For a default-size
|
|
51
|
+
* StatusBadge: `'status-badge'` (chip with an icon), `'status-badge-dot'`
|
|
52
|
+
* (chip with a dot) or `'status-icon'` (`appearance="icon"`). Otherwise a
|
|
53
|
+
* number in px or a CSS length. When the pull-back is wider than the cell's gutter, the column's
|
|
54
|
+
* header and cells indent by the rest. With this set, that indent applies in
|
|
55
|
+
* every state, so the header does not move when the rows replace the
|
|
56
|
+
* skeleton. Without it the table measures the indent from the loaded cells,
|
|
57
|
+
* so the header moves once they arrive.
|
|
58
|
+
*/
|
|
59
|
+
contentInset?: 'status-badge' | 'status-badge-dot' | 'status-icon' | number | string;
|
|
28
60
|
}
|
|
29
61
|
|
|
30
62
|
// Pure type module for the DataTable molecule. Kept runtime-free so it can be
|
|
@@ -35,12 +67,22 @@ export interface DataTableColumnMeta {
|
|
|
35
67
|
/**
|
|
36
68
|
* Controlled OR uncontrolled, per capability. `value` present → controlled: the
|
|
37
69
|
* prop is the source of truth on every render (no internal seeding). `defaultValue`
|
|
38
|
-
* → uncontrolled: the core seeds its own state and `
|
|
39
|
-
* observes.
|
|
70
|
+
* → uncontrolled: the core seeds its own state and `onValueChange` (if given)
|
|
71
|
+
* only observes. `onValueChange` always gets the next value, never an updater
|
|
72
|
+
* function. Never mix `value` with `defaultValue`.
|
|
40
73
|
*/
|
|
41
|
-
export type ControlledState<T> =
|
|
42
|
-
| { value: T;
|
|
43
|
-
| { value?:
|
|
74
|
+
export type ControlledState<T> = (
|
|
75
|
+
| { value: T; onValueChange: (value: T) => void; defaultValue?: never }
|
|
76
|
+
| { value: T; onValueChange?: (value: T) => void; onChange: OnChangeFn<T>; defaultValue?: never }
|
|
77
|
+
| { value?: never; onValueChange?: (value: T) => void; defaultValue?: T }
|
|
78
|
+
) & {
|
|
79
|
+
/**
|
|
80
|
+
* @deprecated Use `onValueChange`, which gets the next value. `onChange`
|
|
81
|
+
* gets TanStack's updater (the next value or a function of the previous
|
|
82
|
+
* one). When both are set, only `onValueChange` is called. Removed in 4.0.
|
|
83
|
+
*/
|
|
84
|
+
onChange?: OnChangeFn<T>;
|
|
85
|
+
};
|
|
44
86
|
|
|
45
87
|
/* --- capability configs; PRESENCE of the config is the only feature switch --- */
|
|
46
88
|
|
|
@@ -68,11 +110,9 @@ export type DataTableRowSelection<TData> = ControlledState<RowSelectionState> &
|
|
|
68
110
|
* controlled via `page`/`onPageChange` — no `defaultValue` branch, because page
|
|
69
111
|
* is typically URL-owned.
|
|
70
112
|
*/
|
|
71
|
-
|
|
72
|
-
mode?: 'server' | 'client'; // default 'server'
|
|
113
|
+
interface DataTablePaginationBase {
|
|
73
114
|
page: number; // 1-based
|
|
74
115
|
pageSize: number;
|
|
75
|
-
totalItems?: number; // required in server mode
|
|
76
116
|
onPageChange: (page: number) => void;
|
|
77
117
|
onPageSizeChange?: (size: number) => void; // presence renders the size selector
|
|
78
118
|
pageSizeOptions?: number[]; // default [10, 25, 50, 100]
|
|
@@ -80,6 +120,24 @@ export interface DataTablePagination {
|
|
|
80
120
|
formatRange?: (range: { start: number; end: number; total: number }) => string;
|
|
81
121
|
}
|
|
82
122
|
|
|
123
|
+
/** The server pages, sorts and filters. The table shows one page of `rows`. */
|
|
124
|
+
export interface DataTableServerPagination extends DataTablePaginationBase {
|
|
125
|
+
mode?: 'server';
|
|
126
|
+
/**
|
|
127
|
+
* The number of rows on all pages. `null` or `undefined` while it is not
|
|
128
|
+
* known (the first page has not loaded yet): the footer range is a skeleton
|
|
129
|
+
* until then. Do not pass 0 for "not known": 0 means the list is empty.
|
|
130
|
+
*/
|
|
131
|
+
totalItems: number | null | undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** `rows` is the full set. The table pages it. */
|
|
135
|
+
export interface DataTableClientPagination extends DataTablePaginationBase {
|
|
136
|
+
mode: 'client';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export type DataTablePagination = DataTableServerPagination | DataTableClientPagination;
|
|
140
|
+
|
|
83
141
|
/**
|
|
84
142
|
* Open operator vocabulary — the core never interprets it; it only stores it in
|
|
85
143
|
* `ColumnFiltersState` and renders chips. Matches the donor's `{ [operator]: value }`.
|
|
@@ -257,23 +315,45 @@ export interface DataTableRowSelectProps {
|
|
|
257
315
|
}
|
|
258
316
|
|
|
259
317
|
export interface DataTableLabels {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
318
|
+
/** @default `messages.dataTable.empty`, "No results" */
|
|
319
|
+
empty?: string;
|
|
320
|
+
/** @default `messages.dataTable.selectAllRows`, "Select all rows" */
|
|
321
|
+
selectAllRows?: string;
|
|
322
|
+
/**
|
|
323
|
+
* Tooltip on the header checkbox while not every row on the page is selected.
|
|
324
|
+
* @default `messages.dataTable.selectAllOnPage`, "Select all on this page"
|
|
325
|
+
*/
|
|
326
|
+
selectAllOnPage?: string;
|
|
327
|
+
/**
|
|
328
|
+
* Tooltip on the header checkbox while every row on the page is selected.
|
|
329
|
+
* @default `messages.dataTable.clearPageSelection`, "Clear selection on this page"
|
|
330
|
+
*/
|
|
331
|
+
clearPageSelection?: string;
|
|
332
|
+
/** @default `messages.dataTable.selectRow`, "Select row {row}", with `{row}` the 1-based row number */
|
|
333
|
+
selectRow?: (index: number) => string;
|
|
334
|
+
/** @default `messages.dataTable.rowActions`, "Open row actions" */
|
|
335
|
+
rowActions?: string;
|
|
336
|
+
/** @default `messages.dataTable.columnsTrigger`, "Columns" */
|
|
337
|
+
columnsTrigger?: string;
|
|
338
|
+
/** @default `messages.dataTable.columnsHeading`, "Toggle columns" */
|
|
339
|
+
columnsHeading?: string;
|
|
340
|
+
/** @default `messages.dataTable.addFilter`, "Add filter" */
|
|
341
|
+
addFilter?: string;
|
|
342
|
+
/**
|
|
343
|
+
* Placeholder and accessible name of the filter editor's default value input.
|
|
344
|
+
* @default `messages.dataTable.filterValue`, "Value"
|
|
345
|
+
*/
|
|
346
|
+
filterValue?: string;
|
|
347
|
+
/** @default `messages.dataTable.clearFilters`, "Clear filters" */
|
|
348
|
+
clearFilters?: string;
|
|
349
|
+
/** @default `messages.dataTable.removeFilter`, "Remove {column} filter" */
|
|
350
|
+
removeFilter?: (columnLabel: string) => string;
|
|
351
|
+
/** @default `messages.dataTable.sortLabel`, "Sort by {column}" */
|
|
352
|
+
sortLabel?: (columnLabel: string) => string;
|
|
353
|
+
/** @default `messages.dataTable.filtersCollapsed`, "{count} filters" */
|
|
354
|
+
filtersCollapsed?: (count: number) => string;
|
|
275
355
|
pagination?: {
|
|
276
|
-
//
|
|
356
|
+
// Forwarded to TablePagination, which resolves them against `messages.tablePagination`.
|
|
277
357
|
navLabel?: string;
|
|
278
358
|
previousLabel?: string;
|
|
279
359
|
nextLabel?: string;
|
|
@@ -282,14 +362,53 @@ export interface DataTableLabels {
|
|
|
282
362
|
}
|
|
283
363
|
|
|
284
364
|
export interface DataTableProps<TData> {
|
|
285
|
-
|
|
365
|
+
/**
|
|
366
|
+
* The current page (server) or the full set (client). The core never
|
|
367
|
+
* fetches. `undefined` until the first load finishes: the body shows
|
|
368
|
+
* skeleton rows. `[]` is a loaded empty list and shows the empty state, also
|
|
369
|
+
* while a new load runs. Do not pass `[]` for "not loaded".
|
|
370
|
+
*/
|
|
371
|
+
rows: TData[] | undefined;
|
|
372
|
+
/**
|
|
373
|
+
* THE cell/header seam. `meta` is passed through; the DataTable reads
|
|
374
|
+
* `meta.align` and `meta.width` (see `DataTableColumnMeta`).
|
|
375
|
+
*/
|
|
286
376
|
// biome-ignore lint/suspicious/noExplicitAny: matches TanStack's ColumnDef value-type convention
|
|
287
|
-
columns: ColumnDef<TData, any>[];
|
|
377
|
+
columns: ColumnDef<TData, any>[];
|
|
288
378
|
/** TanStack getRowId. Required with server pagination + rowSelection. */
|
|
289
379
|
getRowId?: (row: TData, index: number) => string;
|
|
290
380
|
|
|
291
|
-
|
|
292
|
-
|
|
381
|
+
/**
|
|
382
|
+
* A load is running. With `rows` loaded (a page, sort or filter change that
|
|
383
|
+
* keeps the previous rows, or the empty state) they stay: the body is
|
|
384
|
+
* `aria-busy`, the rows and the footer range dim after a short delay, and
|
|
385
|
+
* prev/next ignore presses. Never blanks loaded rows. Before the first load
|
|
386
|
+
* (`rows` is `undefined`) the body shows skeleton rows with or without it.
|
|
387
|
+
*/
|
|
388
|
+
isLoading?: boolean;
|
|
389
|
+
/**
|
|
390
|
+
* Skeleton rows before the first load of a table without `pagination`.
|
|
391
|
+
* Default 10. With `pagination` the count comes from the page size (and
|
|
392
|
+
* `expectedTotal`), so this prop is not used.
|
|
393
|
+
*/
|
|
394
|
+
skeletonRowCount?: number;
|
|
395
|
+
/**
|
|
396
|
+
* The list's total row count (all pages) from an earlier visit, while the
|
|
397
|
+
* rows are not loaded yet. The first load then draws as many skeleton rows
|
|
398
|
+
* as the current page will have: the page size, fewer on a short last page,
|
|
399
|
+
* so the table does not change height when the rows arrive. A page that will
|
|
400
|
+
* be empty renders the empty state (`emptyState` or "No results") hidden and
|
|
401
|
+
* inert instead, so the body has its exact height.
|
|
402
|
+
*
|
|
403
|
+
* - Only the skeleton row count uses it. The footer never shows it: it can
|
|
404
|
+
* be out of date.
|
|
405
|
+
* - Without it a paginated table draws one full page, any other table
|
|
406
|
+
* `skeletonRowCount`.
|
|
407
|
+
*
|
|
408
|
+
* `useRememberedListTotal` keeps the total in local storage. Any other store
|
|
409
|
+
* works too.
|
|
410
|
+
*/
|
|
411
|
+
expectedTotal?: number | null;
|
|
293
412
|
/**
|
|
294
413
|
* Per-column loading cells, keyed by column id, for cells that are not one
|
|
295
414
|
* text line tall (a title over a muted sub-line, an avatar, a button), so the
|
|
@@ -319,7 +438,7 @@ export interface DataTableProps<TData> {
|
|
|
319
438
|
* reload and a visit to another page in the same tab. Needs `rowSelection`.
|
|
320
439
|
*
|
|
321
440
|
* - On the first load the table restores the ids stored under the key and
|
|
322
|
-
* reports them through `rowSelection.
|
|
441
|
+
* reports them through `rowSelection.onValueChange`.
|
|
323
442
|
* - Every change is stored, with the ids of rows on other pages: a selection
|
|
324
443
|
* can span pages. An empty selection removes the entry.
|
|
325
444
|
* - A new key switches to the selection stored under it, or to none. Nothing
|
|
@@ -460,6 +579,14 @@ export interface DataTableProps<TData> {
|
|
|
460
579
|
* item.
|
|
461
580
|
*/
|
|
462
581
|
pinFirstColumn?: boolean;
|
|
582
|
+
/**
|
|
583
|
+
* The rows open an item through `DataTableRowLink`. The table then keeps the
|
|
584
|
+
* row chevron's slot after the last column in every state: while the
|
|
585
|
+
* skeleton shows, when empty and with rows. Without it the slot appears only
|
|
586
|
+
* once a row with a link renders, so on the first load every column moves
|
|
587
|
+
* sideways by the slot's width when the rows arrive.
|
|
588
|
+
*/
|
|
589
|
+
rowLinks?: boolean;
|
|
463
590
|
/**
|
|
464
591
|
* Extra `<TableRow>`s appended inside `TableBody` after the data rows — e.g.
|
|
465
592
|
* an order table's subtotal/shipping/total rows. Rendered only alongside
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
} from '@vendure-io/ui/components/atoms/dropdown-menu';
|
|
14
14
|
import { SlidersHorizontalIcon } from 'lucide-react';
|
|
15
15
|
// Relative: `data-table-helpers` is closed in the package exports.
|
|
16
|
+
import { useMessages } from '../../../lib/format-messages.ts';
|
|
16
17
|
import { getHideableLeafColumns } from './data-table-helpers.tsx';
|
|
17
18
|
|
|
18
19
|
// The column picker ("Columns" trigger). Lists only hideable columns (`getCanHide()`), so
|
|
@@ -22,7 +23,15 @@ import { getHideableLeafColumns } from './data-table-helpers.tsx';
|
|
|
22
23
|
|
|
23
24
|
interface DataTableViewOptionsProps<TData> {
|
|
24
25
|
table: Table<TData>;
|
|
26
|
+
/**
|
|
27
|
+
* The trigger's text.
|
|
28
|
+
* @default `messages.dataTable.columnsTrigger`, "Columns"
|
|
29
|
+
*/
|
|
25
30
|
triggerLabel?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The menu's heading.
|
|
33
|
+
* @default `messages.dataTable.columnsHeading`, "Toggle columns"
|
|
34
|
+
*/
|
|
26
35
|
heading?: string;
|
|
27
36
|
}
|
|
28
37
|
|
|
@@ -33,9 +42,10 @@ function columnLabel<TData>(column: Column<TData, unknown>): string {
|
|
|
33
42
|
|
|
34
43
|
function DataTableViewOptions<TData>({
|
|
35
44
|
table,
|
|
36
|
-
triggerLabel
|
|
37
|
-
heading
|
|
45
|
+
triggerLabel,
|
|
46
|
+
heading,
|
|
38
47
|
}: DataTableViewOptionsProps<TData>) {
|
|
48
|
+
const t = useMessages('dataTable', { columnsTrigger: triggerLabel, columnsHeading: heading });
|
|
39
49
|
const hideableColumns = getHideableLeafColumns(table);
|
|
40
50
|
|
|
41
51
|
return (
|
|
@@ -48,7 +58,7 @@ function DataTableViewOptions<TData>({
|
|
|
48
58
|
render={
|
|
49
59
|
<Button variant="outline">
|
|
50
60
|
<SlidersHorizontalIcon />
|
|
51
|
-
{
|
|
61
|
+
{t.columnsTrigger}
|
|
52
62
|
</Button>
|
|
53
63
|
}
|
|
54
64
|
/>
|
|
@@ -56,7 +66,7 @@ function DataTableViewOptions<TData>({
|
|
|
56
66
|
{/* Base UI ties `GroupLabel` to a `Group` via context: the heading names
|
|
57
67
|
the toggle group and errors if rendered outside one. */}
|
|
58
68
|
<DropdownMenuGroup>
|
|
59
|
-
<DropdownMenuLabel>{
|
|
69
|
+
<DropdownMenuLabel>{t.columnsHeading}</DropdownMenuLabel>
|
|
60
70
|
<DropdownMenuSeparator />
|
|
61
71
|
{hideableColumns.map((column) => (
|
|
62
72
|
<DropdownMenuCheckboxItem
|