torch-glare 2.4.5 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/apps/lib/components/DataViews/{badgeAdapter.ts → badge.ts} +2 -2
  2. package/apps/lib/components/DataViews/cell.tsx +324 -0
  3. package/apps/lib/components/DataViews/context.ts +144 -0
  4. package/apps/lib/components/DataViews/data-views.tsx +383 -0
  5. package/apps/lib/components/DataViews/filters/children.tsx +98 -0
  6. package/apps/lib/components/DataViews/filters/custom.tsx +34 -0
  7. package/apps/lib/components/DataViews/filters/filters.tsx +163 -0
  8. package/apps/lib/components/DataViews/filters/index.ts +4 -0
  9. package/apps/lib/components/DataViews/filters/labelled.tsx +20 -0
  10. package/apps/lib/components/DataViews/filters/presets.tsx +65 -0
  11. package/apps/lib/components/DataViews/filters/summary.tsx +65 -0
  12. package/apps/lib/components/DataViews/filters/sync.tsx +35 -0
  13. package/apps/lib/components/DataViews/filters/values.ts +173 -0
  14. package/apps/lib/components/DataViews/header.tsx +217 -0
  15. package/apps/lib/components/DataViews/hooks/index.ts +5 -0
  16. package/apps/lib/components/DataViews/hooks/useActiveRow.ts +22 -0
  17. package/apps/lib/components/DataViews/hooks/useControllable.ts +52 -0
  18. package/apps/lib/components/DataViews/index.ts +74 -26
  19. package/apps/lib/components/DataViews/panel/columns.tsx +153 -0
  20. package/apps/lib/components/DataViews/panel/controls.tsx +106 -0
  21. package/apps/lib/components/DataViews/panel/index.ts +3 -0
  22. package/apps/lib/components/DataViews/panel/panel.tsx +164 -0
  23. package/apps/lib/components/DataViews/panel/saved-views.tsx +67 -0
  24. package/apps/lib/components/DataViews/panel/section.tsx +79 -0
  25. package/apps/lib/components/DataViews/panel/sort.tsx +42 -0
  26. package/apps/lib/components/DataViews/panel/tab.tsx +31 -0
  27. package/apps/lib/components/DataViews/slots.ts +63 -0
  28. package/apps/lib/components/DataViews/states.tsx +38 -0
  29. package/apps/lib/components/DataViews/types.ts +485 -178
  30. package/apps/lib/components/DataViews/views/board-view.tsx +379 -0
  31. package/apps/lib/components/DataViews/views/card-rows.tsx +36 -0
  32. package/apps/lib/components/DataViews/views/inbox-view.tsx +257 -0
  33. package/apps/lib/components/DataViews/views/pane-views.tsx +192 -0
  34. package/apps/lib/components/DataViews/views/table-view.tsx +426 -0
  35. package/apps/lib/components/DataViews/views/tree-view.tsx +365 -0
  36. package/apps/lib/components/FormBuilder/context.ts +20 -6
  37. package/apps/lib/components/FormBuilder/field-kind.ts +28 -0
  38. package/apps/lib/components/FormBuilder/fields/DateField.tsx +3 -3
  39. package/apps/lib/components/FormBuilder/fields/FieldShell.tsx +7 -6
  40. package/apps/lib/components/FormBuilder/fields/PhoneField.tsx +7 -6
  41. package/apps/lib/components/FormBuilder/fields/SelectField.tsx +7 -7
  42. package/apps/lib/components/FormBuilder/fields/TableField.tsx +1 -1
  43. package/apps/lib/components/FormBuilder/fields/TextField.tsx +9 -9
  44. package/apps/lib/components/FormBuilder/form-builder.tsx +55 -3
  45. package/apps/lib/components/FormBuilder/index.ts +3 -1
  46. package/apps/lib/components/FormBuilder/types.ts +40 -0
  47. package/apps/lib/components/TabSwitch.tsx +16 -4
  48. package/apps/lib/components/TreeFolder/TreeFolder.tsx +6 -3
  49. package/apps/lib/components/TreeFolder/TreeFolderRow.tsx +16 -14
  50. package/apps/lib/components/TreeFolder/index.ts +1 -1
  51. package/apps/lib/components/TreeFolder/useTreeFolderDnD.ts +70 -207
  52. package/apps/lib/hooks/useDragDrop.tsx +365 -0
  53. package/apps/lib/hooks/useInfiniteScroll.ts +108 -0
  54. package/apps/lib/registry.json +216 -4
  55. package/apps/lib/tsconfig.tsbuildinfo +1 -1
  56. package/apps/lib/utils/dataViews/path.ts +67 -0
  57. package/apps/lib/utils/dataViews/query.ts +73 -0
  58. package/apps/lib/utils/dataViews/types.ts +187 -0
  59. package/dist/bin/index.js +8 -4
  60. package/dist/bin/index.js.map +1 -1
  61. package/dist/src/commands/add.d.ts +3 -2
  62. package/dist/src/commands/add.d.ts.map +1 -1
  63. package/dist/src/commands/add.js +27 -35
  64. package/dist/src/commands/add.js.map +1 -1
  65. package/dist/src/commands/hook.d.ts.map +1 -1
  66. package/dist/src/commands/hook.js +23 -13
  67. package/dist/src/commands/hook.js.map +1 -1
  68. package/dist/src/commands/init.d.ts.map +1 -1
  69. package/dist/src/commands/init.js +6 -2
  70. package/dist/src/commands/init.js.map +1 -1
  71. package/dist/src/commands/layout.d.ts.map +1 -1
  72. package/dist/src/commands/layout.js +23 -13
  73. package/dist/src/commands/layout.js.map +1 -1
  74. package/dist/src/commands/provider.d.ts.map +1 -1
  75. package/dist/src/commands/provider.js +22 -12
  76. package/dist/src/commands/provider.js.map +1 -1
  77. package/dist/src/commands/utils.d.ts.map +1 -1
  78. package/dist/src/commands/utils.js +21 -28
  79. package/dist/src/commands/utils.js.map +1 -1
  80. package/dist/src/shared/copyComponentsRecursively.d.ts +5 -3
  81. package/dist/src/shared/copyComponentsRecursively.d.ts.map +1 -1
  82. package/dist/src/shared/copyComponentsRecursively.js +5 -6
  83. package/dist/src/shared/copyComponentsRecursively.js.map +1 -1
  84. package/dist/src/shared/getDependenciesAndInstallNestedComponents.js +1 -1
  85. package/dist/src/shared/getDependenciesAndInstallNestedComponents.js.map +1 -1
  86. package/dist/src/shared/installDependencies.d.ts +16 -1
  87. package/dist/src/shared/installDependencies.d.ts.map +1 -1
  88. package/dist/src/shared/installDependencies.js +41 -21
  89. package/dist/src/shared/installDependencies.js.map +1 -1
  90. package/dist/src/shared/installFromPlan.d.ts +23 -0
  91. package/dist/src/shared/installFromPlan.d.ts.map +1 -0
  92. package/dist/src/shared/installFromPlan.js +77 -0
  93. package/dist/src/shared/installFromPlan.js.map +1 -0
  94. package/dist/src/shared/resolveEntry.d.ts +21 -0
  95. package/dist/src/shared/resolveEntry.d.ts.map +1 -0
  96. package/dist/src/shared/resolveEntry.js +54 -0
  97. package/dist/src/shared/resolveEntry.js.map +1 -0
  98. package/dist/src/shared/suggestOtherCommand.d.ts +8 -0
  99. package/dist/src/shared/suggestOtherCommand.d.ts.map +1 -0
  100. package/dist/src/shared/suggestOtherCommand.js +34 -0
  101. package/dist/src/shared/suggestOtherCommand.js.map +1 -0
  102. package/dist/src/shared/tailwindInit.d.ts +3 -1
  103. package/dist/src/shared/tailwindInit.d.ts.map +1 -1
  104. package/dist/src/shared/tailwindInit.js +3 -1
  105. package/dist/src/shared/tailwindInit.js.map +1 -1
  106. package/dist/src/shared/wireStylesheet.d.ts +32 -0
  107. package/dist/src/shared/wireStylesheet.d.ts.map +1 -0
  108. package/dist/src/shared/wireStylesheet.js +92 -0
  109. package/dist/src/shared/wireStylesheet.js.map +1 -0
  110. package/dist/src/types/main.d.ts +6 -0
  111. package/dist/src/types/main.d.ts.map +1 -1
  112. package/docs/components/breadcrumb.md +1 -1
  113. package/docs/components/button-group.md +1 -1
  114. package/docs/components/button.md +1 -1
  115. package/docs/components/card.md +1 -1
  116. package/docs/components/checkbox.md +1 -1
  117. package/docs/components/data-views/backend-response.md +324 -0
  118. package/docs/components/data-views/examples/a11y-rtl.md +250 -0
  119. package/docs/components/data-views/examples/api-orders-route.md +130 -0
  120. package/docs/components/data-views/examples/fields.md +362 -0
  121. package/docs/components/data-views/examples/filters.md +308 -0
  122. package/docs/components/data-views/examples/inbox-routing.md +218 -0
  123. package/docs/components/data-views/examples/index.md +29 -0
  124. package/docs/components/data-views/examples/overview.md +244 -0
  125. package/docs/components/data-views/examples/panel.md +212 -0
  126. package/docs/components/data-views/examples/scale.md +231 -0
  127. package/docs/components/data-views/examples/server-side.md +210 -0
  128. package/docs/components/data-views/examples/state.md +250 -0
  129. package/docs/components/data-views/examples/tree-custom.md +388 -0
  130. package/docs/components/data-views/examples/view-registry.md +313 -0
  131. package/docs/components/data-views/examples/views.md +534 -0
  132. package/docs/components/data-views/guide.md +405 -0
  133. package/docs/components/data-views/index.md +1504 -0
  134. package/docs/components/data-views/migration.md +79 -0
  135. package/docs/components/date-picker.md +0 -1
  136. package/docs/components/form-builder.md +3 -2
  137. package/docs/components/form-renderer.md +2 -1
  138. package/docs/components/form.md +1 -1
  139. package/docs/components/input-field.md +1 -1
  140. package/docs/components/input-otp.md +1 -1
  141. package/docs/components/input.md +1 -1
  142. package/docs/components/labeled-check-box.md +1 -1
  143. package/docs/components/labeled-radio.md +1 -1
  144. package/docs/components/radio-card.md +1 -1
  145. package/docs/components/radio.md +1 -1
  146. package/docs/components/search-field.md +1 -1
  147. package/docs/components/section-block.md +1 -1
  148. package/docs/components/select.md +1 -1
  149. package/docs/components/simple-select.md +1 -1
  150. package/docs/components/switch.md +1 -1
  151. package/docs/components/tab-switch.md +1 -1
  152. package/docs/components/table.md +1 -1
  153. package/docs/components/text-editor.md +1 -1
  154. package/docs/components/textarea.md +1 -1
  155. package/docs/components/toggle-button.md +1 -1
  156. package/docs/components/toggle.md +1 -1
  157. package/docs/components/tree-folder.md +110 -0
  158. package/docs/how-to/forms-with-form-builder.md +2 -2
  159. package/docs/reference/cli.md +28 -3
  160. package/docs/reference/components.md +16 -6
  161. package/docs/tutorials/component-composition.md +11 -13
  162. package/docs/tutorials/getting-started.md +7 -0
  163. package/package.json +3 -2
  164. package/apps/lib/components/DataViews/DataViewRadio.tsx +0 -49
  165. package/apps/lib/components/DataViews/DataViewsConfigPanel.tsx +0 -393
  166. package/apps/lib/components/DataViews/DataViewsHeader.tsx +0 -207
  167. package/apps/lib/components/DataViews/DataViewsLayout.tsx +0 -332
  168. package/apps/lib/components/DataViews/FilterPanel.tsx +0 -493
  169. package/apps/lib/components/DataViews/HeaderSearch.tsx +0 -93
  170. package/apps/lib/components/DataViews/InboxView.tsx +0 -463
  171. package/apps/lib/components/DataViews/InboxViewCard.tsx +0 -127
  172. package/apps/lib/components/DataViews/KanbanView.tsx +0 -336
  173. package/apps/lib/components/DataViews/PanelControls.tsx +0 -39
  174. package/apps/lib/components/DataViews/SettingsPanel.tsx +0 -279
  175. package/apps/lib/components/DataViews/TableView.tsx +0 -212
  176. package/apps/lib/components/DataViews/TreeView.tsx +0 -364
  177. package/apps/lib/components/DataViews/fieldRenderers.tsx +0 -299
  178. package/apps/lib/components/DataViews/filters/DatePickerRangeFilter.tsx +0 -87
  179. package/apps/lib/components/DataViews/filters/DateRangePopover.tsx +0 -120
  180. package/apps/lib/components/DataViews/filters/PresetChips.tsx +0 -45
  181. package/apps/lib/components/DataViews/filters/RangeSliderWithInputs.tsx +0 -165
  182. package/apps/lib/components/DataViews/tree/TreeDrawer.tsx +0 -50
  183. package/apps/lib/components/DataViews/tree/TreeSidebar.tsx +0 -74
  184. package/apps/lib/hooks/useDataViewsState.ts +0 -175
  185. package/apps/lib/utils/dataViews/columnUtils.ts +0 -132
  186. package/apps/lib/utils/dataViews/fieldUtils.ts +0 -197
  187. package/apps/lib/utils/dataViews/nestedDataUtils.tsx +0 -371
  188. package/apps/lib/utils/dataViews/pathUtils.ts +0 -139
  189. package/apps/lib/utils/dataViews/rangeUtils.ts +0 -234
  190. package/apps/lib/utils/dataViews/treeUtils.ts +0 -396
  191. package/docs/components/data-views-config-panel.md +0 -208
  192. package/docs/components/data-views-layout.md +0 -291
  193. package/docs/components/inbox-view.md +0 -170
  194. package/docs/components/kanban-view.md +0 -135
  195. package/docs/components/table-view.md +0 -141
  196. package/docs/components/tree-view.md +0 -147
  197. package/docs/how-to/data-views-from-backend-response.md +0 -194
