banhaten 0.5.0 → 0.6.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 (290) hide show
  1. package/CHANGELOG.md +177 -139
  2. package/CONTRIBUTING.md +46 -46
  3. package/README.md +192 -192
  4. package/SECURITY.md +15 -15
  5. package/banhaten.config.example.json +11 -11
  6. package/docs/design-spacing.md +13 -13
  7. package/docs/design-system/README.md +22 -22
  8. package/docs/design-system/adoption-quality-remediation.md +87 -87
  9. package/docs/design-system/agent-usage.md +13 -13
  10. package/docs/design-system/appearance-presets.md +189 -189
  11. package/docs/design-system/appearances/default.md +94 -94
  12. package/docs/design-system/appearances/rounded.md +95 -95
  13. package/docs/design-system/appearances/sharp.md +95 -95
  14. package/docs/design-system/component-docs-pattern.md +81 -81
  15. package/docs/design-system/component-implementation-ownership-audit.md +245 -245
  16. package/docs/design-system/component-quality-audit-2026-08-20.md +225 -225
  17. package/docs/design-system/component-showcase-consistency-report.md +219 -219
  18. package/docs/design-system/component-token-consistency-audit.md +173 -173
  19. package/docs/design-system/components/README.md +91 -90
  20. package/docs/design-system/components/accordion.md +52 -52
  21. package/docs/design-system/components/activity-feed.md +93 -93
  22. package/docs/design-system/components/activity-heatmap.md +56 -56
  23. package/docs/design-system/components/alert-dialog.md +71 -71
  24. package/docs/design-system/components/alert.md +103 -97
  25. package/docs/design-system/components/app-header.md +97 -0
  26. package/docs/design-system/components/aspect-ratio.md +45 -45
  27. package/docs/design-system/components/attribute.md +101 -89
  28. package/docs/design-system/components/autocomplete.md +75 -75
  29. package/docs/design-system/components/avatar.md +53 -53
  30. package/docs/design-system/components/badge.md +55 -55
  31. package/docs/design-system/components/banner.md +86 -86
  32. package/docs/design-system/components/breadcrumbs.md +181 -177
  33. package/docs/design-system/components/button-group.md +97 -97
  34. package/docs/design-system/components/button.md +80 -80
  35. package/docs/design-system/components/card.md +102 -88
  36. package/docs/design-system/components/carousel.md +46 -46
  37. package/docs/design-system/components/catalog-components.md +46 -46
  38. package/docs/design-system/components/checkbox.md +53 -53
  39. package/docs/design-system/components/collapsible.md +63 -63
  40. package/docs/design-system/components/command.md +61 -61
  41. package/docs/design-system/components/context-menu.md +45 -45
  42. package/docs/design-system/components/date-picker.md +80 -80
  43. package/docs/design-system/components/divider.md +102 -102
  44. package/docs/design-system/components/empty-state.md +56 -56
  45. package/docs/design-system/components/field.md +70 -70
  46. package/docs/design-system/components/file-upload.md +186 -186
  47. package/docs/design-system/components/hover-card.md +47 -47
  48. package/docs/design-system/components/icons.md +48 -48
  49. package/docs/design-system/components/input-group.md +57 -57
  50. package/docs/design-system/components/input-otp.md +56 -56
  51. package/docs/design-system/components/input.md +111 -109
  52. package/docs/design-system/components/item.md +58 -58
  53. package/docs/design-system/components/kbd.md +49 -49
  54. package/docs/design-system/components/label.md +49 -49
  55. package/docs/design-system/components/menu.md +94 -73
  56. package/docs/design-system/components/menubar.md +46 -46
  57. package/docs/design-system/components/modal.md +104 -104
  58. package/docs/design-system/components/native-select.md +53 -53
  59. package/docs/design-system/components/navigation-menu.md +49 -49
  60. package/docs/design-system/components/onboarding-step-list-item.md +81 -81
  61. package/docs/design-system/components/page-header.md +127 -93
  62. package/docs/design-system/components/pagination.md +56 -56
  63. package/docs/design-system/components/popover.md +59 -59
  64. package/docs/design-system/components/progress-slider.md +49 -49
  65. package/docs/design-system/components/progress.md +76 -76
  66. package/docs/design-system/components/radio-card.md +50 -50
  67. package/docs/design-system/components/radio-group.md +56 -56
  68. package/docs/design-system/components/resizable.md +43 -43
  69. package/docs/design-system/components/scroll-area.md +50 -50
  70. package/docs/design-system/components/segmented-control.md +54 -54
  71. package/docs/design-system/components/select.md +68 -64
  72. package/docs/design-system/components/sheet.md +66 -66
  73. package/docs/design-system/components/sidebar.md +167 -111
  74. package/docs/design-system/components/skeleton.md +74 -74
  75. package/docs/design-system/components/slideout.md +64 -64
  76. package/docs/design-system/components/slider.md +62 -62
  77. package/docs/design-system/components/social-button.md +49 -49
  78. package/docs/design-system/components/spinner.md +62 -62
  79. package/docs/design-system/components/steps.md +65 -65
  80. package/docs/design-system/components/table.md +499 -422
  81. package/docs/design-system/components/tabs.md +74 -59
  82. package/docs/design-system/components/tag.md +79 -79
  83. package/docs/design-system/components/textarea.md +49 -49
  84. package/docs/design-system/components/timeline.md +82 -82
  85. package/docs/design-system/components/toast.md +57 -57
  86. package/docs/design-system/components/toggle.md +80 -80
  87. package/docs/design-system/components/toolbar.md +89 -89
  88. package/docs/design-system/components/tooltip.md +92 -92
  89. package/docs/design-system/components/typography.md +18 -18
  90. package/docs/design-system/density-and-elevation.md +65 -65
  91. package/docs/design-system/design-system-test-missing-items.md +368 -368
  92. package/docs/design-system/direction-and-localization.md +23 -23
  93. package/docs/design-system/form-recipes.md +73 -73
  94. package/docs/design-system/gap-promotion-policy.md +21 -21
  95. package/docs/design-system/icons.md +74 -74
  96. package/docs/design-system/installation.md +70 -70
  97. package/docs/design-system/launch-comparison-shadcn.md +98 -98
  98. package/docs/design-system/registry-and-cli.md +64 -64
  99. package/docs/design-system/release-policy.md +30 -30
  100. package/docs/design-system/roadmap.md +14 -14
  101. package/docs/design-system/support-matrix.md +23 -23
  102. package/docs/design-system/tabs.md +54 -54
  103. package/docs/design-system/template-gap-audit.md +32 -32
  104. package/docs/design-system/templates.md +59 -59
  105. package/docs/design-system/token-governance.md +72 -72
  106. package/package.json +98 -98
  107. package/registry/component-contract.json +40 -40
  108. package/registry/components/accordion.tsx +155 -155
  109. package/registry/components/alert-dialog.tsx +297 -297
  110. package/registry/components/alert.tsx +309 -293
  111. package/registry/components/app-header.tsx +151 -0
  112. package/registry/components/aspect-ratio.tsx +30 -30
  113. package/registry/components/attribute.tsx +489 -454
  114. package/registry/components/autocomplete.tsx +598 -598
  115. package/registry/components/avatar.tsx +385 -385
  116. package/registry/components/badge.tsx +630 -629
  117. package/registry/components/button-group.tsx +565 -565
  118. package/registry/components/button.tsx +128 -127
  119. package/registry/components/card.tsx +651 -638
  120. package/registry/components/carousel.tsx +296 -296
  121. package/registry/components/chart.tsx +203 -203
  122. package/registry/components/checkbox.tsx +247 -247
  123. package/registry/components/collapsible.tsx +109 -109
  124. package/registry/components/command.tsx +208 -208
  125. package/registry/components/context-menu.tsx +236 -236
  126. package/registry/components/date-picker.tsx +1289 -1289
  127. package/registry/components/expanded/ActivityFeed.tsx +227 -227
  128. package/registry/components/expanded/ActivityHeatmap.tsx +64 -64
  129. package/registry/components/expanded/Banner.tsx +179 -179
  130. package/registry/components/expanded/Breadcrumbs.tsx +135 -131
  131. package/registry/components/expanded/CatalogComponentsShowcase.tsx +205 -205
  132. package/registry/components/expanded/CatalogDivider.tsx +49 -49
  133. package/registry/components/expanded/EmptyState.tsx +155 -155
  134. package/registry/components/expanded/FileUpload.tsx +543 -543
  135. package/registry/components/expanded/OnboardingStepListItem.tsx +60 -60
  136. package/registry/components/expanded/PageHeader.tsx +209 -179
  137. package/registry/components/expanded/Slideout.tsx +108 -108
  138. package/registry/components/expanded/Steps.tsx +402 -402
  139. package/registry/components/expanded/Table.tsx +1109 -1092
  140. package/registry/components/expanded/Timeline.tsx +203 -203
  141. package/registry/components/expanded/activityFeed.css +248 -248
  142. package/registry/components/expanded/activityHeatmap.css +66 -66
  143. package/registry/components/expanded/banner.css +260 -260
  144. package/registry/components/expanded/breadcrumbs.css +157 -140
  145. package/registry/components/expanded/catalogComponentsShowcase.css +87 -87
  146. package/registry/components/expanded/divider.css +75 -75
  147. package/registry/components/expanded/emptyState.css +111 -111
  148. package/registry/components/expanded/fileUpload.css +457 -457
  149. package/registry/components/expanded/onboardingStepListItem.css +135 -135
  150. package/registry/components/expanded/pageHeader.css +284 -248
  151. package/registry/components/expanded/slideout.css +276 -276
  152. package/registry/components/expanded/steps.css +356 -356
  153. package/registry/components/expanded/table.css +655 -606
  154. package/registry/components/expanded/timeline.css +286 -286
  155. package/registry/components/field.tsx +283 -283
  156. package/registry/components/hover-card.tsx +48 -48
  157. package/registry/components/input-content.ts +107 -107
  158. package/registry/components/input-group.tsx +130 -130
  159. package/registry/components/input-otp.tsx +584 -584
  160. package/registry/components/input.tsx +992 -990
  161. package/registry/components/item.tsx +189 -189
  162. package/registry/components/kbd.tsx +246 -246
  163. package/registry/components/label.tsx +78 -78
  164. package/registry/components/menu.tsx +1091 -978
  165. package/registry/components/menubar.tsx +226 -226
  166. package/registry/components/modal.tsx +872 -872
  167. package/registry/components/native-select.tsx +205 -205
  168. package/registry/components/navigation-menu.tsx +171 -171
  169. package/registry/components/pagination.css +36 -36
  170. package/registry/components/pagination.tsx +560 -560
  171. package/registry/components/popover.tsx +338 -338
  172. package/registry/components/progress-slider.tsx +216 -216
  173. package/registry/components/radio-card.tsx +676 -676
  174. package/registry/components/radio-group.tsx +570 -570
  175. package/registry/components/resizable.tsx +108 -108
  176. package/registry/components/scroll-area.tsx +91 -91
  177. package/registry/components/segmented-control.tsx +87 -87
  178. package/registry/components/select-content.tsx +218 -218
  179. package/registry/components/select.tsx +937 -937
  180. package/registry/components/sheet.tsx +305 -305
  181. package/registry/components/sidebar.tsx +1535 -1433
  182. package/registry/components/skeleton.css +54 -54
  183. package/registry/components/skeleton.tsx +482 -482
  184. package/registry/components/slider.tsx +380 -380
  185. package/registry/components/social-button.tsx +387 -387
  186. package/registry/components/spinner.tsx +78 -78
  187. package/registry/components/table-elements.tsx +287 -128
  188. package/registry/components/tabs.css +246 -222
  189. package/registry/components/tabs.tsx +245 -241
  190. package/registry/components/tag.tsx +423 -423
  191. package/registry/components/textarea.tsx +610 -610
  192. package/registry/components/toast.tsx +136 -136
  193. package/registry/components/toggle.tsx +256 -256
  194. package/registry/components/toolbar.tsx +539 -539
  195. package/registry/components/tooltip.tsx +433 -433
  196. package/registry/examples/alert-demo.tsx +34 -34
  197. package/registry/examples/alert-dialog-demo.tsx +42 -42
  198. package/registry/examples/app-header-demo.tsx +40 -0
  199. package/registry/examples/aspect-ratio-demo.tsx +11 -11
  200. package/registry/examples/attribute-demo.tsx +65 -65
  201. package/registry/examples/autocomplete-demo.tsx +109 -109
  202. package/registry/examples/avatar-demo.tsx +132 -132
  203. package/registry/examples/badge-demo.tsx +69 -69
  204. package/registry/examples/button-demo.tsx +86 -86
  205. package/registry/examples/button-group-demo.tsx +63 -63
  206. package/registry/examples/card-demo.tsx +86 -86
  207. package/registry/examples/carousel-demo.tsx +43 -43
  208. package/registry/examples/chart-demo.tsx +18 -18
  209. package/registry/examples/checkbox-demo.tsx +62 -62
  210. package/registry/examples/collapsible-demo.tsx +16 -16
  211. package/registry/examples/collection-filter-bar-demo.tsx +135 -135
  212. package/registry/examples/command-demo.tsx +42 -42
  213. package/registry/examples/command-palette-demo.tsx +235 -235
  214. package/registry/examples/context-menu-demo.tsx +29 -29
  215. package/registry/examples/control-density-contract-demo.tsx +65 -65
  216. package/registry/examples/date-picker-demo.tsx +127 -127
  217. package/registry/examples/divider-demo.tsx +17 -17
  218. package/registry/examples/expanded/activity-heatmap-demo.tsx +29 -29
  219. package/registry/examples/expanded/banner-demo.tsx +48 -48
  220. package/registry/examples/expanded/breadcrumbs-demo.tsx +59 -59
  221. package/registry/examples/expanded/empty-state-demo.tsx +39 -39
  222. package/registry/examples/expanded/file-upload-demo.tsx +60 -60
  223. package/registry/examples/expanded/onboarding-step-demo.tsx +11 -11
  224. package/registry/examples/expanded/slideout-demo.tsx +15 -15
  225. package/registry/examples/expanded/steps-demo.tsx +29 -29
  226. package/registry/examples/expanded/tabs-demo.tsx +13 -13
  227. package/registry/examples/expanded/timeline-demo.tsx +18 -18
  228. package/registry/examples/field-demo.tsx +51 -51
  229. package/registry/examples/hover-card-demo.tsx +23 -23
  230. package/registry/examples/input-actions-demo.tsx +36 -36
  231. package/registry/examples/input-demo.tsx +87 -87
  232. package/registry/examples/input-group-demo.tsx +16 -16
  233. package/registry/examples/input-otp-demo.tsx +72 -72
  234. package/registry/examples/item-demo.tsx +46 -46
  235. package/registry/examples/kbd-demo.tsx +12 -12
  236. package/registry/examples/label-demo.tsx +20 -20
  237. package/registry/examples/menu-demo.tsx +122 -122
  238. package/registry/examples/menubar-demo.tsx +34 -34
  239. package/registry/examples/modal-demo.tsx +74 -74
  240. package/registry/examples/native-select-demo.tsx +16 -16
  241. package/registry/examples/navigation-menu-demo.tsx +29 -29
  242. package/registry/examples/pagination-demo.tsx +20 -20
  243. package/registry/examples/popover-demo.tsx +547 -547
  244. package/registry/examples/progress-demo.tsx +37 -37
  245. package/registry/examples/progress-slider-demo.tsx +29 -29
  246. package/registry/examples/radio-card-demo.tsx +51 -51
  247. package/registry/examples/radio-group-demo.tsx +62 -62
  248. package/registry/examples/resizable-demo.tsx +22 -22
  249. package/registry/examples/results-toolbar-demo.tsx +44 -44
  250. package/registry/examples/scroll-area-demo.tsx +15 -15
  251. package/registry/examples/segmented-control-demo.tsx +39 -39
  252. package/registry/examples/select-demo.tsx +87 -87
  253. package/registry/examples/sheet-demo.tsx +47 -47
  254. package/registry/examples/sidebar-demo.tsx +101 -101
  255. package/registry/examples/slider-demo.tsx +31 -31
  256. package/registry/examples/social-button-demo.tsx +61 -61
  257. package/registry/examples/spinner-demo.tsx +59 -59
  258. package/registry/examples/tabs-demo.tsx +13 -13
  259. package/registry/examples/tag-demo.tsx +29 -29
  260. package/registry/examples/textarea-demo.tsx +79 -79
  261. package/registry/examples/toast-demo.tsx +35 -35
  262. package/registry/examples/toggle-demo.tsx +59 -59
  263. package/registry/examples/tooltip-demo.tsx +115 -115
  264. package/registry/guidance/USAGE.md +27 -27
  265. package/registry/hooks/use-direction.ts +207 -207
  266. package/registry/index.json +387 -17
  267. package/registry/styles/globals.css +46 -0
  268. package/registry.json +24 -2
  269. package/schema/config.schema.json +51 -51
  270. package/schema/registry.schema.json +194 -194
  271. package/schema/tokens.schema.json +167 -167
  272. package/src/cli/command-line.js +194 -194
  273. package/src/cli/config-editors.js +9 -9
  274. package/src/cli/doctor.js +399 -399
  275. package/src/cli/file-transaction.js +302 -302
  276. package/src/cli/global-css.js +153 -153
  277. package/src/cli/index.js +1002 -1002
  278. package/src/cli/install-plan.js +592 -592
  279. package/src/cli/install-receipt.js +171 -171
  280. package/src/cli/jsonc.js +203 -203
  281. package/src/cli/package-install-state.js +254 -254
  282. package/src/cli/package-manager.js +238 -238
  283. package/src/cli/package-plan.js +185 -185
  284. package/src/cli/package-version.js +97 -97
  285. package/src/cli/registry-query.js +207 -207
  286. package/src/cli/reporting.js +55 -55
  287. package/src/cli/schema-validation.js +19 -19
  288. package/src/cli/source-scanner.js +686 -686
  289. package/src/cli/vite-config.js +478 -478
  290. package/tokens/banhaten.tokens.json +184 -0
