@42-central/ui-react 0.26.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/dist/{chunk-YZJUPYPO.js → chunk-2W7H43WL.js} +3 -3
  2. package/dist/{chunk-YZJUPYPO.js.map → chunk-2W7H43WL.js.map} +1 -1
  3. package/dist/{chunk-NLSJ6T7V.js → chunk-4D2V4JE4.js} +3 -3
  4. package/dist/{chunk-NLSJ6T7V.js.map → chunk-4D2V4JE4.js.map} +1 -1
  5. package/dist/{chunk-7B44J3CF.js → chunk-5H5YOPCF.js} +3 -3
  6. package/dist/{chunk-7B44J3CF.js.map → chunk-5H5YOPCF.js.map} +1 -1
  7. package/dist/{chunk-JFB6O7IY.js → chunk-7KFUFAVP.js} +4 -4
  8. package/dist/{chunk-JFB6O7IY.js.map → chunk-7KFUFAVP.js.map} +1 -1
  9. package/dist/{chunk-PT22KEVD.js → chunk-BGDVUC3B.js} +5 -5
  10. package/dist/{chunk-PT22KEVD.js.map → chunk-BGDVUC3B.js.map} +1 -1
  11. package/dist/{chunk-7247WBTV.js → chunk-DJ72KP32.js} +3 -3
  12. package/dist/{chunk-7247WBTV.js.map → chunk-DJ72KP32.js.map} +1 -1
  13. package/dist/{chunk-JDPVD2YE.js → chunk-HCHGERDB.js} +3 -3
  14. package/dist/{chunk-JDPVD2YE.js.map → chunk-HCHGERDB.js.map} +1 -1
  15. package/dist/{chunk-H7WOMZXO.js → chunk-HWBFRDX7.js} +16 -8
  16. package/dist/chunk-HWBFRDX7.js.map +1 -0
  17. package/dist/{chunk-LYGOFMXJ.js → chunk-I4IJOLNA.js} +4 -4
  18. package/dist/{chunk-LYGOFMXJ.js.map → chunk-I4IJOLNA.js.map} +1 -1
  19. package/dist/{chunk-DSZF2TW6.js → chunk-IA6DCC3N.js} +6 -6
  20. package/dist/{chunk-DSZF2TW6.js.map → chunk-IA6DCC3N.js.map} +1 -1
  21. package/dist/{chunk-QEAF57ZQ.js → chunk-ISX3AM32.js} +3 -3
  22. package/dist/{chunk-QEAF57ZQ.js.map → chunk-ISX3AM32.js.map} +1 -1
  23. package/dist/{chunk-YMVUQ2L7.js → chunk-L24N5EON.js} +6 -6
  24. package/dist/chunk-L24N5EON.js.map +1 -0
  25. package/dist/{chunk-J2NW7JID.js → chunk-L5GH7LLD.js} +3 -3
  26. package/dist/{chunk-J2NW7JID.js.map → chunk-L5GH7LLD.js.map} +1 -1
  27. package/dist/{chunk-N42EFOML.js → chunk-MCRTAUSZ.js} +6 -6
  28. package/dist/chunk-MCRTAUSZ.js.map +1 -0
  29. package/dist/{chunk-276RL7FX.js → chunk-MNYY7UU2.js} +6 -6
  30. package/dist/{chunk-276RL7FX.js.map → chunk-MNYY7UU2.js.map} +1 -1
  31. package/dist/{chunk-THGJLR5S.js → chunk-NNPSBC7O.js} +5 -5
  32. package/dist/{chunk-THGJLR5S.js.map → chunk-NNPSBC7O.js.map} +1 -1
  33. package/dist/{chunk-2CL3FKEY.js → chunk-QEMXXQQO.js} +3 -3
  34. package/dist/{chunk-2CL3FKEY.js.map → chunk-QEMXXQQO.js.map} +1 -1
  35. package/dist/{chunk-2NTCQIRP.js → chunk-QRNQQIQM.js} +5 -5
  36. package/dist/{chunk-2NTCQIRP.js.map → chunk-QRNQQIQM.js.map} +1 -1
  37. package/dist/{chunk-E3XLWMEB.js → chunk-RKCUXOU2.js} +4 -4
  38. package/dist/{chunk-E3XLWMEB.js.map → chunk-RKCUXOU2.js.map} +1 -1
  39. package/dist/{chunk-DMI4KS32.js → chunk-SBZJGFZ6.js} +3 -3
  40. package/dist/{chunk-DMI4KS32.js.map → chunk-SBZJGFZ6.js.map} +1 -1
  41. package/dist/{chunk-7P32XCEU.js → chunk-SLUOF7XW.js} +2 -2
  42. package/dist/chunk-SLUOF7XW.js.map +1 -0
  43. package/dist/{chunk-2TZU4LSX.js → chunk-ULF5LOBS.js} +3 -3
  44. package/dist/{chunk-2TZU4LSX.js.map → chunk-ULF5LOBS.js.map} +1 -1
  45. package/dist/components/app-shell/index.js +4 -4
  46. package/dist/components/autocomplete/index.js +4 -4
  47. package/dist/components/calendar-heatmap/index.js +2 -2
  48. package/dist/components/camera/index.js +6 -6
  49. package/dist/components/code-block/chrome.js +5 -5
  50. package/dist/components/code-block/index.js +6 -6
  51. package/dist/components/combobox/index.js +4 -4
  52. package/dist/components/combobox-list/index.js +6 -6
  53. package/dist/components/data-table/data-table-features.d.ts +8 -0
  54. package/dist/components/data-table/data-table-features.d.ts.map +1 -1
  55. package/dist/components/data-table/data-table-group.d.ts +128 -0
  56. package/dist/components/data-table/data-table-group.d.ts.map +1 -0
  57. package/dist/components/data-table/data-table-grouping.d.ts +72 -0
  58. package/dist/components/data-table/data-table-grouping.d.ts.map +1 -0
  59. package/dist/components/data-table/data-table-icons.d.ts +4 -2
  60. package/dist/components/data-table/data-table-icons.d.ts.map +1 -1
  61. package/dist/components/data-table/data-table-meta.d.ts +9 -0
  62. package/dist/components/data-table/data-table-meta.d.ts.map +1 -1
  63. package/dist/components/data-table/data-table-pagination.d.ts +4 -1
  64. package/dist/components/data-table/data-table-pagination.d.ts.map +1 -1
  65. package/dist/components/data-table/data-table-schema.d.ts +4 -0
  66. package/dist/components/data-table/data-table-schema.d.ts.map +1 -1
  67. package/dist/components/data-table/data-table-stack.d.ts +16 -3
  68. package/dist/components/data-table/data-table-stack.d.ts.map +1 -1
  69. package/dist/components/data-table/data-table-toolbar.d.ts +33 -2
  70. package/dist/components/data-table/data-table-toolbar.d.ts.map +1 -1
  71. package/dist/components/data-table/data-table-translations.d.ts +35 -0
  72. package/dist/components/data-table/data-table-translations.d.ts.map +1 -1
  73. package/dist/components/data-table/data-table.d.ts +55 -3
  74. package/dist/components/data-table/data-table.d.ts.map +1 -1
  75. package/dist/components/data-table/index.d.ts +3 -1
  76. package/dist/components/data-table/index.d.ts.map +1 -1
  77. package/dist/components/data-table/index.js +877 -229
  78. package/dist/components/data-table/index.js.map +1 -1
  79. package/dist/components/date-picker/index.js +7 -7
  80. package/dist/components/drawer/index.js +4 -4
  81. package/dist/components/hover-card/index.js +3 -3
  82. package/dist/components/kbd/index.js +3 -3
  83. package/dist/components/lightbox/index.js +4 -4
  84. package/dist/components/menu/index.js +4 -4
  85. package/dist/components/modal/index.js +4 -4
  86. package/dist/components/multi-combobox-list/index.js +6 -6
  87. package/dist/components/multi-select/index.js +4 -4
  88. package/dist/components/nav-link/index.js +3 -3
  89. package/dist/components/nav-link/tanstack.js +3 -3
  90. package/dist/components/notification/create-notifier.d.ts.map +1 -1
  91. package/dist/components/notification/index.js +15 -3
  92. package/dist/components/notification/index.js.map +1 -1
  93. package/dist/components/notification/types.d.ts +5 -0
  94. package/dist/components/notification/types.d.ts.map +1 -1
  95. package/dist/components/number-input/index.js +4 -4
  96. package/dist/components/overlays/index.js +5 -5
  97. package/dist/components/photo-capture/index.js +6 -6
  98. package/dist/components/popover/index.js +3 -3
  99. package/dist/components/schedule/index.js +3 -3
  100. package/dist/components/select/index.js +5 -5
  101. package/dist/components/spotlight/index.js +8 -8
  102. package/dist/components/tags-input/index.js +3 -3
  103. package/dist/components/time-input/index.js +5 -5
  104. package/dist/components/tooltip/index.js +2 -2
  105. package/dist/components/tree-multi-select/index.js +4 -4
  106. package/dist/components/tree-select/index.js +4 -4
  107. package/dist/index.js +2 -2
  108. package/dist/lib/overlay-container.d.ts.map +1 -1
  109. package/dist/lib/overlay-layer.d.ts +10 -3
  110. package/dist/lib/overlay-layer.d.ts.map +1 -1
  111. package/dist/theme.css +14 -6
  112. package/package.json +1 -1
  113. package/src/components/data-table/data-table-features.ts +11 -0
  114. package/src/components/data-table/data-table-group.test.tsx +1007 -0
  115. package/src/components/data-table/data-table-group.tsx +474 -0
  116. package/src/components/data-table/data-table-grouping.ts +160 -0
  117. package/src/components/data-table/data-table-icons.tsx +8 -1
  118. package/src/components/data-table/data-table-meta.ts +9 -0
  119. package/src/components/data-table/data-table-pagination.tsx +5 -1
  120. package/src/components/data-table/data-table-stack.tsx +165 -43
  121. package/src/components/data-table/data-table-toolbar.tsx +110 -2
  122. package/src/components/data-table/data-table-translations.ts +44 -0
  123. package/src/components/data-table/data-table.tsx +420 -150
  124. package/src/components/data-table/index.ts +4 -0
  125. package/src/components/drawer/drawer.tsx +1 -1
  126. package/src/components/modal/modal.tsx +1 -1
  127. package/src/components/notification/create-notifier.tsx +18 -2
  128. package/src/components/notification/notify-in-modal.test.tsx +128 -0
  129. package/src/components/notification/types.ts +5 -0
  130. package/src/lib/overlay-container.tsx +2 -1
  131. package/src/lib/overlay-layer.test.tsx +63 -1
  132. package/src/lib/overlay-layer.tsx +29 -6
  133. package/src/theme.css +14 -6
  134. package/src/theme.test.ts +14 -10
  135. package/dist/chunk-7P32XCEU.js.map +0 -1
  136. package/dist/chunk-H7WOMZXO.js.map +0 -1
  137. package/dist/chunk-N42EFOML.js.map +0 -1
  138. package/dist/chunk-YMVUQ2L7.js.map +0 -1