@@ -0,0 +1,324 @@
1
+ ---
2
+ title: Render a backend response with DataViews
3
+ description: Recipes for turning common backend JSON shapes into a DataViews screen — flat lists, status fields, nested objects, hierarchies, message shapes, and server-driven filtering and paging.
4
+ group: how-to
5
+ keywords: [data-views, dataviews, recipes, backend, json, api, flat, nested, hierarchy, inbox, board, kanban, server-side, filtering, pagination, how-to]
6
+ ---
7
+
8
+ # Render a backend response with DataViews
9
+
10
+ [`DataViews`](./index.md) shows one dataset as a table, a kanban board, an inbox or
11
+ a tree. This guide maps the JSON you get back from an API to what you render.
12
+
13
+ One rule governs every recipe below, so it is worth stating first: **DataViews never reshapes your
14
+ data.** It does not group rows into columns, build a hierarchy, infer a schema, filter, sort or
15
+ page. It paints the `rows` you hand it, in the order you hand them, using the `fields` you describe.
16
+ Anything shaped — `groups`, `nodes` — you build. That is what makes a failed save leave the screen
17
+ showing the truth.
18
+
19
+ ## TL;DR
20
+
21
+ ```tsx
22
+ const { data } = useQuery({ queryKey: ["records"], queryFn: () => api.get("/records") });
23
+
24
+ <DataViews rows={data?.rows ?? []} total={data?.total ?? 0} fields={FIELDS}>
25
+ <DataViews.Header title="Records">
26
+ <DataViews.ViewSwitch />
27
+ <DataViews.Search />
28
+ <DataViews.PanelToggle />
29
+ </DataViews.Header>
30
+ <DataViews.Table />
31
+ </DataViews>
32
+ ```
33
+
34
+ `fields` is the one thing with no default — DataViews will not guess your columns:
35
+
36
+ ```ts
37
+ const FIELDS: FieldConfig[] = [
38
+ { path: "id", label: "ID", type: "number" },
39
+ { path: "name", label: "Name" },
40
+ { path: "createdAt", label: "Created", type: "date-format", dateFormat: "YYYY-MM-DD" },
41
+ ];
42
+ ```
43
+
44
+ ## Recipe 1 — A flat list of objects
45
+
46
+ The common case. Every field you list becomes a column, in the order you list it.
47
+
48
+ ```json
49
+ [{ "id": 1, "name": "Acme", "total": 1240, "createdAt": "2025-09-10" }]
50
+ ```
51
+
52
+ ```tsx
53
+ const FIELDS: FieldConfig[] = [
54
+ { path: "id", label: "Order #", type: "number" },
55
+ { path: "name", label: "Customer" },
56
+ { path: "total", label: "Total", type: "currency", currency: "USD" },
57
+ { path: "createdAt", label: "Created", type: "date-format", dateFormat: "YYYY-MM-DD" },
58
+ ];
59
+
60
+ <DataViews rows={rows} total={rows.length} fields={FIELDS}>
61
+ <DataViews.Header title="Orders"><DataViews.ViewSwitch /></DataViews.Header>
62
+ <DataViews.Table selectable />
63
+ </DataViews>
64
+ ```
65
+
66
+ If your rows have no `id`, tell the component what identity is — selection, drag and the open row
67
+ all key off it:
68
+
69
+ ```tsx
70
+ <DataViews rows={rows} fields={FIELDS} getRowId={(row) => String(row.sku)}>
71
+ ```
72
+
73
+ ## Recipe 2 — A status field → a board
74
+
75
+ The board takes **columns you built**. Grouping is a decision (which statuses exist, in what order,
76
+ what an empty column means) that only you can make correctly.
77
+
78
+ ```json
79
+ [{ "id": 1, "status": "Pending" }, { "id": 2, "status": "Shipped" }]
80
+ ```
81
+
82
+ ```tsx
83
+ const STATUSES = ["Pending", "Shipped", "Delivered", "Cancelled"] as const;
84
+
85
+ const groups = useMemo<RowGroup[]>(
86
+ () =>
87
+ STATUSES.map((status) => ({
88
+ id: status,
89
+ label: status,
90
+ color: ({ Pending: "gray", Shipped: "blue", Delivered: "green", Cancelled: "red" } as const)[status],
91
+ rows: rows.filter((row) => row.status === status),
92
+ })),
93
+ [rows],
94
+ );
95
+
96
+ <DataViews.Board
97
+ groups={groups}
98
+ titlePath="name"
99
+ onRowMove={(intent) => {
100
+ if (intent.to) move.mutate({ id: Number(intent.id), status: intent.to });
101
+ }}
102
+ />
103
+ ```
104
+
105
+ `onRowMove` reports **intent**. The card settles where it landed only once your refetched rows
106
+ agree — which is exactly why a rejected move snaps back on its own.
107
+
108
+ Paint the same field as a coloured chip in the table by giving it a type:
109
+
110
+ ```ts
111
+ { path: "status", label: "Status", type: "enum-badge",
112
+ variants: { Pending: "yellow", Shipped: "blue", Delivered: "green" } }
113
+ ```
114
+
115
+ ## Recipe 3 — Nested objects → dotted paths
116
+
117
+ No flattening step. A `path` reads as deep as you need.
118
+
119
+ ```json
120
+ [{ "id": 1, "customer": { "name": "Acme", "email": "a@acme.com" }, "brand": { "name": "Bosch" } }]
121
+ ```
122
+
123
+ ```ts
124
+ const FIELDS: FieldConfig[] = [
125
+ { path: "customer.name", label: "Customer" },
126
+ { path: "customer.email", label: "Email", type: "link", linkType: "mailto" },
127
+ { path: "brand.name", label: "Brand" },
128
+ ];
129
+ ```
130
+
131
+ Dotted paths work anywhere a path is taken — `titlePath`, `datePath`, `labelPath`, `sort.path` and
132
+ the keys of `filters`.
133
+
134
+ > **The one place `.` is special.** `DataViews.Filters` builds its controls with `FormBuilder`, and
135
+ > react-hook-form reads `.` as object nesting — so a filter on `customer.name` registers as
136
+ > `customer__name`. You still write the real path; only a hand-rolled `setValue` needs the escaped
137
+ > name.
138
+
139
+ ## Recipe 4 — A hierarchy → `nodes`
140
+
141
+ Nested `children[]` or a flat `parentId` list both become `TreeNode[]`, and **you** do the
142
+ conversion: which field is the parent key, whether orphans become roots and how cycles are handled
143
+ are decisions the component cannot make for you.
144
+
145
+ ```json
146
+ [{ "id": "1", "name": "Tools", "children": [{ "id": "2", "name": "Drills", "children": [] }] }]
147
+ ```
148
+
149
+ ```tsx
150
+ const toNodes = (items: ApiCategory[], depth = 0): TreeNode[] =>
151
+ items.map((item) => ({
152
+ id: item.id,
153
+ row: item as Row,
154
+ depth,
155
+ children: toNodes(item.children ?? [], depth + 1),
156
+ }));
157
+ ```
158
+
159
+ From a flat `parentId` list:
160
+
161
+ ```tsx
162
+ const toNodes = (items: ApiCategory[]): TreeNode[] => {
163
+ const byId = new Map(items.map((i) => [i.id, { id: i.id, row: i as Row, depth: 0, children: [] as TreeNode[] }]));
164
+ const roots: TreeNode[] = [];
165
+ for (const item of items) {
166
+ const node = byId.get(item.id)!;
167
+ const parent = item.parentId ? byId.get(item.parentId) : undefined;
168
+ // An orphan becoming a root is a decision — make it deliberately.
169
+ (parent ? parent.children : roots).push(node);
170
+ }
171
+ return roots;
172
+ };
173
+ ```
174
+
175
+ Then render the tree, and say what its pane shows — **the pane's tabs are children, and passing
176
+ none means there is no pane at all**:
177
+
178
+ ```tsx
179
+ <DataViews.Tree nodes={nodes} labelPath="name">
180
+ <DataViews.Tree.Table selectable />
181
+ <DataViews.Tree.Cards />
182
+ </DataViews.Tree>
183
+ ```
184
+
185
+ When the tree holds one kind of thing and the pane must list another — categories in the rail,
186
+ items in the pane — that is `paneRows`:
187
+
188
+ ```tsx
189
+ <DataViews.Tree nodes={categories} paneRows={(node) => itemsInCategory(node.id)}>
190
+ <DataViews.Tree.Table />
191
+ </DataViews.Tree>
192
+ ```
193
+
194
+ ## Recipe 5 — A message shape → the inbox
195
+
196
+ ```json
197
+ [{ "id": 1, "subject": "Delivery delayed", "from": "ops@acme.com", "createdAt": "2025-09-10" }]
198
+ ```
199
+
200
+ ```tsx
201
+ <DataViews.Inbox titlePath="subject" datePath="createdAt">
202
+ <DataViews.Detail />
203
+ </DataViews.Inbox>
204
+ ```
205
+
206
+ `DataViews.Detail` renders every visible field of the open row. For anything richer, the pane is
207
+ `children` and `useActiveRow()` resolves what is open.
208
+
209
+ If the open message should be a **URL** — a back button, a shareable link — make the items links:
210
+
211
+ ```tsx
212
+ <DataViews.Inbox titlePath="subject" datePath="createdAt"
213
+ itemHref={(row, id) => `/inbox/${id}`} linkComponent={Link} />
214
+ ```
215
+
216
+ ## Recipe 6 — Server-driven filtering, sorting and paging
217
+
218
+ Everything the user can ask for arrives as one object, because a query with a new filter and a
219
+ stale page number is not a query anyone meant to ask.
220
+
221
+ ```tsx
222
+ const [query, setQuery] = useState(emptyQuery());
223
+ const { data, isPending } = useQuery({
224
+ queryKey: ["orders", query],
225
+ queryFn: () => fetch(`/api/orders?${queryToParams(query)}`).then((r) => r.json()),
226
+ });
227
+
228
+ <DataViews
229
+ rows={data?.rows ?? []}
230
+ total={data?.total ?? 0}
231
+ fields={FIELDS}
232
+ loading={isPending}
233
+ onQueryChange={setQuery}
234
+ >
235
+ ```
236
+
237
+ The route handler decodes with `parseQuery`, so the encoder and the decoder cannot drift:
238
+
239
+ ```ts
240
+ // app/api/orders/route.ts
241
+ import { parseQuery } from "@/components/DataViews";
242
+
243
+ export async function GET(request: Request) {
244
+ const { search, filters, sort, page, pageSize } = parseQuery(new URL(request.url));
245
+ // your matcher, your ORM
246
+ return Response.json({ rows, total });
247
+ }
248
+ ```
249
+
250
+ `filters` is `Record<path, string[] | { kind: "number", min?, max? } | { kind: "date", from?, to? }>`.
251
+ How the constraints combine — AND across fields, OR within one, fuzzy or exact — is your matcher's
252
+ business. The component reports what was chosen and nothing more.
253
+
254
+ **Paging is scrolling.** There is no pager: hand over `onLoadMore` and **append** each page.
255
+ Whether there is more is derived from `rows.length < total`, so there is no flag to keep in sync.
256
+
257
+ ```tsx
258
+ const { data, fetchNextPage, isPending, isFetchingNextPage } = useInfiniteQuery({
259
+ queryKey: ["orders", { ...query, page: undefined }], // not `page` — pageParam drives that
260
+ initialPageParam: 1,
261
+ queryFn: ({ pageParam }) =>
262
+ fetch(`/api/orders?${queryToParams({ ...query, page: pageParam })}`).then((r) => r.json()),
263
+ getNextPageParam: (last, pages) => {
264
+ const loaded = pages.reduce((n, p) => n + p.rows.length, 0);
265
+ return loaded < last.total ? pages.length + 1 : undefined;
266
+ },
267
+ });
268
+
269
+ <DataViews
270
+ rows={data?.pages.flatMap((p) => p.rows) ?? []}
271
+ total={data?.pages[0]?.total ?? 0}
272
+ loading={isPending}
273
+ onLoadMore={fetchNextPage}
274
+ loadingMore={isFetchingNextPage}
275
+ fields={FIELDS}
276
+ onQueryChange={setQuery}
277
+ />
278
+ ```
279
+
280
+ ## Recipe 7 — A response nothing built-in fits
281
+
282
+ Two seams, in increasing order of commitment.
283
+
284
+ **Repaint a field** — the narrowest, and it applies in every view at once:
285
+
286
+ ```ts
287
+ { path: "health", label: "Health", render: (value, row) => <HealthPill value={Number(value)} row={row} /> }
288
+ ```
289
+
290
+ **Add a view of your own.** `markView` registers it in the switcher beside the built-in four; it
291
+ reads the same context, loading state included:
292
+
293
+ ```tsx
294
+ const GalleryView = markView(function GalleryView() {
295
+ const { rows, visibleFields, loading, getRowId } = useDataViewsData();
296
+ if (loading) return <SkeletonBar className="h-40" />;
297
+ return <div className="grid grid-cols-3 gap-3">{rows.map((row, i) => <Tile key={getRowId(row, i)} row={row} />)}</div>;
298
+ }, { defaultId: "gallery", defaultLabel: "Gallery" });
299
+
300
+ <DataViews …>
301
+ <DataViews.Table />
302
+ <GalleryView icon={<i className="ri-layout-grid-line" />} />
303
+ </DataViews>
304
+ ```
305
+
306
+ ## Gotchas
307
+
308
+ - **Nothing filters itself.** If the rows do not change, nothing on screen changes. That is the
309
+ contract, not a bug.
310
+ - **`total` is the server's count**, not `rows.length`. Scroll loading stops when
311
+ `rows.length >= total`, so a wrong `total` either stops early or loops.
312
+ - **Append, never replace**, when paging — replacing resets the list and the sentinel fires again.
313
+ - **Memoize `groups` and `nodes`.** They are rebuilt on every render otherwise, and both are
314
+ diffed.
315
+ - **A part exists because you rendered it.** There is no `views={{ … }}` map; wrap a view in a
316
+ condition to hide it, and the component switches away if it was open.
317
+ - **Booleans have no filter section.** Use a Yes/No `RadioList`, or `Filters.Custom`.
318
+
319
+ ## Related
320
+
321
+ - [`DataViews` component reference](./index.md) — every part, prop and type
322
+ - [Build a DataViews screen](./guide.md) — the same ground as scenarios, start to finish
323
+ - [Migrating to the DataViews component](./migration.md) — if you are coming from
324
+ `DataViewsLayout`
@@ -0,0 +1,250 @@
1
+ ---
2
+ title: DataViews example — Keyboard & RTL
3
+ description: Keyboard paths and the RTL mirror.
4
+ group: examples
5
+ component: DataViews
6
+ keywords: [data-views, example, examples, a11y, rtl]
7
+ ---
8
+
9
+ # DataViews example — Keyboard & RTL
10
+
11
+ Keyboard paths and the RTL mirror.
12
+
13
+ Complete and runnable — this is the page itself, not an excerpt. In the monorepo it lives at `apps/app/data-views/a11y-rtl/page.tsx`.
14
+
15
+ See the [component reference](../index.md) for what each prop does, or the [guide](../guide.md) for the same ground as scenarios.
16
+
17
+ ```tsx
18
+ "use client";
19
+
20
+ import { useMemo, useState } from "react";
21
+ import { useInfiniteQuery } from "@tanstack/react-query";
22
+ import { Filter, Settings } from "lucide-react";
23
+ import { Button } from "@/components/Button";
24
+ import { DataViews, emptyQuery, queryToParams, type SavedView } from "@/components/DataViews";
25
+ import { FormBuilder } from "@/components/FormBuilder";
26
+ import type {
27
+ DataViewsQuery,
28
+ FieldConfig,
29
+ Row,
30
+ RowGroup,
31
+ TreeNode,
32
+ } from "@/utils/dataViews/types";
33
+ import type { Themes } from "@/utils/types";
34
+
35
+ // ─── Data ─────────────────────────────────────────────────────────────────────
36
+
37
+ interface Order extends Row {
38
+ id: number;
39
+ customer: { name: string };
40
+ status: "Pending" | "Shipped" | "Delivered";
41
+ total: number;
42
+ createdAt: string;
43
+ }
44
+
45
+ const FIELDS: FieldConfig[] = [
46
+ { path: "id", label: "Order #", type: "number" },
47
+ { path: "customer.name", label: "Customer", type: "text" },
48
+ { path: "brand.name", label: "Brand", type: "text" },
49
+ { path: "status", label: "Status", type: "enum-badge", variants: { Pending: "yellow", Shipped: "blue", Delivered: "green" } },
50
+ { path: "total", label: "Total", type: "currency", currency: "USD" },
51
+ { path: "createdAt", label: "Created", type: "date-format", dateFormat: "YYYY-MM-DD" },
52
+ ];
53
+
54
+ /** Arabic labels, so a right-to-left layout is legible rather than mirrored English. */
55
+ const ARABIC_FIELDS: FieldConfig[] = [
56
+ { path: "id", label: "رقم الطلب", type: "number" },
57
+ { path: "customer.name", label: "العميل", type: "text" },
58
+ { path: "status", label: "الحالة", type: "enum-badge", variants: { Pending: "yellow", Shipped: "blue", Delivered: "green" } },
59
+ { path: "total", label: "المجموع", type: "currency", currency: "USD" },
60
+ { path: "createdAt", label: "التاريخ", type: "date-format", dateFormat: "YYYY-MM-DD" },
61
+ ];
62
+
63
+ /** Dynamic sets — in a real app these come from the endpoint that also does the filtering. */
64
+ const CUSTOMER_OPTIONS = [
65
+ "Acme Inc.", "Globex Corp.", "Initech", "Umbrella",
66
+ "Hooli", "Stark Industries", "Wayne Enterprises", "Cyberdyne",
67
+ ].map((v) => ({ label: v, value: v }));
68
+
69
+ const BRAND_OPTIONS = ["Bosch", "Makita", "DeWalt", "Hilti"].map((v) => ({ label: v, value: v }));
70
+
71
+ const PRIORITY_OPTIONS = ["High", "Medium", "Low"].map((v) => ({ label: v, value: v }));
72
+
73
+ const STATUS_OPTIONS = [
74
+ { label: "Pending", value: "Pending" },
75
+ { label: "Shipped", value: "Shipped" },
76
+ { label: "Delivered", value: "Delivered" },
77
+ ];
78
+
79
+ /**
80
+ * The request this page makes. The querying itself happens in `app/api/orders/route.ts` —
81
+ * nothing on this page filters, sorts or pages anything, which is the split DataViews is built
82
+ * around.
83
+ */
84
+ async function fetchOrders(q: DataViewsQuery): Promise<{ rows: Order[]; total: number }> {
85
+ const params = queryToParams(q);
86
+ const res = await fetch(`/api/orders?${params}`);
87
+ if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
88
+ return res.json();
89
+ }
90
+
91
+ const groupByStatus = (rows: readonly Order[]): RowGroup[] =>
92
+ (["Pending", "Shipped", "Delivered"] as const).map((status) => ({
93
+ id: status,
94
+ label: status,
95
+ color: ({ Pending: "gray", Shipped: "blue", Delivered: "green" } as const)[status],
96
+ rows: rows.filter((row) => row.status === status),
97
+ }));
98
+
99
+ const nodesFromRows = (rows: readonly Order[]): TreeNode[] =>
100
+ rows.map((row) => ({ id: String(row.id), row, depth: 0, children: [] }));
101
+
102
+ // ─── Page ─────────────────────────────────────────────────────────────────────
103
+
104
+ /**
105
+ * Keyboard: tab through it with the mouse down. A row with `onRowClick` takes a `tabIndex`, a
106
+ * button role and Enter/Space handling — press Enter on one and it selects. Without the handler a
107
+ * row stays a plain row and is skipped, which is right: a row that does nothing is not a tab stop.
108
+ *
109
+ * The tree handles `←`/`→` and Enter but not yet `↑`/`↓`, `Home`/`End` or roving tabindex, so
110
+ * every node is its own tab stop. Drag-and-drop has no keyboard equivalent at all — if moving
111
+ * records matters, give people a menu action that emits the same intent.
112
+ *
113
+ * Direction comes from the DOM, not a prop: the component is built from logical properties, so
114
+ * `dir="rtl"` moves the rail left and aligns the table to the start edge. `theme` sets
115
+ * `data-theme` — but the filter dropdowns and the date calendar portal to `document.body`, so
116
+ * they follow the *page's* theme rather than this one.
117
+ */
118
+ export default function AccessibilityExample() {
119
+ const [rtl, setRtl] = useState(false);
120
+ const [theme, setTheme] = useState<Themes>("default");
121
+
122
+ const [query, setQuery] = useState(emptyQuery());
123
+ const [activated, setActivated] = useState<string | null>(null);
124
+ const [saved, setSaved] = useState<SavedView[]>([]);
125
+
126
+
127
+ const { data, isPending, fetchNextPage, isFetchingNextPage } = useInfiniteQuery({
128
+ // The key *is* the query: touch any part of it and TanStack refetches, and a response that
129
+ // has been superseded is discarded rather than landing on top of a newer one.
130
+ queryKey: ["a11y-orders", { ...query, page: undefined }],
131
+ queryFn: ({ pageParam }) => fetchOrders({ ...query, page: pageParam }),
132
+ initialPageParam: 1,
133
+ // Undefined means "no more" — which is what the component's `hasMore` resolves to.
134
+ getNextPageParam: (last, pages) => {
135
+ const loaded = pages.reduce((n, page) => n + page.rows.length, 0);
136
+ return loaded < last.total ? pages.length + 1 : undefined;
137
+ },
138
+ });
139
+
140
+ // Memoised because `data?.rows ?? []` is a new array on every render, which would make the
141
+ // `groups`/`nodes` memos below miss every time and hand the board a new array to diff.
142
+ const rows = useMemo(() => data?.pages.flatMap((page) => page.rows) ?? [], [data]);
143
+ const total = data?.pages[0]?.total ?? 0;
144
+ const groups = useMemo(() => groupByStatus(rows), [rows]);
145
+ const nodes = useMemo(() => nodesFromRows(rows), [rows]);
146
+
147
+ return (
148
+ <div dir={rtl ? "rtl" : "ltr"} className="flex h-full min-h-0 flex-col p-4">
149
+ <DataViews
150
+ key={rtl ? "rtl" : "ltr"}
151
+ rows={rows}
152
+ fields={rtl ? ARABIC_FIELDS : FIELDS}
153
+ theme={theme}
154
+ total={total}
155
+ loading={isPending}
156
+ onLoadMore={fetchNextPage}
157
+ loadingMore={isFetchingNextPage}
158
+ onQueryChange={setQuery}
159
+ className="h-full"
160
+ >
161
+ <DataViews.Header title={rtl ? "الطلبات" : "Orders"}>
162
+ <DataViews.ViewSwitch />
163
+ <DataViews.Search />
164
+ <DataViews.Actions>
165
+ {/* Proof the keyboard path works: Enter on a focused row fires `onRowClick`. */}
166
+ <span
167
+ data-testid="activated"
168
+ className="typography-body-small-regular text-content-presentation-global-secondary"
169
+ >
170
+ {activated ? `row ${activated}` : "no row activated"}
171
+ </span>
172
+ {/* Two buttons rather than one toggle: a single button labelled with the current
173
+ direction is ambiguous — it reads as either the state or the action. */}
174
+ <Button variant="BluColStyle" size="M" onClick={() => setRtl(false)}>
175
+ LTR
176
+ </Button>
177
+ <Button variant="BluColStyle" size="M" onClick={() => setRtl(true)}>
178
+ RTL
179
+ </Button>
180
+ {(["default", "dark", "light"] as const).map((t) => (
181
+ <Button variant="BluColStyle"
182
+ size="M"
183
+ key={t}
184
+ onClick={() => setTheme(t)}
185
+ >
186
+ {t}
187
+ </Button>
188
+ ))}
189
+ </DataViews.Actions>
190
+ <DataViews.PanelToggle />
191
+ </DataViews.Header>
192
+
193
+ {/* Enter on a focused row fires `onRowClick` — the visible proof the row is reachable
194
+ without a mouse. What to do about it is the app's call; here it is a readout. */}
195
+ <DataViews.Table selectable onRowClick={(_row, id) => setActivated(id)} />
196
+ <DataViews.Board
197
+ groups={groups}
198
+ titlePath="customer.name"
199
+ />
200
+ {/* The pane's tabs are children, like every other part: pass none and there is no pane. */}
201
+ <DataViews.Tree nodes={nodes} labelPath="customer.name">
202
+ <DataViews.Tree.Table />
203
+ <DataViews.Tree.Cards />
204
+ </DataViews.Tree>
205
+
206
+ <DataViews.Panel>
207
+ <DataViews.Panel.Tab value="config" label="Config." icon={<Settings />}>
208
+ {/* Saving is the app's job — a view outlives the component. Restoring is not:
209
+ hand the snapshot back and selecting it puts everything back internally. */}
210
+ <DataViews.Panel.SavedViews
211
+ views={saved}
212
+ onSave={(snapshot) =>
213
+ setSaved((prev) => [
214
+ ...prev,
215
+ { id: `view-${prev.length + 1}`, label: `View ${prev.length + 1}`, snapshot },
216
+ ])
217
+ }
218
+ />
219
+ <DataViews.Panel.Columns />
220
+ <DataViews.Panel.Sort />
221
+ </DataViews.Panel.Tab>
222
+
223
+ <DataViews.Panel.Tab value="filters" label="Filters" icon={<Filter />}>
224
+ {/* The controls are FormBuilder fields — the same Select and Slider any form in
225
+ this library uses. Filters reads each child's name, label and bounds to learn
226
+ what it is; there is no second description of the form. */}
227
+ <DataViews.Filters
228
+ title={null}
229
+ className="border-b-0 p-0"
230
+ >
231
+ {/* One control per section type. Which one a field gets is decided by the data:
232
+ can the option set grow, and can the user pick more than one. */}
233
+ <FormBuilder.CheckboxGroup name="status" label="Status" options={STATUS_OPTIONS} />
234
+ <FormBuilder.RadioList name="priority" label="Priority" options={PRIORITY_OPTIONS} />
235
+ <FormBuilder.SearchableSelect
236
+ name="customer.name"
237
+ label="Customer"
238
+ options={CUSTOMER_OPTIONS}
239
+ />
240
+ <FormBuilder.MultiSelect name="brand.name" label="Brand" options={BRAND_OPTIONS} />
241
+ <FormBuilder.Slider name="total" label="Total" range min={0} max={15000} step={100} />
242
+ <FormBuilder.DateRange name="createdAt" label="Created" />
243
+ </DataViews.Filters>
244
+ </DataViews.Panel.Tab>
245
+ </DataViews.Panel>
246
+ </DataViews>
247
+ </div>
248
+ );
249
+ }
250
+ ```