@vendure-io/ui 3.0.0-next.4 → 3.0.0-next.5
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 +124 -49
- 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/chart.tsx +3 -3
- package/src/components/atoms/checkbox.tsx +1 -1
- package/src/components/atoms/combobox.tsx +37 -26
- package/src/components/atoms/command.tsx +3 -3
- 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 +9 -3
- 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 +200 -31
- package/src/components/atoms/skeleton.tsx +6 -3
- 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 +52 -8
- package/src/components/molecules/chip.tsx +37 -19
- package/src/components/molecules/choice-card.tsx +114 -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 +11 -1
- package/src/components/molecules/copyable-text.tsx +10 -8
- package/src/components/molecules/data-table/data-table-filters.tsx +14 -2
- 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 +123 -13
- package/src/components/molecules/data-table/data-table.tsx +109 -49
- package/src/components/molecules/data-table/table-pagination.tsx +96 -14
- package/src/components/molecules/date-picker.tsx +55 -24
- package/src/components/molecules/date-range-picker.tsx +54 -23
- package/src/components/molecules/date-time-picker.tsx +60 -24
- 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 +139 -39
- 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 +3 -107
- package/src/components/molecules/issued-secret.tsx +1 -1
- package/src/components/molecules/live-indicator.tsx +1 -1
- package/src/components/molecules/money.tsx +44 -3
- package/src/components/molecules/multi-select.tsx +56 -14
- 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 +10 -2
- package/src/components/molecules/stat-card.tsx +143 -33
- package/src/components/molecules/state-views/empty-state.tsx +16 -12
- package/src/components/molecules/state-views/error-state.tsx +19 -15
- package/src/components/molecules/state-views/loading-state.tsx +16 -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 +192 -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
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
useRef,
|
|
22
22
|
useState,
|
|
23
23
|
} from 'react';
|
|
24
|
+
import { useMessages } from '../../lib/format-messages.ts';
|
|
24
25
|
|
|
25
26
|
// The dialog's buttons show the ring on any focus, not only keyboard focus:
|
|
26
27
|
// the dialog moves focus itself when it opens, and after a mouse click some
|
|
@@ -67,6 +68,11 @@ interface ConfirmDialogProps {
|
|
|
67
68
|
onConfirm: () => void | Promise<void>;
|
|
68
69
|
confirmLabel?: ReactNode;
|
|
69
70
|
cancelLabel?: ReactNode;
|
|
71
|
+
/**
|
|
72
|
+
* Screen-reader text of the spinner while `onConfirm` runs.
|
|
73
|
+
* @default `messages.confirmDialog.loading`, "Loading"
|
|
74
|
+
*/
|
|
75
|
+
loadingLabel?: string;
|
|
70
76
|
/**
|
|
71
77
|
* `default`: Confirm is the filled primary and takes the focus.
|
|
72
78
|
* `caution`: a costly action that is not destructive ("Deploy anyway" after
|
|
@@ -114,6 +120,7 @@ function ConfirmDialog({
|
|
|
114
120
|
onConfirm,
|
|
115
121
|
confirmLabel = 'Confirm',
|
|
116
122
|
cancelLabel = 'Cancel',
|
|
123
|
+
loadingLabel,
|
|
117
124
|
variant = 'default',
|
|
118
125
|
confirmDisabled = false,
|
|
119
126
|
confirmDisabledReason,
|
|
@@ -123,6 +130,7 @@ function ConfirmDialog({
|
|
|
123
130
|
open,
|
|
124
131
|
onOpenChange,
|
|
125
132
|
}: ConfirmDialogProps) {
|
|
133
|
+
const t = useMessages('confirmDialog', { loading: loadingLabel });
|
|
126
134
|
const isControlled = open !== undefined;
|
|
127
135
|
const [internalOpen, setInternalOpen] = useState(false);
|
|
128
136
|
const [pending, setPending] = useState(false);
|
|
@@ -232,7 +240,9 @@ function ConfirmDialog({
|
|
|
232
240
|
<span data-slot="confirm-dialog-label" className={cn(pending && 'opacity-0')}>
|
|
233
241
|
{confirmLabel}
|
|
234
242
|
</span>
|
|
235
|
-
{pending ?
|
|
243
|
+
{pending ? (
|
|
244
|
+
<Spinner aria-label={t.loading} className="absolute inset-0 m-auto" />
|
|
245
|
+
) : null}
|
|
236
246
|
</AlertDialogAction>
|
|
237
247
|
</AlertDialogFooter>
|
|
238
248
|
</AlertDialogContent>
|
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
import { Button } from '@vendure-io/ui/components/atoms/button';
|
|
4
4
|
import { useCopyFeedback } from '@vendure-io/ui/components/molecules/copy-feedback-provider';
|
|
5
5
|
import { useCopy } from '@vendure-io/ui/hooks/use-copy';
|
|
6
|
-
import type { ClipboardSource } from '@vendure-io/ui/lib/clipboard';
|
|
7
6
|
import { cn } from '@vendure-io/ui/lib/utils';
|
|
8
7
|
import { CheckIcon, CopyIcon } from 'lucide-react';
|
|
9
8
|
import type * as React from 'react';
|
|
9
|
+
import type { ClipboardSource } from '../../lib/clipboard.ts';
|
|
10
|
+
import { useMessages } from '../../lib/format-messages.ts';
|
|
10
11
|
|
|
11
12
|
interface UseCopyActionOptions {
|
|
12
13
|
/** How long `copied` stays true after a successful copy, in ms. @default 2000 */
|
|
@@ -88,9 +89,9 @@ interface CopyButtonProps extends Omit<React.ComponentProps<typeof Button>, 'val
|
|
|
88
89
|
* Falls back to `CopyFeedbackProvider` when omitted.
|
|
89
90
|
*/
|
|
90
91
|
onCopyError?: (error: Error) => void;
|
|
91
|
-
/** Accessible label before copying. @default "Copy" */
|
|
92
|
+
/** Accessible label before copying. @default `messages.copyButton.copy`, "Copy" */
|
|
92
93
|
copyLabel?: string;
|
|
93
|
-
/** Accessible label shown while the copied state is active. @default "Copied" */
|
|
94
|
+
/** Accessible label shown while the copied state is active. @default `messages.copyButton.copied`, "Copied" */
|
|
94
95
|
copiedLabel?: string;
|
|
95
96
|
/**
|
|
96
97
|
* A shared copy state from `useCopyAction`. When set, the button uses it
|
|
@@ -109,8 +110,8 @@ function CopyButton({
|
|
|
109
110
|
timeout,
|
|
110
111
|
onCopied,
|
|
111
112
|
onCopyError,
|
|
112
|
-
copyLabel
|
|
113
|
-
copiedLabel
|
|
113
|
+
copyLabel,
|
|
114
|
+
copiedLabel,
|
|
114
115
|
variant = 'ghost',
|
|
115
116
|
size = 'icon-xs',
|
|
116
117
|
className,
|
|
@@ -120,6 +121,7 @@ function CopyButton({
|
|
|
120
121
|
}: CopyButtonProps) {
|
|
121
122
|
const ownAction = useCopyAction({ timeout, onCopied, onCopyError });
|
|
122
123
|
const { copied, copy } = copyAction ?? ownAction;
|
|
124
|
+
const t = useMessages('copyButton', { copy: copyLabel, copied: copiedLabel });
|
|
123
125
|
|
|
124
126
|
return (
|
|
125
127
|
<Button
|
|
@@ -127,7 +129,7 @@ function CopyButton({
|
|
|
127
129
|
data-slot="copy-button"
|
|
128
130
|
variant={variant}
|
|
129
131
|
size={size}
|
|
130
|
-
aria-label={copied ?
|
|
132
|
+
aria-label={copied ? t.copied : t.copy}
|
|
131
133
|
className={cn('text-muted-foreground hover:text-foreground', className)}
|
|
132
134
|
onClick={async (event) => {
|
|
133
135
|
onClick?.(event);
|
|
@@ -153,9 +155,9 @@ interface CopyableTextProps {
|
|
|
153
155
|
onCopied?: () => void;
|
|
154
156
|
/** Called when the clipboard write fails. Falls back to `CopyFeedbackProvider`. */
|
|
155
157
|
onCopyError?: (error: Error) => void;
|
|
156
|
-
/** Accessible label before copying, forwarded to the inner `CopyButton`. @default "Copy" */
|
|
158
|
+
/** Accessible label before copying, forwarded to the inner `CopyButton`. @default `messages.copyButton.copy`, "Copy" */
|
|
157
159
|
copyLabel?: string;
|
|
158
|
-
/** Accessible label shown while the copied state is active, forwarded to the inner `CopyButton`. @default "Copied" */
|
|
160
|
+
/** Accessible label shown while the copied state is active, forwarded to the inner `CopyButton`. @default `messages.copyButton.copied`, "Copied" */
|
|
159
161
|
copiedLabel?: string;
|
|
160
162
|
}
|
|
161
163
|
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
import { cn } from '@vendure-io/ui/lib/utils';
|
|
13
13
|
import { ChevronLeftIcon, ListFilterIcon } from 'lucide-react';
|
|
14
14
|
import * as React from 'react';
|
|
15
|
+
import { useMessages } from '../../../lib/format-messages.ts';
|
|
15
16
|
|
|
16
17
|
// The two filter surfaces. `DataTableAddFilter` is the core-owned column picker
|
|
17
18
|
// + value editor (rendered only when `filters.columns` is declared).
|
|
@@ -25,10 +26,12 @@ function DefaultFilterInput<TData>({
|
|
|
25
26
|
column,
|
|
26
27
|
value,
|
|
27
28
|
onChange,
|
|
29
|
+
label,
|
|
28
30
|
}: {
|
|
29
31
|
column: DataTableFilterColumn<TData>;
|
|
30
32
|
value: DataTableFilterValue | undefined;
|
|
31
33
|
onChange: (next: DataTableFilterValue | undefined) => void;
|
|
34
|
+
label: string;
|
|
32
35
|
}) {
|
|
33
36
|
const operator = column.operators?.[0] ?? 'contains';
|
|
34
37
|
const current = value?.[operator];
|
|
@@ -37,7 +40,8 @@ function DefaultFilterInput<TData>({
|
|
|
37
40
|
<Input
|
|
38
41
|
autoFocus
|
|
39
42
|
value={typeof current === 'string' ? current : ''}
|
|
40
|
-
placeholder=
|
|
43
|
+
placeholder={label}
|
|
44
|
+
aria-label={label}
|
|
41
45
|
onChange={(event) => {
|
|
42
46
|
const next = event.target.value;
|
|
43
47
|
onChange(next ? { [operator]: next } : undefined);
|
|
@@ -50,13 +54,20 @@ interface DataTableAddFilterProps<TData> {
|
|
|
50
54
|
table: Table<TData>;
|
|
51
55
|
columns: DataTableFilterColumn<TData>[];
|
|
52
56
|
label?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Placeholder and accessible name of the default value input.
|
|
59
|
+
* @default `messages.dataTable.filterValue`, "Value"
|
|
60
|
+
*/
|
|
61
|
+
valueLabel?: string;
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
function DataTableAddFilter<TData>({
|
|
56
65
|
table,
|
|
57
66
|
columns,
|
|
58
67
|
label = 'Add filter',
|
|
68
|
+
valueLabel,
|
|
59
69
|
}: DataTableAddFilterProps<TData>) {
|
|
70
|
+
const t = useMessages('dataTable', { filterValue: valueLabel });
|
|
60
71
|
const [open, setOpen] = React.useState(false);
|
|
61
72
|
const [activeId, setActiveId] = React.useState<string | null>(null);
|
|
62
73
|
const active = columns.find((column) => column.id === activeId) ?? null;
|
|
@@ -79,7 +90,7 @@ function DataTableAddFilter<TData>({
|
|
|
79
90
|
</Button>
|
|
80
91
|
}
|
|
81
92
|
/>
|
|
82
|
-
<PopoverContent align="start" className="w-64 gap-2 p-2">
|
|
93
|
+
<PopoverContent align="start" className="w-64 gap-2 p-2" aria-label={label}>
|
|
83
94
|
{active ? (
|
|
84
95
|
<div className="flex flex-col gap-2">
|
|
85
96
|
<button
|
|
@@ -101,6 +112,7 @@ function DataTableAddFilter<TData>({
|
|
|
101
112
|
column={active}
|
|
102
113
|
value={tableColumn?.getFilterValue() as DataTableFilterValue | undefined}
|
|
103
114
|
onChange={(next) => tableColumn?.setFilterValue(next)}
|
|
115
|
+
label={t.filterValue}
|
|
104
116
|
/>
|
|
105
117
|
)}
|
|
106
118
|
</div>
|
|
@@ -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
|
}
|