@scattered-light/loomiere-base-ui 0.6.1 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +135 -5
- package/dist/chunk-JUUPOOQQ.js +3723 -0
- package/dist/chunk-JUUPOOQQ.js.map +1 -0
- package/dist/chunk-M7KJASGK.js +802 -0
- package/dist/chunk-M7KJASGK.js.map +1 -0
- package/dist/chunk-Y2CPBMFJ.js +147 -0
- package/dist/chunk-Y2CPBMFJ.js.map +1 -0
- package/dist/custom/collection/index.d.ts +540 -0
- package/dist/custom/collection/index.js +1489 -0
- package/dist/custom/collection/index.js.map +1 -0
- package/dist/custom/index.d.ts +5 -0
- package/dist/custom/index.js +4 -0
- package/dist/custom/index.js.map +1 -0
- package/dist/design-system/components/Badge/README.md +35 -0
- package/dist/design-system/components/Badge/preview.html +19 -0
- package/dist/design-system/components/Collection/README.md +14 -0
- package/dist/design-system/components/CollectionPagination/README.md +15 -0
- package/dist/design-system/components/CollectionToolbar/README.md +29 -0
- package/dist/design-system/components/DataList/README.md +39 -0
- package/dist/design-system/components/DataList/preview.html +475 -0
- package/dist/design-system/components/DataTable/README.md +41 -0
- package/dist/design-system/components/DataTable/preview.html +475 -0
- package/dist/design-system/components/DatePicker/README.md +55 -0
- package/dist/design-system/components/DatePicker/preview.html +475 -0
- package/dist/design-system/components/EditableList/README.md +33 -0
- package/dist/design-system/components/EditableList/preview.html +475 -0
- package/dist/design-system/components/EmptyState/README.md +60 -0
- package/dist/design-system/components/EmptyState/preview.html +475 -0
- package/dist/design-system/components/ListDetail/README.md +43 -0
- package/dist/design-system/components/ListDetail/preview.html +475 -0
- package/dist/design-system/components/MoneyField/README.md +52 -0
- package/dist/design-system/components/MoneyField/preview.html +475 -0
- package/dist/design-system/components/Splitter/README.md +42 -0
- package/dist/design-system/components/Splitter/preview.html +475 -0
- package/dist/design-system/components/Table/README.md +2 -1
- package/dist/design-system/components/Table/preview.html +1 -1
- package/dist/design-system/components/bundle.js +7 -5
- package/dist/design-system/components/index.d.ts +3 -21
- package/dist/design-system/components/styles.css +2349 -186
- package/dist/index-ChJXlxzq.d.ts +809 -0
- package/dist/index.d.ts +3 -21
- package/dist/index.js +176 -891
- package/dist/index.js.map +1 -1
- package/dist/styles/components/Accordion.css +1 -1
- package/dist/styles/components/AlertDialog.css +1 -1
- package/dist/styles/components/Autocomplete.css +1 -1
- package/dist/styles/components/Avatar.css +1 -1
- package/dist/styles/components/Button.css +1 -1
- package/dist/styles/components/Checkbox.css +1 -1
- package/dist/styles/components/CheckboxGroup.css +1 -1
- package/dist/styles/components/Collapsible.css +1 -1
- package/dist/styles/components/Combobox.css +1 -1
- package/dist/styles/components/ContextMenu.css +1 -1
- package/dist/styles/components/Dialog.css +1 -1
- package/dist/styles/components/Drawer.css +1 -1
- package/dist/styles/components/Field.css +1 -1
- package/dist/styles/components/Fieldset.css +1 -1
- package/dist/styles/components/Form.css +1 -1
- package/dist/styles/components/Input.css +1 -1
- package/dist/styles/components/Menu.css +1 -1
- package/dist/styles/components/Menubar.css +1 -1
- package/dist/styles/components/Meter.css +1 -1
- package/dist/styles/components/NavigationMenu.css +1 -1
- package/dist/styles/components/NumberField.css +1 -1
- package/dist/styles/components/OTPField.css +1 -1
- package/dist/styles/components/Popover.css +1 -1
- package/dist/styles/components/PreviewCard.css +1 -1
- package/dist/styles/components/Progress.css +1 -1
- package/dist/styles/components/Radio.css +1 -1
- package/dist/styles/components/ScrollArea.css +1 -1
- package/dist/styles/components/Select.css +1 -1
- package/dist/styles/components/Separator.css +1 -1
- package/dist/styles/components/Slider.css +1 -1
- package/dist/styles/components/Switch.css +1 -1
- package/dist/styles/components/Tabs.css +1 -1
- package/dist/styles/components/Toast.css +1 -1
- package/dist/styles/components/Toggle.css +1 -1
- package/dist/styles/components/ToggleGroup.css +1 -1
- package/dist/styles/components/Toolbar.css +1 -1
- package/dist/styles/components/Tooltip.css +1 -1
- package/dist/styles/custom/Badge.css +104 -0
- package/dist/styles/custom/Collection.css +15 -0
- package/dist/styles/custom/CollectionPagination.css +24 -0
- package/dist/styles/custom/CollectionToolbar.css +140 -0
- package/dist/styles/custom/DataList.css +123 -0
- package/dist/styles/custom/DataTable.css +120 -0
- package/dist/styles/custom/DatePicker.css +572 -0
- package/dist/styles/custom/EditableList.css +98 -0
- package/dist/styles/custom/EmptyState.css +255 -0
- package/dist/styles/custom/ListDetail.css +126 -0
- package/dist/styles/custom/MoneyField.css +344 -0
- package/dist/styles/custom/Splitter.css +207 -0
- package/dist/styles/{components → custom}/Table.css +31 -4
- package/dist/styles/index.css +12 -2
- package/dist/styles/reset/accessibility.css +1 -1
- package/dist/styles/reset/reset-accessibility.css +1 -1
- package/dist/styles/reset/reset-tailwind.css +1 -1
- package/dist/styles/reset/reset.css +1 -1
- package/dist/styles/tailwind.css +12 -2
- package/package.json +35 -7
|
@@ -0,0 +1,1489 @@
|
|
|
1
|
+
import { Input, Tabs, Checkbox, Menu } from '../../chunk-Y2CPBMFJ.js';
|
|
2
|
+
import { useUnderWidth, Button, cn, Select, Combobox, isEmptyValue, formatPercent, formatNumber, sumMoney, fractionDigitsOf, formatMoney, minorToDecimal, EmptyState, selectableIds, pageFlat, hiddenSelectedCount, nextSort, searchMatches, treeVisibleIds, isoDate, toMinor, comparePresent, collatorFor, formatDate, moneySearchForms, searchHaystack } from '../../chunk-M7KJASGK.js';
|
|
3
|
+
export { balanceOf, compareDecimal, countFacets, formatDate, formatMoney, hiddenSelectedCount, nextSort, pageBounds, pageFlat, searchMatches, selectableIds, sortRows, sumMoney, toMinor, treeVisibleIds } from '../../chunk-M7KJASGK.js';
|
|
4
|
+
import * as React4 from 'react';
|
|
5
|
+
import { useMemo, useRef, useState, useDeferredValue, useEffect } from 'react';
|
|
6
|
+
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
7
|
+
import { tableFeatures, columnVisibilityFeature, columnOrderingFeature, rowExpandingFeature, columnGroupingFeature, rowSelectionFeature, rowPaginationFeature, columnFacetingFeature, globalFilteringFeature, columnFilteringFeature, rowSortingFeature, createFacetedUniqueValues, createFacetedRowModel, createPaginatedRowModel, createExpandedRowModel, createGroupedRowModel, createFilteredRowModel, createSortedRowModel, useTable } from '@tanstack/react-table';
|
|
8
|
+
|
|
9
|
+
// src/custom/collection/columns.ts
|
|
10
|
+
function headerLabel(column) {
|
|
11
|
+
return typeof column.header === "string" ? column.header : column.header.label;
|
|
12
|
+
}
|
|
13
|
+
function defineColumns(columns) {
|
|
14
|
+
if (process.env.NODE_ENV !== "production") {
|
|
15
|
+
const headers = columns.filter((column) => column.rowHeader);
|
|
16
|
+
if (headers.length !== 1) {
|
|
17
|
+
console.warn(`A collection should have exactly one rowHeader column so each row has a name. This one has ${headers.length}.`);
|
|
18
|
+
}
|
|
19
|
+
const tabs = columns.filter((column) => column.filter?.kind === "tabs");
|
|
20
|
+
if (tabs.length > 1) console.warn("Only one column can use a tabs filter. The toolbar shows the first.");
|
|
21
|
+
}
|
|
22
|
+
return columns;
|
|
23
|
+
}
|
|
24
|
+
function defaultAlign(column) {
|
|
25
|
+
if (column.align) return column.align;
|
|
26
|
+
const kind = column.kind ?? "text";
|
|
27
|
+
if (kind === "number" || kind === "money" || kind === "percent") return "end";
|
|
28
|
+
return "start";
|
|
29
|
+
}
|
|
30
|
+
function listSlot(column, columns) {
|
|
31
|
+
if (column.list) return column.list;
|
|
32
|
+
if (column.rowHeader) return "primary";
|
|
33
|
+
if (column.kind === "money") return "amount";
|
|
34
|
+
if (column.filter?.kind === "tabs") return "status";
|
|
35
|
+
if (column.cell && column.kind == null && column.id.toLowerCase().includes("status")) return "status";
|
|
36
|
+
const primary = columns.find((item) => item.rowHeader) ?? columns[0];
|
|
37
|
+
const secondary = columns.find((item) => item !== primary && item.searchable && (item.kind ?? "text") === "text" && !item.rowHeader);
|
|
38
|
+
if (column === secondary) return "secondary";
|
|
39
|
+
const meta = columns.filter((item) => item !== primary && item !== secondary && item.kind !== "money" && item.filter?.kind !== "tabs");
|
|
40
|
+
if (meta.slice(0, 3).includes(column)) return "meta";
|
|
41
|
+
return "hidden";
|
|
42
|
+
}
|
|
43
|
+
function currencyOf(column, row) {
|
|
44
|
+
if (typeof column.currency === "function") return column.currency(row);
|
|
45
|
+
return column.currency ?? "USD";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/custom/collection/format.ts
|
|
49
|
+
function rowSearchText(row, columns, locale) {
|
|
50
|
+
const parts = [];
|
|
51
|
+
for (const column of columns) {
|
|
52
|
+
if (column.searchable == null || column.searchable === false) continue;
|
|
53
|
+
const value = column.value(row);
|
|
54
|
+
if (typeof column.searchable === "function") {
|
|
55
|
+
const extra = column.searchable(row);
|
|
56
|
+
parts.push(...Array.isArray(extra) ? extra : [extra]);
|
|
57
|
+
}
|
|
58
|
+
if (isEmptyValue(value)) continue;
|
|
59
|
+
if (column.kind === "money") {
|
|
60
|
+
parts.push(...moneySearchForms(value, currencyOf(column, row), locale, column.fractionDigits));
|
|
61
|
+
} else if (column.kind === "number" || column.kind === "percent") {
|
|
62
|
+
parts.push(String(value), formatNumber(value, locale, column.fractionDigits));
|
|
63
|
+
} else if (column.kind === "date") {
|
|
64
|
+
parts.push(String(value), formatDate(value, locale, column.dateFormat), isoDate(value));
|
|
65
|
+
} else {
|
|
66
|
+
parts.push(String(value));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return searchHaystack(parts);
|
|
70
|
+
}
|
|
71
|
+
function formattedValue(column, row, locale, hideCurrency = false) {
|
|
72
|
+
const value = column.value(row);
|
|
73
|
+
if (isEmptyValue(value)) return "";
|
|
74
|
+
if (column.kind === "money") {
|
|
75
|
+
const currency = currencyOf(column, row);
|
|
76
|
+
if (hideCurrency) {
|
|
77
|
+
const digits = column.fractionDigits ?? fractionDigitsOf(currency);
|
|
78
|
+
return formatNumber(value, locale, digits);
|
|
79
|
+
}
|
|
80
|
+
return formatMoney(value, {
|
|
81
|
+
locale,
|
|
82
|
+
currency,
|
|
83
|
+
fractionDigits: column.fractionDigits,
|
|
84
|
+
negative: column.negative
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (column.kind === "number") return formatNumber(value, locale, column.fractionDigits);
|
|
88
|
+
if (column.kind === "percent") return formatPercent(value, locale);
|
|
89
|
+
if (column.kind === "date") return formatDate(value, locale, column.dateFormat);
|
|
90
|
+
if (column.kind === "boolean") return value ? "Yes" : "No";
|
|
91
|
+
return String(value);
|
|
92
|
+
}
|
|
93
|
+
function columnSharesCurrency(column, rows) {
|
|
94
|
+
if (column.kind !== "money") return null;
|
|
95
|
+
const currencies = new Set(rows.map((row) => currencyOf(column, row)));
|
|
96
|
+
return currencies.size === 1 ? [...currencies][0] : null;
|
|
97
|
+
}
|
|
98
|
+
function displayHeader(column, rows) {
|
|
99
|
+
const label = headerLabel(column);
|
|
100
|
+
if (!column.showCurrencyInHeader) return label;
|
|
101
|
+
const currency = columnSharesCurrency(column, rows);
|
|
102
|
+
return currency ? `${label} (${currency})` : label;
|
|
103
|
+
}
|
|
104
|
+
function hideCurrencyInCells(column, rows) {
|
|
105
|
+
return Boolean(column.showCurrencyInHeader && columnSharesCurrency(column, rows));
|
|
106
|
+
}
|
|
107
|
+
function totalText(column, rows, locale) {
|
|
108
|
+
if (!column.total) return "";
|
|
109
|
+
if (column.total === "count") return String(rows.length);
|
|
110
|
+
if (typeof column.total === "function") return "";
|
|
111
|
+
if (column.kind === "number" || column.kind === "percent") {
|
|
112
|
+
let sum2 = 0;
|
|
113
|
+
for (const row of rows) {
|
|
114
|
+
const value = column.value(row);
|
|
115
|
+
if (isEmptyValue(value)) continue;
|
|
116
|
+
const numeric = typeof value === "number" ? value : Number(String(value).replace(/,/g, ""));
|
|
117
|
+
if (Number.isFinite(numeric)) sum2 += numeric;
|
|
118
|
+
}
|
|
119
|
+
return column.kind === "percent" ? formatPercent(sum2, locale) : formatNumber(sum2, locale, column.fractionDigits);
|
|
120
|
+
}
|
|
121
|
+
const values = rows.map((row) => ({ amount: column.value(row), currency: currencyOf(column, row) }));
|
|
122
|
+
const sum = sumMoney(values, column.fractionDigits);
|
|
123
|
+
if (sum.mixed) {
|
|
124
|
+
if (process.env.NODE_ENV !== "production") console.warn(`Column "${column.id}" has more than one currency, so it has no single total.`);
|
|
125
|
+
return "Mixed currencies";
|
|
126
|
+
}
|
|
127
|
+
const currency = sum.currency ?? (typeof column.currency === "string" ? column.currency : "USD");
|
|
128
|
+
const digits = column.fractionDigits ?? fractionDigitsOf(currency);
|
|
129
|
+
return formatMoney(minorToDecimal(sum.minor ?? 0n, digits), {
|
|
130
|
+
locale,
|
|
131
|
+
currency,
|
|
132
|
+
fractionDigits: digits,
|
|
133
|
+
negative: column.negative
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function rowHeaderText(collection, row) {
|
|
137
|
+
const column = collection.columns.find((item) => item.id === collection.rowHeaderId);
|
|
138
|
+
if (!column) return collection.getRowId(row);
|
|
139
|
+
const value = column.value(row);
|
|
140
|
+
return value == null || value === "" ? "None" : String(value);
|
|
141
|
+
}
|
|
142
|
+
function levelName(collection, tableRow, text) {
|
|
143
|
+
if (!tableRow.depth) return text;
|
|
144
|
+
const parent = tableRow.getParentRow?.();
|
|
145
|
+
const parentText = parent ? rowHeaderText(collection, parent.original) : "";
|
|
146
|
+
return parentText ? `${text}, level ${tableRow.depth + 1}, in ${parentText}` : `${text}, level ${tableRow.depth + 1}`;
|
|
147
|
+
}
|
|
148
|
+
function CellValue({ column, row, locale, rows, view, selected, active }) {
|
|
149
|
+
const ctx = { view, selected, active };
|
|
150
|
+
if (column.cell) return /* @__PURE__ */ jsx(Fragment, { children: column.cell(row, ctx) });
|
|
151
|
+
const value = column.value(row);
|
|
152
|
+
if (value == null || value === "") {
|
|
153
|
+
return /* @__PURE__ */ jsxs("span", { "data-subtle": "", children: [
|
|
154
|
+
"\u2014",
|
|
155
|
+
/* @__PURE__ */ jsx("span", { className: "visually-hidden", children: " None" })
|
|
156
|
+
] });
|
|
157
|
+
}
|
|
158
|
+
if (column.kind === "boolean") {
|
|
159
|
+
return /* @__PURE__ */ jsxs("span", { children: [
|
|
160
|
+
value ? /* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: "check" }) : null,
|
|
161
|
+
/* @__PURE__ */ jsx("span", { className: "visually-hidden", children: value ? "Yes" : "No" })
|
|
162
|
+
] });
|
|
163
|
+
}
|
|
164
|
+
if (column.kind === "date") {
|
|
165
|
+
const text = formattedValue(column, row, locale);
|
|
166
|
+
return /* @__PURE__ */ jsx("time", { dateTime: isoDate(value), children: text });
|
|
167
|
+
}
|
|
168
|
+
const negative = column.kind === "money" && (toMinor(value, column.fractionDigits ?? 2) ?? 0n) < 0n;
|
|
169
|
+
return /* @__PURE__ */ jsx("span", { "data-negative": negative ? "" : void 0, children: formattedValue(column, row, locale, hideCurrencyInCells(column, rows)) });
|
|
170
|
+
}
|
|
171
|
+
function SelectBox({ checked, indeterminate, label, onChange }) {
|
|
172
|
+
return /* @__PURE__ */ jsx(
|
|
173
|
+
Checkbox.Root,
|
|
174
|
+
{
|
|
175
|
+
checked,
|
|
176
|
+
indeterminate,
|
|
177
|
+
onCheckedChange: (value) => onChange(value === true),
|
|
178
|
+
"aria-label": label,
|
|
179
|
+
children: /* @__PURE__ */ jsx(Checkbox.Indicator, { children: /* @__PURE__ */ jsx("span", { className: "material-symbols-outlined CheckboxIcon", "aria-hidden": "true", children: indeterminate ? "remove" : "check" }) })
|
|
180
|
+
}
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
function RowMenu({ label, actions }) {
|
|
184
|
+
return /* @__PURE__ */ jsxs(Menu.Root, { children: [
|
|
185
|
+
/* @__PURE__ */ jsx(Menu.Trigger, { className: "Button", "data-emphasis": "quiet", "data-icon-only": "", "aria-label": label, children: /* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: "more_horiz" }) }),
|
|
186
|
+
/* @__PURE__ */ jsx(Menu.Portal, { children: /* @__PURE__ */ jsx(Menu.Positioner, { children: /* @__PURE__ */ jsx(Menu.Popup, { children: actions.map((action) => /* @__PURE__ */ jsx(Menu.Item, { disabled: action.disabled, "data-color": action.tone === "danger" ? "error" : void 0, onClick: action.onSelect, children: action.label }, action.label)) }) }) })
|
|
187
|
+
] });
|
|
188
|
+
}
|
|
189
|
+
function useRovingRows(count, enabled) {
|
|
190
|
+
const [cursor, setCursor] = React4.useState(0);
|
|
191
|
+
const refs = React4.useRef([]);
|
|
192
|
+
React4.useEffect(() => {
|
|
193
|
+
if (cursor > count - 1) setCursor(Math.max(0, count - 1));
|
|
194
|
+
}, [count, cursor]);
|
|
195
|
+
const focusAt = (index) => {
|
|
196
|
+
const next = Math.min(Math.max(0, index), Math.max(0, count - 1));
|
|
197
|
+
setCursor(next);
|
|
198
|
+
refs.current[next]?.focus();
|
|
199
|
+
};
|
|
200
|
+
const onKeyDown = (event, index, toggle, activate, tree) => {
|
|
201
|
+
const target = event.target;
|
|
202
|
+
if (target.closest('input, textarea, select, [role="menu"], [role="listbox"]')) return;
|
|
203
|
+
const rtl = Boolean(target.closest('[dir="rtl"]'));
|
|
204
|
+
const openKey = rtl ? "ArrowLeft" : "ArrowRight";
|
|
205
|
+
const closeKey = rtl ? "ArrowRight" : "ArrowLeft";
|
|
206
|
+
if (tree && event.key === openKey && !tree.expanded) {
|
|
207
|
+
event.preventDefault();
|
|
208
|
+
tree.toggle();
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (tree && event.key === closeKey && tree.expanded) {
|
|
212
|
+
event.preventDefault();
|
|
213
|
+
tree.toggle();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (!enabled) return;
|
|
217
|
+
if (event.key === "ArrowDown") {
|
|
218
|
+
event.preventDefault();
|
|
219
|
+
focusAt(index + 1);
|
|
220
|
+
} else if (event.key === "ArrowUp") {
|
|
221
|
+
event.preventDefault();
|
|
222
|
+
focusAt(index - 1);
|
|
223
|
+
} else if (event.key === "Home") {
|
|
224
|
+
event.preventDefault();
|
|
225
|
+
focusAt(0);
|
|
226
|
+
} else if (event.key === "End") {
|
|
227
|
+
event.preventDefault();
|
|
228
|
+
focusAt(count - 1);
|
|
229
|
+
} else if (event.key === " " && toggle) {
|
|
230
|
+
event.preventDefault();
|
|
231
|
+
toggle();
|
|
232
|
+
} else if (event.key === "Enter" && !(event.target instanceof HTMLAnchorElement) && activate) {
|
|
233
|
+
event.preventDefault();
|
|
234
|
+
activate();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
return { cursor, refs, focusAt, onKeyDown };
|
|
238
|
+
}
|
|
239
|
+
function interactiveTarget(target) {
|
|
240
|
+
return target instanceof Element && Boolean(target.closest('a, button, input, textarea, select, label, [role="menu"], [role="menuitem"], [role="listbox"], [role="checkbox"], [role="option"], [data-no-activate]'));
|
|
241
|
+
}
|
|
242
|
+
function selectingText() {
|
|
243
|
+
const selection = typeof window === "undefined" ? null : window.getSelection();
|
|
244
|
+
return Boolean(selection && !selection.isCollapsed);
|
|
245
|
+
}
|
|
246
|
+
function EmptyRow({ colSpan, filtered, onClear, empty, title = "No results", description = "Nothing matches the current search or filters.", clearLabel = "Clear filters", emptyTitle = "Nothing here yet" }) {
|
|
247
|
+
return /* @__PURE__ */ jsx("tr", { children: /* @__PURE__ */ jsx("td", { "data-empty": "", colSpan, children: filtered ? /* @__PURE__ */ jsx(
|
|
248
|
+
EmptyState,
|
|
249
|
+
{
|
|
250
|
+
size: "compact",
|
|
251
|
+
kind: "no-results",
|
|
252
|
+
title,
|
|
253
|
+
description,
|
|
254
|
+
actions: /* @__PURE__ */ jsx(Button, { emphasis: "standard", onClick: onClear, children: clearLabel })
|
|
255
|
+
}
|
|
256
|
+
) : empty ?? /* @__PURE__ */ jsx(EmptyState, { size: "compact", kind: "start", title: emptyTitle }) }) });
|
|
257
|
+
}
|
|
258
|
+
function LoadingNote({ text }) {
|
|
259
|
+
const [live, setLive] = React4.useState("");
|
|
260
|
+
React4.useEffect(() => {
|
|
261
|
+
const frame = requestAnimationFrame(() => setLive(text));
|
|
262
|
+
return () => cancelAnimationFrame(frame);
|
|
263
|
+
}, [text]);
|
|
264
|
+
return /* @__PURE__ */ jsx("span", { className: "CollectionStatus visually-hidden", role: "status", children: live });
|
|
265
|
+
}
|
|
266
|
+
function alignOf(column) {
|
|
267
|
+
const align = defaultAlign(column);
|
|
268
|
+
return align === "start" ? void 0 : align;
|
|
269
|
+
}
|
|
270
|
+
function DataList({
|
|
271
|
+
collection,
|
|
272
|
+
label,
|
|
273
|
+
onRowActivate,
|
|
274
|
+
getRowHref,
|
|
275
|
+
activeRowId,
|
|
276
|
+
rowActions,
|
|
277
|
+
empty,
|
|
278
|
+
loading,
|
|
279
|
+
rowNavigation,
|
|
280
|
+
size = "sm",
|
|
281
|
+
className,
|
|
282
|
+
variant = "rows",
|
|
283
|
+
totals = "summary",
|
|
284
|
+
renderItemExtra
|
|
285
|
+
}) {
|
|
286
|
+
const leaves = collection.pageEntries.filter((entry) => entry.kind === "leaf");
|
|
287
|
+
const navigate = rowNavigation ?? Boolean(onRowActivate || getRowHref);
|
|
288
|
+
const roving = useRovingRows(leaves.length, navigate);
|
|
289
|
+
React4.useEffect(() => {
|
|
290
|
+
collection.registerRowFocus(() => roving.focusAt(0));
|
|
291
|
+
return () => collection.registerRowFocus(null);
|
|
292
|
+
}, [collection, roving]);
|
|
293
|
+
React4.useLayoutEffect(() => {
|
|
294
|
+
if (!collection.focusAfterPage.current) return;
|
|
295
|
+
collection.focusAfterPage.current = false;
|
|
296
|
+
roving.focusAt(0);
|
|
297
|
+
}, [collection, collection.page, roving]);
|
|
298
|
+
const slots = slotMap(collection);
|
|
299
|
+
const filteredEmpty = collection.totalCount > 0 && collection.filteredCount === 0;
|
|
300
|
+
return /* @__PURE__ */ jsxs(
|
|
301
|
+
"div",
|
|
302
|
+
{
|
|
303
|
+
className: cn("DataList", className),
|
|
304
|
+
"data-view": "list",
|
|
305
|
+
"data-size": size,
|
|
306
|
+
"data-variant": variant,
|
|
307
|
+
"data-filtered": collection.isFiltered ? "" : void 0,
|
|
308
|
+
"data-selecting": collection.selection.length ? "" : void 0,
|
|
309
|
+
"data-loading": loading ? "" : void 0,
|
|
310
|
+
"data-grouped": collection.grouping ? "" : void 0,
|
|
311
|
+
"aria-busy": loading || void 0,
|
|
312
|
+
children: [
|
|
313
|
+
/* @__PURE__ */ jsxs("ul", { role: "list", "aria-label": label, children: [
|
|
314
|
+
loading && collection.totalCount === 0 ? /* @__PURE__ */ jsx("li", { children: /* @__PURE__ */ jsx(LoadingNote, { text: collection.messages.loading }) }) : null,
|
|
315
|
+
!loading || collection.totalCount > 0 ? leaves.length === 0 ? /* @__PURE__ */ jsx("li", { children: filteredEmpty || collection.isFiltered ? /* @__PURE__ */ jsx(EmptyState, { size: "compact", kind: "no-results", title: collection.messages.noResults, description: collection.messages.noResultsDescription, actions: /* @__PURE__ */ jsx(Button, { emphasis: "standard", onClick: collection.clearFilters, children: collection.messages.clearFilters }) }) : empty ?? /* @__PURE__ */ jsx(EmptyState, { size: "compact", kind: "start", title: collection.messages.nothingHere }) }) : listItems({ collection, slots, roving, navigate, onRowActivate, getRowHref, activeRowId, rowActions, renderItemExtra }) : null
|
|
316
|
+
] }),
|
|
317
|
+
totals === "summary" && Object.keys(collection.totals).length ? /* @__PURE__ */ jsxs("p", { className: "DataListSummary", children: [
|
|
318
|
+
collection.filteredCount,
|
|
319
|
+
" rows",
|
|
320
|
+
Object.entries(collection.totals).map(([id, text]) => /* @__PURE__ */ jsxs("span", { children: [
|
|
321
|
+
" \xB7 Total ",
|
|
322
|
+
text
|
|
323
|
+
] }, id))
|
|
324
|
+
] }) : null
|
|
325
|
+
]
|
|
326
|
+
}
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
function listItems(args) {
|
|
330
|
+
const nodes = [];
|
|
331
|
+
let leafIndex = 0;
|
|
332
|
+
for (const entry of args.collection.pageEntries) {
|
|
333
|
+
if (entry.kind === "group") {
|
|
334
|
+
const row = entry.row;
|
|
335
|
+
const children = [];
|
|
336
|
+
const rest = args.collection.pageEntries.slice(args.collection.pageEntries.indexOf(entry) + 1);
|
|
337
|
+
for (const child of rest) {
|
|
338
|
+
if (child.kind === "group") break;
|
|
339
|
+
children.push(/* @__PURE__ */ jsx(ListRow, { entry: child, index: leafIndex, ...args }, child.row.id));
|
|
340
|
+
leafIndex += 1;
|
|
341
|
+
}
|
|
342
|
+
const selectable = (row.subRows ?? []).filter((child) => args.collection.canSelect?.(child.original));
|
|
343
|
+
const subtotal = args.slots.amount ? totalText(args.slots.amount, (row.subRows ?? []).map((child) => child.original), args.collection.locale) : "";
|
|
344
|
+
nodes.push(
|
|
345
|
+
/* @__PURE__ */ jsxs("li", { className: "DataListGroup", children: [
|
|
346
|
+
/* @__PURE__ */ jsxs("div", { className: "DataListGroupHeader", children: [
|
|
347
|
+
/* @__PURE__ */ jsxs("button", { type: "button", className: "CollectionGroupButton", "aria-expanded": row.getIsExpanded(), onClick: () => row.toggleExpanded(), children: [
|
|
348
|
+
/* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: row.getIsExpanded() ? "expand_more" : "chevron_right" }),
|
|
349
|
+
String(row.groupingValue ?? ""),
|
|
350
|
+
entry.continued ? " (continued)" : "",
|
|
351
|
+
" \xB7 ",
|
|
352
|
+
selectable.length || (row.subRows?.length ?? 0)
|
|
353
|
+
] }),
|
|
354
|
+
selectable.length && args.collection.canSelect ? /* @__PURE__ */ jsx(SelectBox, { checked: row.getIsAllSubRowsSelected?.() ?? false, indeterminate: Boolean(row.getIsSomeSelected?.() && !row.getIsAllSubRowsSelected?.()), label: `Select ${selectable.length} rows in ${row.groupingValue ?? "group"}`, onChange: () => row.toggleSelected?.() }) : null,
|
|
355
|
+
subtotal ? /* @__PURE__ */ jsx("span", { children: subtotal }) : null
|
|
356
|
+
] }),
|
|
357
|
+
row.getIsExpanded() ? /* @__PURE__ */ jsx("ul", { role: "list", children }) : null
|
|
358
|
+
] }, `g-${row.id}`)
|
|
359
|
+
);
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (entry.row.getParentRow?.()?.getIsGrouped?.()) continue;
|
|
363
|
+
nodes.push(
|
|
364
|
+
/* @__PURE__ */ jsx(ListRow, { entry, index: leafIndex, ...args }, entry.row.id)
|
|
365
|
+
);
|
|
366
|
+
leafIndex += 1;
|
|
367
|
+
}
|
|
368
|
+
return nodes;
|
|
369
|
+
}
|
|
370
|
+
function ListRow({ entry, index, collection, slots, roving, navigate, onRowActivate, getRowHref, activeRowId, rowActions, renderItemExtra }) {
|
|
371
|
+
const original = entry.row.original;
|
|
372
|
+
const selected = collection.selectionSet.has(entry.row.id);
|
|
373
|
+
const active = activeRowId === entry.row.id;
|
|
374
|
+
const name = rowHeaderText(collection, original);
|
|
375
|
+
const named = levelName(collection, entry.row, name);
|
|
376
|
+
const canSelect = collection.canSelect?.(original) ?? false;
|
|
377
|
+
const href = getRowHref?.(original);
|
|
378
|
+
const activate = () => onRowActivate?.(original);
|
|
379
|
+
const tabIndex = navigate ? roving.cursor === index ? 0 : -1 : void 0;
|
|
380
|
+
const onKeyDown = (event) => roving.onKeyDown(event, index, canSelect ? () => entry.row.toggleSelected?.(!selected) : void 0, href ? void 0 : activate);
|
|
381
|
+
const primary = href ? /* @__PURE__ */ jsx("a", { ref: (node) => {
|
|
382
|
+
roving.refs.current[index] = node;
|
|
383
|
+
}, href, tabIndex, "aria-label": named === name ? void 0 : named, onKeyDown, children: name }) : /* @__PURE__ */ jsx("button", { type: "button", className: "CollectionRowButton", ref: (node) => {
|
|
384
|
+
roving.refs.current[index] = node;
|
|
385
|
+
}, tabIndex, "aria-label": named === name ? void 0 : named, onKeyDown, onClick: () => {
|
|
386
|
+
roving.focusAt(index);
|
|
387
|
+
activate();
|
|
388
|
+
}, children: name });
|
|
389
|
+
return (
|
|
390
|
+
// The row is not a control. The primary button is. The checkbox carries selection.
|
|
391
|
+
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/click-events-have-key-events
|
|
392
|
+
/* @__PURE__ */ jsxs(
|
|
393
|
+
"li",
|
|
394
|
+
{
|
|
395
|
+
"data-selected": selected ? "" : void 0,
|
|
396
|
+
"aria-current": active ? "true" : void 0,
|
|
397
|
+
onClick: (event) => {
|
|
398
|
+
if (interactiveTarget(event.target) || selectingText()) return;
|
|
399
|
+
if (event.target instanceof Element && event.target.closest(".DataListExtra, [data-actions]")) return;
|
|
400
|
+
if (href && !onRowActivate) {
|
|
401
|
+
event.currentTarget.querySelector("a")?.click();
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
activate();
|
|
405
|
+
},
|
|
406
|
+
children: [
|
|
407
|
+
collection.canSelect ? canSelect ? /* @__PURE__ */ jsx(SelectBox, { checked: selected, label: `Select ${named}`, onChange: (checked) => entry.row.toggleSelected?.(checked) }) : /* @__PURE__ */ jsx("span", {}) : null,
|
|
408
|
+
/* @__PURE__ */ jsxs("div", { className: "DataListBody", children: [
|
|
409
|
+
/* @__PURE__ */ jsx("div", { className: "DataListPrimary", children: slots.primary ? primary : name }),
|
|
410
|
+
slots.secondary ? /* @__PURE__ */ jsx("div", { className: "DataListSecondary", "data-subtle": "", children: /* @__PURE__ */ jsx(CellValue, { column: slots.secondary, row: original, locale: collection.locale, rows: collection.filteredRows, view: "list", selected, active }) }) : null,
|
|
411
|
+
slots.meta.length ? /* @__PURE__ */ jsx("div", { className: "DataListMeta", "data-subtle": "", children: slots.meta.map((column) => /* @__PURE__ */ jsx("span", { children: /* @__PURE__ */ jsx(CellValue, { column, row: original, locale: collection.locale, rows: collection.filteredRows, view: "list", selected, active }) }, column.id)) }) : null,
|
|
412
|
+
renderItemExtra ? /* @__PURE__ */ jsx("div", { className: "DataListExtra", children: renderItemExtra(original) }) : null
|
|
413
|
+
] }),
|
|
414
|
+
slots.amount ? /* @__PURE__ */ jsx("div", { className: "DataListAmount", "data-align": "end", children: /* @__PURE__ */ jsx(CellValue, { column: slots.amount, row: original, locale: collection.locale, rows: collection.filteredRows, view: "list", selected, active }) }) : null,
|
|
415
|
+
slots.status ? /* @__PURE__ */ jsx("div", { className: "DataListStatus", children: /* @__PURE__ */ jsx(CellValue, { column: slots.status, row: original, locale: collection.locale, rows: collection.filteredRows, view: "list", selected, active }) }) : null,
|
|
416
|
+
rowActions ? /* @__PURE__ */ jsx("span", { "data-actions": "", children: /* @__PURE__ */ jsx(RowMenu, { label: collection.messages.actionsFor(name), actions: rowActions(original) }) }) : null
|
|
417
|
+
]
|
|
418
|
+
}
|
|
419
|
+
)
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
function slotMap(collection) {
|
|
423
|
+
const placed = collection.columns.map((column) => ({ column, slot: listSlot(column, collection.columns) }));
|
|
424
|
+
return {
|
|
425
|
+
primary: placed.find((item) => item.slot === "primary")?.column,
|
|
426
|
+
secondary: placed.find((item) => item.slot === "secondary")?.column,
|
|
427
|
+
amount: placed.find((item) => item.slot === "amount")?.column,
|
|
428
|
+
status: placed.find((item) => item.slot === "status")?.column,
|
|
429
|
+
meta: placed.filter((item) => item.slot === "meta").map((item) => item.column).slice(0, 3)
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
function DataTable({
|
|
433
|
+
collection,
|
|
434
|
+
label,
|
|
435
|
+
caption,
|
|
436
|
+
onRowActivate,
|
|
437
|
+
getRowHref,
|
|
438
|
+
activeRowId,
|
|
439
|
+
rowActions,
|
|
440
|
+
empty,
|
|
441
|
+
loading,
|
|
442
|
+
stickyHeader = true,
|
|
443
|
+
stickyRowHeader = false,
|
|
444
|
+
narrow = "scroll",
|
|
445
|
+
size = "sm",
|
|
446
|
+
rowNavigation,
|
|
447
|
+
className
|
|
448
|
+
}) {
|
|
449
|
+
const narrowWidth = useUnderWidth("--sl-collection-narrow-width");
|
|
450
|
+
const asList = narrow === "list" && narrowWidth.under;
|
|
451
|
+
const columns = visibleColumns(collection);
|
|
452
|
+
const leaves = collection.pageEntries.filter((entry) => entry.kind === "leaf");
|
|
453
|
+
const navigate = rowNavigation ?? Boolean(onRowActivate || getRowHref);
|
|
454
|
+
const colSpan = columns.length + (collection.canSelect ? 1 : 0) + (rowActions ? 1 : 0);
|
|
455
|
+
const selectableIds2 = collection.filteredRows.filter((row) => collection.canSelect?.(row)).map((row) => collection.getRowId(row));
|
|
456
|
+
const selectedFiltered = selectableIds2.filter((id) => collection.selection.includes(id));
|
|
457
|
+
const allSelected = selectableIds2.length > 0 && selectedFiltered.length === selectableIds2.length;
|
|
458
|
+
const someSelected = selectedFiltered.length > 0 && !allSelected;
|
|
459
|
+
const view = asList ? /* @__PURE__ */ jsx(
|
|
460
|
+
DataList,
|
|
461
|
+
{
|
|
462
|
+
collection,
|
|
463
|
+
label,
|
|
464
|
+
onRowActivate,
|
|
465
|
+
getRowHref,
|
|
466
|
+
activeRowId,
|
|
467
|
+
rowActions,
|
|
468
|
+
empty,
|
|
469
|
+
loading,
|
|
470
|
+
size,
|
|
471
|
+
rowNavigation
|
|
472
|
+
}
|
|
473
|
+
) : /* @__PURE__ */ jsx(
|
|
474
|
+
TableView,
|
|
475
|
+
{
|
|
476
|
+
collection,
|
|
477
|
+
columns,
|
|
478
|
+
label,
|
|
479
|
+
caption,
|
|
480
|
+
colSpan,
|
|
481
|
+
leaves,
|
|
482
|
+
navigate,
|
|
483
|
+
allSelected,
|
|
484
|
+
someSelected,
|
|
485
|
+
loading,
|
|
486
|
+
empty,
|
|
487
|
+
stickyHeader,
|
|
488
|
+
stickyRowHeader,
|
|
489
|
+
size,
|
|
490
|
+
className,
|
|
491
|
+
onRowActivate,
|
|
492
|
+
getRowHref,
|
|
493
|
+
activeRowId,
|
|
494
|
+
rowActions
|
|
495
|
+
}
|
|
496
|
+
);
|
|
497
|
+
return /* @__PURE__ */ jsx("div", { ref: narrowWidth.ref, className: cn("CollectionView", asList ? className : void 0), "data-narrow": narrowWidth.under ? "" : void 0, children: view });
|
|
498
|
+
}
|
|
499
|
+
function TableView({ collection, columns, label, caption, colSpan, leaves, navigate, allSelected, someSelected, loading, empty, stickyHeader, stickyRowHeader, size, className, onRowActivate, getRowHref, activeRowId, rowActions }) {
|
|
500
|
+
const roving = useRovingRows(leaves.length, navigate);
|
|
501
|
+
const scrollerRef = React4.useRef(null);
|
|
502
|
+
const [overflows, setOverflows] = React4.useState(false);
|
|
503
|
+
React4.useEffect(() => {
|
|
504
|
+
collection.registerRowFocus(() => roving.focusAt(0));
|
|
505
|
+
return () => collection.registerRowFocus(null);
|
|
506
|
+
}, [collection, roving]);
|
|
507
|
+
React4.useLayoutEffect(() => {
|
|
508
|
+
if (!collection.focusAfterPage.current) return;
|
|
509
|
+
collection.focusAfterPage.current = false;
|
|
510
|
+
roving.focusAt(0);
|
|
511
|
+
}, [collection, collection.page, roving]);
|
|
512
|
+
React4.useEffect(() => {
|
|
513
|
+
const node = scrollerRef.current;
|
|
514
|
+
if (!node) return;
|
|
515
|
+
const watch = () => {
|
|
516
|
+
const wide = node.scrollWidth > node.clientWidth + 1;
|
|
517
|
+
const tall = stickyHeader && node.scrollHeight > node.clientHeight + 1;
|
|
518
|
+
setOverflows(wide || tall);
|
|
519
|
+
};
|
|
520
|
+
const observer = new ResizeObserver(watch);
|
|
521
|
+
observer.observe(node);
|
|
522
|
+
watch();
|
|
523
|
+
return () => observer.disconnect();
|
|
524
|
+
}, [stickyHeader, leaves.length, columns.length]);
|
|
525
|
+
const filteredEmpty = collection.totalCount > 0 && collection.filteredCount === 0;
|
|
526
|
+
const text = collection.messages;
|
|
527
|
+
return /* @__PURE__ */ jsxs(
|
|
528
|
+
"div",
|
|
529
|
+
{
|
|
530
|
+
ref: scrollerRef,
|
|
531
|
+
className: cn("CollectionScroll", className),
|
|
532
|
+
tabIndex: overflows ? 0 : void 0,
|
|
533
|
+
role: overflows ? "region" : void 0,
|
|
534
|
+
"aria-label": overflows ? `${label}, scrollable` : void 0,
|
|
535
|
+
children: [
|
|
536
|
+
/* @__PURE__ */ jsxs(
|
|
537
|
+
"table",
|
|
538
|
+
{
|
|
539
|
+
className: "Table DataTable",
|
|
540
|
+
"aria-label": caption ? void 0 : label,
|
|
541
|
+
"aria-busy": loading || void 0,
|
|
542
|
+
"data-size": size,
|
|
543
|
+
"data-view": "table",
|
|
544
|
+
"data-sorted": collection.sort ? "" : void 0,
|
|
545
|
+
"data-filtered": collection.isFiltered ? "" : void 0,
|
|
546
|
+
"data-selecting": collection.selection.length ? "" : void 0,
|
|
547
|
+
"data-loading": loading ? "" : void 0,
|
|
548
|
+
"data-grouped": collection.grouping ? "" : void 0,
|
|
549
|
+
"data-sticky-header": stickyHeader ? "" : void 0,
|
|
550
|
+
"data-sticky-row-header": stickyRowHeader ? "" : void 0,
|
|
551
|
+
children: [
|
|
552
|
+
caption ? /* @__PURE__ */ jsx("caption", { children: caption }) : null,
|
|
553
|
+
/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
554
|
+
collection.canSelect ? /* @__PURE__ */ jsx("th", { scope: "col", "data-width": "min", className: "CollectionSelectColumn", children: /* @__PURE__ */ jsx(
|
|
555
|
+
SelectBox,
|
|
556
|
+
{
|
|
557
|
+
checked: allSelected,
|
|
558
|
+
indeterminate: someSelected,
|
|
559
|
+
label: text.selectAll(collection.filteredRows.filter((row) => collection.canSelect?.(row)).length),
|
|
560
|
+
onChange: (checked) => checked ? collection.selectAllFiltered() : collection.clearFilteredSelection()
|
|
561
|
+
}
|
|
562
|
+
) }) : null,
|
|
563
|
+
columns.map((column) => {
|
|
564
|
+
const sorted = collection.sort?.id === column.id ? collection.sort.desc ? "descending" : "ascending" : "none";
|
|
565
|
+
return /* @__PURE__ */ jsx("th", { scope: "col", "aria-sort": column.sortable ? sorted : void 0, "data-align": alignOf(column), "data-width": column.width === "min" ? "min" : void 0, style: widthStyle(column.width), children: column.sortable ? /* @__PURE__ */ jsxs("button", { type: "button", className: "TableSortButton", onClick: () => collection.cycleSort(column.id), children: [
|
|
566
|
+
displayHeader(column, collection.filteredRows),
|
|
567
|
+
/* @__PURE__ */ jsx("span", { className: "material-symbols-outlined TableSortIcon", "data-state": sorted, "aria-hidden": "true", children: sorted === "ascending" ? "arrow_upward" : sorted === "descending" ? "arrow_downward" : "swap_vert" })
|
|
568
|
+
] }) : displayHeader(column, collection.filteredRows) }, column.id);
|
|
569
|
+
}),
|
|
570
|
+
rowActions ? /* @__PURE__ */ jsx("th", { scope: "col", "data-width": "min", children: /* @__PURE__ */ jsx("span", { className: "visually-hidden", children: "Actions" }) }) : null
|
|
571
|
+
] }) }),
|
|
572
|
+
collection.pageEntries.some((entry) => entry.kind === "group") ? groupedBodies({ collection, columns, leaves, roving, navigate, onRowActivate, getRowHref, activeRowId, rowActions, colSpan, filteredEmpty, empty, loading }) : /* @__PURE__ */ jsx("tbody", { children: bodyRows({ collection, columns, leaves, roving, navigate, onRowActivate, getRowHref, activeRowId, rowActions, colSpan, filteredEmpty, empty, loading }) }),
|
|
573
|
+
Object.keys(collection.totals).length ? /* @__PURE__ */ jsx("tfoot", { children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
574
|
+
collection.canSelect ? /* @__PURE__ */ jsx("td", {}) : null,
|
|
575
|
+
columns.map((column, index) => /* @__PURE__ */ jsxs("td", { "data-align": alignOf(column), children: [
|
|
576
|
+
index === 0 ? text.total : typeof column.total === "function" ? column.total(collection.filteredRows) : collection.totals[column.id] ?? "",
|
|
577
|
+
index === 0 && collection.isFiltered ? /* @__PURE__ */ jsxs("span", { className: "visually-hidden", children: [
|
|
578
|
+
" of ",
|
|
579
|
+
collection.filteredCount,
|
|
580
|
+
" filtered rows"
|
|
581
|
+
] }) : null
|
|
582
|
+
] }, column.id)),
|
|
583
|
+
rowActions ? /* @__PURE__ */ jsx("td", {}) : null
|
|
584
|
+
] }) }) : null
|
|
585
|
+
]
|
|
586
|
+
}
|
|
587
|
+
),
|
|
588
|
+
loading && collection.totalCount === 0 ? /* @__PURE__ */ jsx(LoadingNote, { text: text.loading }) : null
|
|
589
|
+
]
|
|
590
|
+
}
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
function bodyRows(args) {
|
|
594
|
+
if (args.loading && args.collection.totalCount === 0) return null;
|
|
595
|
+
if (args.leaves.length === 0) {
|
|
596
|
+
return /* @__PURE__ */ jsx(EmptyRow, { colSpan: args.colSpan, filtered: args.filteredEmpty || args.collection.isFiltered, onClear: args.collection.clearFilters, empty: args.empty });
|
|
597
|
+
}
|
|
598
|
+
return args.leaves.map((entry, index) => /* @__PURE__ */ jsx(DataRow, { entry, index, ...args }, entry.row.id));
|
|
599
|
+
}
|
|
600
|
+
function groupedBodies(args) {
|
|
601
|
+
const groups = [];
|
|
602
|
+
let buffer = [];
|
|
603
|
+
let header = null;
|
|
604
|
+
let bodyKey = "rows";
|
|
605
|
+
const flush = () => {
|
|
606
|
+
if (!header && buffer.length === 0) return;
|
|
607
|
+
groups.push(
|
|
608
|
+
/* @__PURE__ */ jsxs("tbody", { children: [
|
|
609
|
+
header,
|
|
610
|
+
buffer.map((item) => /* @__PURE__ */ jsx(DataRow, { entry: item, index: item.index, ...args }, item.row.id))
|
|
611
|
+
] }, bodyKey)
|
|
612
|
+
);
|
|
613
|
+
header = null;
|
|
614
|
+
buffer = [];
|
|
615
|
+
};
|
|
616
|
+
let leafIndex = 0;
|
|
617
|
+
for (const entry of args.collection.pageEntries) {
|
|
618
|
+
if (entry.kind === "group") {
|
|
619
|
+
flush();
|
|
620
|
+
const row = entry.row;
|
|
621
|
+
bodyKey = row.id;
|
|
622
|
+
const selectable = (row.subRows ?? []).filter((child) => args.collection.canSelect?.(child.original));
|
|
623
|
+
const count = selectable.length;
|
|
624
|
+
const name = `${String(row.groupingValue ?? "group")}${entry.continued ? " (continued)" : ""}`;
|
|
625
|
+
const leaves = (row.subRows ?? []).map((child) => child.original);
|
|
626
|
+
header = /* @__PURE__ */ jsxs("tr", { "data-group": "", children: [
|
|
627
|
+
args.collection.canSelect ? /* @__PURE__ */ jsx("td", { "data-width": "min", children: count > 0 ? /* @__PURE__ */ jsx(
|
|
628
|
+
SelectBox,
|
|
629
|
+
{
|
|
630
|
+
checked: row.getIsAllSubRowsSelected?.() ?? false,
|
|
631
|
+
indeterminate: Boolean(row.getIsSomeSelected?.() && !row.getIsAllSubRowsSelected?.()),
|
|
632
|
+
label: `Select ${count} rows in ${row.groupingValue ?? "group"}`,
|
|
633
|
+
onChange: () => row.toggleSelected?.()
|
|
634
|
+
}
|
|
635
|
+
) : null }) : null,
|
|
636
|
+
args.columns.map((column, index) => {
|
|
637
|
+
const subtotal = column.total ? typeof column.total === "function" ? column.total(leaves) : totalText(column, leaves, args.collection.locale) : "";
|
|
638
|
+
const label = `${name} \xB7 ${count}`;
|
|
639
|
+
if (index === 0) {
|
|
640
|
+
return /* @__PURE__ */ jsxs("th", { scope: "rowgroup", "data-align": alignOf(column), children: [
|
|
641
|
+
/* @__PURE__ */ jsxs("button", { type: "button", className: "CollectionGroupButton", "aria-expanded": row.getIsExpanded(), onClick: () => row.toggleExpanded(), children: [
|
|
642
|
+
/* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: row.getIsExpanded() ? "expand_more" : "chevron_right" }),
|
|
643
|
+
label
|
|
644
|
+
] }),
|
|
645
|
+
subtotal ? /* @__PURE__ */ jsx("span", { children: subtotal }) : null
|
|
646
|
+
] }, column.id);
|
|
647
|
+
}
|
|
648
|
+
return /* @__PURE__ */ jsx("td", { "data-align": alignOf(column), children: subtotal }, column.id);
|
|
649
|
+
}),
|
|
650
|
+
args.rowActions ? /* @__PURE__ */ jsx("td", {}) : null
|
|
651
|
+
] });
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
buffer.push({ row: entry.row, index: leafIndex });
|
|
655
|
+
leafIndex += 1;
|
|
656
|
+
}
|
|
657
|
+
flush();
|
|
658
|
+
if (!groups.length) return /* @__PURE__ */ jsx("tbody", { children: bodyRows(args) });
|
|
659
|
+
return groups;
|
|
660
|
+
}
|
|
661
|
+
function DataRow({ entry, index, collection, columns, roving, navigate, onRowActivate, getRowHref, activeRowId, rowActions }) {
|
|
662
|
+
const original = entry.row.original;
|
|
663
|
+
const id = entry.row.id;
|
|
664
|
+
const selected = collection.selectionSet.has(id);
|
|
665
|
+
const active = activeRowId === id;
|
|
666
|
+
const name = rowHeaderText(collection, original);
|
|
667
|
+
const named = levelName(collection, entry.row, name);
|
|
668
|
+
const canSelect = collection.canSelect?.(original) ?? false;
|
|
669
|
+
const href = getRowHref?.(original);
|
|
670
|
+
const activate = () => onRowActivate?.(original);
|
|
671
|
+
const toggle = canSelect ? () => entry.row.toggleSelected?.(!selected) : void 0;
|
|
672
|
+
const tabIndex = navigate ? roving.cursor === index ? 0 : -1 : void 0;
|
|
673
|
+
const parent = Boolean(entry.row.subRows?.length);
|
|
674
|
+
const onKeyDown = (event) => roving.onKeyDown(event, index, toggle, href ? void 0 : activate, parent ? { expanded: Boolean(entry.row.getIsExpanded?.()), toggle: () => entry.row.toggleExpanded?.() } : void 0);
|
|
675
|
+
const disclosure = parent ? /* @__PURE__ */ jsx("button", { type: "button", className: "CollectionGroupButton", "aria-expanded": entry.row.getIsExpanded(), "aria-label": `${entry.row.getIsExpanded() ? "Collapse" : "Expand"} ${name}`, onClick: (event) => {
|
|
676
|
+
event.stopPropagation();
|
|
677
|
+
entry.row.toggleExpanded?.();
|
|
678
|
+
}, children: /* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: entry.row.getIsExpanded() ? "expand_more" : "chevron_right" }) }) : null;
|
|
679
|
+
const header = href ? /* @__PURE__ */ jsx("a", { ref: (node) => {
|
|
680
|
+
roving.refs.current[index] = node;
|
|
681
|
+
}, href, tabIndex, onKeyDown, onClick: () => roving.focusAt(index), children: name }) : /* @__PURE__ */ jsx(
|
|
682
|
+
"button",
|
|
683
|
+
{
|
|
684
|
+
type: "button",
|
|
685
|
+
className: "CollectionRowButton",
|
|
686
|
+
ref: (node) => {
|
|
687
|
+
roving.refs.current[index] = node;
|
|
688
|
+
},
|
|
689
|
+
tabIndex,
|
|
690
|
+
"aria-label": named === name ? void 0 : named,
|
|
691
|
+
onKeyDown,
|
|
692
|
+
onClick: () => {
|
|
693
|
+
roving.focusAt(index);
|
|
694
|
+
activate();
|
|
695
|
+
},
|
|
696
|
+
children: name
|
|
697
|
+
}
|
|
698
|
+
);
|
|
699
|
+
return /* @__PURE__ */ jsxs(
|
|
700
|
+
"tr",
|
|
701
|
+
{
|
|
702
|
+
"aria-selected": selected || void 0,
|
|
703
|
+
"aria-current": active ? "true" : void 0,
|
|
704
|
+
"data-depth": entry.row.depth || void 0,
|
|
705
|
+
onClick: (event) => {
|
|
706
|
+
const target = event.target;
|
|
707
|
+
if (interactiveTarget(target) || selectingText()) return;
|
|
708
|
+
if (target instanceof Element && target.closest(".CollectionSelectColumn, [data-actions]")) return;
|
|
709
|
+
if (href && !onRowActivate) {
|
|
710
|
+
event.currentTarget.querySelector("a")?.click();
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
activate();
|
|
714
|
+
},
|
|
715
|
+
children: [
|
|
716
|
+
collection.canSelect ? /* @__PURE__ */ jsx("td", { "data-width": "min", children: canSelect ? /* @__PURE__ */ jsx(SelectBox, { checked: selected, label: `Select ${named}`, onChange: (checked) => entry.row.toggleSelected?.(checked) }) : null }) : null,
|
|
717
|
+
columns.map((column) => {
|
|
718
|
+
const Tag = column.rowHeader ? "th" : "td";
|
|
719
|
+
return /* @__PURE__ */ jsx(Tag, { scope: column.rowHeader ? "row" : void 0, "data-align": alignOf(column), style: column.rowHeader ? { paddingInlineStart: `calc(var(--sl-table-cell-padding-inline) + var(--sl-collection-tree-indent) * ${entry.row.depth})` } : void 0, children: column.rowHeader ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
720
|
+
disclosure,
|
|
721
|
+
header
|
|
722
|
+
] }) : /* @__PURE__ */ jsx(CellValue, { column, row: original, locale: collection.locale, rows: collection.filteredRows, view: "table", selected, active }) }, column.id);
|
|
723
|
+
}),
|
|
724
|
+
rowActions ? /* @__PURE__ */ jsx("td", { "data-width": "min", "data-actions": "", children: /* @__PURE__ */ jsx(RowMenu, { label: collection.messages.actionsFor(name), actions: rowActions(original) }) }) : null
|
|
725
|
+
]
|
|
726
|
+
}
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
function visibleColumns(collection) {
|
|
730
|
+
const order = collection.columnOrder.length ? collection.columnOrder : collection.columns.map((column) => column.id);
|
|
731
|
+
return order.map((id) => collection.columns.find((column) => column.id === id)).filter((column) => Boolean(column && collection.columnVisibility[column.id] !== false));
|
|
732
|
+
}
|
|
733
|
+
function widthStyle(width) {
|
|
734
|
+
if (!width || width === "auto" || width === "min") return void 0;
|
|
735
|
+
return { width: `var(--sl-${width.replace(/\./g, "-")})` };
|
|
736
|
+
}
|
|
737
|
+
function CollectionPagination({
|
|
738
|
+
page,
|
|
739
|
+
pageCount,
|
|
740
|
+
pageSize,
|
|
741
|
+
total,
|
|
742
|
+
filtered,
|
|
743
|
+
onPageChange,
|
|
744
|
+
onPageSizeChange,
|
|
745
|
+
pageSizeOptions,
|
|
746
|
+
label,
|
|
747
|
+
previousLabel = "Previous",
|
|
748
|
+
nextLabel = "Next",
|
|
749
|
+
rowsPerPageLabel = "Rows per page",
|
|
750
|
+
className
|
|
751
|
+
}) {
|
|
752
|
+
const size = pageSize && pageSize > 0 ? pageSize : total;
|
|
753
|
+
const start = total === 0 ? 0 : (page - 1) * size + 1;
|
|
754
|
+
const end = Math.min(total, page * size);
|
|
755
|
+
return /* @__PURE__ */ jsxs("nav", { className: cn("CollectionPagination", className), "aria-label": `Pagination for ${label}`, children: [
|
|
756
|
+
/* @__PURE__ */ jsxs("p", { children: [
|
|
757
|
+
total === 0 ? "0" : `${start}\u2013${end}`,
|
|
758
|
+
" of ",
|
|
759
|
+
total,
|
|
760
|
+
filtered ? " filtered" : ""
|
|
761
|
+
] }),
|
|
762
|
+
/* @__PURE__ */ jsxs(Button, { emphasis: "quiet", disabled: page <= 1, onClick: () => onPageChange(page - 1), children: [
|
|
763
|
+
/* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: "chevron_left" }),
|
|
764
|
+
previousLabel
|
|
765
|
+
] }),
|
|
766
|
+
/* @__PURE__ */ jsxs(Button, { emphasis: "quiet", disabled: page >= pageCount, onClick: () => onPageChange(page + 1), children: [
|
|
767
|
+
nextLabel,
|
|
768
|
+
/* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: "chevron_right" })
|
|
769
|
+
] }),
|
|
770
|
+
pageSizeOptions?.length && onPageSizeChange ? /* @__PURE__ */ jsxs(
|
|
771
|
+
Select.Root,
|
|
772
|
+
{
|
|
773
|
+
items: pageSizeOptions.map((option) => ({ value: String(option), label: `${option} per page` })),
|
|
774
|
+
value: String(pageSize ?? ""),
|
|
775
|
+
onValueChange: (value) => onPageSizeChange(Number(value)),
|
|
776
|
+
children: [
|
|
777
|
+
/* @__PURE__ */ jsxs(Select.Trigger, { "aria-label": rowsPerPageLabel, children: [
|
|
778
|
+
/* @__PURE__ */ jsx(Select.Value, {}),
|
|
779
|
+
/* @__PURE__ */ jsx(Select.Icon, { children: /* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", children: "expand_more" }) })
|
|
780
|
+
] }),
|
|
781
|
+
/* @__PURE__ */ jsx(Select.Portal, { children: /* @__PURE__ */ jsx(Select.Positioner, { children: /* @__PURE__ */ jsx(Select.Popup, { children: /* @__PURE__ */ jsx(Select.List, { children: pageSizeOptions.map((option) => /* @__PURE__ */ jsxs(Select.Item, { value: String(option), children: [
|
|
782
|
+
option,
|
|
783
|
+
" per page"
|
|
784
|
+
] }, option)) }) }) }) })
|
|
785
|
+
]
|
|
786
|
+
}
|
|
787
|
+
) : null
|
|
788
|
+
] });
|
|
789
|
+
}
|
|
790
|
+
var COMBOBOX_AT = 12;
|
|
791
|
+
function CollectionToolbarImpl({ collection, searchLabel, searchLabelVisible, showSort, groupOptions, actions, bulkActions, className }) {
|
|
792
|
+
const tabs = collection.columns.find((column) => column.filter?.kind === "tabs");
|
|
793
|
+
const selects = collection.columns.filter((column) => column.filter && column.filter.kind !== "tabs");
|
|
794
|
+
const searchable = collection.columns.some((column) => column.searchable);
|
|
795
|
+
if (process.env.NODE_ENV !== "production" && searchable && !searchLabel) {
|
|
796
|
+
console.warn("CollectionToolbar needs searchLabel when a column is searchable.");
|
|
797
|
+
}
|
|
798
|
+
const [announced, setAnnounced] = React4.useState("");
|
|
799
|
+
const seenSelection = React4.useRef(false);
|
|
800
|
+
React4.useEffect(() => {
|
|
801
|
+
const first = !seenSelection.current;
|
|
802
|
+
seenSelection.current = true;
|
|
803
|
+
if (first && collection.selection.length === 0) return;
|
|
804
|
+
setAnnounced("");
|
|
805
|
+
const timer = window.setTimeout(() => {
|
|
806
|
+
if (!collection.selection.length) {
|
|
807
|
+
setAnnounced(collection.messages.selectionCleared);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
const hidden = collection.hiddenSelectedCount ? `, ${collection.messages.hiddenByFilters(collection.hiddenSelectedCount)}` : "";
|
|
811
|
+
setAnnounced(`${collection.messages.selected(collection.selection.length)}${hidden}`);
|
|
812
|
+
}, 200);
|
|
813
|
+
return () => window.clearTimeout(timer);
|
|
814
|
+
}, [collection.selection, collection.hiddenSelectedCount, collection.messages]);
|
|
815
|
+
const selecting = collection.selection.length > 0;
|
|
816
|
+
const narrow = useUnderWidth("--sl-collection-narrow-width");
|
|
817
|
+
return /* @__PURE__ */ jsxs("div", { ref: narrow.ref, className: cn("CollectionToolbar", className), "data-selecting": selecting ? "" : void 0, "data-narrow": narrow.under ? "" : void 0, children: [
|
|
818
|
+
tabs ? /* @__PURE__ */ jsx(ToolbarTabs, { collection, column: tabs }) : null,
|
|
819
|
+
/* @__PURE__ */ jsxs("div", { className: "CollectionToolbarMain", children: [
|
|
820
|
+
searchable ? /* @__PURE__ */ jsx(
|
|
821
|
+
SearchField,
|
|
822
|
+
{
|
|
823
|
+
label: searchLabel ?? "Search",
|
|
824
|
+
visible: searchLabelVisible,
|
|
825
|
+
value: collection.search,
|
|
826
|
+
onChange: collection.setSearch,
|
|
827
|
+
clearLabel: collection.messages.clearSearch
|
|
828
|
+
}
|
|
829
|
+
) : null,
|
|
830
|
+
selecting ? /* @__PURE__ */ jsxs("div", { className: "CollectionSelectionBar", role: "region", "aria-label": "Selection", children: [
|
|
831
|
+
/* @__PURE__ */ jsxs("p", { className: "CollectionCount", children: [
|
|
832
|
+
collection.messages.selected(collection.selection.length),
|
|
833
|
+
collection.hiddenSelectedCount > 0 ? /* @__PURE__ */ jsxs("span", { children: [
|
|
834
|
+
" \xB7 ",
|
|
835
|
+
collection.messages.hiddenByFilters(collection.hiddenSelectedCount)
|
|
836
|
+
] }) : null
|
|
837
|
+
] }),
|
|
838
|
+
/* @__PURE__ */ jsx("div", { className: "CollectionToolbarActions", children: bulkActions }),
|
|
839
|
+
/* @__PURE__ */ jsx(Button, { emphasis: "quiet", onClick: collection.clearSelection, children: "Clear selection" })
|
|
840
|
+
] }) : /* @__PURE__ */ jsxs("div", { className: "CollectionToolbarTrailing", children: [
|
|
841
|
+
selects.map((column) => /* @__PURE__ */ jsx(FilterControl, { collection, column }, column.id)),
|
|
842
|
+
groupOptions?.length ? /* @__PURE__ */ jsx(
|
|
843
|
+
FilterSelect,
|
|
844
|
+
{
|
|
845
|
+
label: "Group by",
|
|
846
|
+
value: collection.grouping ?? "",
|
|
847
|
+
options: [{ value: "", label: "No grouping" }, ...groupOptions],
|
|
848
|
+
onChange: (value) => collection.setGrouping(typeof value === "string" && value ? value : null)
|
|
849
|
+
}
|
|
850
|
+
) : null,
|
|
851
|
+
showSort ? /* @__PURE__ */ jsx(SortControl, { collection }) : null,
|
|
852
|
+
actions ? /* @__PURE__ */ jsx("div", { className: "CollectionToolbarActions", children: actions }) : null
|
|
853
|
+
] })
|
|
854
|
+
] }),
|
|
855
|
+
/* @__PURE__ */ jsx("div", { className: "visually-hidden", "aria-live": "polite", children: announced })
|
|
856
|
+
] });
|
|
857
|
+
}
|
|
858
|
+
function ToolbarTabs({ collection, column }) {
|
|
859
|
+
const counts = collection.facetCounts(column.id);
|
|
860
|
+
const options = column.filter?.options ?? [...counts.keys()].filter((value2) => value2 !== "").sort().map((value2) => ({ value: value2, label: value2 }));
|
|
861
|
+
const allCount = [...counts.values()].reduce((sum, count) => sum + count, 0);
|
|
862
|
+
const current = collection.filters[column.id];
|
|
863
|
+
const value = current == null || current === "" ? "" : String(current);
|
|
864
|
+
return /* @__PURE__ */ jsx(Tabs.Root, { value, onValueChange: (next) => collection.setFilter(column.id, next === "" ? void 0 : next), children: /* @__PURE__ */ jsx(Tabs.List, { children: options.map((option) => {
|
|
865
|
+
const count = option.value === "" || option.all ? allCount : counts.get(option.value) ?? 0;
|
|
866
|
+
const tabValue = option.all ? "" : option.value;
|
|
867
|
+
return /* @__PURE__ */ jsxs(Tabs.Tab, { value: tabValue, children: [
|
|
868
|
+
option.label,
|
|
869
|
+
" ",
|
|
870
|
+
count
|
|
871
|
+
] }, tabValue || "all");
|
|
872
|
+
}) }) });
|
|
873
|
+
}
|
|
874
|
+
function SearchField({ label, visible, value, onChange, clearLabel = "Clear search" }) {
|
|
875
|
+
const id = React4.useId();
|
|
876
|
+
const input = React4.useRef(null);
|
|
877
|
+
return /* @__PURE__ */ jsxs("div", { className: "CollectionSearch", children: [
|
|
878
|
+
/* @__PURE__ */ jsx("label", { htmlFor: id, className: visible ? void 0 : "visually-hidden", children: label }),
|
|
879
|
+
/* @__PURE__ */ jsxs("div", { className: "CollectionSearchField", "data-filled": value ? "" : void 0, children: [
|
|
880
|
+
/* @__PURE__ */ jsx("span", { className: "CollectionSearchIcon material-symbols-outlined", "aria-hidden": "true", children: "search" }),
|
|
881
|
+
/* @__PURE__ */ jsx(
|
|
882
|
+
Input,
|
|
883
|
+
{
|
|
884
|
+
ref: input,
|
|
885
|
+
id,
|
|
886
|
+
type: "search",
|
|
887
|
+
value,
|
|
888
|
+
onChange: (event) => onChange(event.target.value),
|
|
889
|
+
onKeyDown: (event) => {
|
|
890
|
+
if (event.key === "Escape" && value) {
|
|
891
|
+
event.preventDefault();
|
|
892
|
+
onChange("");
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
),
|
|
897
|
+
value ? /* @__PURE__ */ jsx(
|
|
898
|
+
"button",
|
|
899
|
+
{
|
|
900
|
+
type: "button",
|
|
901
|
+
className: "CollectionSearchClear",
|
|
902
|
+
"aria-label": clearLabel,
|
|
903
|
+
onClick: () => {
|
|
904
|
+
onChange("");
|
|
905
|
+
input.current?.focus();
|
|
906
|
+
},
|
|
907
|
+
children: /* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: "close" })
|
|
908
|
+
}
|
|
909
|
+
) : null
|
|
910
|
+
] })
|
|
911
|
+
] });
|
|
912
|
+
}
|
|
913
|
+
function FilterControl({ collection, column }) {
|
|
914
|
+
const counts = collection.facetCounts(column.id);
|
|
915
|
+
const options = column.filter?.options ?? [...counts.keys()].filter((value) => value !== "").sort().map((value) => ({ value, label: value }));
|
|
916
|
+
const label = column.filter?.label ?? headerLabel(column);
|
|
917
|
+
if (column.filter?.kind === "boolean") {
|
|
918
|
+
const value = collection.filters[column.id];
|
|
919
|
+
return /* @__PURE__ */ jsx(
|
|
920
|
+
FilterSelect,
|
|
921
|
+
{
|
|
922
|
+
label,
|
|
923
|
+
value: value == null || value === "" ? "" : String(value),
|
|
924
|
+
options: [{ value: "", label: "Any" }, { value: "true", label: "Yes" }, { value: "false", label: "No" }],
|
|
925
|
+
onChange: (next) => collection.setFilter(column.id, next === "" || Array.isArray(next) ? void 0 : next === "true")
|
|
926
|
+
}
|
|
927
|
+
);
|
|
928
|
+
}
|
|
929
|
+
if (column.filter?.kind === "multi-select") {
|
|
930
|
+
const selected = Array.isArray(collection.filters[column.id]) ? collection.filters[column.id] : [];
|
|
931
|
+
return /* @__PURE__ */ jsx(
|
|
932
|
+
FilterSelect,
|
|
933
|
+
{
|
|
934
|
+
label,
|
|
935
|
+
multiple: true,
|
|
936
|
+
value: selected,
|
|
937
|
+
options,
|
|
938
|
+
onChange: (next) => {
|
|
939
|
+
const values = Array.isArray(next) ? next : [];
|
|
940
|
+
collection.setFilter(column.id, values.length ? values : void 0);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
);
|
|
944
|
+
}
|
|
945
|
+
if (options.length > COMBOBOX_AT) {
|
|
946
|
+
return /* @__PURE__ */ jsx(
|
|
947
|
+
ComboboxField,
|
|
948
|
+
{
|
|
949
|
+
label,
|
|
950
|
+
value: String(collection.filters[column.id] ?? ""),
|
|
951
|
+
options,
|
|
952
|
+
onChange: (next) => collection.setFilter(column.id, next || void 0)
|
|
953
|
+
}
|
|
954
|
+
);
|
|
955
|
+
}
|
|
956
|
+
return /* @__PURE__ */ jsx(
|
|
957
|
+
FilterSelect,
|
|
958
|
+
{
|
|
959
|
+
label,
|
|
960
|
+
value: String(collection.filters[column.id] ?? ""),
|
|
961
|
+
options: [{ value: "", label: "Any" }, ...options],
|
|
962
|
+
onChange: (next) => collection.setFilter(column.id, next || void 0)
|
|
963
|
+
}
|
|
964
|
+
);
|
|
965
|
+
}
|
|
966
|
+
function FilterSelect({ label, value, options, onChange, multiple }) {
|
|
967
|
+
const labelId = React4.useId();
|
|
968
|
+
const valueId = React4.useId();
|
|
969
|
+
const items = options.map((option) => ({ value: option.value, label: option.label }));
|
|
970
|
+
const chosen = Array.isArray(value) ? value.length ? `${value.length} selected` : "Any" : options.find((option) => option.value === value)?.label ?? "Any";
|
|
971
|
+
return /* @__PURE__ */ jsxs(Select.Root, { items, multiple, value: multiple ? value : value, onValueChange: (next) => onChange(multiple ? Array.isArray(next) ? next.map(String) : [] : next == null ? "" : String(next)), children: [
|
|
972
|
+
/* @__PURE__ */ jsxs(Select.Trigger, { "aria-labelledby": `${labelId} ${valueId}`, children: [
|
|
973
|
+
/* @__PURE__ */ jsx("span", { id: labelId, className: "visually-hidden", children: label }),
|
|
974
|
+
/* @__PURE__ */ jsxs("span", { id: valueId, children: [
|
|
975
|
+
label,
|
|
976
|
+
": ",
|
|
977
|
+
chosen
|
|
978
|
+
] }),
|
|
979
|
+
/* @__PURE__ */ jsx(Select.Icon, { children: /* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", children: "expand_more" }) })
|
|
980
|
+
] }),
|
|
981
|
+
/* @__PURE__ */ jsx(Select.Portal, { children: /* @__PURE__ */ jsx(Select.Positioner, { children: /* @__PURE__ */ jsx(Select.Popup, { children: /* @__PURE__ */ jsx(Select.List, { children: items.map((item) => /* @__PURE__ */ jsx(Select.Item, { value: item.value, children: item.label }, item.value || "any")) }) }) }) })
|
|
982
|
+
] });
|
|
983
|
+
}
|
|
984
|
+
function ComboboxField({ label, value, options, onChange }) {
|
|
985
|
+
const id = React4.useId();
|
|
986
|
+
return /* @__PURE__ */ jsxs(Combobox.Root, { items: options.map((option) => option.label), value: options.find((option) => option.value === value)?.label ?? null, onValueChange: (next) => {
|
|
987
|
+
const match = options.find((option) => option.label === next);
|
|
988
|
+
onChange(match?.value ?? "");
|
|
989
|
+
}, children: [
|
|
990
|
+
/* @__PURE__ */ jsx("label", { htmlFor: id, className: "visually-hidden", children: label }),
|
|
991
|
+
/* @__PURE__ */ jsx("div", { className: "ComboboxInputWrapper", children: /* @__PURE__ */ jsx(Combobox.Input, { id, placeholder: label, "aria-label": label }) }),
|
|
992
|
+
/* @__PURE__ */ jsx(Combobox.Portal, { children: /* @__PURE__ */ jsx(Combobox.Positioner, { children: /* @__PURE__ */ jsxs(Combobox.Popup, { children: [
|
|
993
|
+
/* @__PURE__ */ jsx(Combobox.Empty, { children: "No matches" }),
|
|
994
|
+
/* @__PURE__ */ jsx(Combobox.List, { children: (item) => /* @__PURE__ */ jsx(Combobox.Item, { value: item, children: item }, item) })
|
|
995
|
+
] }) }) })
|
|
996
|
+
] });
|
|
997
|
+
}
|
|
998
|
+
function SortControl({ collection }) {
|
|
999
|
+
const columns = collection.columns.filter((column) => column.sortable);
|
|
1000
|
+
return /* @__PURE__ */ jsxs("div", { className: "CollectionSort", children: [
|
|
1001
|
+
/* @__PURE__ */ jsx(
|
|
1002
|
+
FilterSelect,
|
|
1003
|
+
{
|
|
1004
|
+
label: "Sort",
|
|
1005
|
+
value: collection.sort?.id ?? "",
|
|
1006
|
+
options: columns.map((column) => ({ value: column.id, label: headerLabel(column) })),
|
|
1007
|
+
onChange: (id) => {
|
|
1008
|
+
const next = typeof id === "string" ? id : "";
|
|
1009
|
+
collection.setSort(next ? { id: next, desc: collection.sort?.id === next ? collection.sort.desc : false } : null);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
),
|
|
1013
|
+
/* @__PURE__ */ jsx(
|
|
1014
|
+
Button,
|
|
1015
|
+
{
|
|
1016
|
+
emphasis: "quiet",
|
|
1017
|
+
"aria-pressed": Boolean(collection.sort?.desc),
|
|
1018
|
+
"aria-label": collection.sort?.desc ? "Sort direction, descending" : "Sort direction, ascending",
|
|
1019
|
+
disabled: !collection.sort,
|
|
1020
|
+
onClick: () => collection.sort && collection.setSort({ ...collection.sort, desc: !collection.sort.desc }),
|
|
1021
|
+
children: /* @__PURE__ */ jsx("span", { className: "material-symbols-outlined", "aria-hidden": "true", children: collection.sort?.desc ? "arrow_downward" : "arrow_upward" })
|
|
1022
|
+
}
|
|
1023
|
+
)
|
|
1024
|
+
] });
|
|
1025
|
+
}
|
|
1026
|
+
function ToolbarClear({ collection }) {
|
|
1027
|
+
if (!collection.isFiltered) return null;
|
|
1028
|
+
return /* @__PURE__ */ jsx(Button, { emphasis: "quiet", onClick: collection.clearFilters, children: collection.messages.clearFilters });
|
|
1029
|
+
}
|
|
1030
|
+
function ToolbarActions({ children }) {
|
|
1031
|
+
return /* @__PURE__ */ jsx("div", { className: "CollectionToolbarActions", children });
|
|
1032
|
+
}
|
|
1033
|
+
function ToolbarSelection({ collection, children }) {
|
|
1034
|
+
if (!collection.selection.length) return null;
|
|
1035
|
+
return /* @__PURE__ */ jsxs("div", { className: "CollectionSelectionBar", role: "region", "aria-label": "Selection", children: [
|
|
1036
|
+
/* @__PURE__ */ jsx("p", { className: "CollectionCount", children: collection.messages.selected(collection.selection.length) }),
|
|
1037
|
+
children,
|
|
1038
|
+
/* @__PURE__ */ jsx(Button, { emphasis: "quiet", onClick: collection.clearSelection, children: "Clear selection" })
|
|
1039
|
+
] });
|
|
1040
|
+
}
|
|
1041
|
+
function ToolbarRoot({ className, children }) {
|
|
1042
|
+
return /* @__PURE__ */ jsx("div", { className: cn("CollectionToolbar", className), children });
|
|
1043
|
+
}
|
|
1044
|
+
var CollectionToolbar = Object.assign(CollectionToolbarImpl, {
|
|
1045
|
+
Root: ToolbarRoot,
|
|
1046
|
+
Tabs: ToolbarTabs,
|
|
1047
|
+
Search: SearchField,
|
|
1048
|
+
Filter: FilterControl,
|
|
1049
|
+
Sort: SortControl,
|
|
1050
|
+
Clear: ToolbarClear,
|
|
1051
|
+
Actions: ToolbarActions,
|
|
1052
|
+
SelectionBar: ToolbarSelection
|
|
1053
|
+
});
|
|
1054
|
+
var collectionFeatures = tableFeatures({
|
|
1055
|
+
rowSortingFeature,
|
|
1056
|
+
columnFilteringFeature,
|
|
1057
|
+
globalFilteringFeature,
|
|
1058
|
+
columnFacetingFeature,
|
|
1059
|
+
rowPaginationFeature,
|
|
1060
|
+
rowSelectionFeature,
|
|
1061
|
+
columnGroupingFeature,
|
|
1062
|
+
rowExpandingFeature,
|
|
1063
|
+
columnOrderingFeature,
|
|
1064
|
+
columnVisibilityFeature,
|
|
1065
|
+
sortedRowModel: createSortedRowModel(),
|
|
1066
|
+
filteredRowModel: createFilteredRowModel(),
|
|
1067
|
+
groupedRowModel: createGroupedRowModel(),
|
|
1068
|
+
expandedRowModel: createExpandedRowModel(),
|
|
1069
|
+
paginatedRowModel: createPaginatedRowModel(),
|
|
1070
|
+
facetedRowModel: createFacetedRowModel(),
|
|
1071
|
+
facetedUniqueValues: createFacetedUniqueValues()
|
|
1072
|
+
});
|
|
1073
|
+
function toColumnDefs(columns) {
|
|
1074
|
+
return columns.map((column) => ({
|
|
1075
|
+
id: column.id,
|
|
1076
|
+
accessorFn: (row) => {
|
|
1077
|
+
const value = column.value(row);
|
|
1078
|
+
return isEmptyValue(value) ? void 0 : value;
|
|
1079
|
+
},
|
|
1080
|
+
header: headerLabel(column),
|
|
1081
|
+
enableSorting: Boolean(column.sortable),
|
|
1082
|
+
sortUndefined: "last",
|
|
1083
|
+
// A tie returns 0. TanStack multiplies the result by -1 when descending, so an index tie would reverse, and it already falls back to the row index.
|
|
1084
|
+
sortFn: ((rowA, rowB, columnId) => {
|
|
1085
|
+
const locale = rowA.table.options.meta?.locale ?? "en";
|
|
1086
|
+
return comparePresent(rowA.getValue(columnId), rowB.getValue(columnId), column.kind ?? "text", collatorFor(locale));
|
|
1087
|
+
}),
|
|
1088
|
+
enableGlobalFilter: column.searchable != null && column.searchable !== false,
|
|
1089
|
+
enableColumnFilter: Boolean(column.filter),
|
|
1090
|
+
enableGrouping: true,
|
|
1091
|
+
enableHiding: true,
|
|
1092
|
+
filterFn: column.filter ? filterFor() : void 0,
|
|
1093
|
+
meta: column,
|
|
1094
|
+
...column.tanstack
|
|
1095
|
+
}));
|
|
1096
|
+
}
|
|
1097
|
+
function filterFor(column) {
|
|
1098
|
+
const fn = (row, columnId, filterValue) => {
|
|
1099
|
+
if (filterValue == null || filterValue === "" || Array.isArray(filterValue) && filterValue.length === 0) return true;
|
|
1100
|
+
const value = String(row.getValue(columnId) ?? "");
|
|
1101
|
+
if (Array.isArray(filterValue)) return filterValue.map(String).includes(value);
|
|
1102
|
+
if (typeof filterValue === "boolean") return String(Boolean(row.getValue(columnId))) === String(filterValue) || value === String(filterValue);
|
|
1103
|
+
return value === String(filterValue);
|
|
1104
|
+
};
|
|
1105
|
+
fn.autoRemove = (filterValue) => filterValue == null || filterValue === "" || Array.isArray(filterValue) && filterValue.length === 0;
|
|
1106
|
+
return fn;
|
|
1107
|
+
}
|
|
1108
|
+
function collectionTableOptions(input) {
|
|
1109
|
+
const columns = input.columns;
|
|
1110
|
+
const locale = input.locale;
|
|
1111
|
+
const searchKey = `${locale}\0${columns.map((column) => column.id).join("\0")}`;
|
|
1112
|
+
const searchCache = /* @__PURE__ */ new WeakMap();
|
|
1113
|
+
const textOf = (row) => {
|
|
1114
|
+
const cached = searchCache.get(row);
|
|
1115
|
+
if (cached?.key === searchKey) return cached.text;
|
|
1116
|
+
const text = rowSearchText(row, columns, locale);
|
|
1117
|
+
searchCache.set(row, { key: searchKey, text });
|
|
1118
|
+
return text;
|
|
1119
|
+
};
|
|
1120
|
+
let treeCacheQuery = "\0";
|
|
1121
|
+
let treeCache = /* @__PURE__ */ new Set();
|
|
1122
|
+
const globalFilterFn = (row, _columnId, query) => {
|
|
1123
|
+
const text = String(query ?? "");
|
|
1124
|
+
if (!input.getSubRows) return searchMatches(text, textOf(row.original));
|
|
1125
|
+
if (treeCacheQuery !== text) {
|
|
1126
|
+
treeCacheQuery = text;
|
|
1127
|
+
treeCache = treeVisibleIds(
|
|
1128
|
+
input.rows,
|
|
1129
|
+
input.getRowId,
|
|
1130
|
+
(item) => input.getSubRows?.(item),
|
|
1131
|
+
(item) => searchMatches(text, textOf(item))
|
|
1132
|
+
);
|
|
1133
|
+
}
|
|
1134
|
+
return treeCache.has(input.getRowId(row.original));
|
|
1135
|
+
};
|
|
1136
|
+
return {
|
|
1137
|
+
features: collectionFeatures,
|
|
1138
|
+
data: input.rows,
|
|
1139
|
+
columns: toColumnDefs(columns),
|
|
1140
|
+
getRowId: (row) => input.getRowId(row),
|
|
1141
|
+
getSubRows: input.getSubRows,
|
|
1142
|
+
// A column filter on a tree keeps the parents of a matching child, the same way search does.
|
|
1143
|
+
filterFromLeafRows: Boolean(input.getSubRows),
|
|
1144
|
+
getColumnCanGlobalFilter: (column) => {
|
|
1145
|
+
const meta = column.columnDef.meta;
|
|
1146
|
+
return meta?.searchable != null && meta.searchable !== false;
|
|
1147
|
+
},
|
|
1148
|
+
enableRowSelection: input.canSelect ? (row) => input.canSelect(row.original) : false,
|
|
1149
|
+
enableMultiRowSelection: true,
|
|
1150
|
+
enableSubRowSelection: true,
|
|
1151
|
+
globalFilterFn,
|
|
1152
|
+
meta: { locale, columns },
|
|
1153
|
+
state: input.state,
|
|
1154
|
+
initialState: input.initialState,
|
|
1155
|
+
onGlobalFilterChange: input.onStateChange?.globalFilter,
|
|
1156
|
+
onSortingChange: input.onStateChange?.sorting,
|
|
1157
|
+
onColumnFiltersChange: input.onStateChange?.columnFilters,
|
|
1158
|
+
onPaginationChange: input.onStateChange?.pagination,
|
|
1159
|
+
onRowSelectionChange: input.onStateChange?.rowSelection,
|
|
1160
|
+
onGroupingChange: input.onStateChange?.grouping,
|
|
1161
|
+
onExpandedChange: input.onStateChange?.expanded,
|
|
1162
|
+
onColumnOrderChange: input.onStateChange?.columnOrder,
|
|
1163
|
+
onColumnVisibilityChange: input.onStateChange?.columnVisibility,
|
|
1164
|
+
autoResetPageIndex: false,
|
|
1165
|
+
// Resetting expanded would close every group on the first data change. Groups start open.
|
|
1166
|
+
autoResetExpanded: false
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
// src/custom/collection/use-collection.ts
|
|
1171
|
+
var defaultMessages = {
|
|
1172
|
+
selectAll: (count) => `Select all ${count} filtered rows`,
|
|
1173
|
+
actionsFor: (name) => `Actions for ${name}`,
|
|
1174
|
+
total: "Total",
|
|
1175
|
+
selected: (count) => `${count} selected`,
|
|
1176
|
+
hiddenByFilters: (count) => `${count} hidden by filters`,
|
|
1177
|
+
noResults: "No results",
|
|
1178
|
+
noResultsDescription: "Nothing matches the current search or filters.",
|
|
1179
|
+
clearFilters: "Clear filters",
|
|
1180
|
+
clearSearch: "Clear search",
|
|
1181
|
+
nothingHere: "Nothing here yet",
|
|
1182
|
+
previous: "Previous",
|
|
1183
|
+
next: "Next",
|
|
1184
|
+
rowsPerPage: "Rows per page",
|
|
1185
|
+
selectionCleared: "Selection cleared",
|
|
1186
|
+
loading: "Loading"
|
|
1187
|
+
};
|
|
1188
|
+
function readStored(storage, key, columnIds, pageSizeOptions) {
|
|
1189
|
+
if (!storage || !key) return {};
|
|
1190
|
+
try {
|
|
1191
|
+
const raw = storage.getItem(`loomiere:collection:${key}`);
|
|
1192
|
+
const stored = raw ? JSON.parse(raw) : {};
|
|
1193
|
+
if (stored.sort && !columnIds.has(stored.sort.id)) stored.sort = null;
|
|
1194
|
+
if (stored.filters) {
|
|
1195
|
+
stored.filters = Object.fromEntries(Object.entries(stored.filters).filter(([id]) => columnIds.has(id)));
|
|
1196
|
+
}
|
|
1197
|
+
if (stored.grouping && !columnIds.has(stored.grouping)) stored.grouping = null;
|
|
1198
|
+
if (stored.pageSize != null && pageSizeOptions?.length && !pageSizeOptions.includes(stored.pageSize)) stored.pageSize = void 0;
|
|
1199
|
+
return stored;
|
|
1200
|
+
} catch {
|
|
1201
|
+
return {};
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
function withTabDefaults(columns, filters) {
|
|
1205
|
+
const next = { ...filters };
|
|
1206
|
+
for (const column of columns) {
|
|
1207
|
+
if (column.filter?.kind !== "tabs") continue;
|
|
1208
|
+
const current = next[column.id];
|
|
1209
|
+
if (current != null && current !== "") continue;
|
|
1210
|
+
const options = column.filter.options ?? [];
|
|
1211
|
+
if (options.some((option) => option.value === "" || option.all)) continue;
|
|
1212
|
+
if (options[0]) next[column.id] = options[0].value;
|
|
1213
|
+
}
|
|
1214
|
+
return next;
|
|
1215
|
+
}
|
|
1216
|
+
function filtersToList(filters) {
|
|
1217
|
+
return Object.entries(filters).filter(([, value]) => value != null && value !== "" && !(Array.isArray(value) && value.length === 0)).map(([id, value]) => ({ id, value }));
|
|
1218
|
+
}
|
|
1219
|
+
function selectionRecord(ids) {
|
|
1220
|
+
const record = {};
|
|
1221
|
+
for (const id of ids) record[id] = true;
|
|
1222
|
+
return record;
|
|
1223
|
+
}
|
|
1224
|
+
function useCollection(options) {
|
|
1225
|
+
const storage = options.storage ?? (typeof sessionStorage === "undefined" ? void 0 : sessionStorage);
|
|
1226
|
+
const columnIds = useMemo(() => new Set(options.columns.map((column) => column.id)), [options.columns]);
|
|
1227
|
+
const stored = useRef(readStored(storage, options.stateKey, columnIds, options.pageSizeOptions)).current;
|
|
1228
|
+
const locale = options.locale || (typeof document === "undefined" ? "en" : document.documentElement.lang) || "en";
|
|
1229
|
+
const messages = { ...defaultMessages, ...options.messages };
|
|
1230
|
+
const initialSort = options.initialSort ?? null;
|
|
1231
|
+
const initialFilters = withTabDefaults(options.columns, options.initialFilters ?? {});
|
|
1232
|
+
const initialGrouping = options.groupBy ?? null;
|
|
1233
|
+
const initialVisibility = Object.fromEntries(options.columns.map((column) => [column.id, !column.hidden]));
|
|
1234
|
+
const initialOrder = options.columns.map((column) => column.id);
|
|
1235
|
+
const [searchState, setSearchState] = useState(stored.search ?? "");
|
|
1236
|
+
const [sortState, setSortState] = useState(stored.sort ?? initialSort);
|
|
1237
|
+
const [filterState, setFilterState] = useState(stored.filters ?? initialFilters);
|
|
1238
|
+
const [selectionState, setSelectionState] = useState([]);
|
|
1239
|
+
const [pageState, setPageState] = useState(1);
|
|
1240
|
+
const [pageSizeState, setPageSizeState] = useState(stored.pageSize ?? options.pageSize);
|
|
1241
|
+
const [groupingState, setGroupingState] = useState(stored.grouping ?? initialGrouping);
|
|
1242
|
+
const [orderState, setOrderState] = useState(initialOrder);
|
|
1243
|
+
const [visibilityState, setVisibilityState] = useState(initialVisibility);
|
|
1244
|
+
const [expanded, setExpanded] = useState(options.defaultExpanded === false ? {} : true);
|
|
1245
|
+
const expandedBeforeSearch = useRef(null);
|
|
1246
|
+
const focusAfterPage = useRef(false);
|
|
1247
|
+
const search = options.search ?? searchState;
|
|
1248
|
+
const sort = options.sort === void 0 ? sortState : options.sort;
|
|
1249
|
+
const filters = options.filters ?? filterState;
|
|
1250
|
+
const selection = options.selection ?? selectionState;
|
|
1251
|
+
const page = options.page ?? pageState;
|
|
1252
|
+
const grouping = options.grouping === void 0 ? groupingState : options.grouping;
|
|
1253
|
+
const columnOrder = options.columnOrder ?? orderState;
|
|
1254
|
+
const columnVisibility = options.columnVisibility ?? visibilityState;
|
|
1255
|
+
const pageSize = pageSizeState;
|
|
1256
|
+
const deferredSearch = useDeferredValue(search);
|
|
1257
|
+
const focusRef = useRef(null);
|
|
1258
|
+
const rowsRef = useRef(options.rows);
|
|
1259
|
+
const idOf = options.getRowId;
|
|
1260
|
+
const updatePage = (value) => {
|
|
1261
|
+
options.onPageChange?.(value);
|
|
1262
|
+
if (options.page === void 0) setPageState(value);
|
|
1263
|
+
};
|
|
1264
|
+
const setSearch = (value) => {
|
|
1265
|
+
options.onSearchChange?.(value);
|
|
1266
|
+
if (options.search === void 0) setSearchState(value);
|
|
1267
|
+
updatePage(1);
|
|
1268
|
+
};
|
|
1269
|
+
const setSort = (value) => {
|
|
1270
|
+
options.onSortChange?.(value);
|
|
1271
|
+
if (options.sort === void 0) setSortState(value);
|
|
1272
|
+
};
|
|
1273
|
+
const setFilters = (value) => {
|
|
1274
|
+
options.onFiltersChange?.(value);
|
|
1275
|
+
if (options.filters === void 0) setFilterState(value);
|
|
1276
|
+
updatePage(1);
|
|
1277
|
+
};
|
|
1278
|
+
const setSelection = (ids) => {
|
|
1279
|
+
options.onSelectionChange?.(ids);
|
|
1280
|
+
if (options.selection === void 0) setSelectionState(ids);
|
|
1281
|
+
};
|
|
1282
|
+
const setGrouping = (value) => {
|
|
1283
|
+
options.onGroupingChange?.(value);
|
|
1284
|
+
if (options.grouping === void 0) setGroupingState(value);
|
|
1285
|
+
};
|
|
1286
|
+
const tableOptions = collectionTableOptions({
|
|
1287
|
+
rows: options.rows,
|
|
1288
|
+
columns: options.columns,
|
|
1289
|
+
getRowId: idOf,
|
|
1290
|
+
getSubRows: options.getSubRows,
|
|
1291
|
+
locale,
|
|
1292
|
+
canSelect: options.canSelect,
|
|
1293
|
+
state: {
|
|
1294
|
+
globalFilter: deferredSearch,
|
|
1295
|
+
sorting: sort ? [{ id: sort.id, desc: sort.desc }] : [],
|
|
1296
|
+
columnFilters: filtersToList(filters),
|
|
1297
|
+
pagination: { pageIndex: 0, pageSize: Number.POSITIVE_INFINITY },
|
|
1298
|
+
rowSelection: selectionRecord(selection),
|
|
1299
|
+
grouping: grouping ? [grouping] : [],
|
|
1300
|
+
expanded,
|
|
1301
|
+
columnOrder,
|
|
1302
|
+
columnVisibility
|
|
1303
|
+
},
|
|
1304
|
+
initialState: {
|
|
1305
|
+
sorting: initialSort ? [{ id: initialSort.id, desc: initialSort.desc }] : [],
|
|
1306
|
+
columnFilters: filtersToList(initialFilters),
|
|
1307
|
+
grouping: initialGrouping ? [initialGrouping] : [],
|
|
1308
|
+
columnVisibility: initialVisibility,
|
|
1309
|
+
columnOrder: initialOrder
|
|
1310
|
+
},
|
|
1311
|
+
onStateChange: {
|
|
1312
|
+
rowSelection: (updater) => {
|
|
1313
|
+
const current = selectionRecord(selection);
|
|
1314
|
+
const next = typeof updater === "function" ? updater(current) : updater;
|
|
1315
|
+
setSelection(Object.keys(next).filter((id) => next[id]));
|
|
1316
|
+
},
|
|
1317
|
+
expanded: (updater) => {
|
|
1318
|
+
setExpanded((current) => typeof updater === "function" ? updater(current) : updater);
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
});
|
|
1322
|
+
const table = useTable(tableOptions);
|
|
1323
|
+
useEffect(() => {
|
|
1324
|
+
if (!options.getSubRows) return;
|
|
1325
|
+
if (deferredSearch) {
|
|
1326
|
+
if (expandedBeforeSearch.current == null) expandedBeforeSearch.current = expanded;
|
|
1327
|
+
setExpanded(true);
|
|
1328
|
+
return;
|
|
1329
|
+
}
|
|
1330
|
+
if (expandedBeforeSearch.current != null) {
|
|
1331
|
+
setExpanded(expandedBeforeSearch.current);
|
|
1332
|
+
expandedBeforeSearch.current = null;
|
|
1333
|
+
}
|
|
1334
|
+
}, [deferredSearch, options.getSubRows]);
|
|
1335
|
+
useEffect(() => {
|
|
1336
|
+
if (options.clearSelectionOnRowsChange && rowsRef.current !== options.rows) {
|
|
1337
|
+
rowsRef.current = options.rows;
|
|
1338
|
+
setSelection([]);
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1341
|
+
rowsRef.current = options.rows;
|
|
1342
|
+
const kept = selectableIds(selection, options.rows, idOf, options.canSelect, options.getSubRows);
|
|
1343
|
+
if (kept.length !== selection.length) setSelection(kept);
|
|
1344
|
+
}, [options.rows, options.clearSelectionOnRowsChange, options.canSelect, options.getSubRows]);
|
|
1345
|
+
useEffect(() => {
|
|
1346
|
+
if (!options.stateKey || !storage) return;
|
|
1347
|
+
try {
|
|
1348
|
+
storage.setItem(
|
|
1349
|
+
`loomiere:collection:${options.stateKey}`,
|
|
1350
|
+
JSON.stringify({ search, sort, filters, pageSize, grouping })
|
|
1351
|
+
);
|
|
1352
|
+
} catch {
|
|
1353
|
+
}
|
|
1354
|
+
}, [options.stateKey, storage, search, sort, filters, pageSize, grouping]);
|
|
1355
|
+
const filteredModel = table.getFilteredRowModel();
|
|
1356
|
+
const filteredLeaves = filteredModel.flatRows.filter((row) => !row.getIsGrouped?.() && !(options.getSubRows && row.subRows?.length));
|
|
1357
|
+
const filteredRows = filteredLeaves.map((row) => row.original);
|
|
1358
|
+
const filteredIds = new Set(filteredLeaves.map((row) => row.id));
|
|
1359
|
+
const topRows = table.getRowModel().rows;
|
|
1360
|
+
const flat = flattenVisible(topRows);
|
|
1361
|
+
const paged = pageFlat(flat, page - 1, pageSize && pageSize > 0 ? pageSize : flat.filter((entry) => entry.kind === "leaf").length || 1);
|
|
1362
|
+
const totals = useMemo(() => {
|
|
1363
|
+
const rows = filteredModel.flatRows.filter((row) => !row.getIsGrouped?.() && !(options.getSubRows && row.subRows?.length)).map((row) => row.original);
|
|
1364
|
+
const result = {};
|
|
1365
|
+
for (const column of options.columns) {
|
|
1366
|
+
if (!column.total) continue;
|
|
1367
|
+
result[column.id] = totalText(column, rows, locale);
|
|
1368
|
+
}
|
|
1369
|
+
return result;
|
|
1370
|
+
}, [options.columns, options.getSubRows, filteredModel, locale]);
|
|
1371
|
+
const byId = useMemo(() => {
|
|
1372
|
+
const map = /* @__PURE__ */ new Map();
|
|
1373
|
+
const walk = (rows) => {
|
|
1374
|
+
for (const row of rows) {
|
|
1375
|
+
map.set(idOf(row), row);
|
|
1376
|
+
const children = options.getSubRows?.(row);
|
|
1377
|
+
if (children) walk(children);
|
|
1378
|
+
}
|
|
1379
|
+
};
|
|
1380
|
+
walk(options.rows);
|
|
1381
|
+
return map;
|
|
1382
|
+
}, [options.rows, options.getSubRows, idOf]);
|
|
1383
|
+
const isFiltered = Boolean(search) || !sameFilters(filters, initialFilters);
|
|
1384
|
+
return {
|
|
1385
|
+
table,
|
|
1386
|
+
columns: options.columns,
|
|
1387
|
+
getRowId: idOf,
|
|
1388
|
+
locale,
|
|
1389
|
+
search,
|
|
1390
|
+
setSearch,
|
|
1391
|
+
sort,
|
|
1392
|
+
setSort,
|
|
1393
|
+
cycleSort: (columnId) => setSort(nextSort(sort, columnId, initialSort)),
|
|
1394
|
+
filters,
|
|
1395
|
+
setFilter: (columnId, value) => setFilters({ ...filters, [columnId]: value }),
|
|
1396
|
+
clearFilters: () => {
|
|
1397
|
+
setSearch("");
|
|
1398
|
+
setFilters({ ...initialFilters });
|
|
1399
|
+
},
|
|
1400
|
+
facetCounts: (columnId) => {
|
|
1401
|
+
const column = table.getColumn(columnId);
|
|
1402
|
+
const counts = column?.getFacetedUniqueValues?.() ?? /* @__PURE__ */ new Map();
|
|
1403
|
+
const mapped = /* @__PURE__ */ new Map();
|
|
1404
|
+
for (const [key, count] of counts) mapped.set(key == null ? "" : String(key), count);
|
|
1405
|
+
return mapped;
|
|
1406
|
+
},
|
|
1407
|
+
selection,
|
|
1408
|
+
selectedRows: selection.map((id) => byId.get(id)).filter((row) => row != null),
|
|
1409
|
+
selectAllFiltered: () => table.toggleAllRowsSelected(true),
|
|
1410
|
+
clearSelection: () => setSelection([]),
|
|
1411
|
+
clearFilteredSelection: () => setSelection(selection.filter((id) => !filteredIds.has(id))),
|
|
1412
|
+
selectionSet: new Set(selection),
|
|
1413
|
+
messages,
|
|
1414
|
+
hiddenSelectedCount: hiddenSelectedCount(selection, filteredIds),
|
|
1415
|
+
filteredRows,
|
|
1416
|
+
filteredCount: filteredRows.length,
|
|
1417
|
+
totalCount: options.rows.length,
|
|
1418
|
+
totals,
|
|
1419
|
+
page: paged.bounds.pageIndex + 1,
|
|
1420
|
+
pageCount: pageSize ? paged.bounds.pageCount : 1,
|
|
1421
|
+
setPage: (value) => {
|
|
1422
|
+
focusAfterPage.current = true;
|
|
1423
|
+
updatePage(value);
|
|
1424
|
+
},
|
|
1425
|
+
pageSize,
|
|
1426
|
+
setPageSize: (size) => {
|
|
1427
|
+
setPageSizeState(size);
|
|
1428
|
+
updatePage(1);
|
|
1429
|
+
},
|
|
1430
|
+
isFiltered,
|
|
1431
|
+
reset: () => {
|
|
1432
|
+
setSearch("");
|
|
1433
|
+
setSort(initialSort);
|
|
1434
|
+
setFilters({ ...initialFilters });
|
|
1435
|
+
setGrouping(initialGrouping);
|
|
1436
|
+
setPageSizeState(options.pageSize);
|
|
1437
|
+
setExpanded(options.defaultExpanded === false ? {} : true);
|
|
1438
|
+
expandedBeforeSearch.current = null;
|
|
1439
|
+
updatePage(1);
|
|
1440
|
+
setOrderState(initialOrder);
|
|
1441
|
+
setVisibilityState(initialVisibility);
|
|
1442
|
+
options.onColumnOrderChange?.(initialOrder);
|
|
1443
|
+
options.onColumnVisibilityChange?.(initialVisibility);
|
|
1444
|
+
},
|
|
1445
|
+
grouping,
|
|
1446
|
+
setGrouping,
|
|
1447
|
+
columnOrder,
|
|
1448
|
+
setColumnOrder: (order) => {
|
|
1449
|
+
options.onColumnOrderChange?.(order);
|
|
1450
|
+
if (options.columnOrder === void 0) setOrderState(order);
|
|
1451
|
+
},
|
|
1452
|
+
columnVisibility,
|
|
1453
|
+
setColumnVisibility: (visibility) => {
|
|
1454
|
+
options.onColumnVisibilityChange?.(visibility);
|
|
1455
|
+
if (options.columnVisibility === void 0) setVisibilityState(visibility);
|
|
1456
|
+
},
|
|
1457
|
+
canSelect: options.canSelect,
|
|
1458
|
+
pageEntries: paged.entries,
|
|
1459
|
+
rowHeaderId: options.columns.find((column) => column.rowHeader)?.id,
|
|
1460
|
+
registerRowFocus: (focus) => {
|
|
1461
|
+
focusRef.current = focus;
|
|
1462
|
+
},
|
|
1463
|
+
focusFirstRow: () => focusRef.current?.(),
|
|
1464
|
+
focusAfterPage
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
function sameFilters(a, b) {
|
|
1468
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
1469
|
+
for (const key of keys) if (String(a[key] ?? "") !== String(b[key] ?? "")) return false;
|
|
1470
|
+
return true;
|
|
1471
|
+
}
|
|
1472
|
+
function flattenVisible(rows, groupId, nested = false) {
|
|
1473
|
+
const out = [];
|
|
1474
|
+
for (const row of rows) {
|
|
1475
|
+
if (!nested && !row.getIsGrouped?.() && row.getParentRow?.()) continue;
|
|
1476
|
+
if (row.getIsGrouped?.()) {
|
|
1477
|
+
out.push({ kind: "group", row, groupId: row.id });
|
|
1478
|
+
if (row.getIsExpanded()) out.push(...flattenVisible(row.subRows, row.id, true));
|
|
1479
|
+
} else {
|
|
1480
|
+
out.push({ kind: "leaf", row, groupId });
|
|
1481
|
+
if (row.subRows?.length && row.getIsExpanded?.()) out.push(...flattenVisible(row.subRows, groupId, true));
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
return out;
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
export { CollectionPagination, CollectionToolbar, DataList, DataTable, collectionTableOptions, defineColumns, headerLabel, totalText, useCollection };
|
|
1488
|
+
//# sourceMappingURL=index.js.map
|
|
1489
|
+
//# sourceMappingURL=index.js.map
|