@42-central/ui-react 0.25.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 (232) 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-QZ4N5LYG.js → chunk-5H5YOPCF.js} +3 -3
  6. package/dist/{chunk-QZ4N5LYG.js.map → chunk-5H5YOPCF.js.map} +1 -1
  7. package/dist/{chunk-6II2DVYR.js → chunk-7KFUFAVP.js} +4 -4
  8. package/dist/{chunk-6II2DVYR.js.map → chunk-7KFUFAVP.js.map} +1 -1
  9. package/dist/{chunk-XUFCI262.js → chunk-7OGZT7U2.js} +12 -4
  10. package/dist/chunk-7OGZT7U2.js.map +1 -0
  11. package/dist/{chunk-PI7X5YFJ.js → chunk-BGDVUC3B.js} +5 -5
  12. package/dist/{chunk-PI7X5YFJ.js.map → chunk-BGDVUC3B.js.map} +1 -1
  13. package/dist/{chunk-7247WBTV.js → chunk-DJ72KP32.js} +3 -3
  14. package/dist/{chunk-7247WBTV.js.map → chunk-DJ72KP32.js.map} +1 -1
  15. package/dist/{chunk-FZSLSUSX.js → chunk-HCHGERDB.js} +3 -3
  16. package/dist/{chunk-FZSLSUSX.js.map → chunk-HCHGERDB.js.map} +1 -1
  17. package/dist/{chunk-H7WOMZXO.js → chunk-HWBFRDX7.js} +16 -8
  18. package/dist/chunk-HWBFRDX7.js.map +1 -0
  19. package/dist/{chunk-HQPU73WV.js → chunk-I4IJOLNA.js} +4 -4
  20. package/dist/{chunk-HQPU73WV.js.map → chunk-I4IJOLNA.js.map} +1 -1
  21. package/dist/{chunk-UDCCOUWR.js → chunk-IA6DCC3N.js} +6 -6
  22. package/dist/{chunk-UDCCOUWR.js.map → chunk-IA6DCC3N.js.map} +1 -1
  23. package/dist/{chunk-36NWC4GV.js → chunk-ISX3AM32.js} +3 -3
  24. package/dist/{chunk-36NWC4GV.js.map → chunk-ISX3AM32.js.map} +1 -1
  25. package/dist/{chunk-LMCUGYHT.js → chunk-L24N5EON.js} +6 -6
  26. package/dist/chunk-L24N5EON.js.map +1 -0
  27. package/dist/{chunk-HRNWWL3Y.js → chunk-L5GH7LLD.js} +61 -29
  28. package/dist/chunk-L5GH7LLD.js.map +1 -0
  29. package/dist/chunk-LJVIZNPE.js +9 -0
  30. package/dist/chunk-LJVIZNPE.js.map +1 -0
  31. package/dist/{chunk-AHKMN3YQ.js → chunk-MCRTAUSZ.js} +6 -6
  32. package/dist/chunk-MCRTAUSZ.js.map +1 -0
  33. package/dist/{chunk-LHUWOA2U.js → chunk-MNYY7UU2.js} +6 -6
  34. package/dist/{chunk-LHUWOA2U.js.map → chunk-MNYY7UU2.js.map} +1 -1
  35. package/dist/{chunk-7HZBD4YZ.js → chunk-NF2PBGD3.js} +36 -23
  36. package/dist/chunk-NF2PBGD3.js.map +1 -0
  37. package/dist/{chunk-54W6VAI3.js → chunk-NNPSBC7O.js} +5 -5
  38. package/dist/{chunk-54W6VAI3.js.map → chunk-NNPSBC7O.js.map} +1 -1
  39. package/dist/{chunk-MW42XVPD.js → chunk-PBSVX5DB.js} +15 -5
  40. package/dist/chunk-PBSVX5DB.js.map +1 -0
  41. package/dist/{chunk-2CL3FKEY.js → chunk-QEMXXQQO.js} +3 -3
  42. package/dist/{chunk-2CL3FKEY.js.map → chunk-QEMXXQQO.js.map} +1 -1
  43. package/dist/{chunk-6UIQJJNJ.js → chunk-QRNQQIQM.js} +5 -5
  44. package/dist/{chunk-6UIQJJNJ.js.map → chunk-QRNQQIQM.js.map} +1 -1
  45. package/dist/{chunk-N66NC6QK.js → chunk-RKCUXOU2.js} +4 -4
  46. package/dist/{chunk-N66NC6QK.js.map → chunk-RKCUXOU2.js.map} +1 -1
  47. package/dist/{chunk-N5M4PDBK.js → chunk-SBZJGFZ6.js} +3 -3
  48. package/dist/{chunk-N5M4PDBK.js.map → chunk-SBZJGFZ6.js.map} +1 -1
  49. package/dist/{chunk-7P32XCEU.js → chunk-SLUOF7XW.js} +2 -2
  50. package/dist/chunk-SLUOF7XW.js.map +1 -0
  51. package/dist/{chunk-SAXWCGWR.js → chunk-SOUDKESO.js} +14 -7
  52. package/dist/chunk-SOUDKESO.js.map +1 -0
  53. package/dist/{chunk-T44RW5DW.js → chunk-ULF5LOBS.js} +4 -4
  54. package/dist/{chunk-T44RW5DW.js.map → chunk-ULF5LOBS.js.map} +1 -1
  55. package/dist/components/ambient-background/index.js +9 -23
  56. package/dist/components/ambient-background/index.js.map +1 -1
  57. package/dist/components/ambient-background/starfield.d.ts.map +1 -1
  58. package/dist/components/app-shell/app-shell.d.ts +55 -10
  59. package/dist/components/app-shell/app-shell.d.ts.map +1 -1
  60. package/dist/components/app-shell/index.d.ts +1 -1
  61. package/dist/components/app-shell/index.d.ts.map +1 -1
  62. package/dist/components/app-shell/index.js +299 -38
  63. package/dist/components/app-shell/index.js.map +1 -1
  64. package/dist/components/autocomplete/index.js +4 -4
  65. package/dist/components/avatar/avatar.d.ts +1 -1
  66. package/dist/components/avatar/avatar.d.ts.map +1 -1
  67. package/dist/components/avatar/index.js +10 -1
  68. package/dist/components/avatar/index.js.map +1 -1
  69. package/dist/components/button-group/button-group.d.ts +10 -2
  70. package/dist/components/button-group/button-group.d.ts.map +1 -1
  71. package/dist/components/button-group/index.js +1 -1
  72. package/dist/components/button-multi-select/index.js +2 -2
  73. package/dist/components/button-select/button-select-control.d.ts +4 -1
  74. package/dist/components/button-select/button-select-control.d.ts.map +1 -1
  75. package/dist/components/button-select/button-select.d.ts +4 -1
  76. package/dist/components/button-select/button-select.d.ts.map +1 -1
  77. package/dist/components/button-select/index.js +2 -2
  78. package/dist/components/calendar-heatmap/index.js +2 -2
  79. package/dist/components/camera/index.js +6 -6
  80. package/dist/components/code-block/chrome.js +5 -5
  81. package/dist/components/code-block/index.js +6 -6
  82. package/dist/components/combobox/index.js +4 -4
  83. package/dist/components/combobox-list/index.js +6 -6
  84. package/dist/components/data-table/data-table-features.d.ts +8 -0
  85. package/dist/components/data-table/data-table-features.d.ts.map +1 -1
  86. package/dist/components/data-table/data-table-group.d.ts +128 -0
  87. package/dist/components/data-table/data-table-group.d.ts.map +1 -0
  88. package/dist/components/data-table/data-table-grouping.d.ts +72 -0
  89. package/dist/components/data-table/data-table-grouping.d.ts.map +1 -0
  90. package/dist/components/data-table/data-table-icons.d.ts +4 -2
  91. package/dist/components/data-table/data-table-icons.d.ts.map +1 -1
  92. package/dist/components/data-table/data-table-meta.d.ts +9 -0
  93. package/dist/components/data-table/data-table-meta.d.ts.map +1 -1
  94. package/dist/components/data-table/data-table-pagination.d.ts +4 -1
  95. package/dist/components/data-table/data-table-pagination.d.ts.map +1 -1
  96. package/dist/components/data-table/data-table-schema.d.ts +4 -0
  97. package/dist/components/data-table/data-table-schema.d.ts.map +1 -1
  98. package/dist/components/data-table/data-table-stack.d.ts +16 -3
  99. package/dist/components/data-table/data-table-stack.d.ts.map +1 -1
  100. package/dist/components/data-table/data-table-toolbar.d.ts +33 -2
  101. package/dist/components/data-table/data-table-toolbar.d.ts.map +1 -1
  102. package/dist/components/data-table/data-table-translations.d.ts +35 -0
  103. package/dist/components/data-table/data-table-translations.d.ts.map +1 -1
  104. package/dist/components/data-table/data-table.d.ts +55 -3
  105. package/dist/components/data-table/data-table.d.ts.map +1 -1
  106. package/dist/components/data-table/index.d.ts +3 -1
  107. package/dist/components/data-table/index.d.ts.map +1 -1
  108. package/dist/components/data-table/index.js +878 -230
  109. package/dist/components/data-table/index.js.map +1 -1
  110. package/dist/components/date-picker/index.js +8 -8
  111. package/dist/components/drawer/index.js +4 -4
  112. package/dist/components/geodesic/a11y.d.ts +3 -3
  113. package/dist/components/geodesic/a11y.d.ts.map +1 -1
  114. package/dist/components/geodesic/badge-frame.d.ts +13 -1
  115. package/dist/components/geodesic/badge-frame.d.ts.map +1 -1
  116. package/dist/components/geodesic/exam-badge.d.ts +4 -1
  117. package/dist/components/geodesic/exam-badge.d.ts.map +1 -1
  118. package/dist/components/geodesic/geometry.d.ts +14 -9
  119. package/dist/components/geodesic/geometry.d.ts.map +1 -1
  120. package/dist/components/geodesic/index.js +62 -23
  121. package/dist/components/geodesic/index.js.map +1 -1
  122. package/dist/components/geodesic/status.d.ts +8 -6
  123. package/dist/components/geodesic/status.d.ts.map +1 -1
  124. package/dist/components/geodesic/styles.css +15 -2
  125. package/dist/components/hover-card/index.js +3 -3
  126. package/dist/components/indicator/index.js +23 -8
  127. package/dist/components/indicator/index.js.map +1 -1
  128. package/dist/components/indicator/indicator.d.ts +15 -6
  129. package/dist/components/indicator/indicator.d.ts.map +1 -1
  130. package/dist/components/kbd/index.js +3 -3
  131. package/dist/components/lightbox/index.js +4 -4
  132. package/dist/components/menu/index.js +4 -4
  133. package/dist/components/modal/index.js +4 -4
  134. package/dist/components/multi-combobox-list/index.js +6 -6
  135. package/dist/components/multi-select/index.js +4 -4
  136. package/dist/components/nav-link/index.js +5 -1
  137. package/dist/components/nav-link/nav-link.d.ts.map +1 -1
  138. package/dist/components/nav-link/tanstack.js +5 -1
  139. package/dist/components/nav-link/tanstack.js.map +1 -1
  140. package/dist/components/notification/create-notifier.d.ts.map +1 -1
  141. package/dist/components/notification/index.js +15 -3
  142. package/dist/components/notification/index.js.map +1 -1
  143. package/dist/components/notification/types.d.ts +5 -0
  144. package/dist/components/notification/types.d.ts.map +1 -1
  145. package/dist/components/number-input/index.js +4 -4
  146. package/dist/components/overlays/index.js +5 -5
  147. package/dist/components/photo-capture/index.js +6 -6
  148. package/dist/components/popover/index.js +3 -3
  149. package/dist/components/schedule/index.js +4 -4
  150. package/dist/components/segment-group/index.js +1 -1
  151. package/dist/components/segment-group/segment-group.d.ts +6 -1
  152. package/dist/components/segment-group/segment-group.d.ts.map +1 -1
  153. package/dist/components/select/index.js +5 -5
  154. package/dist/components/spotlight/index.js +8 -8
  155. package/dist/components/tabs/index.js +1 -1
  156. package/dist/components/tabs/tabs-data.d.ts +4 -1
  157. package/dist/components/tabs/tabs-data.d.ts.map +1 -1
  158. package/dist/components/tabs/tabs.d.ts +4 -1
  159. package/dist/components/tabs/tabs.d.ts.map +1 -1
  160. package/dist/components/tags-input/index.js +3 -3
  161. package/dist/components/time-input/index.js +6 -6
  162. package/dist/components/tooltip/index.js +2 -2
  163. package/dist/components/tree-multi-select/index.js +4 -4
  164. package/dist/components/tree-select/index.js +4 -4
  165. package/dist/index.js +2 -2
  166. package/dist/lib/overlay-container.d.ts.map +1 -1
  167. package/dist/lib/overlay-layer.d.ts +10 -3
  168. package/dist/lib/overlay-layer.d.ts.map +1 -1
  169. package/dist/lib/sidebar-rail.d.ts +21 -0
  170. package/dist/lib/sidebar-rail.d.ts.map +1 -0
  171. package/dist/theme.css +21 -18
  172. package/package.json +1 -1
  173. package/src/as-child.contract.test.tsx +6 -0
  174. package/src/components/ambient-background/starfield.tsx +14 -41
  175. package/src/components/app-shell/app-shell.test.tsx +430 -5
  176. package/src/components/app-shell/app-shell.tsx +484 -24
  177. package/src/components/app-shell/index.ts +2 -0
  178. package/src/components/avatar/avatar.test.tsx +12 -0
  179. package/src/components/avatar/avatar.tsx +9 -0
  180. package/src/components/button-group/button-group.test.tsx +24 -0
  181. package/src/components/button-group/button-group.tsx +21 -4
  182. package/src/components/button-multi-select/button-multi-select.test.tsx +15 -0
  183. package/src/components/button-select/button-select-control.tsx +5 -0
  184. package/src/components/button-select/button-select.test.tsx +25 -0
  185. package/src/components/button-select/button-select.tsx +13 -2
  186. package/src/components/data-table/data-table-features.ts +11 -0
  187. package/src/components/data-table/data-table-group.test.tsx +1007 -0
  188. package/src/components/data-table/data-table-group.tsx +474 -0
  189. package/src/components/data-table/data-table-grouping.ts +160 -0
  190. package/src/components/data-table/data-table-icons.tsx +8 -1
  191. package/src/components/data-table/data-table-meta.ts +9 -0
  192. package/src/components/data-table/data-table-pagination.tsx +5 -1
  193. package/src/components/data-table/data-table-stack.tsx +165 -43
  194. package/src/components/data-table/data-table-toolbar.tsx +110 -2
  195. package/src/components/data-table/data-table-translations.ts +44 -0
  196. package/src/components/data-table/data-table.tsx +420 -150
  197. package/src/components/data-table/index.ts +4 -0
  198. package/src/components/drawer/drawer.tsx +1 -1
  199. package/src/components/geodesic/badge-frame.tsx +67 -13
  200. package/src/components/geodesic/exam-badge.tsx +8 -4
  201. package/src/components/geodesic/geometry.ts +8 -6
  202. package/src/components/geodesic/status.test.ts +11 -2
  203. package/src/components/geodesic/status.ts +11 -7
  204. package/src/components/geodesic/styles.css +15 -2
  205. package/src/components/indicator/indicator.test.tsx +45 -0
  206. package/src/components/indicator/indicator.tsx +40 -13
  207. package/src/components/modal/modal.tsx +1 -1
  208. package/src/components/nav-link/nav-link.test.tsx +121 -1
  209. package/src/components/nav-link/nav-link.tsx +78 -29
  210. package/src/components/notification/create-notifier.tsx +18 -2
  211. package/src/components/notification/notify-in-modal.test.tsx +128 -0
  212. package/src/components/notification/types.ts +5 -0
  213. package/src/components/segment-group/segment-group.test.tsx +22 -0
  214. package/src/components/segment-group/segment-group.tsx +35 -19
  215. package/src/components/tabs/tabs-data.tsx +5 -1
  216. package/src/components/tabs/tabs.test.tsx +28 -0
  217. package/src/components/tabs/tabs.tsx +14 -4
  218. package/src/lib/overlay-container.tsx +2 -1
  219. package/src/lib/overlay-layer.test.tsx +63 -1
  220. package/src/lib/overlay-layer.tsx +29 -6
  221. package/src/lib/sidebar-rail.ts +24 -0
  222. package/src/theme.css +21 -18
  223. package/src/theme.test.ts +14 -11
  224. package/dist/chunk-7HZBD4YZ.js.map +0 -1
  225. package/dist/chunk-7P32XCEU.js.map +0 -1
  226. package/dist/chunk-AHKMN3YQ.js.map +0 -1
  227. package/dist/chunk-H7WOMZXO.js.map +0 -1
  228. package/dist/chunk-HRNWWL3Y.js.map +0 -1
  229. package/dist/chunk-LMCUGYHT.js.map +0 -1
  230. package/dist/chunk-MW42XVPD.js.map +0 -1
  231. package/dist/chunk-SAXWCGWR.js.map +0 -1
  232. package/dist/chunk-XUFCI262.js.map +0 -1