@@ -6,7 +6,9 @@ import {
6
6
  type ColumnFiltersState,
7
7
  type ColumnSizingState,
8
8
  type ColumnVisibilityState,
9
+ type ExpandedState,
9
10
  flexRender,
11
+ type GroupingState,
10
12
  type OnChangeFn,
11
13
  type PaginationState,
12
14
  type RowData,
@@ -14,14 +16,23 @@ import {
14
16
  type SortingState,
15
17
  useTable,
16
18
  } from "@tanstack/react-table"
17
- import type { MouseEvent, ReactNode } from "react"
19
+ import type { CSSProperties, MouseEvent, ReactNode } from "react"
18
20
  import { useId, useMemo, useRef } from "react"
19
21
  import { cn } from "../../lib/cn"
22
+ import { devWarnOnce } from "../../lib/dev-warn"
20
23
  import type { WithTestId } from "../../lib/test-id"
21
24
  import { Checkbox } from "../checkbox/checkbox"
22
25
  import { Table, type TableSize } from "../table/table"
23
26
  import { type ColumnDef, dataTableFeatures, type Row } from "./data-table-features"
24
27
  import { resolveFilterFn } from "./data-table-filter"
28
+ import {
29
+ DataTableGroupedBody,
30
+ type DataTableGroupHeaderContext,
31
+ type DataTableGroupShowMore,
32
+ DataTableGroupSummary,
33
+ useDataTableGrouping,
34
+ } from "./data-table-group"
35
+ import { type DataTableGroup, isGroupExpanded, MAX_GROUPING_DEPTH } from "./data-table-grouping"
25
36
  import { DataTableColumnResizeHandle, DataTableHeaderCell } from "./data-table-header-cell"
26
37
  import { type DataTableIcons, resolveDataTableIcons } from "./data-table-icons"
27
38
  import "./data-table-meta"
@@ -39,7 +50,11 @@ import {
39
50
  RenderPlaceholder,
40
51
  } from "./data-table-placeholder"
41
52
  import { DataTableStackedRows, DataTableStackOptions } from "./data-table-stack"
42
- import { DataTableColumnVisibilityMenu, DataTableGlobalFilter } from "./data-table-toolbar"
53
+ import {
54
+ DataTableColumnVisibilityMenu,
55
+ DataTableGlobalFilter,
56
+ DataTableGroupMenu,
57
+ } from "./data-table-toolbar"
43
58
  import {
44
59
  type DataTableTranslations,
45
60
  type ResolvedDataTableTranslations,
@@ -113,6 +128,25 @@ export type DataTableProps<TEntity extends RowData> = WithTestId & {
113
128
  * with no entry here renders at its own `size` (default `150`). */
114
129
  columnSizing?: ColumnSizingState
115
130
  onColumnSizingChange?: OnChangeFn<ColumnSizingState>
131
+ /** Controlled grouping: the ids of the columns the rows are grouped by, outermost
132
+ * first. Up to two levels (one under `manualGrouping`); each column must opt in
133
+ * with `meta.groupable` or `enableGrouping`. `[]` is an ungrouped table.
134
+ *
135
+ * A grouped column is hidden by default, see `hideGroupedColumns`. */
136
+ grouping?: GroupingState
137
+ onGroupingChange?: OnChangeFn<GroupingState>
138
+ /** Controlled fold state, react-table's own `expanded`, keyed by group id
139
+ * (`status:fixed`, nested as `status:fixed>building:b1`). A group is open unless
140
+ * its id maps to `false`, so `{ "status:fixed": false }` folds one group and a
141
+ * group that appears later starts open. `true` opens everything; it is the
142
+ * uncontrolled default. */
143
+ expanded?: ExpandedState
144
+ onExpandedChange?: OnChangeFn<ExpandedState>
145
+ /** Controlled "Hide empty groups" switch of the group menu. A group can only be
146
+ * empty when the table knows it exists without a row: from the grouped column's
147
+ * `select` filter options, or from `groups`. Default `true`. */
148
+ hideEmptyGroups?: boolean
149
+ onHideEmptyGroupsChange?: (value: boolean) => void
116
150
 
117
151
  /** Seeds internal state for any omitted (uncontrolled) slice. */
118
152
  initialState?: Partial<{
@@ -123,6 +157,9 @@ export type DataTableProps<TEntity extends RowData> = WithTestId & {
123
157
  rowSelection: RowSelectionState
124
158
  columnVisibility: ColumnVisibilityState
125
159
  columnSizing: ColumnSizingState
160
+ grouping: GroupingState
161
+ expanded: ExpandedState
162
+ hideEmptyGroups: boolean
126
163
  }>
127
164
 
128
165
  // Server mode — flip a slice to server-owned; the table renders `data` as-is.
@@ -130,6 +167,18 @@ export type DataTableProps<TEntity extends RowData> = WithTestId & {
130
167
  manualSorting?: boolean
131
168
  manualFiltering?: boolean
132
169
  manualPagination?: boolean
170
+ /** The server owns the groups: `data` holds the rows loaded so far for every
171
+ * group, and `groups` their totals. The table still sorts the rows it has into
172
+ * their groups. A group's "Show more" appears while it has fewer rows than its
173
+ * count and calls `onGroupShowMore`. One grouping level. */
174
+ manualGrouping?: boolean
175
+ /** `manualGrouping`: every group of the grouped column, in display order, with
176
+ * its total row count. A group listed here with no row in `data` yet still
177
+ * draws its header (and is an empty group when its count is `0`). */
178
+ groups?: ReadonlyArray<DataTableGroup>
179
+ /** `manualGrouping`: a group's "Show more" was clicked. Fetch the next
180
+ * `groupPageSize` rows of that group and add them to `data`. */
181
+ onGroupShowMore?: (group: DataTableGroupShowMore) => void
133
182
  /** Shift-click multi-column sort. Off by default (single-column). */
134
183
  enableMultiSort?: boolean
135
184
  /** Total server rows; lets react-table derive `pageCount`. */
@@ -146,7 +195,7 @@ export type DataTableProps<TEntity extends RowData> = WithTestId & {
146
195
  * the kit; a partial override only replaces the keys it sets. */
147
196
  translations?: DataTableTranslations
148
197
  /** Replaces the default icons — sort (`asc`, `desc`, `unsorted`), `filter`, `search`,
149
- * `columns`, `prev`, `next`, the states' `empty`/`error` and their `clear`/`retry` actions,
198
+ * `columns`, `group`, a group header's `fold`, `prev`, `next`, the states' `empty`/`error` and their `clear`/`retry` actions,
150
199
  * and the `number` filter's `min`/`max`. A missing key keeps its default; `null` hides an
151
200
  * icon that has text or a label beside it. */
152
201
  icons?: DataTableIcons
@@ -185,6 +234,22 @@ export type DataTableProps<TEntity extends RowData> = WithTestId & {
185
234
  globalFilterDebounce?: number
186
235
  /** Shows the column-visibility `Menu` (only hideable columns are listed). */
187
236
  enableColumnVisibility?: boolean
237
+ /** Shows the "Group" `Menu`: the groupable columns, a second level once a first
238
+ * is picked, and "Hide empty groups". `grouping` works without it. */
239
+ enableGrouping?: boolean
240
+ /** Hides a column while the rows are grouped by it, since its value is in every
241
+ * group header. The column-visibility menu, or an explicit `columnVisibility`
242
+ * entry, still shows it. `false` leaves grouped columns in the table. Default
243
+ * `true`. */
244
+ hideGroupedColumns?: boolean
245
+ /** Rows a group shows before its "Show more" button, and how many each click
246
+ * adds. Groups page on their own: pages across the whole list would split a
247
+ * group in two. Only applies while `enablePagination` is on. Default `25`. */
248
+ groupPageSize?: number
249
+ /** Replaces what a group header draws for its value. By default that is the
250
+ * grouped column's own cell, so a status badge stays a badge. It sits inside
251
+ * the fold button: return text or decoration, nothing interactive. */
252
+ renderGroupHeader?: (context: DataTableGroupHeaderContext<TEntity>) => ReactNode
188
253
  /** Shows a drag handle on each resizable header cell (mouse + touch, via
189
254
  * react-table's built-in `getResizeHandler`). Off by default. A column's
190
255
  * `size` (default width) and `minSize` (drag floor) are plain `ColumnDef`
@@ -374,22 +439,27 @@ function createSelectColumn(size: TableSize, labels: SelectColumnLabels): Column
374
439
  meta: {
375
440
  hideFromMenu: true,
376
441
  },
377
- header: ({ table, mode }) =>
378
- mode === "table" ? (
442
+ header: ({ table, mode }) => {
443
+ if (mode !== "table") return null
444
+ // A grouped table has no page: its header checkbox covers every row that
445
+ // passes the filters, shown or still behind a "Show more".
446
+ const grouped = table.atoms.grouping.get().length > 0
447
+ const all = grouped ? table.getIsAllRowsSelected() : table.getIsAllPageRowsSelected()
448
+ const some = grouped ? table.getIsSomeRowsSelected() : table.getIsSomePageRowsSelected()
449
+ return (
379
450
  <Checkbox
380
451
  size={size}
381
- checked={
382
- table.getIsAllPageRowsSelected()
383
- ? true
384
- : table.getIsSomePageRowsSelected()
385
- ? "indeterminate"
386
- : false
452
+ checked={all ? true : some ? "indeterminate" : false}
453
+ onCheckedChange={(checked) =>
454
+ grouped
455
+ ? table.toggleAllRowsSelected(checked === true)
456
+ : table.toggleAllPageRowsSelected(checked === true)
387
457
  }
388
- onCheckedChange={(checked) => table.toggleAllPageRowsSelected(checked === true)}
389
458
  label={labels.current.selectAllRows}
390
459
  classNames={{ label: "sr-only" }}
391
460
  />
392
- ) : null,
461
+ )
462
+ },
393
463
  cell: ({ row }) => (
394
464
  <span className="flex flex-row justify-center items-center">
395
465
  <Checkbox
@@ -445,10 +515,19 @@ function DataTableComponent<TEntity extends RowData>({
445
515
  onColumnVisibilityChange,
446
516
  columnSizing: columnSizingProp,
447
517
  onColumnSizingChange,
518
+ grouping: groupingProp,
519
+ onGroupingChange,
520
+ expanded: expandedProp,
521
+ onExpandedChange,
522
+ hideEmptyGroups: hideEmptyGroupsProp,
523
+ onHideEmptyGroupsChange,
448
524
  initialState,
449
525
  manualSorting,
450
526
  manualFiltering,
451
527
  manualPagination,
528
+ manualGrouping = false,
529
+ groups: manualGroups,
530
+ onGroupShowMore,
452
531
  enableMultiSort,
453
532
  rowCount,
454
533
  pageCount,
@@ -469,6 +548,10 @@ function DataTableComponent<TEntity extends RowData>({
469
548
  globalFilterPlaceholder,
470
549
  globalFilterDebounce,
471
550
  enableColumnVisibility: _enableColumnVisibility,
551
+ enableGrouping,
552
+ hideGroupedColumns = true,
553
+ groupPageSize = 25,
554
+ renderGroupHeader,
472
555
  enableColumnResizing,
473
556
  enablePagination = true,
474
557
  sizing = "fixed",
@@ -540,6 +623,24 @@ function DataTableComponent<TEntity extends RowData>({
540
623
  onChange: onColumnSizingChange,
541
624
  defaultValue: initialState?.columnSizing ?? {},
542
625
  })
626
+ const [grouping, handleGroupingChange] = useUncontrolled({
627
+ value: groupingProp,
628
+ onChange: (value) => {
629
+ resetPagignation()
630
+ onGroupingChange?.(value)
631
+ },
632
+ defaultValue: initialState?.grouping ?? [],
633
+ })
634
+ const [expanded, handleExpandedChange] = useUncontrolled<ExpandedState>({
635
+ value: expandedProp,
636
+ onChange: onExpandedChange,
637
+ defaultValue: initialState?.expanded ?? true,
638
+ })
639
+ const [hideEmptyGroups, handleHideEmptyGroupsChange] = useUncontrolled({
640
+ value: hideEmptyGroupsProp,
641
+ onChange: onHideEmptyGroupsChange,
642
+ defaultValue: initialState?.hideEmptyGroups ?? true,
643
+ })
543
644
 
544
645
  const resolvedTranslations = resolveDataTableTranslations(translations)
545
646
  const selectLabels = { ...resolvedTranslations, rowLabel: getRowLabel }
@@ -552,7 +653,13 @@ function DataTableComponent<TEntity extends RowData>({
552
653
  // for a multi-select array or a date/date-range/number filter object.
553
654
  // Assign the type-matched one explicitly (see `resolveFilterFn`) unless
554
655
  // the column already declares its own.
555
- const withFilterFns = columns.map((col) => {
656
+ const withFilterFns = columns.map((column) => {
657
+ // Grouping is opt-in per column (`meta.groupable`, or the column's own
658
+ // `enableGrouping`), where react-table's default is every accessor column.
659
+ const col = {
660
+ ...column,
661
+ enableGrouping: column.enableGrouping ?? column.meta?.groupable === true,
662
+ }
556
663
  const filter = col.meta?.filter
557
664
  if (!filter || col.filterFn != null) return col
558
665
  return { ...col, filterFn: resolveFilterFn(filter) }
@@ -563,6 +670,48 @@ function DataTableComponent<TEntity extends RowData>({
563
670
  return [selectColumn as ColumnDef<TEntity, any>, ...withFilterFns]
564
671
  }, [columns, enableRowSelection, size])
565
672
 
673
+ // The levels the table actually groups by: groupable columns only, and no deeper
674
+ // than it draws. Resolved from the column defs, since react-table needs the
675
+ // answer in its state before the instance below exists.
676
+ const maxGroupingDepth = manualGrouping ? 1 : MAX_GROUPING_DEPTH
677
+ const { activeGrouping, groupingVisibility } = useMemo(() => {
678
+ // react-table's own id for a column def: its `id`, else its accessor key.
679
+ const idOf = (col: (typeof tableColumns)[number]) =>
680
+ "accessorKey" in col && col.id == null ? String(col.accessorKey).replaceAll(".", "_") : col.id
681
+ const groupable = new Set(
682
+ tableColumns
683
+ .filter(
684
+ (col) =>
685
+ col.enableGrouping &&
686
+ ("accessorKey" in col || "accessorFn" in col || col.getGroupingValue != null),
687
+ )
688
+ .map(idOf),
689
+ )
690
+ const pinnedVisible = new Set(
691
+ tableColumns.filter((col) => col.enableHiding === false).map(idOf),
692
+ )
693
+ const active = grouping.filter((id) => groupable.has(id)).slice(0, maxGroupingDepth)
694
+ return {
695
+ activeGrouping: active,
696
+ // A grouped column is hidden by default: every row of a group would repeat
697
+ // what its header already says. Only a default, laid under `columnVisibility`
698
+ // below, so an explicit entry (the column menu's toggle, or the app's own
699
+ // state) shows it again. A column that can't be hidden is left alone, since
700
+ // nothing could bring it back.
701
+ groupingVisibility: Object.fromEntries(
702
+ active
703
+ .filter((id) => hideGroupedColumns && !pinnedVisible.has(id))
704
+ .map((id) => [id, false]),
705
+ ),
706
+ }
707
+ }, [tableColumns, grouping, maxGroupingDepth, hideGroupedColumns])
708
+ if (activeGrouping.length < grouping.length) {
709
+ devWarnOnce(
710
+ `[DataTable] \`grouping\` is ${JSON.stringify(grouping)}, but only ${JSON.stringify(activeGrouping)} applies: ` +
711
+ `a column must opt in with \`meta.groupable\` and the table groups ${maxGroupingDepth === 1 ? "one level deep under `manualGrouping`" : `at most ${maxGroupingDepth} levels deep`}.`,
712
+ )
713
+ }
714
+
566
715
  // Under RTL a column's resize handle sits on its left edge, so dragging it left widens it.
567
716
  const { dir } = useLocaleContext()
568
717
  const table = useTable({
@@ -581,8 +730,12 @@ function DataTableComponent<TEntity extends RowData>({
581
730
  globalFilter,
582
731
  pagination,
583
732
  rowSelection,
584
- columnVisibility,
733
+ // `onColumnVisibilityChange` still resolves against `columnVisibility` alone,
734
+ // so the grouping default never leaks into the app's state.
735
+ columnVisibility: { ...groupingVisibility, ...columnVisibility },
585
736
  columnSizing,
737
+ grouping: activeGrouping,
738
+ expanded,
586
739
  },
587
740
  onSortingChange: handleSortingChange,
588
741
  onColumnFiltersChange: handleColumnFiltersChange,
@@ -591,11 +744,29 @@ function DataTableComponent<TEntity extends RowData>({
591
744
  onRowSelectionChange: handleRowSelectionChange,
592
745
  onColumnVisibilityChange: handleColumnVisibilityChange,
593
746
  onColumnSizingChange: handleColumnSizingChange,
747
+ onGroupingChange: handleGroupingChange,
748
+ onExpandedChange: handleExpandedChange,
749
+ // The grouped column stays where the app put it, after the select column.
750
+ groupedColumnMode: false,
751
+ // A refetch or a filter change must not reopen what the user folded.
752
+ autoResetExpanded: false,
753
+ // Groups are open unless folded, see `isGroupExpanded`; any other row keeps
754
+ // react-table's reading of `expanded`.
755
+ getIsRowExpanded: (row) =>
756
+ row.getIsGrouped()
757
+ ? isGroupExpanded(expanded, row.id)
758
+ : expanded === true || expanded[row.id] === true,
594
759
  manualSorting,
595
760
  manualFiltering,
596
761
  manualPagination,
597
762
  enableMultiSort,
598
- enableRowSelection,
763
+ // A group row is never selectable itself: its checkbox selects its rows, and
764
+ // `rowSelection` only ever holds real row ids.
765
+ enableRowSelection: (row) =>
766
+ !row.getIsGrouped() &&
767
+ (typeof enableRowSelection === "function"
768
+ ? enableRowSelection(row)
769
+ : (enableRowSelection ?? true)),
599
770
  // No fixed width for a drag to adjust in "auto" mode — see the `sizing` prop doc.
600
771
  enableColumnResizing: Boolean(enableColumnResizing) && sizing === "fixed",
601
772
  columnResizeMode: "onChange",
@@ -604,10 +775,41 @@ function DataTableComponent<TEntity extends RowData>({
604
775
  pageCount,
605
776
  })
606
777
 
607
- const rows = table.getRowModel().rows
778
+ // Server pages with client grouping: the table can only group the page it holds.
779
+ // That page stays whole (no "Show more" inside it) and the page controls stay,
780
+ // or the other pages would be out of reach.
781
+ const groupsOnePage = Boolean(manualPagination) && !manualGrouping
782
+ if (groupsOnePage && activeGrouping.length > 0) {
783
+ devWarnOnce(
784
+ "[DataTable] `grouping` with `manualPagination` only groups the rows of the current page, " +
785
+ "so a group's count is its count on this page. Pass `manualGrouping` with `groups` and " +
786
+ "`onGroupShowMore` to let the server own the groups.",
787
+ )
788
+ }
789
+ const groupingView = useDataTableGrouping({
790
+ table,
791
+ grouping: activeGrouping,
792
+ expanded,
793
+ onExpandedChange: handleExpandedChange,
794
+ hideEmptyGroups,
795
+ paginate: enablePagination && !groupsOnePage,
796
+ groupPageSize,
797
+ manualGrouping,
798
+ manualGroups,
799
+ onGroupShowMore,
800
+ renderGroupHeader,
801
+ translations: resolvedTranslations,
802
+ resetKey: JSON.stringify([activeGrouping, sorting, columnFilters, globalFilter]),
803
+ })
804
+ // A grouped table draws from the group tree, not from a page of rows.
805
+ const rows = groupingView ? [] : table.getRowModel().rows
806
+ const loadedCount = groupingView
807
+ ? groupingView.groups.reduce((sum, group) => sum + group.loaded, 0)
808
+ : rows.length
809
+ const itemCount = groupingView ? groupingView.groups.length : rows.length
608
810
  const visibleColumnCount = table.getVisibleLeafColumns().length
609
- const showSkeleton = loading && rows.length === 0
610
- const isRefetching = loading && rows.length > 0
811
+ const showSkeleton = loading && loadedCount === 0
812
+ const isRefetching = loading && loadedCount > 0
611
813
  const skeletonRowCount = Math.min(Math.max(pagination.pageSize, 1), 100)
612
814
 
613
815
  const handleRowClick = (row: TEntity) => (event: MouseEvent<HTMLTableRowElement>) => {
@@ -621,8 +823,14 @@ function DataTableComponent<TEntity extends RowData>({
621
823
  const enableColumnVisibility =
622
824
  _enableColumnVisibility &&
623
825
  table.getAllLeafColumns().some((column) => !column.columnDef.meta?.hideFromMenu)
826
+ const showGroupMenu =
827
+ enableGrouping && table.getAllLeafColumns().some((column) => column.getCanGroup())
624
828
  const showToolbar =
625
- title != null || toolbarActions != null || enableGlobalFilter || enableColumnVisibility
829
+ title != null ||
830
+ toolbarActions != null ||
831
+ enableGlobalFilter ||
832
+ enableColumnVisibility ||
833
+ showGroupMenu
626
834
  // Names the table, and with it the focusable scroll region around it, after the title.
627
835
  const titleId = useId()
628
836
  const isStacked = responsive === "stack"
@@ -636,12 +844,69 @@ function DataTableComponent<TEntity extends RowData>({
636
844
  // order is important here as it's the priority over views.
637
845
  // Shared with `DataTableStackedRows` (passed down below) so the `<table>`
638
846
  // and stacked-card renderings stay in parity on a single source of truth.
639
- const view: DataTableView = match({ error, showSkeleton, items: rows.length })
847
+ const view: DataTableView = match({ error, showSkeleton, items: itemCount })
640
848
  .with({ error: P.nonNullable }, () => "error" as const)
641
849
  .with({ showSkeleton: true }, () => "loading" as const)
642
850
  .with({ items: P.number.lte(0) }, () => "empty" as const)
643
851
  .otherwise(() => "items" as const)
644
852
 
853
+ const bodyClassName = cn(
854
+ classNames?.body,
855
+ isRefetching && "pointer-events-none opacity-60 transition-opacity",
856
+ )
857
+ // A row under a sub-group is indented one tree step, in its first data cell: the
858
+ // select column stays a straight line of checkboxes. The offset lands the cell's
859
+ // text under the sub-group's label, past its chevron; the group header starts
860
+ // after the checkbox when there is one, hence the two values.
861
+ const leafIndent = `${SELECT_CELL_PADDING_PX[size ?? "md"] + (enableRowSelection ? 28 : 44)}px`
862
+ const renderRow = (
863
+ row: Row<TEntity>,
864
+ { className: rowClassName, indent = false }: { className?: string; indent?: boolean } = {},
865
+ ) => {
866
+ const rowTestId = getRowTestId ? getRowTestId(row.original, row.id) : defaultRowTestId(row.id)
867
+ const indentedColumnId = indent
868
+ ? row.getVisibleCells().find((cell) => cell.column.id !== "__select__")?.column.id
869
+ : undefined
870
+ return (
871
+ <Table.Row
872
+ key={row.id}
873
+ testId={rowTestId}
874
+ onClick={onRowClick ? handleRowClick(row.original) : undefined}
875
+ className={cn(onRowClick && "cursor-pointer", rowClassName, classNames?.row)}
876
+ {...props({ "data-selected": row.getIsSelected() })}
877
+ >
878
+ {row.getVisibleCells().map((cell, index, cells) => {
879
+ const fillsRemaining = isFillLast && index === cells.length - 1
880
+ const indented = cell.column.id === indentedColumnId
881
+ return (
882
+ <Table.Cell
883
+ key={cell.id}
884
+ testId={`${rowTestId}-cell-${cell.column.id}`}
885
+ style={
886
+ {
887
+ width: isFixedSizing && !fillsRemaining ? cell.column.getSize() : undefined,
888
+ "--data-table-leaf-indent": indented ? leafIndent : undefined,
889
+ } as CSSProperties
890
+ }
891
+ className={cn(
892
+ tableCell({
893
+ align: cell.column.columnDef.meta?.align,
894
+ }),
895
+ isFixedSizing && cell.column.id !== "__select__" && "truncate",
896
+ // Through a custom property, with `!`: the cell padding comes from a
897
+ // descendant rule on the table root, which a plain utility loses to.
898
+ indented && "ps-(--data-table-leaf-indent)!",
899
+ classNames?.cell,
900
+ )}
901
+ >
902
+ {flexRender(cell.column.columnDef.cell, cell.getContext())}
903
+ </Table.Cell>
904
+ )
905
+ })}
906
+ </Table.Row>
907
+ )
908
+ }
909
+
645
910
  return (
646
911
  <Table
647
912
  testId={testId}
@@ -676,6 +941,19 @@ function DataTableComponent<TEntity extends RowData>({
676
941
  />
677
942
  )}
678
943
  {toolbarActions}
944
+ {showGroupMenu && (
945
+ <DataTableGroupMenu
946
+ table={table}
947
+ grouping={activeGrouping}
948
+ onGroupingChange={handleGroupingChange}
949
+ maxDepth={maxGroupingDepth}
950
+ hideEmptyGroups={hideEmptyGroups}
951
+ onHideEmptyGroupsChange={handleHideEmptyGroupsChange}
952
+ translations={resolvedTranslations}
953
+ icon={icons.group}
954
+ className={isStacked ? "hidden @lg:flex" : undefined}
955
+ />
956
+ )}
679
957
  {enableColumnVisibility && (
680
958
  <DataTableColumnVisibilityMenu
681
959
  table={table}
@@ -753,136 +1031,107 @@ function DataTableComponent<TEntity extends RowData>({
753
1031
  </Table.Row>
754
1032
  ))}
755
1033
  </Table.Head>
756
- <Table.Body
757
- className={cn(
758
- classNames?.body,
759
- isRefetching && "pointer-events-none opacity-60 transition-opacity",
760
- )}
761
- >
762
- {match(view)
763
- .with("error", () => (
764
- <Table.Row data-placeholder>
765
- <Table.Cell
766
- colSpan={visibleColumnCount}
767
- className={cn("py-6 text-center", classNames?.error)}
768
- >
769
- {/* The alert lives inside the cell: on the `<td>` it replaced the
770
- cell's own role, leaving a row with no cell in the table. */}
771
- <div
772
- role="alert"
773
- className="flex flex-col gap-3 items-center justify-center min-h-48.5"
1034
+ {view === "items" && groupingView ? (
1035
+ <DataTableGroupedBody
1036
+ grouping={groupingView}
1037
+ table={table}
1038
+ colSpan={visibleColumnCount}
1039
+ renderRow={renderRow}
1040
+ selectable={Boolean(enableRowSelection)}
1041
+ size={size ?? "md"}
1042
+ translations={resolvedTranslations}
1043
+ icons={icons}
1044
+ className={bodyClassName}
1045
+ />
1046
+ ) : (
1047
+ <Table.Body className={bodyClassName}>
1048
+ {match(view)
1049
+ .with("error", () => (
1050
+ <Table.Row data-placeholder>
1051
+ <Table.Cell
1052
+ colSpan={visibleColumnCount}
1053
+ className={cn("py-6 text-center", classNames?.error)}
774
1054
  >
775
- {icons.error != null && <ThemeIcon>{icons.error}</ThemeIcon>}
1055
+ {/* The alert lives inside the cell: on the `<td>` it replaced the
1056
+ cell's own role, leaving a row with no cell in the table. */}
1057
+ <div
1058
+ role="alert"
1059
+ className="flex flex-col gap-3 items-center justify-center min-h-48.5"
1060
+ >
1061
+ {icons.error != null && <ThemeIcon>{icons.error}</ThemeIcon>}
776
1062
 
777
- <RenderPlaceholder
778
- node={error}
779
- defaultActions={
780
- onRetry && (
1063
+ <RenderPlaceholder
1064
+ node={error}
1065
+ defaultActions={
1066
+ onRetry && (
1067
+ <Button
1068
+ variant="light"
1069
+ color="neutral"
1070
+ size="sm"
1071
+ startSlot={icons.retry}
1072
+ onClick={onRetry}
1073
+ >
1074
+ {resolvedTranslations.retry}
1075
+ </Button>
1076
+ )
1077
+ }
1078
+ />
1079
+ </div>
1080
+ </Table.Cell>
1081
+ </Table.Row>
1082
+ ))
1083
+ .with("empty", () => (
1084
+ <Table.Row data-placeholder>
1085
+ <Table.Cell
1086
+ colSpan={visibleColumnCount}
1087
+ className={cn("py-6 text-center", classNames?.empty)}
1088
+ >
1089
+ <div className="flex flex-col gap-3 items-center justify-center min-h-48.5">
1090
+ {icons.empty != null && <ThemeIcon>{icons.empty}</ThemeIcon>}
1091
+ <RenderPlaceholder
1092
+ node={empty}
1093
+ defaultActions={
781
1094
  <Button
782
1095
  variant="light"
783
1096
  color="neutral"
784
1097
  size="sm"
785
- startSlot={icons.retry}
786
- onClick={onRetry}
1098
+ startSlot={icons.clear}
1099
+ onClick={() => table.resetColumnFilters()}
787
1100
  >
788
- {resolvedTranslations.retry}
1101
+ {resolvedTranslations.clearFilters}
789
1102
  </Button>
790
- )
791
- }
792
- />
793
- </div>
794
- </Table.Cell>
795
- </Table.Row>
796
- ))
797
- .with("empty", () => (
798
- <Table.Row data-placeholder>
799
- <Table.Cell
800
- colSpan={visibleColumnCount}
801
- className={cn("py-6 text-center", classNames?.empty)}
802
- >
803
- <div className="flex flex-col gap-3 items-center justify-center min-h-48.5">
804
- {icons.empty != null && <ThemeIcon>{icons.empty}</ThemeIcon>}
805
- <RenderPlaceholder
806
- node={empty}
807
- defaultActions={
808
- <Button
809
- variant="light"
810
- color="neutral"
811
- size="sm"
812
- startSlot={icons.clear}
813
- onClick={() => table.resetColumnFilters()}
814
- >
815
- {resolvedTranslations.clearFilters}
816
- </Button>
817
- }
818
- />
819
- </div>
820
- </Table.Cell>
821
- </Table.Row>
822
- ))
823
- .with("loading", () =>
824
- Array.from({ length: skeletonRowCount }).map((_, rowIndex) => (
825
- <Table.Row
826
- // biome-ignore lint/suspicious/noArrayIndexKey: placeholder rows, no identity
827
- key={`skeleton-${rowIndex}`}
828
- testId={`skeleton-row-${rowIndex}`}
829
- aria-hidden="true"
830
- data-placeholder
831
- >
832
- {table.getVisibleLeafColumns().map((column) => (
833
- <Table.Cell
834
- key={column.id}
835
- testId={`skeleton-row-${rowIndex}-cell-${column.id}`}
836
- >
837
- {column.columnDef.meta?.skeleton ?? (
838
- <Skeleton shape="rectangular" size={size} className="w-full" />
839
- )}
840
- </Table.Cell>
841
- ))}
1103
+ }
1104
+ />
1105
+ </div>
1106
+ </Table.Cell>
842
1107
  </Table.Row>
843
- )),
844
- )
845
- .with("items", () =>
846
- rows.map((row) => {
847
- const rowTestId = getRowTestId
848
- ? getRowTestId(row.original, row.id)
849
- : defaultRowTestId(row.id)
850
- return (
1108
+ ))
1109
+ .with("loading", () =>
1110
+ Array.from({ length: skeletonRowCount }).map((_, rowIndex) => (
851
1111
  <Table.Row
852
- key={row.id}
853
- testId={rowTestId}
854
- onClick={onRowClick ? handleRowClick(row.original) : undefined}
855
- className={cn(onRowClick && "cursor-pointer", classNames?.row)}
856
- {...props({ "data-selected": row.getIsSelected() })}
1112
+ // biome-ignore lint/suspicious/noArrayIndexKey: placeholder rows, no identity
1113
+ key={`skeleton-${rowIndex}`}
1114
+ testId={`skeleton-row-${rowIndex}`}
1115
+ aria-hidden="true"
1116
+ data-placeholder
857
1117
  >
858
- {row.getVisibleCells().map((cell, index, cells) => {
859
- const fillsRemaining = isFillLast && index === cells.length - 1
860
- return (
861
- <Table.Cell
862
- key={cell.id}
863
- testId={`${rowTestId}-cell-${cell.column.id}`}
864
- style={{
865
- width:
866
- isFixedSizing && !fillsRemaining ? cell.column.getSize() : undefined,
867
- }}
868
- className={cn(
869
- tableCell({
870
- align: cell.column.columnDef.meta?.align,
871
- }),
872
- isFixedSizing && cell.column.id !== "__select__" && "truncate",
873
- classNames?.cell,
874
- )}
875
- >
876
- {flexRender(cell.column.columnDef.cell, cell.getContext())}
877
- </Table.Cell>
878
- )
879
- })}
1118
+ {table.getVisibleLeafColumns().map((column) => (
1119
+ <Table.Cell
1120
+ key={column.id}
1121
+ testId={`skeleton-row-${rowIndex}-cell-${column.id}`}
1122
+ >
1123
+ {column.columnDef.meta?.skeleton ?? (
1124
+ <Skeleton shape="rectangular" size={size} className="w-full" />
1125
+ )}
1126
+ </Table.Cell>
1127
+ ))}
880
1128
  </Table.Row>
881
- )
882
- }),
883
- )
884
- .exhaustive()}
885
- </Table.Body>
1129
+ )),
1130
+ )
1131
+ .with("items", () => rows.map((row) => renderRow(row)))
1132
+ .exhaustive()}
1133
+ </Table.Body>
1134
+ )}
886
1135
  </Table.Content>
887
1136
 
888
1137
  {isStacked && (
@@ -890,12 +1139,24 @@ function DataTableComponent<TEntity extends RowData>({
890
1139
  <DataTableStackOptions
891
1140
  table={table}
892
1141
  enableColumnVisibility={enableColumnVisibility}
1142
+ grouping={
1143
+ showGroupMenu
1144
+ ? {
1145
+ value: activeGrouping,
1146
+ onChange: handleGroupingChange,
1147
+ maxDepth: maxGroupingDepth,
1148
+ hideEmptyGroups,
1149
+ onHideEmptyGroupsChange: handleHideEmptyGroupsChange,
1150
+ }
1151
+ : undefined
1152
+ }
893
1153
  translations={resolvedTranslations}
894
1154
  icons={icons}
895
1155
  className="@lg:hidden"
896
1156
  />
897
1157
  <DataTableStackedRows
898
1158
  rows={rows}
1159
+ grouping={groupingView}
899
1160
  view={view}
900
1161
  error={error}
901
1162
  refetching={isRefetching}
@@ -911,17 +1172,26 @@ function DataTableComponent<TEntity extends RowData>({
911
1172
  />
912
1173
  </>
913
1174
  )}
914
- {enablePagination && (
915
- <DataTablePagination
916
- table={table}
917
- pageSizeOptions={pageSizeOptions}
918
- hasNextPage={hasNextPage}
919
- translations={translations}
920
- paginationMode={paginationMode}
921
- icons={icons}
922
- className={classNames?.pagination}
923
- />
924
- )}
1175
+ {enablePagination &&
1176
+ (groupingView && !groupsOnePage ? (
1177
+ <DataTableGroupSummary
1178
+ grouping={groupingView}
1179
+ translations={resolvedTranslations}
1180
+ className={classNames?.pagination}
1181
+ />
1182
+ ) : (
1183
+ <DataTablePagination
1184
+ table={table}
1185
+ // Grouped, the row model also holds the group rows: count the real ones.
1186
+ rowsOnPage={groupingView ? loadedCount : undefined}
1187
+ pageSizeOptions={pageSizeOptions}
1188
+ hasNextPage={hasNextPage}
1189
+ translations={translations}
1190
+ paginationMode={paginationMode}
1191
+ icons={icons}
1192
+ className={classNames?.pagination}
1193
+ />
1194
+ ))}
925
1195
  </Table>
926
1196
  )
927
1197
  }