@tristan2828/ui-foundation 4.81.0 → 4.83.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/conventions/docs/add-an-entity.md +88 -24
- package/conventions/docs/cell-patterns.md +111 -50
- package/conventions/docs/entity-plan-template.md +3 -3
- package/dist/components/app/data-table.d.ts +36 -8
- package/dist/components/app/data-table.js +142 -90
- package/dist/components/app/editable-value.js +8 -10
- package/dist/components/app/form-dialog.d.ts +29 -0
- package/dist/components/app/form-dialog.js +42 -0
- package/dist/components/app/record-dialog-context.d.ts +17 -0
- package/dist/components/app/record-dialog-context.js +10 -0
- package/dist/components/app/record-dialog.d.ts +9 -2
- package/dist/components/app/record-dialog.js +36 -9
- package/dist/components/ui/badge.d.ts +1 -1
- package/dist/components/ui/dialog.d.ts +32 -3
- package/dist/components/ui/dialog.js +63 -6
- package/dist/hooks/use-table-url-state.d.ts +19 -1
- package/dist/hooks/use-table-url-state.js +31 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
- package/styles/theme.css +6 -0
|
@@ -22,6 +22,7 @@ import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuLabel
|
|
|
22
22
|
import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from '../ui/empty.js';
|
|
23
23
|
import { Sheet, SheetContent, SheetHeader, SheetTitle } from '../ui/sheet.js';
|
|
24
24
|
import { Skeleton } from '../ui/skeleton.js';
|
|
25
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from '../ui/tooltip.js';
|
|
25
26
|
import { ToggleGroup, ToggleGroupItem } from '../ui/toggle-group.js';
|
|
26
27
|
import { EditInPlaceContext } from './edit-in-place.js';
|
|
27
28
|
import { EditInPlaceStore } from './edit-in-place-store.js';
|
|
@@ -94,6 +95,40 @@ function GroupMenu({ groupings, value, onValueChange, }) {
|
|
|
94
95
|
const current = groupings.find((grouping) => grouping.id === value);
|
|
95
96
|
return (_jsxs(DropdownMenu, { children: [_jsxs(DropdownMenuTrigger, { render: _jsx(Button, { variant: "outline", size: "sm", "data-slot": "data-table-group-menu" }), children: [_jsx(ListTreeIcon, { "aria-hidden": "true" }), current ? `Group: ${current.label}` : 'Group'] }), _jsx(DropdownMenuContent, { align: "end", className: "w-48", children: _jsxs(DropdownMenuGroup, { children: [_jsx(DropdownMenuLabel, { children: "Group by" }), _jsxs(DropdownMenuRadioGroup, { value: current?.id ?? NO_GROUPING, onValueChange: (next) => onValueChange(next === NO_GROUPING ? '' : String(next)), children: [_jsx(DropdownMenuRadioItem, { value: NO_GROUPING, closeOnClick: true, children: "None" }), groupings.map((grouping) => (_jsx(DropdownMenuRadioItem, { value: grouping.id, closeOnClick: true, children: grouping.label }, grouping.id)))] })] }) })] }));
|
|
96
97
|
}
|
|
98
|
+
// The rows-per-page menu in the pagination bar (`pageSizes`): a radio list
|
|
99
|
+
// that closes on a pick. The button says the size in words of its own
|
|
100
|
+
// ("50 rows", as the list page standard drew it), and the menu says
|
|
101
|
+
// what it is.
|
|
102
|
+
function PageSizeMenu({ pageSizes, value, onValueChange, }) {
|
|
103
|
+
return (_jsxs(DropdownMenu, { children: [_jsxs(DropdownMenuTrigger, { render: _jsx(Button, { variant: "outline", size: "sm", "data-slot": "data-table-page-size", "aria-label": `${value} rows per page` }), children: [value, " rows", _jsx(ChevronDownIcon, { "aria-hidden": "true" })] }), _jsx(DropdownMenuContent, { align: "end", className: "w-40", children: _jsxs(DropdownMenuGroup, { children: [_jsx(DropdownMenuLabel, { children: "Rows per page" }), _jsx(DropdownMenuRadioGroup, { value: String(value), onValueChange: (next) => onValueChange(Number(next)), children: pageSizes.map((size) => (_jsx(DropdownMenuRadioItem, { value: String(size), closeOnClick: true, children: size }, size))) })] }) })] }));
|
|
104
|
+
}
|
|
105
|
+
// A cell's text cut to one line (`meta.clip`). Only when it is cut does it
|
|
106
|
+
// take a tab stop and a tooltip with the whole text, on hover and on focus;
|
|
107
|
+
// re-measured on every render (the text may change) and on every resize.
|
|
108
|
+
function ClippedText({ maxWidth, children }) {
|
|
109
|
+
const ref = useRef(null);
|
|
110
|
+
const [fullText, setFullText] = useState(null);
|
|
111
|
+
const measure = () => {
|
|
112
|
+
const element = ref.current;
|
|
113
|
+
if (!element)
|
|
114
|
+
return;
|
|
115
|
+
const isCut = element.scrollWidth > element.clientWidth + 0.5;
|
|
116
|
+
setFullText(isCut ? element.textContent?.trim() || null : null);
|
|
117
|
+
};
|
|
118
|
+
useLayoutEffect(measure);
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
const element = ref.current;
|
|
121
|
+
if (!element)
|
|
122
|
+
return;
|
|
123
|
+
const observer = new ResizeObserver(() => measure());
|
|
124
|
+
observer.observe(element);
|
|
125
|
+
return () => observer.disconnect();
|
|
126
|
+
}, []);
|
|
127
|
+
return (_jsxs(Tooltip, { disabled: fullText === null, children: [_jsx(TooltipTrigger, { render: _jsx("span", { ref: ref, "data-slot": "data-table-clip", className: CLIP_CLASS,
|
|
128
|
+
// Never more than 60% of the table's area, so on a phone the
|
|
129
|
+
// cut column leaves the name and the rest room.
|
|
130
|
+
style: { maxWidth: `min(${maxWidth}, 60cqw)` }, tabIndex: fullText === null ? undefined : 0 }), children: children }), _jsx(TooltipContent, { className: "max-w-sm break-words whitespace-normal", children: fullText })] }));
|
|
131
|
+
}
|
|
97
132
|
// Whether the detail panel (`detailPanel`) fits beside the table, or opens
|
|
98
133
|
// as a bottom sheet. Wide on the server and wherever matchMedia is missing
|
|
99
134
|
// (a test's DOM), so markup never depends on it.
|
|
@@ -121,44 +156,49 @@ function TableSkeleton({ columnCount }) {
|
|
|
121
156
|
// The pinned cell gets an opaque background, not `bg-inherit`: the zebra
|
|
122
157
|
// stripe (table.tsx) is a *translucent* color, so inheriting it would let
|
|
123
158
|
// the columns scrolling behind show through at 40% opacity — exactly the
|
|
124
|
-
// bleed-through a solid background avoids.
|
|
125
|
-
// pre-mixed onto the page (--table-stripe), so the stripe
|
|
126
|
-
// row's width (issue #92); `:where()` keeps that rule's
|
|
127
|
-
// background's, so the
|
|
128
|
-
const PINNED_CELL_CLASS = 'z-10 bg-background
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
// an inset shadow rather than a border, so nothing reflows.
|
|
132
|
-
const PINNED_COLUMN_CLASS = `sticky left-0 border-r ${PINNED_CELL_CLASS} group-hover/row:bg-accent ` +
|
|
133
|
-
'group-hover/row:shadow-[inset_3px_0_0_0_var(--primary)]';
|
|
134
|
-
// The right-edge mirror, opaque for the same reason, but with no hover
|
|
135
|
-
// highlight (issue #103): it holds the row's actions, which name nothing,
|
|
136
|
-
// so lighting it too read as a second selected thing. Only the first
|
|
137
|
-
// column marks the hovered row.
|
|
159
|
+
// bleed-through a solid background avoids. With `striped`, on an even row
|
|
160
|
+
// it's the stripe pre-mixed onto the page (--table-stripe), so the stripe
|
|
161
|
+
// runs the whole row's width (issue #92); `:where()` keeps that rule's
|
|
162
|
+
// specificity at the background's, so the row's hover below still wins.
|
|
163
|
+
const PINNED_CELL_CLASS = 'z-10 bg-background';
|
|
164
|
+
const PINNED_STRIPED_CELL_CLASS = '[:where(tbody>tr:nth-child(even))>&]:bg-(--table-stripe)';
|
|
165
|
+
const PINNED_COLUMN_CLASS = `sticky left-0 border-r ${PINNED_CELL_CLASS}`;
|
|
138
166
|
const PINNED_LAST_COLUMN_CLASS = `sticky right-0 border-l ${PINNED_CELL_CLASS}`;
|
|
139
167
|
// The totals row's label, pinned with the first column (`totals`): the
|
|
140
168
|
// same opaque cell, with no hover, since the row isn't one to pick.
|
|
141
|
-
const PINNED_TOTALS_LABEL_CLASS =
|
|
169
|
+
const PINNED_TOTALS_LABEL_CLASS = PINNED_COLUMN_CLASS;
|
|
170
|
+
// `striped` off (the default, 4.83): table.tsx's zebra taken back off the
|
|
171
|
+
// body (tailwind-merge drops its even-row tint for this one), so rows are
|
|
172
|
+
// told apart by the line under each.
|
|
173
|
+
const UNSTRIPED_BODY_CLASS = '[&_tr:nth-child(even)]:bg-transparent';
|
|
174
|
+
// A body row under the pointer (4.83): every cell, pinned ones included,
|
|
175
|
+
// in --table-hover (solid, so a pinned cell can wear it), with a bar in
|
|
176
|
+
// --primary on the first cell, so it reads without colour too; an inset
|
|
177
|
+
// shadow rather than a border, so nothing reflows. On a cell, not the row,
|
|
178
|
+
// so it covers a stripe and a pinned cell's opaque background alike
|
|
179
|
+
// (`.row:hover > td` outranks both). Muted text on it takes the darker grey
|
|
180
|
+
// a highlighted row's does (theme.css --table-hover). table.tsx's own row
|
|
181
|
+
// hover comes off: it would sit under the cells' tint anyway. A row that
|
|
182
|
+
// opens a record adds the hand cursor (SELECTABLE_ROW_CLASS); one edited in
|
|
183
|
+
// place keeps the arrow. A highlighted or picked row keeps its own look.
|
|
184
|
+
const HOVER_ROW_CLASS = 'hover:bg-transparent hover:[--muted-foreground:var(--table-highlight-muted-foreground)] ' +
|
|
185
|
+
'hover:[&>td]:bg-(--table-hover) hover:[&>td:first-child]:shadow-[inset_3px_0_0_0_var(--primary)]';
|
|
186
|
+
// Its bar and a focused row's ring, both on the first cell's shadow.
|
|
187
|
+
const HOVER_FOCUSED_ROW_CLASS = 'focus-visible:hover:[&>td:first-child]:shadow-[inset_3px_0_0_0_var(--primary),inset_0_2px_0_0_var(--ring),inset_0_-2px_0_0_var(--ring)]';
|
|
188
|
+
// A column's `meta.clip`: one line, cut at `meta.maxWidth` with an ellipsis.
|
|
189
|
+
const DEFAULT_CLIP_WIDTH = '24rem';
|
|
190
|
+
const CLIP_CLASS = 'block truncate rounded-sm outline-none focus-visible:ring-3 focus-visible:ring-ring/50';
|
|
142
191
|
// A pinned first column's content, capped below md at 45% of the table's
|
|
143
192
|
// scroll area (`cqw`: the container is an inline-size query container)
|
|
144
193
|
// and wrapping, so a long title can't widen the column past the screen
|
|
145
194
|
// and leave every other column scrolling underneath it (issue #91).
|
|
146
195
|
// Wider screens are unchanged: the column sizes to its content.
|
|
147
196
|
const PINNED_FIRST_CONTENT_CLASS = 'max-md:max-w-[45cqw] max-md:whitespace-normal max-md:break-words';
|
|
148
|
-
// Hover highlights the pinned cell, not the whole row. A row-level hover
|
|
149
|
-
// cannot be made consistent against the zebra stripe: `tr:nth-child(even)`
|
|
150
|
-
// is specificity (0,2,1) and `tr:hover` is (0,2,0), so the stripe wins and
|
|
151
|
-
// even rows stay unlit — odd rows highlight, even rows don't.
|
|
152
|
-
// `hover:bg-transparent` drops table.tsx's own `hover:bg-muted/50` via
|
|
153
|
-
// tailwind-merge so no row hover is attempted at all. The pinned cell is
|
|
154
|
-
// the better target anyway: it names the row, it sits on an opaque
|
|
155
|
-
// background so it reads identically on striped and unstriped rows, and it
|
|
156
|
-
// stays on screen when the table is scrolled right.
|
|
157
|
-
const ROW_GROUP_CLASS = 'group/row hover:bg-transparent';
|
|
158
197
|
// `highlightRow`: the wash on every cell, so it covers the zebra stripe
|
|
159
198
|
// (on the row) and the pinned cells' opaque backgrounds (on the cell, and
|
|
160
|
-
// one class weaker than this)
|
|
161
|
-
//
|
|
199
|
+
// one class weaker than this); hover leaves it as it is, so "next up" never
|
|
200
|
+
// reads as the pointer's row. A bar in info-text on the first cell, as
|
|
201
|
+
// hover's is in --primary, so it reads without colour too.
|
|
162
202
|
// Inside, muted text takes the darker grey the wash needs (theme.css).
|
|
163
203
|
const HIGHLIGHTED_ROW_CLASS = '[--muted-foreground:var(--table-highlight-muted-foreground)] [&>td]:bg-(--table-highlight) ' +
|
|
164
204
|
'[&>td:first-child]:shadow-[inset_3px_0_0_0_var(--info-text)]';
|
|
@@ -225,12 +265,13 @@ const WITH_DETAIL_OPEN_CLASS = 'grid-cols-[minmax(0,1fr)_minmax(18rem,26rem)]';
|
|
|
225
265
|
const DETAIL_ASIDE_CLASS = 'sticky top-4 flex max-h-[calc(100dvh-2rem)] min-w-0 flex-col gap-3 overflow-y-auto rounded-lg border ' +
|
|
226
266
|
'border-border bg-card p-4 text-card-foreground';
|
|
227
267
|
const EMPTY_KEYS = new Set();
|
|
228
|
-
// `width="content"`:
|
|
229
|
-
// need (`w-fit` over the table's own `w-full`), at least
|
|
230
|
-
// width it last had loaded while it shows another state
|
|
231
|
-
// (--data-table-last-width),
|
|
232
|
-
|
|
233
|
-
|
|
268
|
+
// `width="content"`: the table and its pagination (not the toolbar) as wide
|
|
269
|
+
// as the columns need (`w-fit` over the table's own `w-full`), at least
|
|
270
|
+
// 36rem, or the width it last had loaded while it shows another state
|
|
271
|
+
// (--data-table-last-width), and never wider than the area: `min()` keeps
|
|
272
|
+
// the minimum from pushing past a phone's width, since a min-width
|
|
273
|
+
// outranks a max-width.
|
|
274
|
+
const CONTENT_WIDTH_CLASS = 'w-fit max-w-full min-w-[min(100%,max(--spacing(144),var(--data-table-last-width,0px)))]';
|
|
234
275
|
// A second horizontal scrollbar, stuck to the viewport's bottom edge. The
|
|
235
276
|
// real one sits directly under the last row, which on a full page is
|
|
236
277
|
// off-screen until you have already scrolled past every row. Only rendered
|
|
@@ -320,7 +361,7 @@ function useStickyHeadings(containerRef, enabled) {
|
|
|
320
361
|
}, [containerRef, enabled]);
|
|
321
362
|
return { scrollsSideways, headHeight };
|
|
322
363
|
}
|
|
323
|
-
export function DataTable({ columns, data, total, page, pageSize, onPageChange, sorting, onSortingChange, isLoading, error, onRetry, emptyTitle, emptyDescription, emptyAction, toolbar, getRowId, pinFirstColumn, pinLastColumn, enableSortingRemoval, visibleColumns, width = '
|
|
364
|
+
export function DataTable({ columns, data, total, page, pageSize, onPageChange, pageSizes, onPageSizeChange, sorting, onSortingChange, isLoading, error, onRetry, emptyTitle, emptyDescription, emptyAction, toolbar, getRowId, pinFirstColumn, pinLastColumn, enableSortingRemoval, visibleColumns, width = 'content', striped = false, groupBy, groupSummary, stickyGroupHeadings = false, stickyTop = '0px', header: showHeader = true, highlightRow, totals, layouts, layout: layoutProp, onLayoutChange, tableLayout, groupings, grouping: groupingProp, onGroupingChange, opens, detailPanel, onRowClick: onRowClickProp, detailRow, onDetailRowChange, detailLabel = 'Details', }) {
|
|
324
365
|
// The switch's own state, for a table that doesn't pass `layout`.
|
|
325
366
|
const [ownLayout, setOwnLayout] = useState('table');
|
|
326
367
|
const requestedLayout = layoutProp ?? ownLayout;
|
|
@@ -429,16 +470,19 @@ export function DataTable({ columns, data, total, page, pageSize, onPageChange,
|
|
|
429
470
|
const isSuccessView = !error && !isLoading && !isPastLastPage && !isEmpty;
|
|
430
471
|
// `width="content"`: the width the table last had loaded, kept while it
|
|
431
472
|
// shows another state.
|
|
473
|
+
// A card layout's grid takes the whole width whatever `width` says.
|
|
474
|
+
const fitsColumns = width === 'content' && !cardLayout;
|
|
432
475
|
const root = useRef(null);
|
|
476
|
+
const body = useRef(null);
|
|
433
477
|
const [lastWidth, setLastWidth] = useState(null);
|
|
434
478
|
useLayoutEffect(() => {
|
|
435
|
-
const element =
|
|
436
|
-
if (
|
|
479
|
+
const element = body.current;
|
|
480
|
+
if (!fitsColumns || !isSuccessView || !element)
|
|
437
481
|
return;
|
|
438
482
|
const observer = new ResizeObserver(() => setLastWidth(Math.round(element.getBoundingClientRect().width)));
|
|
439
483
|
observer.observe(element);
|
|
440
484
|
return () => observer.disconnect();
|
|
441
|
-
}, [
|
|
485
|
+
}, [fitsColumns, isSuccessView]);
|
|
442
486
|
const containerRef = useRef(null);
|
|
443
487
|
const { barRef, isOverflowing, scrollWidth } = useBottomScrollbar(containerRef, Boolean(pinFirstColumn) && isSuccessView && !cardLayout);
|
|
444
488
|
const rows = table.getRowModel().rows;
|
|
@@ -577,68 +621,76 @@ export function DataTable({ columns, data, total, page, pageSize, onPageChange,
|
|
|
577
621
|
// One body row, the same in a plain table and a grouped one.
|
|
578
622
|
const renderRow = (row, key = row.id) => {
|
|
579
623
|
const isSelected = Boolean(detailPanel) && key === selectedKey;
|
|
580
|
-
|
|
624
|
+
const isHighlighted = Boolean(highlightRow?.(row.original));
|
|
625
|
+
const hovers = !isHighlighted && !isSelected;
|
|
626
|
+
return (_jsx(TableRow, { "data-highlighted": isHighlighted || undefined, ...(rowsOpen
|
|
581
627
|
? {
|
|
582
628
|
'data-row-key': key,
|
|
583
629
|
...(detailPanel ? { 'data-selected': isSelected || undefined, 'aria-selected': isSelected } : {}),
|
|
584
630
|
tabIndex: key === tabStopKey ? 0 : -1,
|
|
585
631
|
onClickCapture: (event) => onRowClick(event, { key, rowId: row.id }),
|
|
586
632
|
}
|
|
587
|
-
: {}), className: cn(
|
|
633
|
+
: {}), className: cn('hover:bg-transparent', hovers && HOVER_ROW_CLASS, isHighlighted && HIGHLIGHTED_ROW_CLASS, rowsOpen && SELECTABLE_ROW_CLASS, rowsOpen && hovers && HOVER_FOCUSED_ROW_CLASS, isSelected && SELECTED_ROW_CLASS), children: row.getVisibleCells().map((cell, index) => {
|
|
634
|
+
const meta = cell.column.columnDef.meta;
|
|
635
|
+
const value = flexRender(cell.column.columnDef.cell, cell.getContext());
|
|
636
|
+
const shown = meta?.clip ? (_jsx(ClippedText, { maxWidth: meta.maxWidth ?? DEFAULT_CLIP_WIDTH, children: value })) : meta?.maxWidth ? (_jsx("div", { className: "whitespace-normal break-words", style: { maxWidth: meta.maxWidth }, children: value })) : (value);
|
|
637
|
+
const isPinnedFirst = pinFirstColumn && index === 0;
|
|
638
|
+
const isPinnedLast = pinLastColumn && index === row.getVisibleCells().length - 1;
|
|
639
|
+
return (_jsx(TableCell, { className: cn(isPinnedFirst && PINNED_COLUMN_CLASS, isPinnedLast && PINNED_LAST_COLUMN_CLASS, (isPinnedFirst || isPinnedLast) && striped && PINNED_STRIPED_CELL_CLASS, alignClass(meta?.align)), children: isPinnedFirst ? _jsx("div", { className: PINNED_FIRST_CONTENT_CLASS, children: shown }) : shown }, cell.id));
|
|
640
|
+
}) }, key));
|
|
588
641
|
};
|
|
589
|
-
const content = (_jsxs("div", { ref: root, className:
|
|
590
|
-
? { '--data-table-last-width': `${lastWidth}px` }
|
|
591
|
-
: undefined, children: [layouts?.length || groupings?.length ? (_jsxs("div", { className: "flex flex-wrap items-end gap-x-4 gap-y-2", children: [_jsx("div", { className: "min-w-0 flex-1", children: toolbar }), groupings?.length ? (_jsxs("div", { className: "flex items-center gap-2", children: [activeGrouping && (_jsx(Button, { variant: "ghost", size: "sm", "data-slot": "data-table-fold-all", disabled: !runs?.length, onClick: foldAll, children: allFolded ? 'Unfold all' : 'Fold all' })), _jsx(GroupMenu, { groupings: groupings, value: activeGrouping?.id ?? '', onValueChange: setGrouping })] })) : null, layouts?.length ? (_jsx(LayoutSwitch, { options: [
|
|
642
|
+
const content = (_jsxs("div", { ref: root, className: "flex flex-col gap-4", "data-width": width, "data-layout": layouts?.length ? currentLayout : undefined, children: [layouts?.length || groupings?.length ? (_jsxs("div", { className: "flex flex-wrap items-end gap-x-4 gap-y-2", children: [_jsx("div", { className: "min-w-0 flex-1", children: toolbar }), groupings?.length ? (_jsxs("div", { className: "flex items-center gap-2", children: [activeGrouping && (_jsx(Button, { variant: "ghost", size: "sm", "data-slot": "data-table-fold-all", disabled: !runs?.length, onClick: foldAll, children: allFolded ? 'Unfold all' : 'Fold all' })), _jsx(GroupMenu, { groupings: groupings, value: activeGrouping?.id ?? '', onValueChange: setGrouping })] })) : null, layouts?.length ? (_jsx(LayoutSwitch, { options: [
|
|
592
643
|
{ id: 'table', label: tableLayout?.label ?? 'Table', icon: tableLayout?.icon ?? TableIcon },
|
|
593
644
|
...layouts,
|
|
594
|
-
], value: currentLayout, onValueChange: setLayout })) : null] })) : (toolbar),
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
645
|
+
], value: currentLayout, onValueChange: setLayout })) : null] })) : (toolbar), _jsx("div", { ref: body, "data-slot": "data-table-body", className: cn('flex flex-col gap-4', fitsColumns && CONTENT_WIDTH_CLASS), style: fitsColumns && !isSuccessView && lastWidth !== null
|
|
646
|
+
? { '--data-table-last-width': `${lastWidth}px` }
|
|
647
|
+
: undefined, children: error ? (_jsx(ErrorState, { error: error, onRetry: onRetry })) : isLoading || isPastLastPage ? (cardLayout ? (_jsx(CardsSkeleton, { layout: cardLayout })) : (_jsx(TableSkeleton, { columnCount: table.getVisibleLeafColumns().length }))) : isEmpty ? (_jsxs(Empty, { "data-state": "empty", children: [_jsxs(EmptyHeader, { children: [_jsx(EmptyTitle, { children: emptyTitle }), emptyDescription && _jsx(EmptyDescription, { children: emptyDescription })] }), emptyAction] })) : (_jsxs(DetailFrame, { enabled: Boolean(detailPanel) && !cardLayout, aside: selectedRow && detailPanel && isWide ? (_jsxs("aside", { "aria-label": detailLabel, "data-slot": "data-table-detail", className: DETAIL_ASIDE_CLASS, onKeyDown: (event) => {
|
|
648
|
+
const target = event.target;
|
|
649
|
+
if (event.key !== 'Escape' || event.defaultPrevented || target.closest(TYPING_SELECTOR))
|
|
650
|
+
return;
|
|
651
|
+
event.preventDefault();
|
|
652
|
+
closeDetail();
|
|
653
|
+
}, children: [_jsxs("div", { className: "flex items-center justify-between gap-2", children: [_jsx("h2", { className: "type-label text-muted-foreground", children: detailLabel }), _jsx(Button, { variant: "ghost", size: "icon-sm", "aria-label": "Close", onClick: closeDetail, children: _jsx(XIcon, { "aria-hidden": "true" }) })] }), detailPanel(selectedRow.original)] })) : null, children: [cardLayout ? (_jsx(Cards, { layout: cardLayout, runs: runs, rows: rows, folded: folded, onToggleFold: toggleFold, groupSummary: groupSummary, groupIcons: groupIcons, onCardClick: onCardClick })) : (_jsxs(Table, { "data-state": "success", containerRef: containerRef, onKeyDown: rowsOpen ? onListKeyDown : undefined, className: cn(stickyGroups && STICKY_FOCUS_CLASS), containerStyle: stickyGroups
|
|
654
|
+
? {
|
|
655
|
+
'--data-table-head-height': `${headHeight}px`,
|
|
656
|
+
'--data-table-sticky-top': stickyTop,
|
|
657
|
+
'--data-table-pin-top': scrollsSideways ? '0px' : stickyTop,
|
|
658
|
+
}
|
|
659
|
+
: undefined,
|
|
660
|
+
// With the sticky bar below as the visible scrollbar, hide the
|
|
661
|
+
// container's own. It stays overflow-x-auto and fully
|
|
662
|
+
// scrollable (wheel, trackpad, drag, and the sync effect); it
|
|
663
|
+
// just no longer draws a bar directly under the last row.
|
|
664
|
+
containerClassName: cn(
|
|
665
|
+
// A size container has no width of its own to give, so
|
|
666
|
+
// `width="content"` couldn't fit the table: there the
|
|
667
|
+
// pinned column's `cqw` measures the table's area
|
|
668
|
+
// (`@container/data-table`) instead.
|
|
669
|
+
pinFirstColumn && width !== 'content' && '@container', pinFirstColumn && '[scrollbar-width:none] [&::-webkit-scrollbar]:hidden', stickyGroups && (scrollsSideways ? STICKY_OWN_SCROLL_CLASS : STICKY_PAGE_SCROLL_CLASS)), children: [_jsx(TableHeader, { className: cn((!showHeader || stickyGroups) && '[&_tr]:border-0'), children: table.getHeaderGroups().map((headerGroup) => (_jsx(TableRow, { children: headerGroup.headers.map((header, index) => {
|
|
670
|
+
const canSort = header.column.getCanSort();
|
|
671
|
+
const sortDirection = header.column.getIsSorted();
|
|
672
|
+
// Hidden, a header is its name only: a sort button no
|
|
673
|
+
// one can see would still take focus.
|
|
674
|
+
const content = header.isPlaceholder ? null : canSort && showHeader ? (_jsxs("button", { type: "button", className: cn('flex items-center gap-1 text-foreground', header.column.columnDef.meta?.align === 'center' && 'w-full justify-center',
|
|
675
|
+
// The arrow on the label's left, so the label's
|
|
676
|
+
// right edge lines up with the numbers under it.
|
|
677
|
+
header.column.columnDef.meta?.align === 'right' && 'ml-auto flex-row-reverse'), onClick: header.column.getToggleSortingHandler(), children: [flexRender(header.column.columnDef.header, header.getContext()), sortDirection === 'asc' ? (_jsx(ArrowUpIcon, { className: "size-3.5 text-muted-foreground" })) : sortDirection === 'desc' ? (_jsx(ArrowDownIcon, { className: "size-3.5 text-muted-foreground" })) : (_jsx(ArrowUpDownIcon, { className: "size-3.5 text-muted-foreground/50" }))] })) : (flexRender(header.column.columnDef.header, header.getContext()));
|
|
678
|
+
return (_jsx(TableHead, { className: cn(!showHeader && HIDDEN_HEAD_CLASS, pinFirstColumn && index === 0 && PINNED_COLUMN_CLASS, pinLastColumn &&
|
|
679
|
+
index === headerGroup.headers.length - 1 &&
|
|
680
|
+
PINNED_LAST_COLUMN_CLASS, stickyGroups && showHeader && STICKY_HEAD_CELL_CLASS, alignClass(header.column.columnDef.meta?.align)), "aria-sort": !canSort
|
|
681
|
+
? undefined
|
|
682
|
+
: sortDirection === 'asc'
|
|
683
|
+
? 'ascending'
|
|
684
|
+
: sortDirection === 'desc'
|
|
685
|
+
? 'descending'
|
|
686
|
+
: 'none', children: !showHeader ? (_jsx("span", { className: "sr-only", children: content })) : pinFirstColumn && index === 0 ? (_jsx("div", { className: PINNED_FIRST_CONTENT_CLASS, children: content })) : (content) }, header.id));
|
|
687
|
+
}) }, headerGroup.id))) }), runs ? (runs.map((run, runIndex) => {
|
|
688
|
+
const isFolded = folded.has(run.key);
|
|
689
|
+
return (_jsxs(TableBody, { "data-slot": "table-group", "data-folded": isFolded || undefined, className: cn(!striped && UNSTRIPED_BODY_CLASS), children: [_jsx(TableRow, { "data-slot": "table-group-heading", className: cn('hover:bg-transparent has-aria-expanded:bg-transparent', stickyGroups && 'border-b-0'), children: _jsx(TableHead, { scope: "rowgroup", colSpan: visibleColumnCount, className: cn(GROUP_HEADING_CLASS, stickyGroups && STICKY_GROUP_HEADING_CLASS), children: _jsxs("span", { className: "sticky left-(--table-cell-px) inline-flex items-baseline gap-2", children: [_jsx(GroupLabel, { run: run, icon: groupIcons?.get(run.key), isFolded: isFolded, onToggle: () => toggleFold(run.key) }), groupSummary && (_jsx("span", { className: "type-caption text-muted-foreground tabular-nums", children: groupSummary(run.rows.map((row) => row.original), { key: run.key, label: run.label }) }))] }) }) }), _jsx("tr", { hidden: true, "aria-hidden": "true", "data-slot": "table-group-stripe-offset" }), !isFolded && run.rows.map((row) => renderRow(row, placeKey(run.key, row.id)))] }, `${runIndex}:${run.key}`));
|
|
690
|
+
})) : (_jsx(TableBody, { className: cn(!striped && UNSTRIPED_BODY_CLASS), children: rows.map((row) => renderRow(row)) })), totals && (_jsx(TableFooter, { className: TOTALS_FOOTER_CLASS, children: _jsx(TableRow, { "data-slot": "table-totals", className: "hover:bg-transparent", children: visibleLeafColumns.map((column, index) => {
|
|
691
|
+
const className = cn(pinFirstColumn && index === 0 && PINNED_TOTALS_LABEL_CLASS, pinLastColumn && index === visibleColumnCount - 1 && PINNED_LAST_COLUMN_CLASS, alignClass(column.columnDef.meta?.align));
|
|
692
|
+
return index === 0 ? (_jsx(TableHead, { scope: "row", className: cn(TOTALS_LABEL_CLASS, className), children: pinFirstColumn ? (_jsx("div", { className: PINNED_FIRST_CONTENT_CLASS, children: totals.label })) : (totals.label) }, column.id)) : (_jsx(TableCell, { className: className, children: totals.cells?.[column.id] }, column.id));
|
|
693
|
+
}) }) }))] })), pinFirstColumn && isOverflowing && !cardLayout && (_jsx("div", { ref: barRef, "aria-hidden": "true", tabIndex: -1, "data-slot": "bottom-scrollbar", className: "sticky bottom-0 z-20 h-4 overflow-x-auto border-t border-border bg-background", children: _jsx("div", { style: { width: scrollWidth, height: 1 } }) })), _jsxs("div", { className: "flex flex-wrap items-center justify-between gap-x-4 gap-y-2", "data-slot": "data-table-pagination", children: [_jsxs("p", { className: "type-body text-muted-foreground", children: [total === 0 ? 0 : (page - 1) * pageSize + 1, '–', Math.min(page * pageSize, total), " of ", total] }), _jsxs("div", { className: "flex items-center gap-2", children: [pageSizes && pageSizes.length > 1 && onPageSizeChange ? (_jsx(PageSizeMenu, { pageSizes: pageSizes, value: pageSize, onValueChange: onPageSizeChange })) : null, _jsx(Button, { variant: "outline", size: "icon-sm", "aria-label": "Previous page", disabled: page <= 1, onClick: () => onPageChange(page - 1), children: _jsx(ChevronLeftIcon, {}) }), _jsxs("span", { className: "type-body text-muted-foreground", children: ["Page ", page, " of ", pageCount] }), _jsx(Button, { variant: "outline", size: "icon-sm", "aria-label": "Next page", disabled: page >= pageCount, onClick: () => onPageChange(page + 1), children: _jsx(ChevronRightIcon, {}) })] })] })] })) })] }));
|
|
642
694
|
return (_jsxs(EditInPlaceContext.Provider, { value: store, children: [editing && _jsx(LeaveGuard, { store: store }), width === 'content' ? _jsx("div", { className: "@container/data-table", children: content }) : content, detailPanel && (_jsx(Sheet, { open: Boolean(selectedRow) && !isWide && isSuccessView && !cardLayout, onOpenChange: (open) => {
|
|
643
695
|
if (!open)
|
|
644
696
|
setSelection(null);
|
|
@@ -23,7 +23,7 @@ import { useCallback, useContext, useEffect, useId, useLayoutEffect, useRef, use
|
|
|
23
23
|
import { useBlocker } from 'react-router';
|
|
24
24
|
import { cn } from 'cn';
|
|
25
25
|
import { Button } from '../ui/button.js';
|
|
26
|
-
import {
|
|
26
|
+
import { DiscardChangesDialog } from '../ui/dialog.js';
|
|
27
27
|
import { FieldError } from '../ui/field.js';
|
|
28
28
|
import { Spinner } from '../ui/spinner.js';
|
|
29
29
|
import { EditInPlaceContext } from './edit-in-place.js';
|
|
@@ -278,16 +278,14 @@ export function LeaveGuard({ store }) {
|
|
|
278
278
|
document.removeEventListener('visibilitychange', onVisibilityChange);
|
|
279
279
|
};
|
|
280
280
|
}, [dirty, store]);
|
|
281
|
-
return (_jsx(
|
|
282
|
-
if (open)
|
|
283
|
-
return;
|
|
281
|
+
return (_jsx(DiscardChangesDialog, { open: blocker.state === 'blocked' && (!savesOnLeave || saveFailed), description: "A field you're editing hasn't been saved. Leaving now loses what you typed.", "data-leave-guard": "", onKeepEditing: () => {
|
|
284
282
|
setSaveFailed(false);
|
|
285
283
|
if (blocker.state === 'blocked')
|
|
286
284
|
blocker.reset();
|
|
287
|
-
},
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
285
|
+
}, onDiscard: () => {
|
|
286
|
+
store.discard();
|
|
287
|
+
setSaveFailed(false);
|
|
288
|
+
if (blocker.state === 'blocked')
|
|
289
|
+
blocker.proceed();
|
|
290
|
+
} }));
|
|
293
291
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type FormEvent, type ReactNode } from 'react';
|
|
2
|
+
import type { AppError } from '../../api/contracts.js';
|
|
3
|
+
export type FormDialogProps = {
|
|
4
|
+
/** "New book": the dialog's name. */
|
|
5
|
+
title: string;
|
|
6
|
+
/** One short line under the title, only where the title can't say it. */
|
|
7
|
+
description?: ReactNode;
|
|
8
|
+
/**
|
|
9
|
+
* Whether it's open. Leave it out inside RecordDialogRoute's `newPath`
|
|
10
|
+
* route, which opens it; anywhere else it's closed until it's given.
|
|
11
|
+
*/
|
|
12
|
+
open?: boolean;
|
|
13
|
+
/** Asked to close (Esc, the close button, a click outside, Cancel). */
|
|
14
|
+
onOpenChange?: (open: boolean) => void;
|
|
15
|
+
onSubmit: (event: FormEvent<HTMLFormElement>) => void;
|
|
16
|
+
isSubmitting: boolean;
|
|
17
|
+
/** "Add book": the word Add on every create form. */
|
|
18
|
+
submitLabel: string;
|
|
19
|
+
/** A save that failed: a Notice above the buttons, with Try again. */
|
|
20
|
+
submitError?: AppError | null;
|
|
21
|
+
/** Try again's action. Defaults to submitting the form again. */
|
|
22
|
+
onRetry?: () => void;
|
|
23
|
+
/** Something was typed: closing asks "Discard your changes?" first. */
|
|
24
|
+
dirty?: boolean;
|
|
25
|
+
/** The fields. */
|
|
26
|
+
children: ReactNode;
|
|
27
|
+
className?: string;
|
|
28
|
+
};
|
|
29
|
+
export declare function FormDialog({ title, description, open, onOpenChange, onSubmit, isSubmitting, submitLabel, submitError, onRetry, dirty, children, className, }: FormDialogProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// A small form in a dialog (4.82): a new record of up to about eight
|
|
3
|
+
// fields over its list, or any short form a page opens (a weak spot, a
|
|
4
|
+
// quick add). The title and the buttons stay put and the fields scroll
|
|
5
|
+
// between them; 640px wide (DialogContent `size="form"`), the whole screen
|
|
6
|
+
// on a phone, with Cancel and the submit button as two wide buttons.
|
|
7
|
+
//
|
|
8
|
+
// Two ways to open one:
|
|
9
|
+
//
|
|
10
|
+
// - Anywhere, with `open` and `onOpenChange`, as any dialog.
|
|
11
|
+
// - As RecordDialogRoute's `newPath` route (`/widgets/new`), with neither:
|
|
12
|
+
// the route opens it over the list and closing goes back, as a record's
|
|
13
|
+
// dialog does. After the save, `useNewRecordDialog().openRecord(to)`
|
|
14
|
+
// opens the new record over the same list.
|
|
15
|
+
//
|
|
16
|
+
// `dirty` (react-hook-form's `formState.isDirty`) makes Esc, the close
|
|
17
|
+
// button, a click outside and Cancel ask "Discard your changes?" first.
|
|
18
|
+
// A failed save is a Notice above the buttons with Try again, which
|
|
19
|
+
// submits again unless `onRetry` says otherwise; what was typed stays.
|
|
20
|
+
//
|
|
21
|
+
// The form is the app's: its fields, its schema and its react-hook-form
|
|
22
|
+
// wiring, as with EntityForm. This draws the chrome around them.
|
|
23
|
+
import { useId, useRef } from 'react';
|
|
24
|
+
import { cn } from 'cn';
|
|
25
|
+
import { Button } from '../ui/button.js';
|
|
26
|
+
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogTitle } from '../ui/dialog.js';
|
|
27
|
+
import { FieldGroup } from '../ui/field.js';
|
|
28
|
+
import { Spinner } from '../ui/spinner.js';
|
|
29
|
+
import { Notice } from './notice.js';
|
|
30
|
+
import { useNewRecordDialog } from './record-dialog-context.js';
|
|
31
|
+
export function FormDialog({ title, description, open, onOpenChange, onSubmit, isSubmitting, submitLabel, submitError, onRetry, dirty = false, children, className, }) {
|
|
32
|
+
const route = useNewRecordDialog();
|
|
33
|
+
const form = useRef(null);
|
|
34
|
+
const formId = useId();
|
|
35
|
+
const isOpen = open ?? route !== null;
|
|
36
|
+
return (_jsx(Dialog, { open: isOpen, onOpenChange: (next) => {
|
|
37
|
+
if (onOpenChange)
|
|
38
|
+
onOpenChange(next);
|
|
39
|
+
else if (!next)
|
|
40
|
+
route?.close();
|
|
41
|
+
}, children: _jsxs(DialogContent, { size: "form", dirty: dirty, "data-slot": "form-dialog", className: cn('gap-0 p-0', className), children: [_jsxs("div", { "data-slot": "form-dialog-header", className: "flex flex-col gap-1 px-4 pt-4 pb-2 pr-12 md:px-6 md:pt-5 md:pr-14", children: [_jsx(DialogTitle, { className: "type-section-title", children: title }), description && _jsx(DialogDescription, { children: description })] }), _jsxs("form", { ref: form, id: formId, onSubmit: onSubmit, noValidate: true, className: "flex min-h-0 flex-1 flex-col", children: [_jsx("div", { "data-slot": "form-dialog-body", className: "min-h-0 flex-1 overflow-y-auto px-4 py-4 md:px-6", children: _jsx(FieldGroup, { children: children }) }), _jsxs("div", { "data-slot": "form-dialog-bar", className: "flex flex-col gap-3 border-t px-4 py-3 pb-[calc(0.75rem+env(safe-area-inset-bottom))] md:px-6 md:pb-3", children: [_jsx("div", { role: "status", className: "empty:hidden", children: submitError && (_jsx(Notice, { tone: "destructive", action: _jsx(Button, { type: "button", size: "sm", variant: "outline", disabled: isSubmitting, onClick: () => (onRetry ? onRetry() : form.current?.requestSubmit()), children: "Try again" }), children: submitError.message })) }), _jsxs("div", { className: "flex justify-end gap-2 max-md:[&>*]:h-10 max-md:[&>*]:flex-1", children: [_jsx(DialogClose, { render: _jsx(Button, { type: "button", variant: "outline", disabled: isSubmitting }), children: "Cancel" }), _jsxs(Button, { type: "submit", disabled: isSubmitting, children: [isSubmitting && _jsx(Spinner, { className: "size-4" }), submitLabel] })] })] })] })] }) }));
|
|
42
|
+
}
|
|
@@ -37,3 +37,20 @@ export declare const RecordDialogContext: import("react").Context<RecordDialogCo
|
|
|
37
37
|
* on the record's full page.
|
|
38
38
|
*/
|
|
39
39
|
export declare function useRecordDialog(): RecordDialogContextValue | null;
|
|
40
|
+
export type NewRecordDialogContextValue = {
|
|
41
|
+
/** Closes the new record's form: back to the list, as Back does. */
|
|
42
|
+
close: () => void;
|
|
43
|
+
/**
|
|
44
|
+
* After the save: the new record, over the same list (this entry
|
|
45
|
+
* replaced, so Back from it is the list). `open('/widgets/12')`.
|
|
46
|
+
*/
|
|
47
|
+
openRecord: (to: string) => void;
|
|
48
|
+
};
|
|
49
|
+
export declare const NewRecordDialogContext: import("react").Context<NewRecordDialogContextValue | null>;
|
|
50
|
+
/**
|
|
51
|
+
* Inside RecordDialogRoute's `newPath` route (`/widgets/new`): its `close`
|
|
52
|
+
* and `openRecord`. FormDialog reads it to open itself over the list with
|
|
53
|
+
* no `open` of its own; the route's form calls `openRecord` once it has
|
|
54
|
+
* saved. `null` anywhere else.
|
|
55
|
+
*/
|
|
56
|
+
export declare function useNewRecordDialog(): NewRecordDialogContextValue | null;
|
|
@@ -46,3 +46,13 @@ export const RecordDialogContext = createContext(null);
|
|
|
46
46
|
export function useRecordDialog() {
|
|
47
47
|
return useContext(RecordDialogContext);
|
|
48
48
|
}
|
|
49
|
+
export const NewRecordDialogContext = createContext(null);
|
|
50
|
+
/**
|
|
51
|
+
* Inside RecordDialogRoute's `newPath` route (`/widgets/new`): its `close`
|
|
52
|
+
* and `openRecord`. FormDialog reads it to open itself over the list with
|
|
53
|
+
* no `open` of its own; the route's form calls `openRecord` once it has
|
|
54
|
+
* saved. `null` anywhere else.
|
|
55
|
+
*/
|
|
56
|
+
export function useNewRecordDialog() {
|
|
57
|
+
return useContext(NewRecordDialogContext);
|
|
58
|
+
}
|
|
@@ -8,7 +8,7 @@ export type RecordDialogRouteProps = {
|
|
|
8
8
|
* loads, fails or isn't found. Once it loads, its title names the dialog.
|
|
9
9
|
*/
|
|
10
10
|
label: string;
|
|
11
|
-
/** Extra classes on the dialog's popup
|
|
11
|
+
/** Extra classes on the dialog's popup. It's 1,120px (`size="record"`) by default. */
|
|
12
12
|
className?: string;
|
|
13
13
|
/**
|
|
14
14
|
* False: the record has no full page, only this dialog (DataTable's
|
|
@@ -26,8 +26,15 @@ export type RecordDialogRouteProps = {
|
|
|
26
26
|
* records (the nested pattern).
|
|
27
27
|
*/
|
|
28
28
|
listPath?: string;
|
|
29
|
+
/**
|
|
30
|
+
* The child route that creates a record (`'new'` for `/widgets/new`):
|
|
31
|
+
* drawn over the list with no record dialog round it, its element a
|
|
32
|
+
* FormDialog, which opens itself (useNewRecordDialog). Left out, every
|
|
33
|
+
* child is a record.
|
|
34
|
+
*/
|
|
35
|
+
newPath?: string;
|
|
29
36
|
};
|
|
30
|
-
export declare function RecordDialogRoute({ list, label, className, fullPage, listPath: listAt }: RecordDialogRouteProps): import("react").JSX.Element | null;
|
|
37
|
+
export declare function RecordDialogRoute({ list, label, className, fullPage, listPath: listAt, newPath: newAt, }: RecordDialogRouteProps): import("react").JSX.Element | null;
|
|
31
38
|
export type RecordLinkProps = Omit<ComponentProps<typeof Link>, 'state'>;
|
|
32
39
|
/**
|
|
33
40
|
* A link to a record that opens it over the current list (a board's card,
|