@@ -0,0 +1,474 @@
1
+ "use client"
2
+
3
+ import {
4
+ type Column,
5
+ type ExpandedState,
6
+ flexRender,
7
+ type RowData,
8
+ type Updater,
9
+ } from "@tanstack/react-table"
10
+ import { type ReactNode, useId, useState } from "react"
11
+ import { cn } from "../../lib/cn"
12
+ import { focusStyles } from "../../lib/focus-styles"
13
+ import { useLocale } from "../../lib/intl"
14
+ import { props } from "../../lib/props"
15
+ import { Badge } from "../badge/badge"
16
+ import { Button } from "../button"
17
+ import { Checkbox } from "../checkbox/checkbox"
18
+ import { Table, type TableSize } from "../table/table"
19
+ import { Text } from "../text/text"
20
+ import type { DataTableFeatures, DataTableInstance, Row } from "./data-table-features"
21
+ import {
22
+ buildGroupTree,
23
+ type DataTableGroup,
24
+ type DataTableGroupNode,
25
+ groupLeafRows,
26
+ groupOptions,
27
+ isGroupExpanded,
28
+ toggleGroupExpanded,
29
+ } from "./data-table-grouping"
30
+ import type { ResolvedDataTableIcons } from "./data-table-icons"
31
+ import type { ResolvedDataTableTranslations } from "./data-table-translations"
32
+
33
+ /** What `renderGroupHeader` receives for one group. */
34
+ export type DataTableGroupHeaderContext<TEntity extends RowData> = {
35
+ /** The column the group is made from. */
36
+ column: Column<DataTableFeatures, TEntity, unknown>
37
+ /** The group's value in that column, as a string. */
38
+ value: string
39
+ /** Its plain-text label: the matching `select` option's label, else the value. */
40
+ label: string
41
+ /** The group's rows present in `data`, across its sub-groups. */
42
+ rows: Row<TEntity>[]
43
+ /** Every row the group holds; the server's total under `manualGrouping`. */
44
+ count: number
45
+ expanded: boolean
46
+ toggle: () => void
47
+ }
48
+
49
+ /** What a "Show more" click reports under `manualGrouping`. */
50
+ export type DataTableGroupShowMore = {
51
+ columnId: string
52
+ value: string
53
+ /** How many of the group's rows are in `data` already. */
54
+ loaded: number
55
+ }
56
+
57
+ /** Everything the two renderings (table body, stacked cards) need to draw groups. */
58
+ export type DataTableGrouping<TEntity extends RowData> = {
59
+ groups: DataTableGroupNode<TEntity>[]
60
+ /** Rows across the drawn groups, by their `count`. */
61
+ rowCount: number
62
+ label: (node: DataTableGroupNode<TEntity>) => string
63
+ renderValue: (node: DataTableGroupNode<TEntity>) => ReactNode
64
+ isExpanded: (node: DataTableGroupNode<TEntity>) => boolean
65
+ toggle: (node: DataTableGroupNode<TEntity>) => void
66
+ /** The group's leaf rows up to its current "Show more" limit. */
67
+ visibleRows: (node: DataTableGroupNode<TEntity>) => Row<TEntity>[]
68
+ /** How many rows the next "Show more" adds; `0` draws no button. */
69
+ nextCount: (node: DataTableGroupNode<TEntity>) => number
70
+ showMore: (node: DataTableGroupNode<TEntity>) => void
71
+ /** Groups the user opened since mount: their rows fade in. */
72
+ opened: ReadonlySet<string>
73
+ }
74
+
75
+ const NO_VALUE = new Set(["", "null", "undefined"])
76
+
77
+ /**
78
+ * Builds the group tree for the current `grouping` and holds what is local to
79
+ * drawing it: each group's "Show more" limit and which groups were just opened.
80
+ * Returns `null` while the table isn't grouped.
81
+ */
82
+ export function useDataTableGrouping<TEntity extends RowData>({
83
+ table,
84
+ grouping,
85
+ expanded,
86
+ onExpandedChange,
87
+ hideEmptyGroups,
88
+ paginate,
89
+ groupPageSize,
90
+ manualGrouping,
91
+ manualGroups,
92
+ onGroupShowMore,
93
+ renderGroupHeader,
94
+ translations,
95
+ resetKey,
96
+ }: {
97
+ table: DataTableInstance<TEntity>
98
+ grouping: string[]
99
+ expanded: ExpandedState
100
+ onExpandedChange: (updater: Updater<ExpandedState>) => void
101
+ hideEmptyGroups: boolean
102
+ /** `false` shows every row of a group at once (`enablePagination={false}`). */
103
+ paginate: boolean
104
+ groupPageSize: number
105
+ manualGrouping: boolean
106
+ manualGroups?: ReadonlyArray<DataTableGroup>
107
+ onGroupShowMore?: (group: DataTableGroupShowMore) => void
108
+ renderGroupHeader?: (context: DataTableGroupHeaderContext<TEntity>) => ReactNode
109
+ translations: ResolvedDataTableTranslations
110
+ /** Changes whenever the row set does (grouping, sorting, filters, search): the
111
+ * limits start over, since "the first 25" no longer names the same rows. */
112
+ resetKey: string
113
+ }): DataTableGrouping<TEntity> | null {
114
+ const [limitState, setLimitState] = useState<{ key: string; limits: Record<string, number> }>({
115
+ key: resetKey,
116
+ limits: {},
117
+ })
118
+ const [opened, setOpened] = useState<ReadonlySet<string>>(() => new Set())
119
+
120
+ if (grouping.length === 0) return null
121
+
122
+ const limits = limitState.key === resetKey ? limitState.limits : {}
123
+ const groups = buildGroupTree({
124
+ table,
125
+ rows: table.getSortedRowModel().rows,
126
+ grouping,
127
+ hideEmptyGroups,
128
+ manualGroups: manualGrouping ? manualGroups : undefined,
129
+ })
130
+
131
+ const limitOf = (node: DataTableGroupNode<TEntity>) =>
132
+ paginate && !manualGrouping ? (limits[node.id] ?? groupPageSize) : Number.POSITIVE_INFINITY
133
+ const isExpanded = (node: DataTableGroupNode<TEntity>) => isGroupExpanded(expanded, node.id)
134
+ const toggle = (node: DataTableGroupNode<TEntity>) => {
135
+ if (!isExpanded(node)) setOpened((previous) => new Set(previous).add(node.id))
136
+ onExpandedChange((old) => toggleGroupExpanded(old, node.id))
137
+ }
138
+ const label = (node: DataTableGroupNode<TEntity>) =>
139
+ groupOptions(table.getColumn(node.columnId)).find((option) => option.value === node.value)
140
+ ?.label ?? (NO_VALUE.has(node.value) ? translations.groupNoValue : node.value)
141
+
142
+ const renderValue = (node: DataTableGroupNode<TEntity>): ReactNode => {
143
+ const column = table.getColumn(node.columnId)
144
+ if (!column) return label(node)
145
+ const leaves = groupLeafRows(node)
146
+ if (renderGroupHeader) {
147
+ return renderGroupHeader({
148
+ column,
149
+ value: node.value,
150
+ label: label(node),
151
+ rows: leaves,
152
+ count: node.count,
153
+ expanded: isExpanded(node),
154
+ toggle: () => toggle(node),
155
+ })
156
+ }
157
+ // The group is drawn the way its cells draw it: the first row's own cell.
158
+ // Not when the column groups by something other than its value
159
+ // (`getGroupingValue`), where one row's cell wouldn't stand for the group,
160
+ // and not for a group with no value or no row.
161
+ const cell =
162
+ column.columnDef.getGroupingValue == null && !NO_VALUE.has(node.value)
163
+ ? leaves[0]?.getAllCells().find((candidate) => candidate.column.id === node.columnId)
164
+ : undefined
165
+ if (cell) return flexRender(column.columnDef.cell, cell.getContext())
166
+ const option = groupOptions(column).find((candidate) => candidate.value === node.value)
167
+ return option?.color ? (
168
+ <Badge size="sm" color={option.color}>
169
+ {option.label}
170
+ </Badge>
171
+ ) : (
172
+ label(node)
173
+ )
174
+ }
175
+
176
+ return {
177
+ groups,
178
+ rowCount: groups.reduce((sum, node) => sum + node.count, 0),
179
+ label,
180
+ renderValue,
181
+ isExpanded,
182
+ toggle,
183
+ visibleRows: (node) => node.rows.slice(0, limitOf(node)),
184
+ nextCount: (node) => {
185
+ if (node.groups.length > 0) return 0
186
+ const rest = manualGrouping ? node.count - node.loaded : node.rows.length - limitOf(node)
187
+ return Math.max(0, Math.min(groupPageSize, rest))
188
+ },
189
+ showMore: (node) => {
190
+ if (manualGrouping) {
191
+ onGroupShowMore?.({ columnId: node.columnId, value: node.value, loaded: node.loaded })
192
+ return
193
+ }
194
+ setLimitState({
195
+ key: resetKey,
196
+ limits: { ...limits, [node.id]: limitOf(node) + groupPageSize },
197
+ })
198
+ },
199
+ opened,
200
+ }
201
+ }
202
+
203
+ /**
204
+ * A group's fold control: the chevron and the group's value in one button, then
205
+ * the row count. Shared by the table's group header row and the stacked layout's
206
+ * section heading.
207
+ *
208
+ * The button is named after the group and says whether it is open through
209
+ * `aria-expanded`. The value is drawn inside it for the click target but hidden
210
+ * from assistive technology, which already has the name; a cell renderer used
211
+ * here must not hold a link or a control of its own.
212
+ */
213
+ export function DataTableGroupToggle<TEntity extends RowData>({
214
+ node,
215
+ grouping,
216
+ controls,
217
+ translations,
218
+ icons,
219
+ className,
220
+ }: {
221
+ node: DataTableGroupNode<TEntity>
222
+ grouping: DataTableGrouping<TEntity>
223
+ /** On the fold button: the table indents a sub-group's with it. */
224
+ className?: string
225
+ /** The id of the element holding the group's rows, for `aria-controls`. */
226
+ controls?: string
227
+ translations: ResolvedDataTableTranslations
228
+ icons: ResolvedDataTableIcons
229
+ }) {
230
+ const format = new Intl.NumberFormat(useLocale()).format
231
+ const expanded = grouping.isExpanded(node)
232
+ const foldable = node.count > 0
233
+ return (
234
+ <>
235
+ <button
236
+ type="button"
237
+ aria-expanded={foldable ? expanded : undefined}
238
+ aria-controls={foldable && expanded ? controls : undefined}
239
+ aria-label={translations.groupToggle(grouping.label(node))}
240
+ disabled={!foldable}
241
+ onClick={() => grouping.toggle(node)}
242
+ className={cn(
243
+ "inline-flex min-w-0 items-center gap-1.5 rounded-xxs font-medium select-none",
244
+ "enabled:cursor-pointer",
245
+ focusStyles({ offset: false }),
246
+ // The chevron is sized and coloured from here, so one passed through
247
+ // `icons` lands the same. It turns down while the group is open; when
248
+ // closed it follows the reading direction.
249
+ "[&>svg]:size-3.5 [&>svg]:shrink-0 [&>svg]:text-brand-900/40 dark:[&>svg]:text-white/35",
250
+ "enabled:hover:[&>svg]:text-brand-950 dark:enabled:hover:[&>svg]:text-white",
251
+ "motion-safe:[&>svg]:transition-transform motion-safe:[&>svg]:duration-(--duration-fast)",
252
+ foldable && expanded ? "[&>svg]:rotate-90" : "rtl:[&>svg]:rotate-180",
253
+ !foldable && "[&>svg]:opacity-40",
254
+ className,
255
+ )}
256
+ >
257
+ {icons.fold}
258
+ <span aria-hidden="true" className="pointer-events-none inline-flex min-w-0 items-center">
259
+ {grouping.renderValue(node)}
260
+ </span>
261
+ </button>
262
+ <Text span size="xs" c="muted" className="tabular-nums">
263
+ <span aria-hidden="true">{format(node.count)}</span>
264
+ <span className="sr-only">{translations.groupCount(node.count, format(node.count))}</span>
265
+ </Text>
266
+ </>
267
+ )
268
+ }
269
+
270
+ /** A group's "Show N more" button, for the table body and the stacked layout alike. */
271
+ export function DataTableGroupShowMore<TEntity extends RowData>({
272
+ node,
273
+ grouping,
274
+ translations,
275
+ }: {
276
+ node: DataTableGroupNode<TEntity>
277
+ grouping: DataTableGrouping<TEntity>
278
+ translations: ResolvedDataTableTranslations
279
+ }) {
280
+ const format = new Intl.NumberFormat(useLocale()).format
281
+ const next = grouping.nextCount(node)
282
+ return (
283
+ <Button variant="subtle" color="neutral" size="xs" onClick={() => grouping.showMore(node)}>
284
+ {translations.showMore(next, format(next))}
285
+ </Button>
286
+ )
287
+ }
288
+
289
+ /** The group header cell's wash and hairlines. The `<tr>` carries
290
+ * `data-placeholder`, so `Table`'s row hover leaves it alone. */
291
+ const groupHeaderCell = cn(
292
+ "bg-black/3 dark:bg-white/4",
293
+ "border-brand-900/20 dark:border-white/8",
294
+ // A hairline under the header unless it closes its `<tbody>` (a folded or empty
295
+ // last group): the next group's header, or the footer, draws that one.
296
+ "[tr:not(:last-child)>&]:border-b",
297
+ )
298
+
299
+ /**
300
+ * The grouped table body: one `<tbody>` per first-level group, which makes each
301
+ * a row group. Its first row is the group header, a `<th scope="rowgroup">`
302
+ * across every column holding the group's checkbox (in selection mode), fold
303
+ * button and count. A second grouping level adds sub-group header rows inside
304
+ * the same `<tbody>`, since a `<tbody>` can't nest.
305
+ */
306
+ export function DataTableGroupedBody<TEntity extends RowData>({
307
+ grouping,
308
+ table,
309
+ colSpan,
310
+ renderRow,
311
+ selectable,
312
+ size,
313
+ translations,
314
+ icons,
315
+ className,
316
+ }: {
317
+ grouping: DataTableGrouping<TEntity>
318
+ table: DataTableInstance<TEntity>
319
+ /** The visible column count: every group row spans the table. */
320
+ colSpan: number
321
+ /** `DataTable`'s own data row, so grouped and flat tables draw rows alike. */
322
+ renderRow: (row: Row<TEntity>, options?: { className?: string; indent?: boolean }) => ReactNode
323
+ /** Draws each group's tri-state checkbox. */
324
+ selectable: boolean
325
+ size: TableSize
326
+ translations: ResolvedDataTableTranslations
327
+ icons: ResolvedDataTableIcons
328
+ className?: string
329
+ }) {
330
+ const baseId = useId()
331
+
332
+ const header = (node: DataTableGroupNode<TEntity>, controls?: string) => {
333
+ const selectableRows = groupLeafRows(node).filter((row) => row.getCanSelect())
334
+ const selectableIds = new Set(selectableRows.map((row) => row.id))
335
+ const selected = selectableRows.filter((row) => row.getIsSelected()).length
336
+ const allLoadedSelected = selected === selectableRows.length
337
+ // Under `manualGrouping` a group can hold more rows than are loaded. Its
338
+ // checkbox only reaches the loaded ones, so it never reads "checked" beside
339
+ // a larger total.
340
+ const partlyLoaded = node.loaded < node.count
341
+ return (
342
+ <Table.Row
343
+ key={node.id}
344
+ testId={`group-${node.id}`}
345
+ data-placeholder
346
+ data-group
347
+ data-depth={node.depth}
348
+ {...props({ "data-expanded": grouping.isExpanded(node) })}
349
+ >
350
+ <Table.HeaderCell
351
+ scope={node.depth === 0 ? "rowgroup" : "row"}
352
+ colSpan={colSpan}
353
+ className={cn(
354
+ groupHeaderCell,
355
+ node.depth === 0 && "[tbody:not(:first-of-type)_&]:border-t",
356
+ )}
357
+ >
358
+ {/* Pinned to the inline start, so the group stays named while the table
359
+ scrolls sideways. */}
360
+ <div className="sticky start-3 flex w-max max-w-full items-center gap-2">
361
+ {selectable && selectableRows.length > 0 && (
362
+ <Checkbox
363
+ size={size}
364
+ checked={
365
+ selected === 0
366
+ ? false
367
+ : allLoadedSelected && !partlyLoaded
368
+ ? true
369
+ : "indeterminate"
370
+ }
371
+ onCheckedChange={() =>
372
+ // Written to the leaf rows directly: the group row itself is never
373
+ // selectable, so `rowSelection` only ever holds real row ids. Decided
374
+ // from the rows, not from the checkbox: a partly loaded group reads
375
+ // indeterminate with every loaded row selected, and must still clear.
376
+ table.setRowSelection((old) => ({
377
+ ...Object.fromEntries(
378
+ Object.entries(old).filter(([id]) => !selectableIds.has(id)),
379
+ ),
380
+ ...(allLoadedSelected
381
+ ? {}
382
+ : Object.fromEntries(selectableRows.map((row) => [row.id, true]))),
383
+ }))
384
+ }
385
+ label={translations.selectGroup(grouping.label(node))}
386
+ classNames={{ label: "sr-only" }}
387
+ />
388
+ )}
389
+ <DataTableGroupToggle
390
+ node={node}
391
+ grouping={grouping}
392
+ controls={controls}
393
+ translations={translations}
394
+ icons={icons}
395
+ // A sub-group steps in after the checkbox, which stays in the select
396
+ // column with the rows' own.
397
+ className={node.depth > 0 ? "ms-6" : undefined}
398
+ />
399
+ </div>
400
+ </Table.HeaderCell>
401
+ </Table.Row>
402
+ )
403
+ }
404
+
405
+ const leaves = (node: DataTableGroupNode<TEntity>) => [
406
+ ...grouping.visibleRows(node).map((row) =>
407
+ renderRow(row, {
408
+ className: grouping.opened.has(node.id) ? "motion-safe:animate-fade-in" : undefined,
409
+ // Rows under a sub-group step in with it, so they read as its children.
410
+ indent: node.depth > 0,
411
+ }),
412
+ ),
413
+ grouping.nextCount(node) > 0 && (
414
+ <Table.Row key={`${node.id}-more`} testId={`group-${node.id}-more`} data-placeholder>
415
+ <Table.Cell colSpan={colSpan}>
416
+ <div className={cn("sticky start-3 w-max", node.depth > 0 && "ps-6")}>
417
+ <DataTableGroupShowMore node={node} grouping={grouping} translations={translations} />
418
+ </div>
419
+ </Table.Cell>
420
+ </Table.Row>
421
+ ),
422
+ ]
423
+
424
+ return grouping.groups.map((node, index) => {
425
+ const bodyId = `${baseId}-group-${index}`
426
+ return (
427
+ <Table.Body key={node.id} id={bodyId} className={className}>
428
+ {header(node, bodyId)}
429
+ {grouping.isExpanded(node) &&
430
+ (node.groups.length > 0
431
+ ? node.groups.flatMap((sub) => [
432
+ header(sub),
433
+ ...(grouping.isExpanded(sub) ? leaves(sub) : []),
434
+ ])
435
+ : leaves(node))}
436
+ </Table.Body>
437
+ )
438
+ })
439
+ }
440
+
441
+ /** The footer while rows are grouped: the total, in the pagination band's place.
442
+ * Pages across the flattened list would split a group, so each group pages
443
+ * itself ("Show more") and the table-level controls step aside. */
444
+ export function DataTableGroupSummary<TEntity extends RowData>({
445
+ grouping,
446
+ translations,
447
+ className,
448
+ }: {
449
+ grouping: DataTableGrouping<TEntity>
450
+ translations: ResolvedDataTableTranslations
451
+ className?: string
452
+ }) {
453
+ const format = new Intl.NumberFormat(useLocale()).format
454
+ const rows = grouping.rowCount
455
+ const groups = grouping.groups.length
456
+ return (
457
+ <div
458
+ className={cn(
459
+ // The pagination band's own fill, hairline and padding.
460
+ "bg-brand-50/90 dark:bg-transparent",
461
+ "flex shrink-0 items-center border-t border-brand-900/20 px-4 py-3 dark:border-white/8",
462
+ className,
463
+ )}
464
+ >
465
+ <Text span size="sm" c="muted" role="status">
466
+ {translations.groupSummary({
467
+ rows,
468
+ groups,
469
+ formatted: { rows: format(rows), groups: format(groups) },
470
+ })}
471
+ </Text>
472
+ </div>
473
+ )
474
+ }
@@ -0,0 +1,160 @@
1
+ import type { ExpandedState, RowData } from "@tanstack/react-table"
2
+ import { type NormalizedItem, normalizeSelectData } from "../../lib/select-data"
3
+ import type { DataTableInstance, Row } from "./data-table-features"
4
+
5
+ /** A group as the server describes it, for `manualGrouping`: its value in the
6
+ * grouped column and how many rows it holds in total, loaded or not. */
7
+ export type DataTableGroup = { value: string; count: number }
8
+
9
+ /**
10
+ * One group to draw. `groups` holds its sub-groups when a second grouping level
11
+ * follows, `rows` its leaf rows otherwise; exactly one of the two is filled.
12
+ */
13
+ export type DataTableGroupNode<TEntity extends RowData> = {
14
+ /** react-table's group row id: `status:fixed`, nested as `status:fixed>building:b1`. */
15
+ id: string
16
+ columnId: string
17
+ /** The group's value, as a string: react-table keys groups by `String(value)`. */
18
+ value: string
19
+ depth: number
20
+ /** Every row the group holds: the loaded ones, or the server's total in manual mode. */
21
+ count: number
22
+ /** The rows of the group present in `data`. */
23
+ loaded: number
24
+ /** Absent for a group that has no row and was added from the column's options. */
25
+ row?: Row<TEntity>
26
+ groups: DataTableGroupNode<TEntity>[]
27
+ rows: Row<TEntity>[]
28
+ }
29
+
30
+ /** How many grouping levels `DataTable` draws. Manual mode stops at one. */
31
+ export const MAX_GROUPING_DEPTH = 2
32
+
33
+ const NO_OPTIONS: NormalizedItem[] = []
34
+
35
+ /** Normalised options per `options` array: labels and order are looked up for
36
+ * every group on every render, and column defs keep their arrays. */
37
+ const optionsCache = new WeakMap<object, NormalizedItem[]>()
38
+
39
+ /** A column's `select` filter options, flattened: the source of a group's label,
40
+ * its colour, its default position and the empty groups worth showing. */
41
+ export const groupOptions = (column?: {
42
+ columnDef: { meta?: { filter?: { type: string; options?: unknown } } }
43
+ }): NormalizedItem[] => {
44
+ const filter = column?.columnDef.meta?.filter
45
+ if (filter?.type !== "select" || !Array.isArray(filter.options)) return NO_OPTIONS
46
+ const cached = optionsCache.get(filter.options)
47
+ if (cached) return cached
48
+ const items = normalizeSelectData(filter.options).items
49
+ optionsCache.set(filter.options, items)
50
+ return items
51
+ }
52
+
53
+ /** A group is open unless the state says `false` for it. A plain `expanded` map
54
+ * lists the open rows, so a group that appears after a filter change would start
55
+ * folded; reading only an explicit `false` keeps new groups open and lets
56
+ * `expanded` hold nothing but the folded ones. */
57
+ export const isGroupExpanded = (expanded: ExpandedState, id: string): boolean =>
58
+ expanded === true || expanded[id] !== false
59
+
60
+ /** The next `expanded` state once the group `id` is folded or opened. */
61
+ export const toggleGroupExpanded = (expanded: ExpandedState, id: string): ExpandedState => {
62
+ const { [id]: current, ...rest } = expanded === true ? {} : expanded
63
+ return current === false ? rest : { ...rest, [id]: false }
64
+ }
65
+
66
+ /** Every leaf row under a group, across its sub-groups. */
67
+ export const groupLeafRows = <TEntity extends RowData>(
68
+ node: DataTableGroupNode<TEntity>,
69
+ ): Row<TEntity>[] => (node.groups.length > 0 ? node.groups.flatMap(groupLeafRows) : node.rows)
70
+
71
+ const ascending = (a: string, b: string) => a.localeCompare(b, undefined, { numeric: true })
72
+
73
+ /**
74
+ * Turns react-table's grouped rows into the ordered tree `DataTable` draws.
75
+ *
76
+ * react-table builds the groups and sorts the rows inside them. What it leaves
77
+ * out is done here, per level:
78
+ * - the order of the groups: `meta.groupOrder`, else the order of the server's
79
+ * `groups` (manual mode), else the column's `select` options, else ascending;
80
+ * - the groups that have no row, taken from the options or from `groups`, at the
81
+ * first level only: a second level would repeat every option under every parent;
82
+ * - the counts, which in manual mode come from the server.
83
+ */
84
+ export function buildGroupTree<TEntity extends RowData>({
85
+ table,
86
+ rows,
87
+ grouping,
88
+ hideEmptyGroups,
89
+ manualGroups,
90
+ }: {
91
+ table: DataTableInstance<TEntity>
92
+ /** The sorted row model's top-level rows: group rows while `grouping` is set. */
93
+ rows: Row<TEntity>[]
94
+ grouping: string[]
95
+ hideEmptyGroups: boolean
96
+ manualGroups?: ReadonlyArray<DataTableGroup>
97
+ }): DataTableGroupNode<TEntity>[] {
98
+ const level = (
99
+ levelRows: Row<TEntity>[],
100
+ depth: number,
101
+ parentId?: string,
102
+ ): DataTableGroupNode<TEntity>[] => {
103
+ const columnId = grouping[depth]
104
+ if (columnId == null) return []
105
+ const column = table.getColumn(columnId)
106
+ const manual = depth === 0 ? manualGroups : undefined
107
+ const order =
108
+ manual?.map((group) => group.value) ?? groupOptions(column).map((option) => option.value)
109
+ const declared = depth === 0 ? order : []
110
+ const rank = new Map(order.map((value, index) => [value, index] as const))
111
+ const counts = new Map(manual?.map((group) => [group.value, group.count] as const))
112
+ const compare =
113
+ column?.columnDef.meta?.groupOrder ??
114
+ ((a: string, b: string) =>
115
+ (rank.get(a) ?? Number.POSITIVE_INFINITY) - (rank.get(b) ?? Number.POSITIVE_INFINITY) ||
116
+ ascending(a, b))
117
+
118
+ const present = levelRows
119
+ .filter((row) => row.getIsGrouped())
120
+ .map((row): DataTableGroupNode<TEntity> => {
121
+ const nested = depth + 1 < grouping.length
122
+ const groups = nested ? level(row.subRows, depth + 1, row.id) : []
123
+ const leaves = nested ? [] : row.subRows
124
+ const loaded = nested ? groups.reduce((sum, group) => sum + group.loaded, 0) : leaves.length
125
+ return {
126
+ id: row.id,
127
+ columnId,
128
+ value: String(row.groupingValue),
129
+ depth,
130
+ count: loaded,
131
+ loaded,
132
+ row,
133
+ groups,
134
+ rows: leaves,
135
+ }
136
+ })
137
+ const known = new Set(present.map((node) => node.value))
138
+ const empty = declared
139
+ .filter((value) => !known.has(value))
140
+ .map(
141
+ (value): DataTableGroupNode<TEntity> => ({
142
+ id: parentId ? `${parentId}>${columnId}:${value}` : `${columnId}:${value}`,
143
+ columnId,
144
+ value,
145
+ depth,
146
+ count: 0,
147
+ loaded: 0,
148
+ groups: [],
149
+ rows: [],
150
+ }),
151
+ )
152
+
153
+ return [...present, ...empty]
154
+ .map((node) => ({ ...node, count: counts.get(node.value) ?? node.count }))
155
+ .filter((node) => !hideEmptyGroups || node.count > 0)
156
+ .sort((a, b) => compare(a.value, b.value))
157
+ }
158
+
159
+ return level(rows, 0)
160
+ }
@@ -8,6 +8,7 @@ import {
8
8
  ChevronUpIcon,
9
9
  Columns3CogIcon,
10
10
  EraserIcon,
11
+ GroupIcon,
11
12
  ListFilterIcon,
12
13
  RotateCwIcon,
13
14
  Search,
@@ -22,7 +23,9 @@ import { type IconMap, iconStroke, mergeIcons } from "../../lib/icons"
22
23
  * `DataTable`'s replaceable icons:
23
24
  * - `asc` / `desc` / `unsorted`: a sortable header's state, also in the stacked options drawer.
24
25
  * - `filter`: a column's filter trigger, and the stacked layout's options button.
25
- * - `search` / `columns`: the toolbar's global search and column-visibility menu.
26
+ * - `search` / `columns` / `group`: the toolbar's global search, column-visibility menu and group menu.
27
+ * - `fold`: a group header's chevron. It points to the inline end and the header turns it down
28
+ * while the group is open.
26
29
  * - `prev` / `next`: the pagination footer.
27
30
  * - `empty` / `error`: the placeholder states' badge; `clear` / `retry`: their actions.
28
31
  * - `min` / `max`: a `number` filter's two bounds.
@@ -42,6 +45,8 @@ export type DataTableIconSlot =
42
45
  | "retry"
43
46
  | "min"
44
47
  | "max"
48
+ | "group"
49
+ | "fold"
45
50
  export type DataTableIcons = IconMap<DataTableIconSlot>
46
51
  export type ResolvedDataTableIcons = Record<DataTableIconSlot, ReactNode>
47
52
 
@@ -69,6 +74,8 @@ export const DEFAULT_ICONS: ResolvedDataTableIcons = {
69
74
  retry: <RotateCwIcon className={iconStroke} />,
70
75
  min: <ArrowDownToLineIcon className={iconStroke} />,
71
76
  max: <ArrowUpToLineIcon className={iconStroke} />,
77
+ group: <GroupIcon className={iconStroke} />,
78
+ fold: <ChevronRight aria-hidden="true" className={iconStroke} />,
72
79
  }
73
80
 
74
81
  export const resolveDataTableIcons = (icons?: DataTableIcons): ResolvedDataTableIcons =>
@@ -98,5 +98,14 @@ declare module "@tanstack/react-table" {
98
98
  * there's no `TEntity` to hand it, just
99
99
  * `Array.from({ length: skeletonRowCount })` counters. */
100
100
  skeleton?: ReactNode
101
+ /** Lists the column in the toolbar's "Group" menu and lets `grouping` name it.
102
+ * Grouping is opt-in per column: set this, or `enableGrouping: true` on the
103
+ * column itself. The column needs an accessor, since a group is the set of
104
+ * rows that share its value. */
105
+ groupable?: boolean
106
+ /** Orders this column's groups, given two group values (always strings).
107
+ * Default: the order of `filter.options` for a `select` filter, then
108
+ * ascending by value. */
109
+ groupOrder?: (a: string, b: string) => number
101
110
  }
102
111
  }