@krizaka/ui 3.75.0 → 3.76.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.
@@ -412,7 +412,7 @@ function useRouteTable(routeKey, defaults, columns) {
412
412
  }
413
413
 
414
414
  // src/data-table/data-table.tsx
415
- import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
415
+ import { Fragment as Fragment3, jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
416
416
  var dataTable = tv2({
417
417
  slots: {
418
418
  root: "@container w-full overflow-clip rounded-xl border border-border-default bg-surface-1",
@@ -598,100 +598,322 @@ var NO_RECORD = {};
598
598
  var apply = (updater, old) => typeof updater === "function" ? updater(old) : updater;
599
599
  var valueOf = (column, row) => column.value ? column.value(row) : row[column.id];
600
600
  var display = (value) => value instanceof Date ? value.toISOString() : String(value ?? "");
601
- function DataTable({
602
- rows,
603
- columns,
604
- getRowId,
605
- caption,
606
- showCaption,
607
- sort: sortProp,
608
- routeKey,
609
- defaultSort = null,
610
- onSortChange,
611
- filters: filtersProp,
612
- defaultFilters = NO_RECORD,
613
- onFiltersChange,
614
- selectable,
615
- selected: selectedProp,
616
- defaultSelected = NONE2,
617
- onSelectedChange,
618
- rowLabel,
619
- bulkActions,
620
- bulkMax,
621
- bulkResult,
622
- groupBy: groupByProp,
623
- defaultGroupBy = NONE2,
624
- onGroupByChange,
625
- columnVisibility: visibilityProp,
626
- defaultColumnVisibility = NO_RECORD,
627
- onColumnVisibilityChange,
628
- pinning,
629
- resizing,
630
- getSubRows,
631
- renderDetail,
632
- expanded: expandedProp,
633
- defaultExpanded = NO_RECORD,
634
- onExpandedChange,
635
- pageSize: pageSizeProp,
636
- defaultPageSize,
637
- onPageSizeChange,
638
- pageSizeOptions,
639
- page: pageProp,
640
- defaultPage = 1,
641
- onPageChange,
642
- manual,
643
- rowCount,
644
- onStateChange,
645
- loading,
646
- loadingRows = 5,
647
- error,
648
- empty: empty2,
649
- layout = "auto",
650
- phone = "cards",
651
- density,
652
- stickyHeader,
653
- striped,
654
- scroll,
655
- stickyFirstColumn,
656
- virtualized,
657
- toolbar,
658
- footer,
659
- labels: given,
660
- className
661
- }) {
662
- const labels = { ...dataTableLabels, ...given };
663
- const effectiveLayout = virtualized ? "table" : layout;
664
- const L = effectiveLayout;
665
- const typed = columns.some((c) => c.type || c.priority);
666
- const list = phone === "list" && L !== "table";
667
- const s2 = dataTable({ layout: L, density, sticky: Boolean(stickyHeader), striped: Boolean(striped), typed, phone: list ? "list" : "cards" });
668
- const route = useRouteTable(routeKey, { sort: defaultSort, page: defaultPage, filters: defaultFilters }, columns.map((c) => c.id));
669
- const [sort, setSort] = useControllable(route ? route.sort : sortProp, defaultSort, route?.onSortChange ?? onSortChange);
670
- const [filters, setFiltersState] = useControllable(route ? route.filters : filtersProp, defaultFilters, route?.onFiltersChange ?? onFiltersChange);
671
- const [selected, setSelected] = useControllable(selectedProp, defaultSelected, onSelectedChange);
672
- const [groupBy, setGroupBy] = useControllable(groupByProp, defaultGroupBy, onGroupByChange);
673
- const [visibility, setVisibility] = useControllable(visibilityProp, defaultColumnVisibility, onColumnVisibilityChange);
674
- const [expanded, setExpanded] = useControllable(expandedProp, defaultExpanded, onExpandedChange);
675
- const paginated = pageSizeProp !== void 0 || defaultPageSize !== void 0;
676
- const [pageSize, setPageSize] = useControllable(pageSizeProp, defaultPageSize ?? Number.POSITIVE_INFINITY, onPageSizeChange);
677
- const [page, setPage] = useControllable(route ? route.page : pageProp, defaultPage, route?.onPageChange ?? onPageChange);
678
- const [sizing, setSizing] = React2.useState({});
679
- const [resizeState, setResizeState] = React2.useState(void 0);
601
+ var sortingOf = (sort) => sort ? [{ id: sort.column, desc: sort.direction === "descending" }] : [];
602
+ var filterListOf = (filters) => Object.entries(filters).map(([id, value]) => ({ id, value }));
603
+ var selectionOf = (selected) => Object.fromEntries(selected.map((id) => [id, true]));
604
+ var priorityOf = (column) => column.priority && column.priority > 1 ? column.priority : 1;
605
+ var placeOf = (titleColumn, column) => {
606
+ if (column.card === "hidden") return "hidden";
607
+ if (column === titleColumn) return "title";
608
+ if (column.type === "status" || column.type === "badge") return "status";
609
+ return priorityOf(column) === 2 ? "facts" : "away";
610
+ };
611
+ var cardOf = (column) => column?.card === "title" || column && !column.card && column.type === "title" ? "title" : column?.card === "hidden" ? "hidden" : void 0;
612
+ var nameOf = (v, row) => v.rowLabel ? v.rowLabel(row.original) : row.id;
613
+ var pin = (column) => {
614
+ const position = column.getIsPinned();
615
+ if (!position) return {};
616
+ const offset = position === "start" ? column.getStart("start") : column.getAfter("end");
617
+ return { "data-pin": position, style: { ["--dt-pin"]: `${offset}px` } };
618
+ };
619
+ var widthOf = (v, column) => v.fixedLayout || v.byId.get(column.id)?.width ? { width: column.getSize() } : void 0;
620
+ function titleContent(v, column, original, text, cut) {
621
+ const sub = column.subtitle?.(original);
622
+ const href = column.href?.(original);
623
+ const link = cn("block font-semibold text-fg no-underline hover:underline focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring", cut, v.list && LIST_LINK[v.L]);
624
+ return /* @__PURE__ */ jsxs3("span", { className: "block min-w-0", children: [
625
+ href ? /* @__PURE__ */ jsx3("a", { href, className: link, title: text, children: text }) : /* @__PURE__ */ jsx3("span", { className: cn("block font-semibold text-fg", cut), title: text, children: text }),
626
+ sub ? /* @__PURE__ */ jsx3("span", { className: cn("block font-mono text-xs text-fg-tertiary", cut), children: sub }) : null
627
+ ] });
628
+ }
629
+ function numberContent(column, value, text) {
630
+ return /* @__PURE__ */ jsxs3("span", { children: [
631
+ typeof value === "number" ? value.toLocaleString("en") : text,
632
+ column.unit ? /* @__PURE__ */ jsx3("span", { className: "ms-1 text-fg-secondary", children: column.unit }) : null
633
+ ] });
634
+ }
635
+ function linkContent(column, original, text, cut) {
636
+ const href = column.href?.(original);
637
+ if (!href) {
638
+ return /* @__PURE__ */ jsx3("span", { className: cn("block", cut), title: text, children: text });
639
+ }
640
+ return /* @__PURE__ */ jsx3("a", { href, title: text, className: cn("block text-fg-accent underline-offset-2 hover:underline", cut), children: text });
641
+ }
642
+ function plainContent(column, text, cut) {
643
+ const lines = column.clamp ?? (column.type === "text" ? 2 : void 0);
644
+ if (!column.nowrap && !lines) return text;
645
+ return /* @__PURE__ */ jsx3("span", { className: column.nowrap ? cn("block", cut || "truncate") : CLAMP[lines], title: text, children: text });
646
+ }
647
+ function contentOf(v, column, original) {
648
+ if (column.cell) return column.cell(original);
649
+ const value = valueOf(column, original);
650
+ const text = column.type === "date" && value instanceof Date ? value.toISOString().slice(0, 10) : display(value);
651
+ const cut = TRUNCATE[v.L];
652
+ switch (column.type) {
653
+ case "title":
654
+ return titleContent(v, column, original, text, cut);
655
+ case "number":
656
+ return numberContent(column, value, text);
657
+ case "link":
658
+ return linkContent(column, original, text, cut);
659
+ default:
660
+ return plainContent(column, text, cut);
661
+ }
662
+ }
663
+ function listPlace(v, row, column) {
664
+ if (row?.getIsGrouped()) return row.groupingColumnId === column.id ? "title" : column.aggregate ? "status" : "away";
665
+ const place = placeOf(v.titleColumn, column);
666
+ return place === "facts" || place === "hidden" ? "away" : place;
667
+ }
668
+ function groupContent(v, row, column, columnId) {
669
+ const leaves = row.getLeafRows().filter((r) => !r.getIsGrouped()).map((r) => r.original);
670
+ if (columnId !== row.groupingColumnId) return column.aggregate ? column.aggregate(leaves) : null;
671
+ const value = display(row.groupingValue);
672
+ return /* @__PURE__ */ jsxs3("button", { type: "button", className: v.s.toggle({ className: "px-1.5 font-semibold text-fg" }), "aria-expanded": row.getIsExpanded(), onClick: row.getToggleExpandedHandler(), children: [
673
+ /* @__PURE__ */ jsx3(ChevronGlyph, { direction: "right", className: "h-4 w-4" }),
674
+ v.labels.group(value, leaves.length)
675
+ ] });
676
+ }
677
+ function withFacts(v, content, original) {
678
+ return /* @__PURE__ */ jsxs3("span", { className: "block min-w-0", children: [
679
+ content,
680
+ /* @__PURE__ */ jsx3("span", { "data-facts": "", className: LIST_FACTS[v.L], children: v.facts.map((c, i) => /* @__PURE__ */ jsxs3(React2.Fragment, { children: [
681
+ i > 0 ? /* @__PURE__ */ jsx3("span", { "aria-hidden": true, children: " \xB7 " }) : null,
682
+ contentOf(v, c, original)
683
+ ] }, c.id)) })
684
+ ] });
685
+ }
686
+ function withToggle(v, content, row) {
687
+ const shown = v.maxPriority > 1 ? TOGGLE[v.L][v.maxPriority] : "hidden";
688
+ return /* @__PURE__ */ jsxs3("span", { className: "flex min-w-0 items-center gap-1", children: [
689
+ /* @__PURE__ */ jsx3(
690
+ "button",
691
+ {
692
+ type: "button",
693
+ className: v.s.toggle({ className: cn(shown, v.listAway.length > 0 && LIST_TOGGLE[v.L], v.list && "relative z-[1]") }),
694
+ "aria-expanded": row.getIsExpanded(),
695
+ "aria-label": v.labels.showMore(nameOf(v, row)),
696
+ onClick: row.getToggleExpandedHandler(),
697
+ children: /* @__PURE__ */ jsx3(ChevronGlyph, { direction: "right", className: "h-4 w-4" })
698
+ }
699
+ ),
700
+ /* @__PURE__ */ jsx3("span", { className: "min-w-0 flex-1", children: content })
701
+ ] });
702
+ }
703
+ function bodyContent(v, row, column, first) {
704
+ let content = contentOf(v, column, row.original);
705
+ if (v.list && column === v.titleColumn && v.facts.length > 0) content = withFacts(v, content, row.original);
706
+ if (first && v.inlineToggle) content = withToggle(v, content, row);
707
+ return content;
708
+ }
709
+ function cellClass(v, column) {
710
+ const level = priorityOf(column);
711
+ const type = column.type;
712
+ return [
713
+ ALIGN[column.align ?? (type === "number" ? "end" : "start")],
714
+ type ? TYPE_CELL[type][v.L] : "",
715
+ level > 1 ? LEAVES[v.L][level] : "",
716
+ (type || column.priority) && column.minWidth !== void 0 ? MIN[v.L] : "",
717
+ column.maxWidth !== void 0 ? MAX[v.L] : ""
718
+ ].join(" ");
719
+ }
720
+ function cardRole(v, row, column, columnId) {
721
+ if (v.list) return column.card === "hidden" ? "hidden" : void 0;
722
+ if (!row.getIsGrouped()) return cardOf(column);
723
+ if (columnId === row.groupingColumnId) return "title";
724
+ return column.aggregate ? void 0 : "hidden";
725
+ }
726
+ function cellStyle(row, column, first, base) {
727
+ const indent = first && row.depth > 0 && !row.getIsGrouped();
728
+ return {
729
+ ...base,
730
+ ...column.minWidth !== void 0 ? { ["--dt-min"]: `${column.minWidth}px` } : {},
731
+ ...column.maxWidth !== void 0 ? { ["--dt-max"]: `${column.maxWidth}px` } : {},
732
+ ...indent ? { paddingInlineStart: `${1 + row.depth * 1.25}rem` } : {}
733
+ };
734
+ }
735
+ function dataCell(v, row, cell, first) {
736
+ const column = v.byId.get(cell.column.id);
737
+ if (!column) return null;
738
+ const { style, ...pinnedProps } = pin(cell.column);
739
+ const content = row.getIsGrouped() ? groupContent(v, row, column, cell.column.id) : bodyContent(v, row, column, first);
740
+ return /* @__PURE__ */ jsx3(
741
+ "td",
742
+ {
743
+ role: "cell",
744
+ "data-label": column.header,
745
+ "data-card": cardRole(v, row, column, cell.column.id),
746
+ "data-list": v.list ? listPlace(v, row, column) : void 0,
747
+ ...pinnedProps,
748
+ className: v.s.td({ className: cellClass(v, column) }),
749
+ style: cellStyle(row, column, first, style),
750
+ children: content
751
+ },
752
+ cell.id
753
+ );
754
+ }
755
+ function detailRow(v, row) {
756
+ const left = v.visibleColumns.filter((c) => {
757
+ const column = v.byId.get(c.id);
758
+ return priorityOf(column) > 1 || v.listAway.includes(column);
759
+ });
760
+ const { L, s: s2, renderDetail } = v;
761
+ const panel = cn("px-4 py-4", v.maxPriority > 1 ? PANEL[L][v.maxPriority] : "hidden", v.listAway.length > 0 && LIST_PANEL[L]);
762
+ return wideRow(
763
+ v,
764
+ renderDetail ? s2.detail() : "bg-surface-0 p-0 text-left",
765
+ /* @__PURE__ */ jsxs3(Fragment3, { children: [
766
+ left.length > 0 ? /* @__PURE__ */ jsx3("dl", { className: cn("m-0 grid gap-3 text-left", renderDetail ? "mb-4" : panel), children: left.map((c) => {
767
+ const column = v.byId.get(c.id);
768
+ const place = v.listAway.includes(column) ? LIST_IN_DETAIL[L] : IN_DETAIL[L][priorityOf(column)];
769
+ return /* @__PURE__ */ jsxs3("div", { className: cn(place, "grid-cols-[minmax(5rem,8rem)_minmax(0,1fr)] items-baseline gap-3"), children: [
770
+ /* @__PURE__ */ jsx3("dt", { className: "text-xs font-semibold text-fg-secondary", children: column.header }),
771
+ /* @__PURE__ */ jsx3("dd", { className: "m-0 min-w-0 text-sm text-fg", children: contentOf(v, column, row.original) })
772
+ ] }, c.id);
773
+ }) }) : null,
774
+ renderDetail ? renderDetail(row.original) : null
775
+ ] })
776
+ );
777
+ }
778
+ function rowLeadCells(v, row, isChosen) {
779
+ const { s: s2, labels } = v;
780
+ const grouped = row.getIsGrouped();
781
+ const canExpand = !grouped && (v.renderDetail ? true : row.getCanExpand());
782
+ return /* @__PURE__ */ jsxs3(Fragment3, { children: [
783
+ v.selectable && /* @__PURE__ */ jsx3("td", { role: "cell", className: s2.td({ className: s2.select() }), children: grouped ? null : /* @__PURE__ */ jsx3(Checkbox, { checked: isChosen, onCheckedChange: () => row.toggleSelected(), "aria-label": labels.selectRow(nameOf(v, row)) }) }),
784
+ v.hasExpander && /* @__PURE__ */ jsx3("td", { role: "cell", className: s2.td({ className: s2.expander() }), children: canExpand ? /* @__PURE__ */ jsx3("button", { type: "button", className: s2.toggle(), "aria-expanded": row.getIsExpanded(), "aria-label": labels.expandRow(nameOf(v, row)), onClick: row.getToggleExpandedHandler(), children: /* @__PURE__ */ jsx3(ChevronGlyph, { direction: "right", className: "h-4 w-4" }) }) : null })
785
+ ] });
786
+ }
787
+ function renderRow(v, row, index) {
788
+ const isChosen = v.chosen.has(row.id);
789
+ const grouped = row.getIsGrouped();
790
+ const showDetail = v.detailed && !grouped && row.getIsExpanded() && !v.virtualized;
791
+ const main = /* @__PURE__ */ jsxs3(
792
+ "tr",
793
+ {
794
+ role: "row",
795
+ "data-index": v.measureElement ? index : void 0,
796
+ ref: v.measureElement,
797
+ "aria-rowindex": v.virtualized ? index + 2 : void 0,
798
+ "data-state": isChosen ? "selected" : void 0,
799
+ "data-group": grouped ? "" : void 0,
800
+ className: v.s.tr({ className: grouped ? v.s.groupRow() : void 0 }),
801
+ children: [
802
+ rowLeadCells(v, row, isChosen),
803
+ row.getVisibleCells().map((cell, i) => dataCell(v, row, cell, i === 0))
804
+ ]
805
+ }
806
+ );
807
+ return /* @__PURE__ */ jsxs3(React2.Fragment, { children: [
808
+ main,
809
+ showDetail ? detailRow(v, row) : null
810
+ ] }, row.id);
811
+ }
812
+ function wideRow(v, tdClass, children) {
813
+ return /* @__PURE__ */ jsx3("tr", { role: "row", className: v.s.tr({ className: "hover:bg-transparent" }), children: /* @__PURE__ */ jsx3("td", { role: "cell", colSpan: v.span, "data-card": "message", className: v.s.td({ className: tdClass }), children }) });
814
+ }
815
+ function messageRow(v, content, alert) {
816
+ return wideRow(v, v.s.message(), alert ? /* @__PURE__ */ jsx3("div", { role: "alert", children: content }) : content);
817
+ }
818
+ function skeletonRows(v, count) {
819
+ const { s: s2, list } = v;
820
+ return Array.from({ length: count }, (_, i) => /* @__PURE__ */ jsxs3("tr", { role: "row", "aria-hidden": true, className: s2.tr(), children: [
821
+ v.selectable && /* @__PURE__ */ jsx3("td", { role: "cell", className: s2.td({ className: s2.select() }) }),
822
+ v.hasExpander && /* @__PURE__ */ jsx3("td", { role: "cell", className: s2.td({ className: s2.expander() }) }),
823
+ v.visibleColumns.map((column) => /* @__PURE__ */ jsx3("td", { role: "cell", "data-card": list ? void 0 : cardOf(v.byId.get(column.id)), "data-list": list ? listPlace(v, null, v.byId.get(column.id)) : void 0, className: s2.td(), children: /* @__PURE__ */ jsx3("div", { className: s2.skeleton() }) }, column.id))
824
+ ] }, `loading-${i}`));
825
+ }
826
+ function bodyRows(v) {
827
+ const { pageRows: rows, windowItems } = v;
828
+ if (v.loading) return skeletonRows(v, v.loadingCount);
829
+ if (v.error) return messageRow(v, v.error, true);
830
+ if (rows.length === 0) return messageRow(v, v.empty ?? v.labels.empty);
831
+ if (windowItems) return windowItems.map((item) => renderRow(v, rows[item.index], item.index));
832
+ return rows.map((row, i) => renderRow(v, row, i));
833
+ }
834
+ function headerLabel(v, header, column, direction) {
835
+ if (column.sortable) {
836
+ return /* @__PURE__ */ jsxs3("button", { type: "button", "aria-pressed": Boolean(direction), className: v.s.sortButton(), onClick: () => header.column.toggleSorting(void 0, false), children: [
837
+ column.header,
838
+ /* @__PURE__ */ jsx3(SortGlyph, { direction, className: "h-3.5 w-3.5" })
839
+ ] });
840
+ }
841
+ return column.headerHidden ? /* @__PURE__ */ jsx3("span", { className: "sr-only", children: column.header }) : column.header;
842
+ }
843
+ function headerCell(v, header) {
844
+ const column = v.byId.get(header.column.id);
845
+ if (!column) return null;
846
+ const sorted = header.column.getIsSorted();
847
+ const direction = sorted === "asc" ? "ascending" : sorted === "desc" ? "descending" : void 0;
848
+ const { style, ...pinnedProps } = pin(header.column);
849
+ const level = priorityOf(column);
850
+ const top = typeof v.stickyHeader === "number" ? { ["--dt-top"]: `${v.stickyHeader}px` } : {};
851
+ return /* @__PURE__ */ jsxs3(
852
+ "th",
853
+ {
854
+ role: "columnheader",
855
+ scope: "col",
856
+ "aria-sort": column.sortable ? direction ?? "none" : void 0,
857
+ "data-keep": column.sortable ? "" : void 0,
858
+ ...pinnedProps,
859
+ className: v.s.th({ className: cn(ALIGN[column.align ?? (column.type === "number" ? "end" : "start")], level > 1 && LEAVES_HEAD[v.L][level]) }),
860
+ style: { ...style, ...top, ...widthOf(v, header.column) },
861
+ children: [
862
+ headerLabel(v, header, column, direction),
863
+ v.resizing && header.column.getCanResize() ? /* @__PURE__ */ jsx3(
864
+ ResizeHandle,
865
+ {
866
+ label: v.labels.resize(column.header),
867
+ size: header.column.getSize(),
868
+ min: column.minWidth ?? 64,
869
+ resizing: header.column.getIsResizing(),
870
+ className: v.s.resizer(),
871
+ onPointerStart: header.getResizeHandler(),
872
+ onSize: (size) => v.setSizing((old) => ({ ...old, [header.column.id]: size }))
873
+ }
874
+ ) : null
875
+ ]
876
+ },
877
+ header.id
878
+ );
879
+ }
880
+ function headerRow(v, headers) {
881
+ const { s: s2, labels } = v;
882
+ return /* @__PURE__ */ jsxs3("tr", { role: "row", "aria-rowindex": v.virtualized ? 1 : void 0, className: s2.headRow(), children: [
883
+ v.selectable && /* @__PURE__ */ jsx3("th", { role: "columnheader", scope: "col", "data-keep": "", className: s2.th({ className: s2.select() }), children: /* @__PURE__ */ jsx3(
884
+ Checkbox,
885
+ {
886
+ checked: v.allChosen ? true : v.someChosen ? "indeterminate" : false,
887
+ onCheckedChange: v.toggleAll,
888
+ disabled: v.loading || v.selectableCount === 0,
889
+ "aria-label": labels.selectAll
890
+ }
891
+ ) }),
892
+ v.hasExpander && /* @__PURE__ */ jsx3("th", { role: "columnheader", scope: "col", className: s2.th({ className: s2.expander() }), children: /* @__PURE__ */ jsx3("span", { className: "sr-only", children: labels.details }) }),
893
+ headers.map((header) => headerCell(v, header))
894
+ ] });
895
+ }
896
+ function useControlled(p, route) {
897
+ const { defaultSort = null, defaultFilters = NO_RECORD, defaultSelected = NONE2, defaultGroupBy = NONE2, defaultColumnVisibility = NO_RECORD, defaultExpanded = NO_RECORD, defaultPage = 1 } = p;
898
+ const paginated = p.pageSize !== void 0 || p.defaultPageSize !== void 0;
899
+ const [sort, setSort] = useControllable(route ? route.sort : p.sort, defaultSort, route?.onSortChange ?? p.onSortChange);
900
+ const [filters, setFiltersState] = useControllable(route ? route.filters : p.filters, defaultFilters, route?.onFiltersChange ?? p.onFiltersChange);
901
+ const [selected, setSelected] = useControllable(p.selected, defaultSelected, p.onSelectedChange);
902
+ const [groupBy, setGroupBy] = useControllable(p.groupBy, defaultGroupBy, p.onGroupByChange);
903
+ const [visibility, setVisibility] = useControllable(p.columnVisibility, defaultColumnVisibility, p.onColumnVisibilityChange);
904
+ const [expanded, setExpanded] = useControllable(p.expanded, defaultExpanded, p.onExpandedChange);
905
+ const [pageSize, setPageSize] = useControllable(p.pageSize, p.defaultPageSize ?? Number.POSITIVE_INFINITY, p.onPageSizeChange);
906
+ const [page, setPage] = useControllable(route ? route.page : p.page, defaultPage, route?.onPageChange ?? p.onPageChange);
907
+ return { paginated, sort, setSort, filters, setFiltersState, selected, setSelected, groupBy, setGroupBy, visibility, setVisibility, expanded, setExpanded, pageSize, setPageSize, page, setPage };
908
+ }
909
+ function useAnnounce(p, c, route) {
910
+ const { paginated, sort, filters, page, pageSize } = c;
911
+ const { defaultSort = null, defaultFilters = NO_RECORD, defaultPage = 1 } = p;
680
912
  const announced = React2.useRef(JSON.stringify([defaultSort, defaultFilters, paginated ? defaultPage : 1, paginated ? pageSize : null]));
681
913
  const announce = (next) => {
682
914
  const query = { sort, filters, page: paginated ? page : 1, pageSize: paginated ? pageSize : Number.POSITIVE_INFINITY, ...next };
683
915
  if (route) announced.current = JSON.stringify([query.sort, query.filters, query.page, paginated ? query.pageSize : null]);
684
- onStateChange?.(query);
685
- };
686
- const changeSort = (next) => {
687
- setSort(next);
688
- if (paginated && page !== 1 && !route) setPage(1);
689
- announce({ sort: next, page: 1 });
690
- };
691
- const changeFilters = (next) => {
692
- setFiltersState(next);
693
- if (paginated && page !== 1 && !route) setPage(1);
694
- announce({ filters: next, page: 1 });
916
+ p.onStateChange?.(query);
695
917
  };
696
918
  const routeQuery = route ? JSON.stringify([route.sort, route.filters, paginated ? route.page : 1, paginated ? pageSize : null]) : null;
697
919
  React2.useEffect(() => {
@@ -699,50 +921,108 @@ function DataTable({
699
921
  announced.current = routeQuery;
700
922
  announce({});
701
923
  }, [routeQuery]);
702
- const changePage = (next) => {
703
- setPage(next);
704
- announce({ page: next });
924
+ return announce;
925
+ }
926
+ function useQueryChanges(p, c, route) {
927
+ const announce = useAnnounce(p, c, route);
928
+ const resetPage = () => {
929
+ if (c.paginated && c.page !== 1 && !route) c.setPage(1);
705
930
  };
706
- const changePageSize = (next) => {
707
- setPageSize(next);
708
- setPage(1);
709
- announce({ pageSize: next, page: 1 });
931
+ return {
932
+ changeSort: (next) => {
933
+ c.setSort(next);
934
+ resetPage();
935
+ announce({ sort: next, page: 1 });
936
+ },
937
+ changeFilters: (next) => {
938
+ c.setFiltersState(next);
939
+ resetPage();
940
+ announce({ filters: next, page: 1 });
941
+ },
942
+ changePage: (next) => {
943
+ c.setPage(next);
944
+ announce({ page: next });
945
+ },
946
+ changePageSize: (next) => {
947
+ c.setPageSize(next);
948
+ c.setPage(1);
949
+ announce({ pageSize: next, page: 1 });
950
+ }
710
951
  };
952
+ }
953
+ var columnDefs = (columns, resizing) => columns.map((column) => ({
954
+ id: column.id,
955
+ accessorFn: (row) => valueOf(column, row),
956
+ header: column.header,
957
+ enableSorting: Boolean(column.sortable),
958
+ sortFn: sortByValue,
959
+ sortUndefined: "last",
960
+ enableColumnFilter: Boolean(column.filter),
961
+ filterFn: column.filter ? FILTERS[column.filter] : FILTERS.text,
962
+ enableGrouping: Boolean(column.groupable),
963
+ enableHiding: column.hideable !== false,
964
+ enableResizing: Boolean(resizing),
965
+ ...column.width ? { size: column.width } : {},
966
+ minSize: column.minWidth ?? 64
967
+ }));
968
+ function engineState(c, rowCount, pinned, sizing, resizeState) {
969
+ return {
970
+ sorting: sortingOf(c.sort),
971
+ columnFilters: filterListOf(c.filters),
972
+ rowSelection: selectionOf(c.selected),
973
+ grouping: [...c.groupBy],
974
+ columnVisibility: { ...c.visibility },
975
+ expanded: { ...c.expanded },
976
+ pagination: { pageIndex: Math.max(0, c.page - 1), pageSize: c.paginated ? c.pageSize : Math.max(1, rowCount) },
977
+ columnPinning: { start: [...pinned?.start ?? []], end: [...pinned?.end ?? []] },
978
+ columnSizing: sizing,
979
+ ...resizeState ? { columnResizing: resizeState } : {}
980
+ };
981
+ }
982
+ function engineHandlers(c, changes, setSizing, setResizeState, currentRows) {
983
+ return {
984
+ onSortingChange: (updater) => {
985
+ const [first] = apply(updater, sortingOf(c.sort));
986
+ changes.changeSort(first ? { column: first.id, direction: first.desc ? "descending" : "ascending" } : null);
987
+ },
988
+ onColumnFiltersChange: (updater) => {
989
+ const next = apply(updater, filterListOf(c.filters));
990
+ changes.changeFilters(Object.fromEntries(next.map((f) => [f.id, f.value])));
991
+ },
992
+ onRowSelectionChange: (updater) => {
993
+ const next = apply(updater, selectionOf(c.selected));
994
+ c.setSelected(Object.keys(next).filter((id) => next[id]));
995
+ },
996
+ onGroupingChange: (updater) => c.setGroupBy(apply(updater, [...c.groupBy])),
997
+ onColumnVisibilityChange: (updater) => c.setVisibility(apply(updater, { ...c.visibility })),
998
+ onExpandedChange: (updater) => {
999
+ const next = apply(updater, { ...c.expanded });
1000
+ c.setExpanded(next === true ? Object.fromEntries(currentRows().map((r) => [r.id, true])) : next);
1001
+ },
1002
+ onColumnSizingChange: (updater) => setSizing((old) => apply(updater, old)),
1003
+ onColumnResizingChange: (updater) => setResizeState((old) => apply(updater, old))
1004
+ };
1005
+ }
1006
+ function shapeOf(props) {
1007
+ const { columns, phone = "cards", virtualized, renderDetail, getSubRows } = props;
1008
+ const L = virtualized ? "table" : props.layout ?? "auto";
1009
+ const typed = columns.some((c) => c.type || c.priority);
1010
+ const list = phone === "list" && L !== "table";
711
1011
  const hasExpander = Boolean(renderDetail || getSubRows);
712
- const priorityOf = (column) => column.priority && column.priority > 1 ? column.priority : 1;
713
1012
  const maxPriority = columns.reduce((max, column) => priorityOf(column) > max ? priorityOf(column) : max, 1);
714
- const titleColumn = columns.find((c) => c.card === "title" || !c.card && c.type === "title") ?? columns[0];
715
- const placeOf = (column) => {
716
- if (column.card === "hidden") return "hidden";
717
- if (column === titleColumn) return "title";
718
- if (column.type === "status" || column.type === "badge") return "status";
719
- return priorityOf(column) === 2 ? "facts" : "away";
720
- };
721
- const listAway = list ? columns.filter((c) => placeOf(c) === "away" && priorityOf(c) === 1) : [];
722
- const facts = list ? columns.filter((c) => placeOf(c) === "facts") : [];
1013
+ const titleColumn = columns.find((col) => col.card === "title" || !col.card && col.type === "title") ?? columns[0];
1014
+ const listAway = list ? columns.filter((col) => placeOf(titleColumn, col) === "away" && priorityOf(col) === 1) : [];
1015
+ const facts = list ? columns.filter((col) => placeOf(titleColumn, col) === "facts") : [];
723
1016
  const inlineToggle = (maxPriority > 1 || listAway.length > 0) && !hasExpander;
724
- const detailed = Boolean(renderDetail) || inlineToggle;
725
- const defs = React2.useMemo(
726
- () => columns.map((column) => ({
727
- id: column.id,
728
- accessorFn: (row) => valueOf(column, row),
729
- header: column.header,
730
- enableSorting: Boolean(column.sortable),
731
- sortFn: sortByValue,
732
- sortUndefined: "last",
733
- enableColumnFilter: Boolean(column.filter),
734
- filterFn: column.filter ? FILTERS[column.filter] : FILTERS.text,
735
- enableGrouping: Boolean(column.groupable),
736
- enableHiding: column.hideable !== false,
737
- enableResizing: Boolean(resizing),
738
- ...column.width ? { size: column.width } : {},
739
- minSize: column.minWidth ?? 64
740
- })),
741
- [columns, resizing]
742
- );
1017
+ return { L, typed, list, hasExpander, maxPriority, inlineToggle, detailed: Boolean(renderDetail) || inlineToggle, titleColumn, listAway, facts };
1018
+ }
1019
+ function useEngine(props, c, changes, detailed) {
1020
+ const { rows, columns, getRowId, getSubRows, pinning, resizing, manual, rowCount, stickyFirstColumn } = props;
1021
+ const [sizing, setSizing] = React2.useState({});
1022
+ const [resizeState, setResizeState] = React2.useState(void 0);
1023
+ const defs = React2.useMemo(() => columnDefs(columns, resizing), [columns, resizing]);
743
1024
  const firstId = columns[0]?.id;
744
1025
  const pinned = stickyFirstColumn && firstId && !pinning?.start?.length ? { ...pinning, start: [firstId] } : pinning;
745
- const pageIndex = Math.max(0, page - 1);
746
1026
  const table = useTable({
747
1027
  features,
748
1028
  columns: defs,
@@ -750,43 +1030,13 @@ function DataTable({
750
1030
  getRowId: (row) => getRowId(row),
751
1031
  getSubRows: getSubRows ? (row) => getSubRows(row) : void 0,
752
1032
  getRowCanExpand: detailed ? () => true : void 0,
753
- state: {
754
- sorting: sort ? [{ id: sort.column, desc: sort.direction === "descending" }] : [],
755
- columnFilters: Object.entries(filters).map(([id, value]) => ({ id, value })),
756
- rowSelection: Object.fromEntries(selected.map((id) => [id, true])),
757
- grouping: [...groupBy],
758
- columnVisibility: { ...visibility },
759
- expanded: { ...expanded },
760
- pagination: { pageIndex, pageSize: paginated ? pageSize : Math.max(1, rows.length) },
761
- columnPinning: { start: [...pinned?.start ?? []], end: [...pinned?.end ?? []] },
762
- columnSizing: sizing,
763
- ...resizeState ? { columnResizing: resizeState } : {}
764
- },
765
- onSortingChange: (updater) => {
766
- const [first] = apply(updater, sort ? [{ id: sort.column, desc: sort.direction === "descending" }] : []);
767
- changeSort(first ? { column: first.id, direction: first.desc ? "descending" : "ascending" } : null);
768
- },
769
- onColumnFiltersChange: (updater) => {
770
- const next = apply(updater, Object.entries(filters).map(([id, value]) => ({ id, value })));
771
- changeFilters(Object.fromEntries(next.map((f) => [f.id, f.value])));
772
- },
773
- onRowSelectionChange: (updater) => {
774
- const next = apply(updater, Object.fromEntries(selected.map((id) => [id, true])));
775
- setSelected(Object.keys(next).filter((id) => next[id]));
776
- },
777
- onGroupingChange: (updater) => setGroupBy(apply(updater, [...groupBy])),
778
- onColumnVisibilityChange: (updater) => setVisibility(apply(updater, { ...visibility })),
779
- onExpandedChange: (updater) => {
780
- const next = apply(updater, { ...expanded });
781
- setExpanded(next === true ? Object.fromEntries(table.getRowModel().rows.map((r) => [r.id, true])) : next);
782
- },
783
- onColumnSizingChange: (updater) => setSizing((old) => apply(updater, old)),
784
- onColumnResizingChange: (updater) => setResizeState((old) => apply(updater, old)),
1033
+ state: engineState(c, rows.length, pinned, sizing, resizeState),
1034
+ ...engineHandlers(c, changes, setSizing, setResizeState, () => table.getRowModel().rows),
785
1035
  enableSortingRemoval: true,
786
1036
  sortDescFirst: false,
787
1037
  manualSorting: manual?.sorting,
788
1038
  manualFiltering: manual?.filtering,
789
- manualPagination: manual?.pagination || !paginated,
1039
+ manualPagination: manual?.pagination || !c.paginated,
790
1040
  rowCount: manual?.pagination ? rowCount : void 0,
791
1041
  autoResetPageIndex: false,
792
1042
  autoResetExpanded: false,
@@ -794,302 +1044,126 @@ function DataTable({
794
1044
  columnResizeMode: "onChange",
795
1045
  enableRowSelection: (row) => !row.getIsGrouped()
796
1046
  });
797
- const visibleColumns = table.getVisibleLeafColumns();
798
- const byId = new Map(columns.map((c) => [c.id, c]));
799
- const pageRows = table.getRowModel().rows;
1047
+ return { table, pinned, setSizing };
1048
+ }
1049
+ function usePaging(props, c, changes, table) {
1050
+ const { manual, rowCount, rows, loading } = props;
1051
+ const { paginated, page, pageSize } = c;
800
1052
  const leafCount = (manual?.pagination || manual?.filtering) && rowCount !== void 0 ? rowCount : table.getFilteredRowModel().rows.length;
801
1053
  const pageCount = paginated ? Math.max(1, manual?.pagination ? Math.ceil((rowCount ?? rows.length) / pageSize) : table.getPageCount()) : 1;
802
1054
  React2.useEffect(() => {
803
- if (paginated && !loading && page > pageCount) changePage(pageCount);
1055
+ if (paginated && !loading && page > pageCount) changes.changePage(pageCount);
804
1056
  }, [pageCount]);
1057
+ return { leafCount, pageCount };
1058
+ }
1059
+ function useChoice(pageRows, selected, setSelected) {
805
1060
  const selectableIds = pageRows.filter((r) => !r.getIsGrouped()).map((r) => r.id);
806
1061
  const chosen = new Set(selected);
807
1062
  const allChosen = selectableIds.length > 0 && selectableIds.every((id) => chosen.has(id));
808
1063
  const someChosen = selectableIds.some((id) => chosen.has(id));
809
1064
  const toggleAll = () => setSelected(allChosen ? selected.filter((id) => !selectableIds.includes(id)) : [.../* @__PURE__ */ new Set([...selected, ...selectableIds])]);
810
- const span = visibleColumns.length + (selectable ? 1 : 0) + (hasExpander ? 1 : 0);
811
- const fixedLayout = Boolean(resizing || pinned?.start?.length || pinned?.end?.length);
812
- const sideways = scroll ?? (!typed && !stickyHeader);
813
- const nameOf = (row) => rowLabel ? rowLabel(row.original) : row.id;
814
- const scroller = React2.useRef(null);
815
- const fallbacks = useLayoutFallbacks(virtualized?.height ?? 0, virtualized?.estimateRowHeight ?? 44);
816
- const virtualizer = useVirtualizer({
817
- ...fallbacks,
818
- count: pageRows.length,
819
- getScrollElement: () => scroller.current,
820
- estimateSize: () => virtualized?.estimateRowHeight ?? 44,
821
- overscan: virtualized?.overscan ?? 6,
822
- initialRect: { width: 0, height: virtualized?.height ?? 0 },
823
- getItemKey: (index) => pageRows[index]?.id ?? index,
824
- enabled: Boolean(virtualized) && !loading
825
- });
826
- const windowItems = virtualized && !loading ? virtualizer.getVirtualItems() : null;
827
- const before = windowItems?.[0]?.start ?? 0;
828
- const after = windowItems && windowItems.length > 0 ? virtualizer.getTotalSize() - (windowItems.at(-1)?.end ?? 0) : 0;
829
- const pin = (column) => {
830
- const position = column.getIsPinned();
831
- if (!position) return {};
832
- const offset = position === "start" ? column.getStart("start") : column.getAfter("end");
833
- return { "data-pin": position, style: { ["--dt-pin"]: `${offset}px` } };
834
- };
835
- const width = (column) => fixedLayout || byId.get(column.id)?.width ? { width: column.getSize() } : void 0;
836
- const message = (content, alert) => /* @__PURE__ */ jsx3("tr", { role: "row", className: s2.tr({ className: "hover:bg-transparent" }), children: /* @__PURE__ */ jsx3("td", { role: "cell", colSpan: span, "data-card": "message", className: s2.td({ className: s2.message() }), children: alert ? /* @__PURE__ */ jsx3("div", { role: "alert", children: content }) : content }) });
837
- const cardOf = (column) => column?.card === "title" || column && !column.card && column.type === "title" ? "title" : column?.card === "hidden" ? "hidden" : void 0;
838
- const contentOf = (column, original) => {
839
- if (column.cell) return column.cell(original);
840
- const value = valueOf(column, original);
841
- const text = column.type === "date" && value instanceof Date ? value.toISOString().slice(0, 10) : display(value);
842
- const cut = TRUNCATE[L];
843
- switch (column.type) {
844
- case "title": {
845
- const sub = column.subtitle?.(original);
846
- const href = column.href?.(original);
847
- return /* @__PURE__ */ jsxs3("span", { className: "block min-w-0", children: [
848
- href ? /* @__PURE__ */ jsx3("a", { href, className: cn("block font-semibold text-fg no-underline hover:underline focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring", cut, list && LIST_LINK[L]), title: text, children: text }) : /* @__PURE__ */ jsx3("span", { className: cn("block font-semibold text-fg", cut), title: text, children: text }),
849
- sub ? /* @__PURE__ */ jsx3("span", { className: cn("block font-mono text-xs text-fg-tertiary", cut), children: sub }) : null
850
- ] });
851
- }
852
- case "number":
853
- return /* @__PURE__ */ jsxs3("span", { children: [
854
- typeof value === "number" ? value.toLocaleString("en") : text,
855
- column.unit ? /* @__PURE__ */ jsx3("span", { className: "ms-1 text-fg-secondary", children: column.unit }) : null
856
- ] });
857
- case "link": {
858
- const href = column.href?.(original);
859
- return href ? /* @__PURE__ */ jsx3("a", { href, title: text, className: cn("block text-fg-accent underline-offset-2 hover:underline", cut), children: text }) : /* @__PURE__ */ jsx3("span", { className: cn("block", cut), title: text, children: text });
860
- }
861
- default: {
862
- const lines = column.clamp ?? (column.type === "text" ? 2 : void 0);
863
- if (!column.nowrap && !lines) return text;
864
- return /* @__PURE__ */ jsx3("span", { className: column.nowrap ? cn("block", cut || "truncate") : CLAMP[lines], title: text, children: text });
865
- }
866
- }
867
- };
868
- const listPlace = (row, column) => {
869
- if (row?.getIsGrouped()) return row.groupingColumnId === column.id ? "title" : column.aggregate ? "status" : "away";
870
- const place = placeOf(column);
871
- return place === "facts" || place === "hidden" ? "away" : place;
872
- };
873
- const dataCell = (row, cell, first) => {
874
- const column = byId.get(cell.column.id);
875
- if (!column) return null;
876
- const { style, ...pinnedProps } = pin(cell.column);
877
- let content;
878
- if (row.getIsGrouped()) {
879
- const leaves = row.getLeafRows().filter((r) => !r.getIsGrouped()).map((r) => r.original);
880
- if (cell.column.id === row.groupingColumnId) {
881
- const value = display(row.groupingValue);
882
- content = /* @__PURE__ */ jsxs3("button", { type: "button", className: s2.toggle({ className: "px-1.5 font-semibold text-fg" }), "aria-expanded": row.getIsExpanded(), onClick: row.getToggleExpandedHandler(), children: [
883
- /* @__PURE__ */ jsx3(ChevronGlyph, { direction: "right", className: "h-4 w-4" }),
884
- labels.group(value, leaves.length)
885
- ] });
886
- } else content = column.aggregate ? column.aggregate(leaves) : null;
887
- } else {
888
- content = contentOf(column, row.original);
889
- if (list && column === titleColumn && facts.length > 0) {
890
- content = /* @__PURE__ */ jsxs3("span", { className: "block min-w-0", children: [
891
- content,
892
- /* @__PURE__ */ jsx3("span", { "data-facts": "", className: LIST_FACTS[L], children: facts.map((c, i) => /* @__PURE__ */ jsxs3(React2.Fragment, { children: [
893
- i > 0 ? /* @__PURE__ */ jsx3("span", { "aria-hidden": true, children: " \xB7 " }) : null,
894
- contentOf(c, row.original)
895
- ] }, c.id)) })
896
- ] });
897
- }
898
- if (first && inlineToggle) {
899
- content = /* @__PURE__ */ jsxs3("span", { className: "flex min-w-0 items-center gap-1", children: [
900
- /* @__PURE__ */ jsx3(
901
- "button",
902
- {
903
- type: "button",
904
- className: s2.toggle({ className: cn(maxPriority > 1 ? TOGGLE[L][maxPriority] : "hidden", listAway.length > 0 && LIST_TOGGLE[L], list && "relative z-[1]") }),
905
- "aria-expanded": row.getIsExpanded(),
906
- "aria-label": labels.showMore(nameOf(row)),
907
- onClick: row.getToggleExpandedHandler(),
908
- children: /* @__PURE__ */ jsx3(ChevronGlyph, { direction: "right", className: "h-4 w-4" })
909
- }
910
- ),
911
- /* @__PURE__ */ jsx3("span", { className: "min-w-0 flex-1", children: content })
912
- ] });
913
- }
914
- }
915
- const level = priorityOf(column);
916
- const type = column.type;
917
- const cellClass = [
918
- ALIGN[column.align ?? (type === "number" ? "end" : "start")],
919
- type ? TYPE_CELL[type][L] : "",
920
- level > 1 ? LEAVES[L][level] : "",
921
- (type || column.priority) && column.minWidth !== void 0 ? MIN[L] : "",
922
- column.maxWidth !== void 0 ? MAX[L] : ""
923
- ].join(" ");
924
- return /* @__PURE__ */ jsx3(
925
- "td",
926
- {
927
- role: "cell",
928
- "data-label": column.header,
929
- "data-card": list ? placeOf(column) === "hidden" ? "hidden" : void 0 : row.getIsGrouped() ? cell.column.id === row.groupingColumnId ? "title" : column.aggregate ? void 0 : "hidden" : cardOf(column),
930
- "data-list": list ? listPlace(row, column) : void 0,
931
- ...pinnedProps,
932
- className: s2.td({ className: cellClass }),
933
- style: {
934
- ...style,
935
- ...column.minWidth !== void 0 ? { ["--dt-min"]: `${column.minWidth}px` } : {},
936
- ...column.maxWidth !== void 0 ? { ["--dt-max"]: `${column.maxWidth}px` } : {},
937
- ...first && row.depth > 0 && !row.getIsGrouped() ? { paddingInlineStart: `${1 + row.depth * 1.25}rem` } : {}
938
- },
939
- children: content
940
- },
941
- cell.id
942
- );
943
- };
944
- const renderRow = (row, index) => {
945
- const isChosen = chosen.has(row.id);
946
- const grouped = row.getIsGrouped();
947
- const canExpand = !grouped && (renderDetail ? true : row.getCanExpand());
948
- const showDetail = detailed && !grouped && row.getIsExpanded() && !virtualized;
949
- const cells = row.getVisibleCells();
950
- const main = /* @__PURE__ */ jsxs3(
951
- "tr",
952
- {
953
- role: "row",
954
- "data-index": windowItems ? index : void 0,
955
- ref: windowItems ? virtualizer.measureElement : void 0,
956
- "aria-rowindex": virtualized ? index + 2 : void 0,
957
- "data-state": isChosen ? "selected" : void 0,
958
- "data-group": grouped ? "" : void 0,
959
- className: s2.tr({ className: grouped ? s2.groupRow() : void 0 }),
960
- children: [
961
- selectable && /* @__PURE__ */ jsx3("td", { role: "cell", className: s2.td({ className: s2.select() }), children: grouped ? null : /* @__PURE__ */ jsx3(Checkbox, { checked: isChosen, onCheckedChange: () => row.toggleSelected(), "aria-label": labels.selectRow(nameOf(row)) }) }),
962
- hasExpander && /* @__PURE__ */ jsx3("td", { role: "cell", className: s2.td({ className: s2.expander() }), children: canExpand ? /* @__PURE__ */ jsx3("button", { type: "button", className: s2.toggle(), "aria-expanded": row.getIsExpanded(), "aria-label": labels.expandRow(nameOf(row)), onClick: row.getToggleExpandedHandler(), children: /* @__PURE__ */ jsx3(ChevronGlyph, { direction: "right", className: "h-4 w-4" }) }) : null }),
963
- cells.map((cell, i) => dataCell(row, cell, i === 0))
964
- ]
965
- }
966
- );
967
- if (!showDetail) return /* @__PURE__ */ jsx3(React2.Fragment, { children: main }, row.id);
968
- const left = visibleColumns.filter((c) => {
969
- const column = byId.get(c.id);
970
- return priorityOf(column) > 1 || listAway.includes(column);
971
- });
972
- return /* @__PURE__ */ jsxs3(React2.Fragment, { children: [
973
- main,
974
- /* @__PURE__ */ jsx3("tr", { role: "row", className: s2.tr({ className: "hover:bg-transparent" }), children: /* @__PURE__ */ jsxs3("td", { role: "cell", colSpan: span, "data-card": "message", className: s2.td({ className: renderDetail ? s2.detail() : "bg-surface-0 p-0 text-left" }), children: [
975
- left.length > 0 ? /* @__PURE__ */ jsx3("dl", { className: cn("m-0 grid gap-3 text-left", renderDetail ? "mb-4" : cn("px-4 py-4", maxPriority > 1 ? PANEL[L][maxPriority] : "hidden", listAway.length > 0 && LIST_PANEL[L])), children: left.map((c) => {
976
- const column = byId.get(c.id);
977
- return /* @__PURE__ */ jsxs3("div", { className: cn(listAway.includes(column) ? LIST_IN_DETAIL[L] : IN_DETAIL[L][priorityOf(column)], "grid-cols-[minmax(5rem,8rem)_minmax(0,1fr)] items-baseline gap-3"), children: [
978
- /* @__PURE__ */ jsx3("dt", { className: "text-xs font-semibold text-fg-secondary", children: column.header }),
979
- /* @__PURE__ */ jsx3("dd", { className: "m-0 min-w-0 text-sm text-fg", children: contentOf(column, row.original) })
980
- ] }, c.id);
981
- }) }) : null,
982
- renderDetail ? renderDetail(row.original) : null
983
- ] }) })
984
- ] }, row.id);
985
- };
986
- let body;
987
- if (loading) {
988
- body = Array.from({ length: loadingRows }, (_, i) => /* @__PURE__ */ jsxs3("tr", { role: "row", "aria-hidden": true, className: s2.tr(), children: [
989
- selectable && /* @__PURE__ */ jsx3("td", { role: "cell", className: s2.td({ className: s2.select() }) }),
990
- hasExpander && /* @__PURE__ */ jsx3("td", { role: "cell", className: s2.td({ className: s2.expander() }) }),
991
- visibleColumns.map((column) => /* @__PURE__ */ jsx3("td", { role: "cell", "data-card": list ? void 0 : cardOf(byId.get(column.id)), "data-list": list ? listPlace(null, byId.get(column.id)) : void 0, className: s2.td(), children: /* @__PURE__ */ jsx3("div", { className: s2.skeleton() }) }, column.id))
992
- ] }, `loading-${i}`));
993
- } else if (error) body = message(error, true);
994
- else if (pageRows.length === 0) body = message(empty2 ?? labels.empty);
995
- else if (windowItems) body = windowItems.map((item) => renderRow(pageRows[item.index], item.index));
996
- else body = pageRows.map((row, i) => renderRow(row, i));
1065
+ return { chosen, allChosen, someChosen, toggleAll, selectableCount: selectableIds.length };
1066
+ }
1067
+ function useRoute(props) {
1068
+ const { defaultSort, defaultPage, defaultFilters, columns } = props;
1069
+ return useRouteTable(props.routeKey, { sort: defaultSort ?? null, page: defaultPage ?? 1, filters: defaultFilters ?? NO_RECORD }, columns.map((c) => c.id));
1070
+ }
1071
+ function tableOf(v, table, caption, showCaption, before, after) {
1072
+ const { s: s2, labels, virtualized, span, fixedLayout, loading } = v;
997
1073
  const headers = table.getHeaderGroups().at(-1)?.headers ?? [];
998
- const tableElement = /* @__PURE__ */ jsxs3(
1074
+ const spacer = (height) => /* @__PURE__ */ jsx3("tr", { "aria-hidden": true, style: { height }, children: /* @__PURE__ */ jsx3("td", { colSpan: span }) });
1075
+ return /* @__PURE__ */ jsxs3(
999
1076
  "table",
1000
1077
  {
1001
1078
  role: "table",
1002
1079
  "aria-busy": loading || void 0,
1003
- "aria-rowcount": virtualized ? pageRows.length + 1 : void 0,
1080
+ "aria-rowcount": virtualized ? v.pageRows.length + 1 : void 0,
1004
1081
  className: s2.table({ className: fixedLayout ? "table-fixed" : void 0 }),
1005
- style: fixedLayout ? { minWidth: table.getTotalSize() + (selectable ? 40 : 0) + (hasExpander ? 40 : 0) } : void 0,
1082
+ style: fixedLayout ? { minWidth: table.getTotalSize() + (v.selectable ? 40 : 0) + (v.hasExpander ? 40 : 0) } : void 0,
1006
1083
  children: [
1007
1084
  /* @__PURE__ */ jsxs3("caption", { className: showCaption ? s2.caption() : "sr-only", children: [
1008
1085
  caption,
1009
1086
  loading ? /* @__PURE__ */ jsx3("span", { className: "sr-only", children: ` \u2014 ${labels.loading}` }) : null
1010
1087
  ] }),
1011
- /* @__PURE__ */ jsx3("thead", { role: "rowgroup", className: s2.thead({ className: virtualized ? "sticky top-0 z-10 bg-surface-2" : void 0 }), children: /* @__PURE__ */ jsxs3("tr", { role: "row", "aria-rowindex": virtualized ? 1 : void 0, className: s2.headRow(), children: [
1012
- selectable && /* @__PURE__ */ jsx3("th", { role: "columnheader", scope: "col", "data-keep": "", className: s2.th({ className: s2.select() }), children: /* @__PURE__ */ jsx3(
1013
- Checkbox,
1014
- {
1015
- checked: allChosen ? true : someChosen ? "indeterminate" : false,
1016
- onCheckedChange: toggleAll,
1017
- disabled: loading || selectableIds.length === 0,
1018
- "aria-label": labels.selectAll
1019
- }
1020
- ) }),
1021
- hasExpander && /* @__PURE__ */ jsx3("th", { role: "columnheader", scope: "col", className: s2.th({ className: s2.expander() }), children: /* @__PURE__ */ jsx3("span", { className: "sr-only", children: labels.details }) }),
1022
- headers.map((header) => {
1023
- const column = byId.get(header.column.id);
1024
- if (!column) return null;
1025
- const sorted = header.column.getIsSorted();
1026
- const direction = sorted === "asc" ? "ascending" : sorted === "desc" ? "descending" : void 0;
1027
- const { style, ...pinnedProps } = pin(header.column);
1028
- const level = priorityOf(column);
1029
- return /* @__PURE__ */ jsxs3(
1030
- "th",
1031
- {
1032
- role: "columnheader",
1033
- scope: "col",
1034
- "aria-sort": column.sortable ? direction ?? "none" : void 0,
1035
- "data-keep": column.sortable ? "" : void 0,
1036
- ...pinnedProps,
1037
- className: s2.th({ className: cn(ALIGN[column.align ?? (column.type === "number" ? "end" : "start")], level > 1 && LEAVES_HEAD[L][level]) }),
1038
- style: { ...style, ...typeof stickyHeader === "number" ? { ["--dt-top"]: `${stickyHeader}px` } : {}, ...width(header.column) },
1039
- children: [
1040
- column.sortable ? /* @__PURE__ */ jsxs3("button", { type: "button", "aria-pressed": Boolean(direction), className: s2.sortButton(), onClick: () => header.column.toggleSorting(void 0, false), children: [
1041
- column.header,
1042
- /* @__PURE__ */ jsx3(SortGlyph, { direction, className: "h-3.5 w-3.5" })
1043
- ] }) : column.headerHidden ? /* @__PURE__ */ jsx3("span", { className: "sr-only", children: column.header }) : column.header,
1044
- resizing && header.column.getCanResize() ? /* @__PURE__ */ jsx3(
1045
- ResizeHandle,
1046
- {
1047
- label: labels.resize(column.header),
1048
- size: header.column.getSize(),
1049
- min: column.minWidth ?? 64,
1050
- resizing: header.column.getIsResizing(),
1051
- className: s2.resizer(),
1052
- onPointerStart: header.getResizeHandler(),
1053
- onSize: (size) => setSizing((old) => ({ ...old, [header.column.id]: size }))
1054
- }
1055
- ) : null
1056
- ]
1057
- },
1058
- header.id
1059
- );
1060
- })
1061
- ] }) }),
1088
+ /* @__PURE__ */ jsx3("thead", { role: "rowgroup", className: s2.thead({ className: virtualized ? "sticky top-0 z-10 bg-surface-2" : void 0 }), children: headerRow(v, headers) }),
1062
1089
  /* @__PURE__ */ jsxs3("tbody", { role: "rowgroup", className: s2.tbody(), children: [
1063
- before > 0 && /* @__PURE__ */ jsx3("tr", { "aria-hidden": true, style: { height: before }, children: /* @__PURE__ */ jsx3("td", { colSpan: span }) }),
1064
- body,
1065
- after > 0 && /* @__PURE__ */ jsx3("tr", { "aria-hidden": true, style: { height: after }, children: /* @__PURE__ */ jsx3("td", { colSpan: span }) })
1090
+ before > 0 && spacer(before),
1091
+ bodyRows(v),
1092
+ after > 0 && spacer(after)
1066
1093
  ] })
1067
1094
  ]
1068
1095
  }
1069
1096
  );
1097
+ }
1098
+ function pager(props, c, changes, pageCount, labels) {
1099
+ const { pageSizeOptions, loading } = props;
1100
+ return /* @__PURE__ */ jsx3(
1101
+ Pagination,
1102
+ {
1103
+ page: Math.min(c.page, pageCount),
1104
+ pageCount,
1105
+ onPageChange: changes.changePage,
1106
+ pageSize: pageSizeOptions ? c.pageSize : void 0,
1107
+ pageSizeOptions,
1108
+ onPageSizeChange: pageSizeOptions ? changes.changePageSize : void 0,
1109
+ disabled: loading,
1110
+ labels: labels.pagination
1111
+ }
1112
+ );
1113
+ }
1114
+ function DataTable(props) {
1115
+ const { columns, caption, showCaption, selectable, bulkActions, bulkMax, bulkResult, resizing, renderDetail, rowLabel, toolbar, footer, className, virtualized, loading, stickyHeader, scroll, manual } = props;
1116
+ const labels = { ...dataTableLabels, ...props.labels };
1117
+ const shape = shapeOf(props);
1118
+ const s2 = dataTable({ layout: shape.L, density: props.density, sticky: Boolean(stickyHeader), striped: Boolean(props.striped), typed: shape.typed, phone: shape.list ? "list" : "cards" });
1119
+ const route = useRoute(props);
1120
+ const c = useControlled(props, route);
1121
+ const changes = useQueryChanges(props, c, route);
1122
+ const { table, pinned, setSizing } = useEngine(props, c, changes, shape.detailed);
1123
+ const { leafCount, pageCount } = usePaging(props, c, changes, table);
1124
+ const pageRows = table.getRowModel().rows;
1125
+ const pick = useChoice(pageRows, c.selected, c.setSelected);
1126
+ const scroller = React2.useRef(null);
1127
+ const win = useWindow(virtualized, loading, pageRows, scroller);
1128
+ const span = table.getVisibleLeafColumns().length + (selectable ? 1 : 0) + (shape.hasExpander ? 1 : 0);
1129
+ const fixedLayout = Boolean(resizing || pinned?.start?.length || pinned?.end?.length);
1130
+ const v = {
1131
+ ...shape,
1132
+ ...pick,
1133
+ s: s2,
1134
+ labels,
1135
+ byId: new Map(columns.map((col) => [col.id, col])),
1136
+ selectable: Boolean(selectable),
1137
+ span,
1138
+ virtualized: Boolean(virtualized),
1139
+ fixedLayout,
1140
+ renderDetail,
1141
+ rowLabel,
1142
+ visibleColumns: table.getVisibleLeafColumns(),
1143
+ measureElement: win.measureElement,
1144
+ loading,
1145
+ loadingCount: props.loadingRows ?? 5,
1146
+ error: props.error,
1147
+ empty: props.empty,
1148
+ pageRows,
1149
+ windowItems: win.items,
1150
+ resizing,
1151
+ stickyHeader,
1152
+ setSizing
1153
+ };
1070
1154
  const context = {
1071
1155
  table,
1072
1156
  columns,
1073
1157
  labels,
1074
- filters,
1075
- setFilters: changeFilters,
1158
+ filters: c.filters,
1159
+ setFilters: changes.changeFilters,
1076
1160
  resultCount: leafCount,
1077
1161
  exportRows: () => table.getSortedRowModel().flatRows.filter((r) => !r.getIsGrouped()).map((r) => r.original),
1078
1162
  manualFiltering: Boolean(manual?.filtering)
1079
1163
  };
1080
- const pagination2 = paginated ? /* @__PURE__ */ jsx3(
1081
- Pagination,
1082
- {
1083
- page: Math.min(page, pageCount),
1084
- pageCount,
1085
- onPageChange: changePage,
1086
- pageSize: pageSizeOptions ? pageSize : void 0,
1087
- pageSizeOptions,
1088
- onPageSizeChange: pageSizeOptions ? changePageSize : void 0,
1089
- disabled: loading,
1090
- labels: labels.pagination
1091
- }
1092
- ) : null;
1164
+ const tableElement = tableOf(v, table, caption, showCaption, win.before, win.after);
1165
+ const sideways = scroll ?? (!shape.typed && !stickyHeader);
1166
+ const pagination2 = c.paginated ? pager(props, c, changes, pageCount, labels) : null;
1093
1167
  return /* @__PURE__ */ jsx3(DataTableContext.Provider, { value: context, children: /* @__PURE__ */ jsxs3("div", { className: s2.root({ className }), children: [
1094
1168
  toolbar ? /* @__PURE__ */ jsx3("div", { className: s2.toolbar(), children: toolbar }) : null,
1095
1169
  virtualized ? (
@@ -1106,14 +1180,31 @@ function DataTable({
1106
1180
  children: tableElement
1107
1181
  }
1108
1182
  )
1109
- ) : /* @__PURE__ */ jsx3("div", { className: s2.scroller({ className: sideways ? scroll ? SHADOWS : CARD_CLIP[L] : "overflow-x-clip" }), children: tableElement }),
1110
- selectable && bulkActions ? /* @__PURE__ */ jsx3(SelectionBar, { count: selected.length, max: bulkMax, result: bulkResult, labels: labels.selection, onClear: () => setSelected([]), children: bulkActions({ selected: [...selected], clear: () => setSelected([]) }) }) : null,
1183
+ ) : /* @__PURE__ */ jsx3("div", { className: s2.scroller({ className: sideways ? scroll ? SHADOWS : CARD_CLIP[shape.L] : "overflow-x-clip" }), children: tableElement }),
1184
+ selectable && bulkActions ? /* @__PURE__ */ jsx3(SelectionBar, { count: c.selected.length, max: bulkMax, result: bulkResult, labels: labels.selection, onClear: () => c.setSelected([]), children: bulkActions({ selected: [...c.selected], clear: () => c.setSelected([]) }) }) : null,
1111
1185
  pagination2 || footer ? /* @__PURE__ */ jsxs3("div", { className: s2.footer({ className: "flex flex-col gap-3" }), children: [
1112
1186
  pagination2,
1113
1187
  footer
1114
1188
  ] }) : null
1115
1189
  ] }) });
1116
1190
  }
1191
+ function useWindow(virtualized, loading, pageRows, scroller) {
1192
+ const fallbacks = useLayoutFallbacks(virtualized?.height ?? 0, virtualized?.estimateRowHeight ?? 44);
1193
+ const virtualizer = useVirtualizer({
1194
+ ...fallbacks,
1195
+ count: pageRows.length,
1196
+ getScrollElement: () => scroller.current,
1197
+ estimateSize: () => virtualized?.estimateRowHeight ?? 44,
1198
+ overscan: virtualized?.overscan ?? 6,
1199
+ initialRect: { width: 0, height: virtualized?.height ?? 0 },
1200
+ getItemKey: (index) => pageRows[index]?.id ?? index,
1201
+ enabled: Boolean(virtualized) && !loading
1202
+ });
1203
+ const items = virtualized && !loading ? virtualizer.getVirtualItems() : null;
1204
+ const before = items?.[0]?.start ?? 0;
1205
+ const after = items && items.length > 0 ? virtualizer.getTotalSize() - (items.at(-1)?.end ?? 0) : 0;
1206
+ return { items, before, after, measureElement: items ? virtualizer.measureElement : void 0 };
1207
+ }
1117
1208
  function ResizeHandle({
1118
1209
  label,
1119
1210
  size,
@@ -1352,4 +1443,4 @@ export {
1352
1443
  DataTableColumnsMenu,
1353
1444
  DataTableExport
1354
1445
  };
1355
- //# sourceMappingURL=chunk-RI3AA2E3.js.map
1446
+ //# sourceMappingURL=chunk-CEW3LQOZ.js.map