@@ -1,1092 +1,1109 @@
1
- import { useMemo, useState, type CSSProperties, type ReactNode } from "react";
2
- import {
3
- ArrowUpDownIcon,
4
- EditIcon as LucideEditIcon,
5
- EllipsisIcon,
6
- EyeIcon,
7
- FileIcon as LucideFileIcon,
8
- StarIcon,
9
- Trash2Icon,
10
- } from "lucide-react";
11
- import { Pagination, type PaginationPage, type PaginationProps } from "@/components/ui/pagination";
12
- import { Spinner } from "@/components/ui/spinner";
13
- import { Avatar as AvatarPrimitive, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
14
- import { Badge } from "@/components/ui/badge";
15
- import { Button } from "@/components/ui/button";
16
- import { Checkbox } from "@/components/ui/checkbox";
17
- import { Progress as ProgressPrimitive } from "@/components/ui/progress";
18
- import { Tag } from "@/components/ui/tag";
19
- import { Toggle } from "@/components/ui/toggle";
20
- import {
21
- Toolbar,
22
- ToolbarBadge,
23
- ToolbarButton,
24
- ToolbarSearch,
25
- ToolbarSection,
26
- ToolbarText,
27
- type ToolbarButtonProps,
28
- } from "@/components/ui/toolbar";
29
- import "./table.css";
30
-
31
- export type TableDirection = "ltr" | "rtl";
32
- export type TableSize = "sm" | "lg";
33
- export type TableRowState = "default" | "selected" | "disabled";
34
- export type TableSortDirection = "asc" | "desc";
35
- export type TableSortValue = string | number | boolean | Date | null | undefined;
36
- export type TableSortState = {
37
- columnId: string;
38
- direction: TableSortDirection;
39
- } | null;
40
-
41
- export type TableRowBase = {
42
- id: string;
43
- state?: TableRowState;
44
- };
45
-
46
- export type TableColumn<Row extends TableRowBase> = {
47
- align?: "start" | "center" | "end";
48
- header?: string;
49
- id: string;
50
- item?: TableItemFactory<Row>;
51
- kind?: "data" | "selection" | "actions";
52
- minWidth?: number;
53
- renderCell?: (row: Row) => ReactNode;
54
- searchValue?: (row: Row) => string;
55
- sortable?: boolean;
56
- sortValue?: (row: Row) => TableSortValue;
57
- width?: number | "fill";
58
- };
59
-
60
- export type TableItemFactory<Row extends TableRowBase> = (row: Row) => TableItem;
61
-
62
- export type BadgeItem = {
63
- dot?: boolean;
64
- label: string;
65
- tone?: "blue" | "fuchsia" | "amber" | "neutral" | "success" | "warning" | "danger";
66
- };
67
-
68
- export type AvatarItem = {
69
- alt: string;
70
- src?: string;
71
- };
72
-
73
- export type TableAction = {
74
- disabled?: boolean;
75
- icon: "view" | "edit" | "delete" | "more";
76
- label: string;
77
- onAction?: () => void;
78
- };
79
-
80
- export type TableItem =
81
- | { type: "checkbox"; ariaLabel?: string; checked: boolean; disabled?: boolean; indeterminate?: boolean; onCheckedChange?: (checked: boolean) => void }
82
- | { type: "text"; tone?: "default" | "subtle"; value: string; weight?: "regular" | "medium" }
83
- | { type: "avatarText"; caption?: string; name: string; src?: string; status?: "top" | "bottom" }
84
- | { type: "rating"; max?: 5; value: number }
85
- | { type: "progress"; label?: string; tone?: "success" | "brand"; value: number }
86
- | { type: "toggle"; ariaLabel?: string; checked: boolean; disabled?: boolean; onCheckedChange?: (checked: boolean) => void }
87
- | { type: "badges"; items: BadgeItem[]; maxVisible?: number }
88
- | { type: "onlyAvatar"; alt: string; src?: string }
89
- | { type: "avatarStack"; addLabel?: string; avatars: AvatarItem[]; canAdd?: boolean; count?: number; onAdd?: () => void }
90
- | { type: "tags"; items: string[]; maxVisible?: number }
91
- | { type: "file"; label?: string }
92
- | { type: "brandLogoText"; label: string; logoSrc?: string }
93
- | { type: "payment"; brand: string; label?: string }
94
- | { type: "image"; label: string; src: string }
95
- | { type: "action"; icon: TableAction["icon"]; label: string; onAction?: () => void }
96
- | { type: "actionGroup"; actions: TableAction[] };
97
-
98
- export type TableProps<Row extends TableRowBase> = {
99
- columns: TableColumn<Row>[];
100
- dir?: TableDirection;
101
- onSelectedRowIdsChange?: (rowIds: string[]) => void;
102
- onSortChange?: (sort: TableSortState) => void;
103
- rows: Row[];
104
- selectedRowIds?: string[];
105
- size?: TableSize;
106
- sort?: TableSortState;
107
- };
108
-
109
- export type DataTableAction = {
110
- ariaLabel?: string;
111
- disabled?: boolean;
112
- label: string;
113
- onAction?: () => void;
114
- variant?: ToolbarButtonProps["variant"];
115
- };
116
-
117
- export type DataTableFilter<Row extends TableRowBase> = {
118
- active?: boolean;
119
- id: string;
120
- label: string;
121
- onAction?: () => void;
122
- predicate?: (row: Row) => boolean;
123
- value?: string;
124
- };
125
-
126
- export type DataTableSearch<Row extends TableRowBase> =
127
- | boolean
128
- | {
129
- defaultValue?: string;
130
- disabled?: boolean;
131
- getRowText?: (row: Row) => string;
132
- label?: string;
133
- onValueChange?: (value: string) => void;
134
- placeholder?: string;
135
- value?: string;
136
- };
137
-
138
- export type DataTablePagination = {
139
- ariaLabel?: string;
140
- defaultPage?: number;
141
- manual?: boolean;
142
- messages?: PaginationProps["messages"];
143
- onPageChange?: (page: number) => void;
144
- page?: number;
145
- pageSize?: number;
146
- pages?: PaginationPage[];
147
- showCaption?: boolean;
148
- totalRows?: number;
149
- type?: PaginationProps["type"];
150
- variant?: PaginationProps["variant"];
151
- };
152
-
153
- export type DataTableState = {
154
- action?: DataTableAction;
155
- description?: ReactNode;
156
- title?: ReactNode;
157
- };
158
-
159
- export type DataTableLabels = {
160
- emptyDescription: ReactNode;
161
- emptyTitle: ReactNode;
162
- errorDescription: ReactNode;
163
- errorTitle: ReactNode;
164
- loadingDescription: ReactNode;
165
- loadingTitle: ReactNode;
166
- resultsSummary: (summary: { from: number; to: number; total: number }) => ReactNode;
167
- searchPlaceholder: string;
168
- selectedCount: (count: number) => ReactNode;
169
- };
170
-
171
- export type DataTableProps<Row extends TableRowBase> = Omit<TableProps<Row>, "onSortChange" | "rows" | "sort"> & {
172
- actions?: DataTableAction[];
173
- bulkActions?: DataTableAction[];
174
- className?: string;
175
- defaultSort?: TableSortState;
176
- description?: ReactNode;
177
- emptyState?: DataTableState;
178
- error?: boolean | string | DataTableState;
179
- filters?: DataTableFilter<Row>[];
180
- getRowSearchText?: (row: Row) => string;
181
- labels?: Partial<DataTableLabels>;
182
- loading?: boolean;
183
- onSortChange?: (sort: TableSortState) => void;
184
- pagination?: false | DataTablePagination;
185
- rows: Row[];
186
- search?: DataTableSearch<Row>;
187
- sort?: TableSortState;
188
- title?: ReactNode;
189
- };
190
-
191
- function cx(...classes: Array<string | false | undefined>) {
192
- return classes.filter(Boolean).join(" ");
193
- }
194
-
195
- export function Table<Row extends TableRowBase>({
196
- columns,
197
- dir,
198
- onSelectedRowIdsChange,
199
- onSortChange,
200
- rows,
201
- selectedRowIds = [],
202
- size = "sm",
203
- sort = null,
204
- }: TableProps<Row>) {
205
- const selectableRows = rows.filter((row) => row.state !== "disabled");
206
- const allSelected = selectableRows.length > 0 && selectableRows.every((row) => selectedRowIds.includes(row.id));
207
- const someSelected = selectableRows.some((row) => selectedRowIds.includes(row.id)) && !allSelected;
208
-
209
- function toggleAll(checked: boolean) {
210
- onSelectedRowIdsChange?.(checked ? selectableRows.map((row) => row.id) : []);
211
- }
212
-
213
- function toggleRow(row: Row, checked: boolean) {
214
- if (row.state === "disabled") return;
215
- const next = checked
216
- ? Array.from(new Set([...selectedRowIds, row.id]))
217
- : selectedRowIds.filter((rowId) => rowId !== row.id);
218
- onSelectedRowIdsChange?.(next);
219
- }
220
-
221
- return (
222
- <div
223
- aria-label="Scrollable table"
224
- className={`ds-table-wrap ds-table-wrap--${size}`}
225
- dir={dir}
226
- tabIndex={0}
227
- >
228
- <table className="ds-table">
229
- <thead>
230
- <tr>
231
- {columns.map((column) => (
232
- <th
233
- aria-sort={column.sortable ? getAriaSort(sort, column.id) : undefined}
234
- className={columnClass(column)}
235
- key={column.id}
236
- style={columnStyle(column)}
237
- scope="col"
238
- >
239
- {column.kind === "selection" ? (
240
- <SelectionCheckbox
241
- checked={allSelected}
242
- indeterminate={someSelected}
243
- label="Select all rows"
244
- onChange={toggleAll}
245
- />
246
- ) : column.header ? (
247
- column.sortable ? (
248
- onSortChange ? (
249
- <Button
250
- aria-label={`Sort by ${column.header}`}
251
- className={cx(
252
- "ds-table__sort",
253
- sort?.columnId === column.id && `ds-table__sort--${sort.direction}`,
254
- )}
255
- onClick={() => onSortChange(getNextSort(sort, column.id))}
256
- size="xs"
257
- type="button"
258
- variant="ghost"
259
- >
260
- <span>{column.header}</span>
261
- <SortIcon direction={sort?.columnId === column.id ? sort.direction : undefined} />
262
- </Button>
263
- ) : (
264
- <span className="ds-table__sort ds-table__sort--static">
265
- <span>{column.header}</span>
266
- <SortIcon />
267
- </span>
268
- )
269
- ) : (
270
- column.header
271
- )
272
- ) : (
273
- <span className="sr-only">
274
- {column.kind === "actions" ? "Actions" : column.id}
275
- </span>
276
- )}
277
- </th>
278
- ))}
279
- </tr>
280
- </thead>
281
- <tbody>
282
- {rows.map((row) => {
283
- const selected = selectedRowIds.includes(row.id);
284
- const state = row.state === "disabled" ? "disabled" : selected ? "selected" : row.state ?? "default";
285
-
286
- return (
287
- <tr aria-selected={selected || undefined} className={`ds-table__row ds-table__row--${state}`} key={row.id}>
288
- {columns.map((column) => (
289
- <td className={columnClass(column)} key={column.id} style={columnStyle(column)}>
290
- {column.kind === "selection" ? (
291
- <SelectionCheckbox
292
- checked={selected}
293
- disabled={row.state === "disabled"}
294
- label={`Select row ${row.id}`}
295
- onChange={(checked) => toggleRow(row, checked)}
296
- />
297
- ) : (
298
- renderTableCell(column, row, size)
299
- )}
300
- </td>
301
- ))}
302
- </tr>
303
- );
304
- })}
305
- </tbody>
306
- </table>
307
- </div>
308
- );
309
- }
310
-
311
- function renderTableCell<Row extends TableRowBase>(
312
- column: TableColumn<Row>,
313
- row: Row,
314
- size: TableSize,
315
- ) {
316
- if (column.renderCell) return column.renderCell(row);
317
- if (column.item) return renderTableItem(column.item(row), size);
318
- if ((column.kind ?? "data") !== "data") return null;
319
- if (!Object.prototype.hasOwnProperty.call(row, column.id)) return null;
320
-
321
- const value: unknown = Reflect.get(row, column.id);
322
- if (typeof value !== "string" && typeof value !== "number") return null;
323
-
324
- return renderTableItem({ type: "text", value: String(value) }, size);
325
- }
326
-
327
- const defaultDataTablePagination: DataTablePagination = {};
328
-
329
- const defaultDataTableLabels: DataTableLabels = {
330
- emptyDescription: "Try adjusting the search or filters.",
331
- emptyTitle: "No rows found",
332
- errorDescription: "Refresh the table or try again later.",
333
- errorTitle: "Table unavailable",
334
- loadingDescription: "Please wait while the table updates.",
335
- loadingTitle: "Loading rows",
336
- resultsSummary: ({ from, to, total }) => (total === 0 ? "Showing 0 rows" : `Showing ${from} to ${to} of ${total} rows`),
337
- searchPlaceholder: "Search rows...",
338
- selectedCount: (count) => `${count} selected`,
339
- };
340
-
341
- export function DataTable<Row extends TableRowBase>({
342
- actions = [],
343
- bulkActions = [],
344
- className,
345
- columns,
346
- defaultSort = null,
347
- description,
348
- dir,
349
- emptyState,
350
- error = false,
351
- filters = [],
352
- getRowSearchText,
353
- labels: labelOverrides,
354
- loading = false,
355
- onSelectedRowIdsChange,
356
- onSortChange,
357
- pagination = defaultDataTablePagination,
358
- rows,
359
- search = true,
360
- selectedRowIds = [],
361
- size = "sm",
362
- sort,
363
- title,
364
- }: DataTableProps<Row>) {
365
- const labels: DataTableLabels = { ...defaultDataTableLabels, ...labelOverrides };
366
- const searchConfig = search === true ? {} : search && typeof search === "object" ? search : null;
367
- const paginationConfig = pagination === false ? null : pagination;
368
- const [uncontrolledSearch, setUncontrolledSearch] = useState(() => searchConfig?.defaultValue ?? "");
369
- const [uncontrolledSort, setUncontrolledSort] = useState<TableSortState>(() => sort ?? defaultSort);
370
- const [uncontrolledPage, setUncontrolledPage] = useState(() => paginationConfig?.page ?? paginationConfig?.defaultPage ?? 1);
371
- const selectedSort = sort !== undefined ? sort : uncontrolledSort;
372
- const searchValue = searchConfig?.value ?? uncontrolledSearch;
373
- const normalizedSearchValue = searchValue.trim().toLowerCase();
374
-
375
- const processedRows = useMemo(() => {
376
- let nextRows = rows;
377
-
378
- if (normalizedSearchValue) {
379
- nextRows = nextRows.filter((row) =>
380
- getDataTableSearchText({
381
- columns,
382
- getRowSearchText: getRowSearchText ?? searchConfig?.getRowText,
383
- row,
384
- })
385
- .toLowerCase()
386
- .includes(normalizedSearchValue),
387
- );
388
- }
389
-
390
- for (const filter of filters) {
391
- if (filter.active && filter.predicate) {
392
- nextRows = nextRows.filter(filter.predicate);
393
- }
394
- }
395
-
396
- return sortDataTableRows(nextRows, columns, selectedSort);
397
- }, [columns, filters, getRowSearchText, normalizedSearchValue, rows, searchConfig?.getRowText, selectedSort]);
398
-
399
- const pageSize = Math.max(1, paginationConfig?.pageSize ?? 10);
400
- const totalRows = paginationConfig?.totalRows ?? processedRows.length;
401
- const pageCount = Math.max(1, Math.ceil(Math.max(totalRows, 0) / pageSize));
402
- const currentPage = paginationConfig ? clampNumber(paginationConfig.page ?? uncontrolledPage, 1, pageCount) : 1;
403
- const startIndex = (currentPage - 1) * pageSize;
404
- const displayedRows = paginationConfig?.manual ? processedRows : paginationConfig ? processedRows.slice(startIndex, startIndex + pageSize) : processedRows;
405
- const visibleFrom = totalRows === 0 ? 0 : startIndex + 1;
406
- const visibleTo = paginationConfig?.manual ? Math.min(currentPage * pageSize, totalRows) : Math.min(startIndex + displayedRows.length, totalRows);
407
- const resultsSummary = paginationConfig
408
- ? labels.resultsSummary({ from: visibleFrom, to: visibleTo, total: totalRows })
409
- : null;
410
- const hasHeader = Boolean(title || description || actions.length > 0);
411
- const hasToolbar = Boolean(searchConfig || filters.length > 0 || selectedRowIds.length > 0 || bulkActions.length > 0);
412
- const activeBulkActions = selectedRowIds.length > 0 ? bulkActions : [];
413
- const state = getDataTableState({
414
- emptyState,
415
- error,
416
- labels,
417
- loading,
418
- rowCount: processedRows.length,
419
- });
420
-
421
- function setDataTablePage(nextPage: number) {
422
- if (!paginationConfig) return;
423
-
424
- const safePage = clampNumber(nextPage, 1, pageCount);
425
- if (paginationConfig.page === undefined) {
426
- setUncontrolledPage(safePage);
427
- }
428
- paginationConfig.onPageChange?.(safePage);
429
- }
430
-
431
- function resetPagination() {
432
- setDataTablePage(1);
433
- }
434
-
435
- function handleSearchChange(value: string) {
436
- if (searchConfig?.value === undefined) {
437
- setUncontrolledSearch(value);
438
- }
439
- searchConfig?.onValueChange?.(value);
440
- resetPagination();
441
- }
442
-
443
- function handleSortChange(nextSort: TableSortState) {
444
- if (sort === undefined) {
445
- setUncontrolledSort(nextSort);
446
- }
447
- onSortChange?.(nextSort);
448
- resetPagination();
449
- }
450
-
451
- return (
452
- <section className={cx("ds-data-table", className)} dir={dir}>
453
- {hasHeader && (
454
- <header className="ds-data-table__header">
455
- <div className="ds-data-table__heading">
456
- {title && <h2>{title}</h2>}
457
- {description && <p>{description}</p>}
458
- </div>
459
- {actions.length > 0 && (
460
- <div className="ds-data-table__actions">
461
- {actions.map((action) => (
462
- <DataTableActionButton action={action} key={action.label} />
463
- ))}
464
- </div>
465
- )}
466
- </header>
467
- )}
468
-
469
- {hasToolbar && (
470
- <Toolbar className="ds-data-table__toolbar" dir={dir} layout="split" wrap>
471
- <ToolbarSection grow wrap>
472
- {searchConfig && (
473
- <ToolbarSearch
474
- aria-label={searchConfig.label ?? "Search table rows"}
475
- disabled={searchConfig.disabled}
476
- onChange={(event) => handleSearchChange(event.currentTarget.value)}
477
- placeholder={searchConfig.placeholder ?? labels.searchPlaceholder}
478
- value={searchValue}
479
- width="full"
480
- />
481
- )}
482
- {filters.map((filter) => (
483
- <ToolbarButton
484
- aria-pressed={filter.active || undefined}
485
- key={filter.id}
486
- onClick={() => {
487
- filter.onAction?.();
488
- resetPagination();
489
- }}
490
- size="sm"
491
- variant={filter.active ? "default" : "soft"}
492
- >
493
- {filter.value ? `${filter.label}: ${filter.value}` : filter.label}
494
- </ToolbarButton>
495
- ))}
496
- </ToolbarSection>
497
- {(selectedRowIds.length > 0 || activeBulkActions.length > 0) && (
498
- <ToolbarSection align="end" wrap>
499
- {selectedRowIds.length > 0 && <ToolbarBadge variant="brand">{labels.selectedCount(selectedRowIds.length)}</ToolbarBadge>}
500
- {activeBulkActions.map((action) => (
501
- <DataTableActionButton action={action} key={action.label} />
502
- ))}
503
- </ToolbarSection>
504
- )}
505
- </Toolbar>
506
- )}
507
-
508
- <div className="ds-data-table__body">
509
- <Table
510
- columns={columns}
511
- dir={dir}
512
- onSelectedRowIdsChange={onSelectedRowIdsChange}
513
- onSortChange={handleSortChange}
514
- rows={state ? [] : displayedRows}
515
- selectedRowIds={selectedRowIds}
516
- size={size}
517
- sort={selectedSort}
518
- />
519
- {state && <DataTableStateView loading={loading} state={state} />}
520
- </div>
521
-
522
- {paginationConfig && (
523
- <footer className="ds-data-table__footer">
524
- <ToolbarText>{resultsSummary}</ToolbarText>
525
- <Pagination
526
- aria-label={
527
- paginationConfig.ariaLabel ??
528
- paginationConfig.messages?.navigation ??
529
- (typeof title === "string" ? `${title} pagination` : "Table pagination")
530
- }
531
- dir={dir}
532
- messages={paginationConfig.messages}
533
- nextDisabled={currentPage >= pageCount}
534
- onPageChange={setDataTablePage}
535
- page={currentPage}
536
- pages={paginationConfig.pages ?? getDataTablePaginationPages(currentPage, pageCount)}
537
- previousDisabled={currentPage <= 1}
538
- caption={resultsSummary}
539
- showCaption={paginationConfig.showCaption ?? false}
540
- summary={resultsSummary}
541
- type={paginationConfig.type ?? "numeric"}
542
- variant={paginationConfig.variant ?? "soft"}
543
- />
544
- </footer>
545
- )}
546
- </section>
547
- );
548
- }
549
-
550
- function columnClass<Row extends TableRowBase>(column: TableColumn<Row>) {
551
- return cx(
552
- "ds-table__cell",
553
- column.width === "fill" && "ds-table__cell--fill",
554
- column.align && `ds-table__cell--${column.align}`,
555
- column.kind === "selection" && "ds-table__cell--selection",
556
- column.kind === "actions" && "ds-table__cell--actions",
557
- );
558
- }
559
-
560
- function columnStyle<Row extends TableRowBase>(column: TableColumn<Row>): CSSProperties {
561
- return {
562
- minWidth: column.minWidth,
563
- width: typeof column.width === "number" ? column.width : undefined,
564
- };
565
- }
566
-
567
- type ResolvedDataTableState = DataTableState & {
568
- tone: "danger" | "default" | "loading";
569
- };
570
-
571
- function DataTableActionButton({ action }: { action: DataTableAction }) {
572
- return (
573
- <ToolbarButton
574
- aria-label={action.ariaLabel}
575
- disabled={action.disabled}
576
- onClick={action.onAction}
577
- size="sm"
578
- variant={action.variant ?? "soft"}
579
- >
580
- {action.label}
581
- </ToolbarButton>
582
- );
583
- }
584
-
585
- function DataTableStateView({ loading, state }: { loading: boolean; state: ResolvedDataTableState }) {
586
- return (
587
- <div className={`ds-data-table__state ds-data-table__state--${state.tone}`} role={state.tone === "danger" ? "alert" : "status"}>
588
- <div className="ds-data-table__state-copy">
589
- {loading && <Spinner className="ds-data-table__spinner" />}
590
- {state.title && <strong>{state.title}</strong>}
591
- {state.description && <p>{state.description}</p>}
592
- </div>
593
- {state.action && (
594
- <div className="ds-data-table__state-action">
595
- <DataTableActionButton action={state.action} />
596
- </div>
597
- )}
598
- </div>
599
- );
600
- }
601
-
602
- function getDataTableState({
603
- emptyState,
604
- error,
605
- labels,
606
- loading,
607
- rowCount,
608
- }: {
609
- emptyState?: DataTableState;
610
- error: boolean | string | DataTableState;
611
- labels: DataTableLabels;
612
- loading: boolean;
613
- rowCount: number;
614
- }): ResolvedDataTableState | null {
615
- if (loading) {
616
- return {
617
- description: labels.loadingDescription,
618
- title: labels.loadingTitle,
619
- tone: "loading",
620
- };
621
- }
622
-
623
- if (error) {
624
- if (typeof error === "string") {
625
- return {
626
- description: error,
627
- title: labels.errorTitle,
628
- tone: "danger",
629
- };
630
- }
631
-
632
- if (typeof error === "object") {
633
- return {
634
- action: error.action,
635
- description: error.description ?? labels.errorDescription,
636
- title: error.title ?? labels.errorTitle,
637
- tone: "danger",
638
- };
639
- }
640
-
641
- return {
642
- description: labels.errorDescription,
643
- title: labels.errorTitle,
644
- tone: "danger",
645
- };
646
- }
647
-
648
- if (rowCount === 0) {
649
- return {
650
- action: emptyState?.action,
651
- description: emptyState?.description ?? labels.emptyDescription,
652
- title: emptyState?.title ?? labels.emptyTitle,
653
- tone: "default",
654
- };
655
- }
656
-
657
- return null;
658
- }
659
-
660
- function getDataTableSearchText<Row extends TableRowBase>({
661
- columns,
662
- getRowSearchText,
663
- row,
664
- }: {
665
- columns: TableColumn<Row>[];
666
- getRowSearchText?: (row: Row) => string;
667
- row: Row;
668
- }) {
669
- const values = [row.id, getRowSearchText?.(row)];
670
-
671
- for (const column of columns) {
672
- const columnValue = column.searchValue?.(row) ?? (column.item ? getTableItemText(column.item(row)) : undefined);
673
- if (columnValue) values.push(columnValue);
674
- }
675
-
676
- return values.filter(Boolean).join(" ");
677
- }
678
-
679
- function sortDataTableRows<Row extends TableRowBase>(rows: Row[], columns: TableColumn<Row>[], sort: TableSortState) {
680
- if (!sort) return rows;
681
-
682
- const column = columns.find((item) => item.id === sort.columnId);
683
- if (!column) return rows;
684
-
685
- const direction = sort.direction === "asc" ? 1 : -1;
686
-
687
- return [...rows].sort((left, right) => {
688
- const result = compareTableSortValues(getColumnSortValue(column, left), getColumnSortValue(column, right));
689
- return result * direction;
690
- });
691
- }
692
-
693
- function getColumnSortValue<Row extends TableRowBase>(column: TableColumn<Row>, row: Row): TableSortValue {
694
- if (column.sortValue) return column.sortValue(row);
695
- if (column.item) return getTableItemSortValue(column.item(row));
696
- return undefined;
697
- }
698
-
699
- function compareTableSortValues(left: TableSortValue, right: TableSortValue) {
700
- if (left === right) return 0;
701
- if (left === null || left === undefined) return 1;
702
- if (right === null || right === undefined) return -1;
703
-
704
- const normalizedLeft = normalizeTableSortValue(left);
705
- const normalizedRight = normalizeTableSortValue(right);
706
-
707
- if (typeof normalizedLeft === "number" && typeof normalizedRight === "number") {
708
- return normalizedLeft - normalizedRight;
709
- }
710
-
711
- return String(normalizedLeft).localeCompare(String(normalizedRight), undefined, {
712
- numeric: true,
713
- sensitivity: "base",
714
- });
715
- }
716
-
717
- function normalizeTableSortValue(value: Exclude<TableSortValue, null | undefined>) {
718
- if (value instanceof Date) return value.getTime();
719
- if (typeof value === "boolean") return value ? 1 : 0;
720
- return value;
721
- }
722
-
723
- function getTableItemSortValue(item: TableItem): TableSortValue {
724
- switch (item.type) {
725
- case "avatarText":
726
- return item.name;
727
- case "badges":
728
- return item.items.map((badge) => badge.label).join(" ");
729
- case "brandLogoText":
730
- return item.label;
731
- case "file":
732
- return item.label;
733
- case "image":
734
- return item.label;
735
- case "payment":
736
- return item.label ?? item.brand;
737
- case "progress":
738
- case "rating":
739
- return item.value;
740
- case "tags":
741
- return item.items.join(" ");
742
- case "text":
743
- return item.value;
744
- case "toggle":
745
- return item.checked;
746
- default:
747
- return undefined;
748
- }
749
- }
750
-
751
- function getTableItemText(item: TableItem) {
752
- const value = getTableItemSortValue(item);
753
- return value === null || value === undefined ? "" : String(value);
754
- }
755
-
756
- function getDataTablePaginationPages(currentPage: number, pageCount: number): PaginationPage[] {
757
- if (pageCount <= 5) {
758
- return Array.from({ length: pageCount }, (_, index) => index + 1);
759
- }
760
-
761
- const pages: PaginationPage[] = [1];
762
- const start = Math.max(2, currentPage - 1);
763
- const end = Math.min(pageCount - 1, currentPage + 1);
764
-
765
- if (start > 2) pages.push("ellipsis");
766
- for (let page = start; page <= end; page += 1) {
767
- pages.push(page);
768
- }
769
- if (end < pageCount - 1) pages.push("ellipsis");
770
- pages.push(pageCount);
771
-
772
- return pages;
773
- }
774
-
775
- function getAriaSort(sort: TableSortState, columnId: string) {
776
- if (sort?.columnId !== columnId) return "none";
777
- return sort.direction === "asc" ? "ascending" : "descending";
778
- }
779
-
780
- function getNextSort(sort: TableSortState, columnId: string): TableSortState {
781
- if (sort?.columnId !== columnId) {
782
- return { columnId, direction: "asc" };
783
- }
784
-
785
- return {
786
- columnId,
787
- direction: sort.direction === "asc" ? "desc" : "asc",
788
- };
789
- }
790
-
791
- function clampNumber(value: number, min: number, max: number) {
792
- if (!Number.isFinite(value)) return min;
793
- return Math.min(max, Math.max(min, Math.floor(value)));
794
- }
795
-
796
- function renderTableItem(item: TableItem, tableSize: TableSize) {
797
- switch (item.type) {
798
- case "checkbox":
799
- return (
800
- <SelectionCheckbox
801
- checked={item.checked}
802
- disabled={item.disabled ?? !item.onCheckedChange}
803
- indeterminate={item.indeterminate}
804
- label={item.ariaLabel ?? "Select row"}
805
- onChange={item.onCheckedChange}
806
- />
807
- );
808
- case "text":
809
- return <span className={`ds-table-item-text ds-table-item-text--${item.tone ?? "default"} ds-table-item-text--${item.weight ?? "regular"}`}>{item.value}</span>;
810
- case "avatarText":
811
- return (
812
- <span className="ds-table-avatar-text">
813
- <Avatar name={item.name} size={tableSize === "lg" ? "md" : "sm"} src={item.src} />
814
- <span>
815
- <strong dir="auto">{item.name}</strong>
816
- {item.caption && <em dir="auto">{item.caption}</em>}
817
- </span>
818
- </span>
819
- );
820
- case "rating":
821
- return <Rating value={item.value} max={item.max ?? 5} />;
822
- case "progress":
823
- return <Progress value={item.value} label={item.label} tone={item.tone ?? "success"} />;
824
- case "toggle":
825
- return (
826
- <Toggle
827
- aria-label={item.ariaLabel ?? "Toggle row value"}
828
- checked={item.checked}
829
- disabled={item.disabled ?? !item.onCheckedChange}
830
- onCheckedChange={item.onCheckedChange}
831
- size="sm"
832
- />
833
- );
834
- case "badges":
835
- return <BadgeList items={item.items} maxVisible={item.maxVisible} />;
836
- case "onlyAvatar":
837
- return <Avatar name={item.alt} size={tableSize === "lg" ? "md" : "xs"} src={item.src} />;
838
- case "avatarStack":
839
- return (
840
- <AvatarStack
841
- addLabel={item.addLabel}
842
- avatars={item.avatars}
843
- canAdd={item.canAdd}
844
- count={item.count}
845
- onAdd={item.onAdd}
846
- size={tableSize}
847
- />
848
- );
849
- case "tags":
850
- return <TagList items={item.items} maxVisible={item.maxVisible} />;
851
- case "file":
852
- return item.label ? <IconLabel icon={<FileIcon />} label={item.label} /> : null;
853
- case "brandLogoText":
854
- return <IconLabel icon={item.logoSrc ? <img alt="" src={item.logoSrc} /> : <BrandIcon />} label={item.label} />;
855
- case "payment":
856
- return <IconLabel icon={<PaymentIcon />} label={item.label ?? item.brand} />;
857
- case "image":
858
- return <IconLabel icon={<img alt="" src={item.src} />} label={item.label} />;
859
- case "action":
860
- return <IconButton icon={item.icon} label={item.label} onClick={item.onAction} />;
861
- case "actionGroup":
862
- return (
863
- <span className="ds-table-action-group">
864
- {item.actions.map((action) => (
865
- <IconButton disabled={action.disabled} key={action.label} icon={action.icon} label={action.label} onClick={action.onAction} />
866
- ))}
867
- </span>
868
- );
869
- default:
870
- return null;
871
- }
872
- }
873
-
874
- function SelectionCheckbox({
875
- checked,
876
- disabled = false,
877
- indeterminate = false,
878
- label,
879
- onChange,
880
- }: {
881
- checked: boolean;
882
- disabled?: boolean;
883
- indeterminate?: boolean;
884
- label: string;
885
- onChange?: (checked: boolean) => void;
886
- }) {
887
- return (
888
- <Checkbox
889
- aria-label={label}
890
- checked={indeterminate ? "indeterminate" : checked}
891
- disabled={disabled}
892
- onCheckedChange={(nextChecked) => onChange?.(nextChecked === true)}
893
- />
894
- );
895
- }
896
-
897
- type TableAvatarSize = "xs" | "sm" | "md";
898
-
899
- function Avatar({ name, size, src }: { name: string; size: TableAvatarSize; src?: string }) {
900
- const initials = name
901
- .split(" ")
902
- .map((part) => part[0])
903
- .slice(0, 2)
904
- .join("")
905
- .toUpperCase();
906
-
907
- return (
908
- <AvatarPrimitive className="ds-table-avatar" size={size}>
909
- {src ? <AvatarImage alt="" src={src} /> : null}
910
- <AvatarFallback size={size}>{initials}</AvatarFallback>
911
- </AvatarPrimitive>
912
- );
913
- }
914
-
915
- function AvatarStack({
916
- addLabel = "Add person",
917
- avatars,
918
- canAdd,
919
- count,
920
- onAdd,
921
- size,
922
- }: {
923
- addLabel?: string;
924
- avatars: AvatarItem[];
925
- canAdd?: boolean;
926
- count?: number;
927
- onAdd?: () => void;
928
- size: TableSize;
929
- }) {
930
- return (
931
- <span className={`ds-table-avatar-stack ds-table-avatar-stack--${size}`}>
932
- {avatars.slice(0, 3).map((avatar) => (
933
- <Avatar key={avatar.alt} name={avatar.alt} size={size === "lg" ? "md" : "xs"} src={avatar.src} />
934
- ))}
935
- {count ? <span className="ds-table-avatar ds-table-avatar--count">+{count}</span> : null}
936
- {canAdd ? (
937
- <Button aria-label={addLabel} disabled={!onAdd} onClick={onAdd} size="icon-xs" type="button" variant="ghost">
938
- +
939
- </Button>
940
- ) : null}
941
- </span>
942
- );
943
- }
944
-
945
- function BadgeList({ items, maxVisible = items.length }: { items: BadgeItem[]; maxVisible?: number }) {
946
- const visible = items.slice(0, maxVisible);
947
- const overflow = items.length - visible.length;
948
-
949
- return (
950
- <span className="ds-table-badges">
951
- {visible.map((item, index) => (
952
- <Badge
953
- badgeStyle="light"
954
- color={badgeColor(item.tone)}
955
- key={`${item.label}-${index}`}
956
- size="xs"
957
- type={item.dot ? "dot" : "default"}
958
- >
959
- {item.label}
960
- </Badge>
961
- ))}
962
- {overflow > 0 && <Badge size="xs">+{overflow}</Badge>}
963
- </span>
964
- );
965
- }
966
-
967
- function TagList({ items, maxVisible = items.length }: { items: string[]; maxVisible?: number }) {
968
- const visible = items.slice(0, maxVisible);
969
- const overflow = items.length - visible.length;
970
-
971
- return (
972
- <span className="ds-table-tags">
973
- {visible.map((item, index) => (
974
- <Tag key={`${item}-${index}`} size="xs">{item}</Tag>
975
- ))}
976
- {overflow > 0 && <Tag size="xs">+{overflow}</Tag>}
977
- </span>
978
- );
979
- }
980
-
981
- function Progress({ label, tone, value }: { label?: string; tone: "success" | "brand"; value: number }) {
982
- return (
983
- <ProgressPrimitive
984
- aria-label={label ?? "Progress"}
985
- className={`ds-table-progress ds-table-progress--${tone}`}
986
- indicator={label}
987
- labelPosition="inline"
988
- size="lg"
989
- value={value}
990
- />
991
- );
992
- }
993
-
994
- function Rating({ max, value }: { max: number; value: number }) {
995
- return (
996
- <span
997
- aria-label={`${value} out of ${max}`}
998
- className="ds-table-rating"
999
- role="img"
1000
- >
1001
- {Array.from({ length: max }, (_, index) => (
1002
- <StarIcon
1003
- aria-hidden="true"
1004
- className={index < value ? "is-filled" : undefined}
1005
- key={index}
1006
- strokeWidth="var(--bh-icon-stroke-190)"
1007
- />
1008
- ))}
1009
- </span>
1010
- );
1011
- }
1012
-
1013
- function IconLabel({ icon, label }: { icon: ReactNode; label: string }) {
1014
- return (
1015
- <span className="ds-table-icon-label">
1016
- <span aria-hidden="true">{icon}</span>
1017
- <span>{label}</span>
1018
- </span>
1019
- );
1020
- }
1021
-
1022
- function IconButton({
1023
- disabled,
1024
- icon,
1025
- label,
1026
- onClick,
1027
- }: {
1028
- disabled?: boolean;
1029
- icon: TableAction["icon"];
1030
- label: string;
1031
- onClick?: () => void;
1032
- }) {
1033
- return (
1034
- <Button aria-label={label} className="ds-table-icon-button" disabled={disabled ?? !onClick} onClick={onClick} size="icon-xs" type="button" variant="ghost">
1035
- {renderActionIcon(icon)}
1036
- </Button>
1037
- );
1038
- }
1039
-
1040
- function badgeColor(tone: BadgeItem["tone"]) {
1041
- if (tone === "success") return "green" as const;
1042
- if (tone === "warning") return "amber" as const;
1043
- return tone ?? "neutral";
1044
- }
1045
-
1046
- function renderActionIcon(icon: TableAction["icon"]) {
1047
- switch (icon) {
1048
- case "view":
1049
- return <ViewIcon />;
1050
- case "edit":
1051
- return <EditIcon />;
1052
- case "delete":
1053
- return <DeleteIcon />;
1054
- case "more":
1055
- default:
1056
- return <MoreIcon />;
1057
- }
1058
- }
1059
-
1060
- function SortIcon({ direction }: { direction?: TableSortDirection }) {
1061
- return (
1062
- <ArrowUpDownIcon aria-hidden="true" data-direction={direction ?? "none"} strokeWidth="var(--bh-icon-stroke-190)" />
1063
- );
1064
- }
1065
-
1066
- function MoreIcon() {
1067
- return <EllipsisIcon aria-hidden="true" strokeWidth="var(--bh-icon-stroke-225)" />;
1068
- }
1069
-
1070
- function ViewIcon() {
1071
- return <EyeIcon aria-hidden="true" strokeWidth="var(--bh-icon-stroke-190)" />;
1072
- }
1073
-
1074
- function EditIcon() {
1075
- return <LucideEditIcon aria-hidden="true" strokeWidth="var(--bh-icon-stroke-190)" />;
1076
- }
1077
-
1078
- function DeleteIcon() {
1079
- return <Trash2Icon aria-hidden="true" strokeWidth="var(--bh-icon-stroke-190)" />;
1080
- }
1081
-
1082
- function FileIcon() {
1083
- return <LucideFileIcon strokeWidth="var(--bh-icon-stroke-190)" />;
1084
- }
1085
-
1086
- function BrandIcon() {
1087
- return <span className="ds-table-brand-icon" />;
1088
- }
1089
-
1090
- function PaymentIcon() {
1091
- return <span className="ds-table-payment-icon" />;
1092
- }
1
+ import { useMemo, useState, type CSSProperties, type ReactNode } from "react";
2
+ import {
3
+ ArrowUpDownIcon,
4
+ EditIcon as LucideEditIcon,
5
+ EllipsisIcon,
6
+ EyeIcon,
7
+ FileIcon as LucideFileIcon,
8
+ StarIcon,
9
+ Trash2Icon,
10
+ } from "lucide-react";
11
+ import { Pagination, type PaginationPage, type PaginationProps } from "@/components/ui/pagination";
12
+ import { Spinner } from "@/components/ui/spinner";
13
+ import { Avatar as AvatarPrimitive, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
14
+ import { Badge } from "@/components/ui/badge";
15
+ import { Button } from "@/components/ui/button";
16
+ import { Checkbox } from "@/components/ui/checkbox";
17
+ import { Progress as ProgressPrimitive } from "@/components/ui/progress";
18
+ import { Tag } from "@/components/ui/tag";
19
+ import { Toggle } from "@/components/ui/toggle";
20
+ import {
21
+ Toolbar,
22
+ ToolbarBadge,
23
+ ToolbarButton,
24
+ ToolbarSearch,
25
+ ToolbarSection,
26
+ ToolbarText,
27
+ type ToolbarButtonProps,
28
+ } from "@/components/ui/toolbar";
29
+ import "./table.css";
30
+
31
+ export type TableDirection = "ltr" | "rtl";
32
+ export type TableSize = "sm" | "lg" | "comfortable";
33
+ export type TableRowState = "default" | "selected" | "disabled";
34
+ export type TableSortDirection = "asc" | "desc";
35
+ export type TableSortValue = string | number | boolean | Date | null | undefined;
36
+ export type TableSortState = {
37
+ columnId: string;
38
+ direction: TableSortDirection;
39
+ } | null;
40
+
41
+ export type TableRowBase = {
42
+ id: string;
43
+ state?: TableRowState;
44
+ };
45
+
46
+ export type TableColumn<Row extends TableRowBase> = {
47
+ align?: "start" | "center" | "end";
48
+ header?: string;
49
+ id: string;
50
+ item?: TableItemFactory<Row>;
51
+ kind?: "data" | "selection" | "actions";
52
+ minWidth?: number;
53
+ renderCell?: (row: Row) => ReactNode;
54
+ searchValue?: (row: Row) => string;
55
+ sortable?: boolean;
56
+ sortValue?: (row: Row) => TableSortValue;
57
+ width?: number | "fill";
58
+ };
59
+
60
+ export type TableItemFactory<Row extends TableRowBase> = (row: Row) => TableItem;
61
+
62
+ export type BadgeItem = {
63
+ dot?: boolean;
64
+ label: string;
65
+ tone?: "blue" | "fuchsia" | "amber" | "neutral" | "success" | "warning" | "danger";
66
+ };
67
+
68
+ export type AvatarItem = {
69
+ alt: string;
70
+ src?: string;
71
+ };
72
+
73
+ export type TableAction = {
74
+ disabled?: boolean;
75
+ icon: "view" | "edit" | "delete" | "more";
76
+ label: string;
77
+ onAction?: () => void;
78
+ };
79
+
80
+ export type TableItem =
81
+ | { type: "checkbox"; ariaLabel?: string; checked: boolean; disabled?: boolean; indeterminate?: boolean; onCheckedChange?: (checked: boolean) => void }
82
+ | { type: "text"; caption?: string; tone?: "default" | "subtle"; value: string; weight?: "regular" | "medium" }
83
+ | { type: "avatarText"; caption?: string; name: string; src?: string; status?: "top" | "bottom" }
84
+ | { type: "rating"; max?: 5; value: number }
85
+ | { type: "progress"; label?: string; tone?: "success" | "brand"; value: number }
86
+ | { type: "toggle"; ariaLabel?: string; checked: boolean; disabled?: boolean; onCheckedChange?: (checked: boolean) => void }
87
+ | { type: "badges"; items: BadgeItem[]; maxVisible?: number }
88
+ | { type: "onlyAvatar"; alt: string; src?: string }
89
+ | { type: "avatarStack"; addLabel?: string; avatars: AvatarItem[]; canAdd?: boolean; count?: number; onAdd?: () => void }
90
+ | { type: "tags"; items: string[]; maxVisible?: number }
91
+ | { type: "file"; label?: string }
92
+ | { type: "brandLogoText"; caption?: string; label: string; logoSrc?: string }
93
+ | { type: "payment"; brand: string; caption?: string; label?: string }
94
+ | { type: "image"; caption?: string; label: string; src: string }
95
+ | { type: "action"; icon: TableAction["icon"]; label: string; onAction?: () => void }
96
+ | { type: "actionGroup"; actions: TableAction[] };
97
+
98
+ export type TableProps<Row extends TableRowBase> = {
99
+ columns: TableColumn<Row>[];
100
+ dir?: TableDirection;
101
+ onSelectedRowIdsChange?: (rowIds: string[]) => void;
102
+ onSortChange?: (sort: TableSortState) => void;
103
+ rows: Row[];
104
+ selectedRowIds?: string[];
105
+ size?: TableSize;
106
+ sort?: TableSortState;
107
+ };
108
+
109
+ export type DataTableAction = {
110
+ ariaLabel?: string;
111
+ disabled?: boolean;
112
+ label: string;
113
+ onAction?: () => void;
114
+ variant?: ToolbarButtonProps["variant"];
115
+ };
116
+
117
+ export type DataTableFilter<Row extends TableRowBase> = {
118
+ active?: boolean;
119
+ id: string;
120
+ label: string;
121
+ onAction?: () => void;
122
+ predicate?: (row: Row) => boolean;
123
+ value?: string;
124
+ };
125
+
126
+ export type DataTableSearch<Row extends TableRowBase> =
127
+ | boolean
128
+ | {
129
+ defaultValue?: string;
130
+ disabled?: boolean;
131
+ getRowText?: (row: Row) => string;
132
+ label?: string;
133
+ onValueChange?: (value: string) => void;
134
+ placeholder?: string;
135
+ value?: string;
136
+ };
137
+
138
+ export type DataTablePagination = {
139
+ ariaLabel?: string;
140
+ defaultPage?: number;
141
+ manual?: boolean;
142
+ messages?: PaginationProps["messages"];
143
+ onPageChange?: (page: number) => void;
144
+ page?: number;
145
+ pageSize?: number;
146
+ pages?: PaginationPage[];
147
+ showCaption?: boolean;
148
+ totalRows?: number;
149
+ type?: PaginationProps["type"];
150
+ variant?: PaginationProps["variant"];
151
+ };
152
+
153
+ export type DataTableState = {
154
+ action?: DataTableAction;
155
+ description?: ReactNode;
156
+ title?: ReactNode;
157
+ };
158
+
159
+ export type DataTableLabels = {
160
+ emptyDescription: ReactNode;
161
+ emptyTitle: ReactNode;
162
+ errorDescription: ReactNode;
163
+ errorTitle: ReactNode;
164
+ loadingDescription: ReactNode;
165
+ loadingTitle: ReactNode;
166
+ resultsSummary: (summary: { from: number; to: number; total: number }) => ReactNode;
167
+ searchPlaceholder: string;
168
+ selectedCount: (count: number) => ReactNode;
169
+ };
170
+
171
+ export type DataTableProps<Row extends TableRowBase> = Omit<TableProps<Row>, "onSortChange" | "rows" | "sort"> & {
172
+ actions?: DataTableAction[];
173
+ bulkActions?: DataTableAction[];
174
+ className?: string;
175
+ defaultSort?: TableSortState;
176
+ description?: ReactNode;
177
+ emptyState?: DataTableState;
178
+ error?: boolean | string | DataTableState;
179
+ filters?: DataTableFilter<Row>[];
180
+ getRowSearchText?: (row: Row) => string;
181
+ labels?: Partial<DataTableLabels>;
182
+ loading?: boolean;
183
+ onSortChange?: (sort: TableSortState) => void;
184
+ pagination?: false | DataTablePagination;
185
+ rows: Row[];
186
+ search?: DataTableSearch<Row>;
187
+ sort?: TableSortState;
188
+ title?: ReactNode;
189
+ };
190
+
191
+ function cx(...classes: Array<string | false | undefined>) {
192
+ return classes.filter(Boolean).join(" ");
193
+ }
194
+
195
+ export function Table<Row extends TableRowBase>({
196
+ columns,
197
+ dir,
198
+ onSelectedRowIdsChange,
199
+ onSortChange,
200
+ rows,
201
+ selectedRowIds = [],
202
+ size = "sm",
203
+ sort = null,
204
+ }: TableProps<Row>) {
205
+ const selectableRows = rows.filter((row) => row.state !== "disabled");
206
+ const allSelected = selectableRows.length > 0 && selectableRows.every((row) => selectedRowIds.includes(row.id));
207
+ const someSelected = selectableRows.some((row) => selectedRowIds.includes(row.id)) && !allSelected;
208
+
209
+ function toggleAll(checked: boolean) {
210
+ onSelectedRowIdsChange?.(checked ? selectableRows.map((row) => row.id) : []);
211
+ }
212
+
213
+ function toggleRow(row: Row, checked: boolean) {
214
+ if (row.state === "disabled") return;
215
+ const next = checked
216
+ ? Array.from(new Set([...selectedRowIds, row.id]))
217
+ : selectedRowIds.filter((rowId) => rowId !== row.id);
218
+ onSelectedRowIdsChange?.(next);
219
+ }
220
+
221
+ return (
222
+ <div
223
+ aria-label="Scrollable table"
224
+ className={`ds-table-wrap ds-table-wrap--${size}`}
225
+ dir={dir}
226
+ tabIndex={0}
227
+ >
228
+ <table className="ds-table">
229
+ <thead>
230
+ <tr>
231
+ {columns.map((column) => (
232
+ <th
233
+ aria-sort={column.sortable ? getAriaSort(sort, column.id) : undefined}
234
+ className={columnClass(column)}
235
+ key={column.id}
236
+ style={columnStyle(column)}
237
+ scope="col"
238
+ >
239
+ {column.kind === "selection" ? (
240
+ <SelectionCheckbox
241
+ checked={allSelected}
242
+ indeterminate={someSelected}
243
+ label="Select all rows"
244
+ onChange={toggleAll}
245
+ />
246
+ ) : column.header ? (
247
+ column.sortable ? (
248
+ onSortChange ? (
249
+ <Button
250
+ aria-label={`Sort by ${column.header}`}
251
+ className={cx(
252
+ "ds-table__sort",
253
+ sort?.columnId === column.id && `ds-table__sort--${sort.direction}`,
254
+ )}
255
+ onClick={() => onSortChange(getNextSort(sort, column.id))}
256
+ size="xs"
257
+ type="button"
258
+ variant="ghost"
259
+ >
260
+ <span>{column.header}</span>
261
+ <SortIcon direction={sort?.columnId === column.id ? sort.direction : undefined} />
262
+ </Button>
263
+ ) : (
264
+ <span className="ds-table__sort ds-table__sort--static">
265
+ <span>{column.header}</span>
266
+ <SortIcon />
267
+ </span>
268
+ )
269
+ ) : (
270
+ column.header
271
+ )
272
+ ) : (
273
+ <span className="sr-only">
274
+ {column.kind === "actions" ? "Actions" : column.id}
275
+ </span>
276
+ )}
277
+ </th>
278
+ ))}
279
+ </tr>
280
+ </thead>
281
+ <tbody>
282
+ {rows.map((row) => {
283
+ const selected = selectedRowIds.includes(row.id);
284
+ const state = row.state === "disabled" ? "disabled" : selected ? "selected" : row.state ?? "default";
285
+
286
+ return (
287
+ <tr aria-selected={selected || undefined} className={`ds-table__row ds-table__row--${state}`} key={row.id}>
288
+ {columns.map((column) => (
289
+ <td className={columnClass(column)} key={column.id} style={columnStyle(column)}>
290
+ {column.kind === "selection" ? (
291
+ <SelectionCheckbox
292
+ checked={selected}
293
+ disabled={row.state === "disabled"}
294
+ label={`Select row ${row.id}`}
295
+ onChange={(checked) => toggleRow(row, checked)}
296
+ />
297
+ ) : (
298
+ renderTableCell(column, row, size === "comfortable" ? "lg" : size)
299
+ )}
300
+ </td>
301
+ ))}
302
+ </tr>
303
+ );
304
+ })}
305
+ </tbody>
306
+ </table>
307
+ </div>
308
+ );
309
+ }
310
+
311
+ function renderTableCell<Row extends TableRowBase>(
312
+ column: TableColumn<Row>,
313
+ row: Row,
314
+ size: TableSize,
315
+ ) {
316
+ if (column.renderCell) return column.renderCell(row);
317
+ if (column.item) return renderTableItem(column.item(row), size);
318
+ if ((column.kind ?? "data") !== "data") return null;
319
+ if (!Object.prototype.hasOwnProperty.call(row, column.id)) return null;
320
+
321
+ const value: unknown = Reflect.get(row, column.id);
322
+ if (typeof value !== "string" && typeof value !== "number") return null;
323
+
324
+ return renderTableItem({ type: "text", value: String(value) }, size);
325
+ }
326
+
327
+ const defaultDataTablePagination: DataTablePagination = {};
328
+
329
+ const defaultDataTableLabels: DataTableLabels = {
330
+ emptyDescription: "Try adjusting the search or filters.",
331
+ emptyTitle: "No rows found",
332
+ errorDescription: "Refresh the table or try again later.",
333
+ errorTitle: "Table unavailable",
334
+ loadingDescription: "Please wait while the table updates.",
335
+ loadingTitle: "Loading rows",
336
+ resultsSummary: ({ from, to, total }) => (total === 0 ? "Showing 0 rows" : `Showing ${from} to ${to} of ${total} rows`),
337
+ searchPlaceholder: "Search rows...",
338
+ selectedCount: (count) => `${count} selected`,
339
+ };
340
+
341
+ export function DataTable<Row extends TableRowBase>({
342
+ actions = [],
343
+ bulkActions = [],
344
+ className,
345
+ columns,
346
+ defaultSort = null,
347
+ description,
348
+ dir,
349
+ emptyState,
350
+ error = false,
351
+ filters = [],
352
+ getRowSearchText,
353
+ labels: labelOverrides,
354
+ loading = false,
355
+ onSelectedRowIdsChange,
356
+ onSortChange,
357
+ pagination = defaultDataTablePagination,
358
+ rows,
359
+ search = true,
360
+ selectedRowIds = [],
361
+ size = "sm",
362
+ sort,
363
+ title,
364
+ }: DataTableProps<Row>) {
365
+ const labels: DataTableLabels = { ...defaultDataTableLabels, ...labelOverrides };
366
+ const searchConfig = search === true ? {} : search && typeof search === "object" ? search : null;
367
+ const paginationConfig = pagination === false ? null : pagination;
368
+ const [uncontrolledSearch, setUncontrolledSearch] = useState(() => searchConfig?.defaultValue ?? "");
369
+ const [uncontrolledSort, setUncontrolledSort] = useState<TableSortState>(() => sort ?? defaultSort);
370
+ const [uncontrolledPage, setUncontrolledPage] = useState(() => paginationConfig?.page ?? paginationConfig?.defaultPage ?? 1);
371
+ const selectedSort = sort !== undefined ? sort : uncontrolledSort;
372
+ const searchValue = searchConfig?.value ?? uncontrolledSearch;
373
+ const normalizedSearchValue = searchValue.trim().toLowerCase();
374
+
375
+ const processedRows = useMemo(() => {
376
+ let nextRows = rows;
377
+
378
+ if (normalizedSearchValue) {
379
+ nextRows = nextRows.filter((row) =>
380
+ getDataTableSearchText({
381
+ columns,
382
+ getRowSearchText: getRowSearchText ?? searchConfig?.getRowText,
383
+ row,
384
+ })
385
+ .toLowerCase()
386
+ .includes(normalizedSearchValue),
387
+ );
388
+ }
389
+
390
+ for (const filter of filters) {
391
+ if (filter.active && filter.predicate) {
392
+ nextRows = nextRows.filter(filter.predicate);
393
+ }
394
+ }
395
+
396
+ return sortDataTableRows(nextRows, columns, selectedSort);
397
+ }, [columns, filters, getRowSearchText, normalizedSearchValue, rows, searchConfig?.getRowText, selectedSort]);
398
+
399
+ const pageSize = Math.max(1, paginationConfig?.pageSize ?? 10);
400
+ const totalRows = paginationConfig?.totalRows ?? processedRows.length;
401
+ const pageCount = Math.max(1, Math.ceil(Math.max(totalRows, 0) / pageSize));
402
+ const currentPage = paginationConfig ? clampNumber(paginationConfig.page ?? uncontrolledPage, 1, pageCount) : 1;
403
+ const startIndex = (currentPage - 1) * pageSize;
404
+ const displayedRows = paginationConfig?.manual ? processedRows : paginationConfig ? processedRows.slice(startIndex, startIndex + pageSize) : processedRows;
405
+ const visibleFrom = totalRows === 0 ? 0 : startIndex + 1;
406
+ const visibleTo = paginationConfig?.manual ? Math.min(currentPage * pageSize, totalRows) : Math.min(startIndex + displayedRows.length, totalRows);
407
+ const resultsSummary = paginationConfig
408
+ ? labels.resultsSummary({ from: visibleFrom, to: visibleTo, total: totalRows })
409
+ : null;
410
+ const hasHeader = Boolean(title || description || actions.length > 0);
411
+ const hasToolbar = Boolean(searchConfig || filters.length > 0 || selectedRowIds.length > 0 || bulkActions.length > 0);
412
+ const activeBulkActions = selectedRowIds.length > 0 ? bulkActions : [];
413
+ const state = getDataTableState({
414
+ emptyState,
415
+ error,
416
+ labels,
417
+ loading,
418
+ rowCount: processedRows.length,
419
+ });
420
+
421
+ function setDataTablePage(nextPage: number) {
422
+ if (!paginationConfig) return;
423
+
424
+ const safePage = clampNumber(nextPage, 1, pageCount);
425
+ if (paginationConfig.page === undefined) {
426
+ setUncontrolledPage(safePage);
427
+ }
428
+ paginationConfig.onPageChange?.(safePage);
429
+ }
430
+
431
+ function resetPagination() {
432
+ setDataTablePage(1);
433
+ }
434
+
435
+ function handleSearchChange(value: string) {
436
+ if (searchConfig?.value === undefined) {
437
+ setUncontrolledSearch(value);
438
+ }
439
+ searchConfig?.onValueChange?.(value);
440
+ resetPagination();
441
+ }
442
+
443
+ function handleSortChange(nextSort: TableSortState) {
444
+ if (sort === undefined) {
445
+ setUncontrolledSort(nextSort);
446
+ }
447
+ onSortChange?.(nextSort);
448
+ resetPagination();
449
+ }
450
+
451
+ return (
452
+ <section className={cx("ds-data-table", className)} dir={dir}>
453
+ {hasHeader && (
454
+ <header className="ds-data-table__header">
455
+ <div className="ds-data-table__heading">
456
+ {title && <h2>{title}</h2>}
457
+ {description && <p>{description}</p>}
458
+ </div>
459
+ {actions.length > 0 && (
460
+ <div className="ds-data-table__actions">
461
+ {actions.map((action) => (
462
+ <DataTableActionButton action={action} key={action.label} />
463
+ ))}
464
+ </div>
465
+ )}
466
+ </header>
467
+ )}
468
+
469
+ {hasToolbar && (
470
+ <Toolbar className="ds-data-table__toolbar" dir={dir} layout="split" wrap>
471
+ <ToolbarSection grow wrap>
472
+ {searchConfig && (
473
+ <ToolbarSearch
474
+ aria-label={searchConfig.label ?? "Search table rows"}
475
+ disabled={searchConfig.disabled}
476
+ onChange={(event) => handleSearchChange(event.currentTarget.value)}
477
+ placeholder={searchConfig.placeholder ?? labels.searchPlaceholder}
478
+ value={searchValue}
479
+ width="full"
480
+ />
481
+ )}
482
+ {filters.map((filter) => (
483
+ <ToolbarButton
484
+ aria-pressed={filter.active || undefined}
485
+ key={filter.id}
486
+ onClick={() => {
487
+ filter.onAction?.();
488
+ resetPagination();
489
+ }}
490
+ size="sm"
491
+ variant={filter.active ? "default" : "soft"}
492
+ >
493
+ {filter.value ? `${filter.label}: ${filter.value}` : filter.label}
494
+ </ToolbarButton>
495
+ ))}
496
+ </ToolbarSection>
497
+ {(selectedRowIds.length > 0 || activeBulkActions.length > 0) && (
498
+ <ToolbarSection align="end" wrap>
499
+ {selectedRowIds.length > 0 && <ToolbarBadge variant="brand">{labels.selectedCount(selectedRowIds.length)}</ToolbarBadge>}
500
+ {activeBulkActions.map((action) => (
501
+ <DataTableActionButton action={action} key={action.label} />
502
+ ))}
503
+ </ToolbarSection>
504
+ )}
505
+ </Toolbar>
506
+ )}
507
+
508
+ <div className="ds-data-table__body">
509
+ <Table
510
+ columns={columns}
511
+ dir={dir}
512
+ onSelectedRowIdsChange={onSelectedRowIdsChange}
513
+ onSortChange={handleSortChange}
514
+ rows={state ? [] : displayedRows}
515
+ selectedRowIds={selectedRowIds}
516
+ size={size}
517
+ sort={selectedSort}
518
+ />
519
+ {state && <DataTableStateView loading={loading} state={state} />}
520
+ </div>
521
+
522
+ {paginationConfig && (
523
+ <footer className="ds-data-table__footer">
524
+ <ToolbarText>{resultsSummary}</ToolbarText>
525
+ <Pagination
526
+ aria-label={
527
+ paginationConfig.ariaLabel ??
528
+ paginationConfig.messages?.navigation ??
529
+ (typeof title === "string" ? `${title} pagination` : "Table pagination")
530
+ }
531
+ dir={dir}
532
+ messages={paginationConfig.messages}
533
+ nextDisabled={currentPage >= pageCount}
534
+ onPageChange={setDataTablePage}
535
+ page={currentPage}
536
+ pages={paginationConfig.pages ?? getDataTablePaginationPages(currentPage, pageCount)}
537
+ previousDisabled={currentPage <= 1}
538
+ caption={resultsSummary}
539
+ showCaption={paginationConfig.showCaption ?? false}
540
+ summary={resultsSummary}
541
+ type={paginationConfig.type ?? "numeric"}
542
+ variant={paginationConfig.variant ?? "soft"}
543
+ />
544
+ </footer>
545
+ )}
546
+ </section>
547
+ );
548
+ }
549
+
550
+ function columnClass<Row extends TableRowBase>(column: TableColumn<Row>) {
551
+ return cx(
552
+ "ds-table__cell",
553
+ column.width === "fill" && "ds-table__cell--fill",
554
+ column.align && `ds-table__cell--${column.align}`,
555
+ column.kind === "selection" && "ds-table__cell--selection",
556
+ column.kind === "actions" && "ds-table__cell--actions",
557
+ );
558
+ }
559
+
560
+ function columnStyle<Row extends TableRowBase>(column: TableColumn<Row>): CSSProperties {
561
+ return {
562
+ minWidth: column.minWidth,
563
+ width: typeof column.width === "number" ? column.width : undefined,
564
+ };
565
+ }
566
+
567
+ type ResolvedDataTableState = DataTableState & {
568
+ tone: "danger" | "default" | "loading";
569
+ };
570
+
571
+ function DataTableActionButton({ action }: { action: DataTableAction }) {
572
+ return (
573
+ <ToolbarButton
574
+ aria-label={action.ariaLabel}
575
+ disabled={action.disabled}
576
+ onClick={action.onAction}
577
+ size="sm"
578
+ variant={action.variant ?? "soft"}
579
+ >
580
+ {action.label}
581
+ </ToolbarButton>
582
+ );
583
+ }
584
+
585
+ function DataTableStateView({ loading, state }: { loading: boolean; state: ResolvedDataTableState }) {
586
+ return (
587
+ <div className={`ds-data-table__state ds-data-table__state--${state.tone}`} role={state.tone === "danger" ? "alert" : "status"}>
588
+ <div className="ds-data-table__state-copy">
589
+ {loading && <Spinner className="ds-data-table__spinner" />}
590
+ {state.title && <strong>{state.title}</strong>}
591
+ {state.description && <p>{state.description}</p>}
592
+ </div>
593
+ {state.action && (
594
+ <div className="ds-data-table__state-action">
595
+ <DataTableActionButton action={state.action} />
596
+ </div>
597
+ )}
598
+ </div>
599
+ );
600
+ }
601
+
602
+ function getDataTableState({
603
+ emptyState,
604
+ error,
605
+ labels,
606
+ loading,
607
+ rowCount,
608
+ }: {
609
+ emptyState?: DataTableState;
610
+ error: boolean | string | DataTableState;
611
+ labels: DataTableLabels;
612
+ loading: boolean;
613
+ rowCount: number;
614
+ }): ResolvedDataTableState | null {
615
+ if (loading) {
616
+ return {
617
+ description: labels.loadingDescription,
618
+ title: labels.loadingTitle,
619
+ tone: "loading",
620
+ };
621
+ }
622
+
623
+ if (error) {
624
+ if (typeof error === "string") {
625
+ return {
626
+ description: error,
627
+ title: labels.errorTitle,
628
+ tone: "danger",
629
+ };
630
+ }
631
+
632
+ if (typeof error === "object") {
633
+ return {
634
+ action: error.action,
635
+ description: error.description ?? labels.errorDescription,
636
+ title: error.title ?? labels.errorTitle,
637
+ tone: "danger",
638
+ };
639
+ }
640
+
641
+ return {
642
+ description: labels.errorDescription,
643
+ title: labels.errorTitle,
644
+ tone: "danger",
645
+ };
646
+ }
647
+
648
+ if (rowCount === 0) {
649
+ return {
650
+ action: emptyState?.action,
651
+ description: emptyState?.description ?? labels.emptyDescription,
652
+ title: emptyState?.title ?? labels.emptyTitle,
653
+ tone: "default",
654
+ };
655
+ }
656
+
657
+ return null;
658
+ }
659
+
660
+ function getDataTableSearchText<Row extends TableRowBase>({
661
+ columns,
662
+ getRowSearchText,
663
+ row,
664
+ }: {
665
+ columns: TableColumn<Row>[];
666
+ getRowSearchText?: (row: Row) => string;
667
+ row: Row;
668
+ }) {
669
+ const values = [row.id, getRowSearchText?.(row)];
670
+
671
+ for (const column of columns) {
672
+ const columnValue = column.searchValue?.(row) ?? (column.item ? getTableItemText(column.item(row)) : undefined);
673
+ if (columnValue) values.push(columnValue);
674
+ }
675
+
676
+ return values.filter(Boolean).join(" ");
677
+ }
678
+
679
+ function sortDataTableRows<Row extends TableRowBase>(rows: Row[], columns: TableColumn<Row>[], sort: TableSortState) {
680
+ if (!sort) return rows;
681
+
682
+ const column = columns.find((item) => item.id === sort.columnId);
683
+ if (!column) return rows;
684
+
685
+ const direction = sort.direction === "asc" ? 1 : -1;
686
+
687
+ return [...rows].sort((left, right) => {
688
+ const result = compareTableSortValues(getColumnSortValue(column, left), getColumnSortValue(column, right));
689
+ return result * direction;
690
+ });
691
+ }
692
+
693
+ function getColumnSortValue<Row extends TableRowBase>(column: TableColumn<Row>, row: Row): TableSortValue {
694
+ if (column.sortValue) return column.sortValue(row);
695
+ if (column.item) return getTableItemSortValue(column.item(row));
696
+ return undefined;
697
+ }
698
+
699
+ function compareTableSortValues(left: TableSortValue, right: TableSortValue) {
700
+ if (left === right) return 0;
701
+ if (left === null || left === undefined) return 1;
702
+ if (right === null || right === undefined) return -1;
703
+
704
+ const normalizedLeft = normalizeTableSortValue(left);
705
+ const normalizedRight = normalizeTableSortValue(right);
706
+
707
+ if (typeof normalizedLeft === "number" && typeof normalizedRight === "number") {
708
+ return normalizedLeft - normalizedRight;
709
+ }
710
+
711
+ return String(normalizedLeft).localeCompare(String(normalizedRight), undefined, {
712
+ numeric: true,
713
+ sensitivity: "base",
714
+ });
715
+ }
716
+
717
+ function normalizeTableSortValue(value: Exclude<TableSortValue, null | undefined>) {
718
+ if (value instanceof Date) return value.getTime();
719
+ if (typeof value === "boolean") return value ? 1 : 0;
720
+ return value;
721
+ }
722
+
723
+ function getTableItemSortValue(item: TableItem): TableSortValue {
724
+ switch (item.type) {
725
+ case "avatarText":
726
+ return item.name;
727
+ case "badges":
728
+ return item.items.map((badge) => badge.label).join(" ");
729
+ case "brandLogoText":
730
+ return item.label;
731
+ case "file":
732
+ return item.label;
733
+ case "image":
734
+ return item.label;
735
+ case "payment":
736
+ return item.label ?? item.brand;
737
+ case "progress":
738
+ case "rating":
739
+ return item.value;
740
+ case "tags":
741
+ return item.items.join(" ");
742
+ case "text":
743
+ return item.value;
744
+ case "toggle":
745
+ return item.checked;
746
+ default:
747
+ return undefined;
748
+ }
749
+ }
750
+
751
+ function getTableItemText(item: TableItem) {
752
+ const value = getTableItemSortValue(item);
753
+ const text = value === null || value === undefined ? "" : String(value);
754
+ const caption = "caption" in item ? item.caption : undefined;
755
+ return caption ? `${text} ${caption}` : text;
756
+ }
757
+
758
+ function getDataTablePaginationPages(currentPage: number, pageCount: number): PaginationPage[] {
759
+ if (pageCount <= 5) {
760
+ return Array.from({ length: pageCount }, (_, index) => index + 1);
761
+ }
762
+
763
+ const pages: PaginationPage[] = [1];
764
+ const start = Math.max(2, currentPage - 1);
765
+ const end = Math.min(pageCount - 1, currentPage + 1);
766
+
767
+ if (start > 2) pages.push("ellipsis");
768
+ for (let page = start; page <= end; page += 1) {
769
+ pages.push(page);
770
+ }
771
+ if (end < pageCount - 1) pages.push("ellipsis");
772
+ pages.push(pageCount);
773
+
774
+ return pages;
775
+ }
776
+
777
+ function getAriaSort(sort: TableSortState, columnId: string) {
778
+ if (sort?.columnId !== columnId) return "none";
779
+ return sort.direction === "asc" ? "ascending" : "descending";
780
+ }
781
+
782
+ function getNextSort(sort: TableSortState, columnId: string): TableSortState {
783
+ if (sort?.columnId !== columnId) {
784
+ return { columnId, direction: "asc" };
785
+ }
786
+
787
+ return {
788
+ columnId,
789
+ direction: sort.direction === "asc" ? "desc" : "asc",
790
+ };
791
+ }
792
+
793
+ function clampNumber(value: number, min: number, max: number) {
794
+ if (!Number.isFinite(value)) return min;
795
+ return Math.min(max, Math.max(min, Math.floor(value)));
796
+ }
797
+
798
+ function renderTableItem(item: TableItem, tableSize: TableSize) {
799
+ switch (item.type) {
800
+ case "checkbox":
801
+ return (
802
+ <SelectionCheckbox
803
+ checked={item.checked}
804
+ disabled={item.disabled ?? !item.onCheckedChange}
805
+ indeterminate={item.indeterminate}
806
+ label={item.ariaLabel ?? "Select row"}
807
+ onChange={item.onCheckedChange}
808
+ />
809
+ );
810
+ case "text": {
811
+ const className = `ds-table-item-text ds-table-item-text--${item.tone ?? "default"} ds-table-item-text--${item.weight ?? "regular"}`;
812
+ if (!item.caption) return <span className={className}>{item.value}</span>;
813
+ return (
814
+ <span className={`${className} ds-table-text-stack`}>
815
+ <span dir="auto">{item.value}</span>
816
+ <span className="ds-table-caption" dir="auto">{item.caption}</span>
817
+ </span>
818
+ );
819
+ }
820
+ case "avatarText":
821
+ return (
822
+ <span className="ds-table-avatar-text">
823
+ <Avatar name={item.name} size={tableSize === "lg" ? "md" : "sm"} src={item.src} />
824
+ <span>
825
+ <strong dir="auto">{item.name}</strong>
826
+ {item.caption && <em className="ds-table-caption" dir="auto">{item.caption}</em>}
827
+ </span>
828
+ </span>
829
+ );
830
+ case "rating":
831
+ return <Rating value={item.value} max={item.max ?? 5} />;
832
+ case "progress":
833
+ return <Progress value={item.value} label={item.label} tone={item.tone ?? "success"} />;
834
+ case "toggle":
835
+ return (
836
+ <Toggle
837
+ aria-label={item.ariaLabel ?? "Toggle row value"}
838
+ checked={item.checked}
839
+ disabled={item.disabled ?? !item.onCheckedChange}
840
+ onCheckedChange={item.onCheckedChange}
841
+ size="sm"
842
+ />
843
+ );
844
+ case "badges":
845
+ return <BadgeList items={item.items} maxVisible={item.maxVisible} />;
846
+ case "onlyAvatar":
847
+ return <Avatar name={item.alt} size={tableSize === "lg" ? "md" : "xs"} src={item.src} />;
848
+ case "avatarStack":
849
+ return (
850
+ <AvatarStack
851
+ addLabel={item.addLabel}
852
+ avatars={item.avatars}
853
+ canAdd={item.canAdd}
854
+ count={item.count}
855
+ onAdd={item.onAdd}
856
+ size={tableSize}
857
+ />
858
+ );
859
+ case "tags":
860
+ return <TagList items={item.items} maxVisible={item.maxVisible} />;
861
+ case "file":
862
+ return item.label ? <IconLabel icon={<FileIcon />} label={item.label} /> : null;
863
+ case "brandLogoText":
864
+ return <IconLabel caption={item.caption} icon={item.logoSrc ? <img alt="" src={item.logoSrc} /> : <BrandIcon />} label={item.label} />;
865
+ case "payment":
866
+ return <IconLabel caption={item.caption} icon={<PaymentIcon />} label={item.label ?? item.brand} />;
867
+ case "image":
868
+ return <IconLabel caption={item.caption} icon={<img alt="" src={item.src} />} label={item.label} />;
869
+ case "action":
870
+ return <IconButton icon={item.icon} label={item.label} onClick={item.onAction} />;
871
+ case "actionGroup":
872
+ return (
873
+ <span className="ds-table-action-group">
874
+ {item.actions.map((action) => (
875
+ <IconButton disabled={action.disabled} key={action.label} icon={action.icon} label={action.label} onClick={action.onAction} />
876
+ ))}
877
+ </span>
878
+ );
879
+ default:
880
+ return null;
881
+ }
882
+ }
883
+
884
+ function SelectionCheckbox({
885
+ checked,
886
+ disabled = false,
887
+ indeterminate = false,
888
+ label,
889
+ onChange,
890
+ }: {
891
+ checked: boolean;
892
+ disabled?: boolean;
893
+ indeterminate?: boolean;
894
+ label: string;
895
+ onChange?: (checked: boolean) => void;
896
+ }) {
897
+ return (
898
+ <Checkbox
899
+ aria-label={label}
900
+ checked={indeterminate ? "indeterminate" : checked}
901
+ disabled={disabled}
902
+ onCheckedChange={(nextChecked) => onChange?.(nextChecked === true)}
903
+ />
904
+ );
905
+ }
906
+
907
+ type TableAvatarSize = "xs" | "sm" | "md";
908
+
909
+ function Avatar({ name, size, src }: { name: string; size: TableAvatarSize; src?: string }) {
910
+ const initials = name
911
+ .split(" ")
912
+ .map((part) => part[0])
913
+ .slice(0, 2)
914
+ .join("")
915
+ .toUpperCase();
916
+
917
+ return (
918
+ <AvatarPrimitive className="ds-table-avatar" size={size}>
919
+ {src ? <AvatarImage alt="" src={src} /> : null}
920
+ <AvatarFallback size={size}>{initials}</AvatarFallback>
921
+ </AvatarPrimitive>
922
+ );
923
+ }
924
+
925
+ function AvatarStack({
926
+ addLabel = "Add person",
927
+ avatars,
928
+ canAdd,
929
+ count,
930
+ onAdd,
931
+ size,
932
+ }: {
933
+ addLabel?: string;
934
+ avatars: AvatarItem[];
935
+ canAdd?: boolean;
936
+ count?: number;
937
+ onAdd?: () => void;
938
+ size: TableSize;
939
+ }) {
940
+ return (
941
+ <span className={`ds-table-avatar-stack ds-table-avatar-stack--${size}`}>
942
+ {avatars.slice(0, 3).map((avatar) => (
943
+ <Avatar key={avatar.alt} name={avatar.alt} size={size === "lg" ? "md" : "xs"} src={avatar.src} />
944
+ ))}
945
+ {count ? <span className="ds-table-avatar ds-table-avatar--count">+{count}</span> : null}
946
+ {canAdd ? (
947
+ <Button aria-label={addLabel} disabled={!onAdd} onClick={onAdd} size="icon-xs" type="button" variant="ghost">
948
+ +
949
+ </Button>
950
+ ) : null}
951
+ </span>
952
+ );
953
+ }
954
+
955
+ function BadgeList({ items, maxVisible = items.length }: { items: BadgeItem[]; maxVisible?: number }) {
956
+ const visible = items.slice(0, maxVisible);
957
+ const overflow = items.length - visible.length;
958
+
959
+ return (
960
+ <span className="ds-table-badges">
961
+ {visible.map((item, index) => (
962
+ <Badge
963
+ badgeStyle="light"
964
+ color={badgeColor(item.tone)}
965
+ key={`${item.label}-${index}`}
966
+ size="xs"
967
+ type={item.dot ? "dot" : "default"}
968
+ >
969
+ {item.label}
970
+ </Badge>
971
+ ))}
972
+ {overflow > 0 && <Badge size="xs">+{overflow}</Badge>}
973
+ </span>
974
+ );
975
+ }
976
+
977
+ function TagList({ items, maxVisible = items.length }: { items: string[]; maxVisible?: number }) {
978
+ const visible = items.slice(0, maxVisible);
979
+ const overflow = items.length - visible.length;
980
+
981
+ return (
982
+ <span className="ds-table-tags">
983
+ {visible.map((item, index) => (
984
+ <Tag key={`${item}-${index}`} size="xs">{item}</Tag>
985
+ ))}
986
+ {overflow > 0 && <Tag size="xs">+{overflow}</Tag>}
987
+ </span>
988
+ );
989
+ }
990
+
991
+ function Progress({ label, tone, value }: { label?: string; tone: "success" | "brand"; value: number }) {
992
+ return (
993
+ <ProgressPrimitive
994
+ aria-label={label ?? "Progress"}
995
+ className={`ds-table-progress ds-table-progress--${tone}`}
996
+ indicator={label}
997
+ labelPosition="inline"
998
+ size="lg"
999
+ value={value}
1000
+ />
1001
+ );
1002
+ }
1003
+
1004
+ function Rating({ max, value }: { max: number; value: number }) {
1005
+ return (
1006
+ <span
1007
+ aria-label={`${value} out of ${max}`}
1008
+ className="ds-table-rating"
1009
+ role="img"
1010
+ >
1011
+ {Array.from({ length: max }, (_, index) => (
1012
+ <StarIcon
1013
+ aria-hidden="true"
1014
+ className={index < value ? "is-filled" : undefined}
1015
+ key={index}
1016
+ strokeWidth="var(--bh-icon-stroke-190)"
1017
+ />
1018
+ ))}
1019
+ </span>
1020
+ );
1021
+ }
1022
+
1023
+ function IconLabel({ caption, icon, label }: { caption?: string; icon: ReactNode; label: string }) {
1024
+ return (
1025
+ <span className="ds-table-icon-label">
1026
+ <span aria-hidden="true">{icon}</span>
1027
+ {caption ? (
1028
+ <span className="ds-table-text-stack">
1029
+ <span dir="auto">{label}</span>
1030
+ <span className="ds-table-caption" dir="auto">{caption}</span>
1031
+ </span>
1032
+ ) : (
1033
+ <span>{label}</span>
1034
+ )}
1035
+ </span>
1036
+ );
1037
+ }
1038
+
1039
+ function IconButton({
1040
+ disabled,
1041
+ icon,
1042
+ label,
1043
+ onClick,
1044
+ }: {
1045
+ disabled?: boolean;
1046
+ icon: TableAction["icon"];
1047
+ label: string;
1048
+ onClick?: () => void;
1049
+ }) {
1050
+ return (
1051
+ <Button aria-label={label} className="ds-table-icon-button" disabled={disabled ?? !onClick} onClick={onClick} size="icon-xs" type="button" variant="ghost">
1052
+ {renderActionIcon(icon)}
1053
+ </Button>
1054
+ );
1055
+ }
1056
+
1057
+ function badgeColor(tone: BadgeItem["tone"]) {
1058
+ if (tone === "success") return "green" as const;
1059
+ if (tone === "warning") return "amber" as const;
1060
+ return tone ?? "neutral";
1061
+ }
1062
+
1063
+ function renderActionIcon(icon: TableAction["icon"]) {
1064
+ switch (icon) {
1065
+ case "view":
1066
+ return <ViewIcon />;
1067
+ case "edit":
1068
+ return <EditIcon />;
1069
+ case "delete":
1070
+ return <DeleteIcon />;
1071
+ case "more":
1072
+ default:
1073
+ return <MoreIcon />;
1074
+ }
1075
+ }
1076
+
1077
+ function SortIcon({ direction }: { direction?: TableSortDirection }) {
1078
+ return (
1079
+ <ArrowUpDownIcon aria-hidden="true" data-direction={direction ?? "none"} strokeWidth="var(--bh-icon-stroke-190)" />
1080
+ );
1081
+ }
1082
+
1083
+ function MoreIcon() {
1084
+ return <EllipsisIcon aria-hidden="true" strokeWidth="var(--bh-icon-stroke-225)" />;
1085
+ }
1086
+
1087
+ function ViewIcon() {
1088
+ return <EyeIcon aria-hidden="true" strokeWidth="var(--bh-icon-stroke-190)" />;
1089
+ }
1090
+
1091
+ function EditIcon() {
1092
+ return <LucideEditIcon aria-hidden="true" strokeWidth="var(--bh-icon-stroke-190)" />;
1093
+ }
1094
+
1095
+ function DeleteIcon() {
1096
+ return <Trash2Icon aria-hidden="true" strokeWidth="var(--bh-icon-stroke-190)" />;
1097
+ }
1098
+
1099
+ function FileIcon() {
1100
+ return <LucideFileIcon strokeWidth="var(--bh-icon-stroke-190)" />;
1101
+ }
1102
+
1103
+ function BrandIcon() {
1104
+ return <span className="ds-table-brand-icon" />;
1105
+ }
1106
+
1107
+ function PaymentIcon() {
1108
+ return <span className="ds-table-payment-icon" />;
1109
+ }