torch-glare 2.4.4 → 2.5.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.
- package/apps/lib/components/DataViews/{badgeAdapter.ts → badge.ts} +2 -2
- package/apps/lib/components/DataViews/cell.tsx +324 -0
- package/apps/lib/components/DataViews/context.ts +144 -0
- package/apps/lib/components/DataViews/data-views.tsx +383 -0
- package/apps/lib/components/DataViews/filters/children.tsx +98 -0
- package/apps/lib/components/DataViews/filters/custom.tsx +34 -0
- package/apps/lib/components/DataViews/filters/filters.tsx +163 -0
- package/apps/lib/components/DataViews/filters/index.ts +4 -0
- package/apps/lib/components/DataViews/filters/labelled.tsx +20 -0
- package/apps/lib/components/DataViews/filters/presets.tsx +65 -0
- package/apps/lib/components/DataViews/filters/summary.tsx +65 -0
- package/apps/lib/components/DataViews/filters/sync.tsx +35 -0
- package/apps/lib/components/DataViews/filters/values.ts +173 -0
- package/apps/lib/components/DataViews/header.tsx +217 -0
- package/apps/lib/components/DataViews/hooks/index.ts +5 -0
- package/apps/lib/components/DataViews/hooks/useActiveRow.ts +22 -0
- package/apps/lib/components/DataViews/hooks/useControllable.ts +52 -0
- package/apps/lib/components/DataViews/index.ts +74 -26
- package/apps/lib/components/DataViews/panel/columns.tsx +153 -0
- package/apps/lib/components/DataViews/panel/controls.tsx +106 -0
- package/apps/lib/components/DataViews/panel/index.ts +3 -0
- package/apps/lib/components/DataViews/panel/panel.tsx +164 -0
- package/apps/lib/components/DataViews/panel/saved-views.tsx +67 -0
- package/apps/lib/components/DataViews/panel/section.tsx +79 -0
- package/apps/lib/components/DataViews/panel/sort.tsx +42 -0
- package/apps/lib/components/DataViews/panel/tab.tsx +31 -0
- package/apps/lib/components/DataViews/slots.ts +63 -0
- package/apps/lib/components/DataViews/states.tsx +38 -0
- package/apps/lib/components/DataViews/types.ts +485 -178
- package/apps/lib/components/DataViews/views/board-view.tsx +379 -0
- package/apps/lib/components/DataViews/views/card-rows.tsx +36 -0
- package/apps/lib/components/DataViews/views/inbox-view.tsx +257 -0
- package/apps/lib/components/DataViews/views/pane-views.tsx +192 -0
- package/apps/lib/components/DataViews/views/table-view.tsx +426 -0
- package/apps/lib/components/DataViews/views/tree-view.tsx +365 -0
- package/apps/lib/components/FormBuilder/context.ts +20 -6
- package/apps/lib/components/FormBuilder/field-kind.ts +28 -0
- package/apps/lib/components/FormBuilder/fields/DateField.tsx +3 -3
- package/apps/lib/components/FormBuilder/fields/FieldShell.tsx +7 -6
- package/apps/lib/components/FormBuilder/fields/PhoneField.tsx +30 -4
- package/apps/lib/components/FormBuilder/fields/SelectField.tsx +7 -7
- package/apps/lib/components/FormBuilder/fields/TableField.tsx +80 -52
- package/apps/lib/components/FormBuilder/fields/TextField.tsx +9 -9
- package/apps/lib/components/FormBuilder/form-builder.tsx +66 -6
- package/apps/lib/components/FormBuilder/index.ts +3 -1
- package/apps/lib/components/FormBuilder/types.ts +40 -0
- package/apps/lib/components/Input.tsx +3 -0
- package/apps/lib/components/SearchableTable.tsx +5 -4
- package/apps/lib/components/SectionBlock.tsx +58 -11
- package/apps/lib/components/Select.tsx +3 -1
- package/apps/lib/components/TabSwitch.tsx +16 -4
- package/apps/lib/components/Table.tsx +265 -67
- package/apps/lib/components/TreeFolder/TreeFolder.tsx +6 -3
- package/apps/lib/components/TreeFolder/TreeFolderRow.tsx +16 -14
- package/apps/lib/components/TreeFolder/index.ts +1 -1
- package/apps/lib/components/TreeFolder/useTreeFolderDnD.ts +70 -207
- package/apps/lib/hooks/useDragDrop.tsx +365 -0
- package/apps/lib/hooks/useInfiniteScroll.ts +108 -0
- package/apps/lib/registry.json +159 -4
- package/apps/lib/tsconfig.tsbuildinfo +1 -1
- package/apps/lib/utils/dataViews/path.ts +67 -0
- package/apps/lib/utils/dataViews/query.ts +73 -0
- package/apps/lib/utils/dataViews/types.ts +187 -0
- package/docs/components/breadcrumb.md +1 -1
- package/docs/components/button-group.md +1 -1
- package/docs/components/button.md +1 -1
- package/docs/components/card.md +1 -1
- package/docs/components/checkbox.md +1 -1
- package/docs/components/data-views/backend-response.md +324 -0
- package/docs/components/data-views/examples/a11y-rtl.md +250 -0
- package/docs/components/data-views/examples/api-orders-route.md +130 -0
- package/docs/components/data-views/examples/fields.md +362 -0
- package/docs/components/data-views/examples/filters.md +308 -0
- package/docs/components/data-views/examples/inbox-routing.md +218 -0
- package/docs/components/data-views/examples/index.md +29 -0
- package/docs/components/data-views/examples/overview.md +244 -0
- package/docs/components/data-views/examples/panel.md +212 -0
- package/docs/components/data-views/examples/scale.md +231 -0
- package/docs/components/data-views/examples/server-side.md +210 -0
- package/docs/components/data-views/examples/state.md +250 -0
- package/docs/components/data-views/examples/tree-custom.md +388 -0
- package/docs/components/data-views/examples/view-registry.md +313 -0
- package/docs/components/data-views/examples/views.md +534 -0
- package/docs/components/data-views/guide.md +405 -0
- package/docs/components/data-views/index.md +1504 -0
- package/docs/components/data-views/migration.md +79 -0
- package/docs/components/date-picker.md +0 -1
- package/docs/components/form-builder.md +19 -8
- package/docs/components/form-renderer.md +2 -1
- package/docs/components/form.md +1 -1
- package/docs/components/input-field.md +1 -1
- package/docs/components/input-otp.md +1 -1
- package/docs/components/input.md +1 -1
- package/docs/components/labeled-check-box.md +1 -1
- package/docs/components/labeled-radio.md +1 -1
- package/docs/components/radio-card.md +1 -1
- package/docs/components/radio.md +1 -1
- package/docs/components/search-field.md +1 -1
- package/docs/components/section-block.md +79 -3
- package/docs/components/select.md +1 -1
- package/docs/components/simple-select.md +1 -1
- package/docs/components/switch.md +1 -1
- package/docs/components/tab-switch.md +1 -1
- package/docs/components/table.md +45 -8
- package/docs/components/text-editor.md +1 -1
- package/docs/components/textarea.md +1 -1
- package/docs/components/toggle-button.md +1 -1
- package/docs/components/toggle.md +1 -1
- package/docs/components/tree-folder.md +110 -0
- package/docs/how-to/forms-with-form-builder.md +6 -4
- package/docs/reference/components.md +16 -6
- package/docs/tutorials/component-composition.md +11 -13
- package/package.json +3 -2
- package/apps/lib/components/DataViews/DataViewRadio.tsx +0 -49
- package/apps/lib/components/DataViews/DataViewsConfigPanel.tsx +0 -393
- package/apps/lib/components/DataViews/DataViewsHeader.tsx +0 -207
- package/apps/lib/components/DataViews/DataViewsLayout.tsx +0 -332
- package/apps/lib/components/DataViews/FilterPanel.tsx +0 -493
- package/apps/lib/components/DataViews/HeaderSearch.tsx +0 -93
- package/apps/lib/components/DataViews/InboxView.tsx +0 -463
- package/apps/lib/components/DataViews/InboxViewCard.tsx +0 -127
- package/apps/lib/components/DataViews/KanbanView.tsx +0 -336
- package/apps/lib/components/DataViews/PanelControls.tsx +0 -39
- package/apps/lib/components/DataViews/SettingsPanel.tsx +0 -279
- package/apps/lib/components/DataViews/TableView.tsx +0 -212
- package/apps/lib/components/DataViews/TreeView.tsx +0 -364
- package/apps/lib/components/DataViews/fieldRenderers.tsx +0 -299
- package/apps/lib/components/DataViews/filters/DatePickerRangeFilter.tsx +0 -87
- package/apps/lib/components/DataViews/filters/DateRangePopover.tsx +0 -120
- package/apps/lib/components/DataViews/filters/PresetChips.tsx +0 -45
- package/apps/lib/components/DataViews/filters/RangeSliderWithInputs.tsx +0 -165
- package/apps/lib/components/DataViews/tree/TreeDrawer.tsx +0 -50
- package/apps/lib/components/DataViews/tree/TreeSidebar.tsx +0 -74
- package/apps/lib/hooks/useDataViewsState.ts +0 -175
- package/apps/lib/utils/dataViews/columnUtils.ts +0 -132
- package/apps/lib/utils/dataViews/fieldUtils.ts +0 -197
- package/apps/lib/utils/dataViews/nestedDataUtils.tsx +0 -371
- package/apps/lib/utils/dataViews/pathUtils.ts +0 -139
- package/apps/lib/utils/dataViews/rangeUtils.ts +0 -234
- package/apps/lib/utils/dataViews/treeUtils.ts +0 -396
- package/docs/components/data-views-config-panel.md +0 -208
- package/docs/components/data-views-layout.md +0 -291
- package/docs/components/inbox-view.md +0 -170
- package/docs/components/kanban-view.md +0 -135
- package/docs/components/table-view.md +0 -141
- package/docs/components/tree-view.md +0 -147
- package/docs/how-to/data-views-from-backend-response.md +0 -194
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: DataViews example — State
|
|
3
|
+
description: Controlled versus uncontrolled query.
|
|
4
|
+
group: examples
|
|
5
|
+
component: DataViews
|
|
6
|
+
keywords: [data-views, example, examples, state]
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# DataViews example — State
|
|
10
|
+
|
|
11
|
+
Controlled versus uncontrolled query.
|
|
12
|
+
|
|
13
|
+
Complete and runnable — this is the page itself, not an excerpt. In the monorepo it lives at `apps/app/data-views/state/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
|
+
} from "@/utils/dataViews/types";
|
|
32
|
+
|
|
33
|
+
// ─── Data ─────────────────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
interface Order extends Row {
|
|
36
|
+
id: number;
|
|
37
|
+
customer: { name: string };
|
|
38
|
+
status: "Pending" | "Shipped" | "Delivered";
|
|
39
|
+
priority: "High" | "Medium" | "Low";
|
|
40
|
+
total: number;
|
|
41
|
+
createdAt: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const FIELDS: FieldConfig[] = [
|
|
45
|
+
{ path: "id", label: "Order #", type: "number" },
|
|
46
|
+
{ path: "customer.name", label: "Customer", type: "text" },
|
|
47
|
+
{ path: "brand.name", label: "Brand", type: "text" },
|
|
48
|
+
{ path: "status", label: "Status", type: "enum-badge", variants: { Pending: "yellow", Shipped: "blue", Delivered: "green" } },
|
|
49
|
+
{ path: "priority", label: "Priority", type: "enum-badge", variants: { High: "redOrange", Medium: "purple", Low: "gray" } },
|
|
50
|
+
{ path: "total", label: "Total", type: "currency", currency: "USD" },
|
|
51
|
+
{ path: "createdAt", label: "Created", type: "date-format", dateFormat: "YYYY-MM-DD" },
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
/** Dynamic sets — in a real app these come from the endpoint that also does the filtering. */
|
|
55
|
+
const CUSTOMER_OPTIONS = [
|
|
56
|
+
"Acme Inc.", "Globex Corp.", "Initech", "Umbrella",
|
|
57
|
+
"Hooli", "Stark Industries", "Wayne Enterprises", "Cyberdyne",
|
|
58
|
+
].map((v) => ({ label: v, value: v }));
|
|
59
|
+
|
|
60
|
+
const BRAND_OPTIONS = ["Bosch", "Makita", "DeWalt", "Hilti"].map((v) => ({ label: v, value: v }));
|
|
61
|
+
|
|
62
|
+
const PRIORITY_OPTIONS = ["High", "Medium", "Low"].map((v) => ({ label: v, value: v }));
|
|
63
|
+
|
|
64
|
+
const STATUS_OPTIONS = [
|
|
65
|
+
{ label: "Pending", value: "Pending" },
|
|
66
|
+
{ label: "Shipped", value: "Shipped" },
|
|
67
|
+
{ label: "Delivered", value: "Delivered" },
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The request this page makes. The querying itself happens in `app/api/orders/route.ts` —
|
|
72
|
+
* nothing on this page filters, sorts or pages anything, which is the split DataViews is built
|
|
73
|
+
* around.
|
|
74
|
+
*/
|
|
75
|
+
async function fetchOrders(q: DataViewsQuery): Promise<{ rows: Order[]; total: number }> {
|
|
76
|
+
const params = queryToParams(q);
|
|
77
|
+
const res = await fetch(`/api/orders?${params}`);
|
|
78
|
+
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
|
|
79
|
+
return res.json();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const groupByStatus = (rows: readonly Order[]): RowGroup[] =>
|
|
83
|
+
(["Pending", "Shipped", "Delivered"] as const).map((status) => ({
|
|
84
|
+
id: status,
|
|
85
|
+
label: status,
|
|
86
|
+
color: ({ Pending: "gray", Shipped: "blue", Delivered: "green" } as const)[status],
|
|
87
|
+
rows: rows.filter((row) => row.status === status),
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
// ─── Page ─────────────────────────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Every piece of interaction state owned by this page, and the two states that replace the view.
|
|
94
|
+
*
|
|
95
|
+
* Supply all nine pairs and there is no second copy to fall out of step; omit them all and the
|
|
96
|
+
* same component runs uncontrolled. The buttons drive it from the outside, which only works
|
|
97
|
+
* because the props are honoured rather than used as initial values.
|
|
98
|
+
*/
|
|
99
|
+
export default function StateExample() {
|
|
100
|
+
const [query, setQuery] = useState(emptyQuery());
|
|
101
|
+
// The observers: told what the component decided, not driving it.
|
|
102
|
+
const [seenView, setSeenView] = useState("");
|
|
103
|
+
const [seenSelection, setSeenSelection] = useState<readonly string[]>([]);
|
|
104
|
+
const [saved, setSaved] = useState<SavedView[]>([]);
|
|
105
|
+
|
|
106
|
+
const [empty, setEmpty] = useState(false);
|
|
107
|
+
const [loading, setLoading] = useState(false);
|
|
108
|
+
const [withViews, setWithViews] = useState(true);
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
const { data, isPending, fetchNextPage, isFetchingNextPage } = useInfiniteQuery({
|
|
112
|
+
// The key *is* the query: touch any part of it and TanStack refetches, and a response that
|
|
113
|
+
// has been superseded is discarded rather than landing on top of a newer one.
|
|
114
|
+
queryKey: ["state-orders", { ...query, page: undefined }],
|
|
115
|
+
queryFn: ({ pageParam }) => fetchOrders({ ...query, page: pageParam }),
|
|
116
|
+
initialPageParam: 1,
|
|
117
|
+
// Undefined means "no more" — which is what the component's `hasMore` resolves to.
|
|
118
|
+
getNextPageParam: (last, pages) => {
|
|
119
|
+
const loaded = pages.reduce((n, page) => n + page.rows.length, 0);
|
|
120
|
+
return loaded < last.total ? pages.length + 1 : undefined;
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Memoised because `data?.rows ?? []` is a new array on every render, which would make the
|
|
125
|
+
// `groups` memo below miss every time and hand the board a new array to diff.
|
|
126
|
+
const queried = useMemo(() => data?.pages.flatMap((page) => page.rows) ?? [], [data]);
|
|
127
|
+
const total = data?.pages[0]?.total ?? 0;
|
|
128
|
+
const rows = empty ? [] : queried;
|
|
129
|
+
// The board reads `groups`, never `rows`, so its columns stay full even when `rows` is emptied
|
|
130
|
+
// — and `Empty` keys off `rows`, which is why it takes over anyway. Keep `rows` populated
|
|
131
|
+
// alongside `groups`/`nodes`.
|
|
132
|
+
const groups = useMemo(() => groupByStatus(queried), [queried]);
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<div className="flex h-full min-h-0 flex-col p-4">
|
|
136
|
+
<DataViews
|
|
137
|
+
rows={rows}
|
|
138
|
+
fields={FIELDS}
|
|
139
|
+
total={total}
|
|
140
|
+
loading={loading || isPending}
|
|
141
|
+
onLoadMore={fetchNextPage}
|
|
142
|
+
loadingMore={isFetchingNextPage}
|
|
143
|
+
query={query}
|
|
144
|
+
onQueryChange={setQuery}
|
|
145
|
+
onViewChange={setSeenView}
|
|
146
|
+
onSelectionChange={setSeenSelection}
|
|
147
|
+
className="h-full"
|
|
148
|
+
>
|
|
149
|
+
<DataViews.Header title="Orders">
|
|
150
|
+
<DataViews.ViewSwitch />
|
|
151
|
+
<DataViews.Search />
|
|
152
|
+
<DataViews.Actions>
|
|
153
|
+
{/* The query is controlled here, so it can be driven from outside — which is what
|
|
154
|
+
a URL-synced list does. */}
|
|
155
|
+
<Button
|
|
156
|
+
variant="BluColStyle"
|
|
157
|
+
size="M"
|
|
158
|
+
onClick={() => setQuery((q) => ({ ...q, sort: { path: "total", direction: "desc" } }))}
|
|
159
|
+
>
|
|
160
|
+
Sort by total
|
|
161
|
+
</Button>
|
|
162
|
+
<Button
|
|
163
|
+
variant="BluColStyle"
|
|
164
|
+
size="M"
|
|
165
|
+
onClick={() => setQuery((q) => ({ ...q, filters: { status: ["Pending"] }, page: 1 }))}
|
|
166
|
+
>
|
|
167
|
+
Filter to pending
|
|
168
|
+
</Button>
|
|
169
|
+
<Button variant="BluColStyle" size="M" onClick={() => setQuery(emptyQuery())}>
|
|
170
|
+
Clear the query
|
|
171
|
+
</Button>
|
|
172
|
+
<Button variant="BluColStyle" size="M" onClick={() => setEmpty((v) => !v)}>
|
|
173
|
+
{empty ? "Restore rows" : "Empty the rows"}
|
|
174
|
+
</Button>
|
|
175
|
+
<Button variant="BluColStyle" size="M" onClick={() => setLoading((v) => !v)}>
|
|
176
|
+
{loading ? "Stop loading" : "Start loading"}
|
|
177
|
+
</Button>
|
|
178
|
+
<Button variant="BluColStyle" size="M" onClick={() => setWithViews((v) => !v)}>
|
|
179
|
+
{withViews ? "Remove every view" : "Restore the views"}
|
|
180
|
+
</Button>
|
|
181
|
+
</DataViews.Actions>
|
|
182
|
+
<DataViews.PanelToggle />
|
|
183
|
+
</DataViews.Header>
|
|
184
|
+
|
|
185
|
+
{/* What the component reported back. Nothing here drives it. */}
|
|
186
|
+
<div
|
|
187
|
+
data-testid="observed"
|
|
188
|
+
className="typography-body-small-regular text-content-presentation-global-secondary border-border-presentation-global-primary border-b px-4 py-2"
|
|
189
|
+
>
|
|
190
|
+
view: {seenView || "—"} · selected: {seenSelection.length}
|
|
191
|
+
</div>
|
|
192
|
+
|
|
193
|
+
{withViews && (
|
|
194
|
+
<DataViews.Table selectable />
|
|
195
|
+
)}
|
|
196
|
+
{withViews && (
|
|
197
|
+
<DataViews.Board
|
|
198
|
+
groups={groups}
|
|
199
|
+
titlePath="customer.name"
|
|
200
|
+
/>
|
|
201
|
+
)}
|
|
202
|
+
|
|
203
|
+
{/* `loading` only swaps in a child you rendered — drop this and the prop does nothing. */}
|
|
204
|
+
|
|
205
|
+
<DataViews.Panel>
|
|
206
|
+
<DataViews.Panel.Tab value="config" label="Config." icon={<Settings />}>
|
|
207
|
+
{/* Saving is the app's job — a view outlives the component. Restoring is not:
|
|
208
|
+
hand the snapshot back and selecting it puts everything back internally. */}
|
|
209
|
+
<DataViews.Panel.SavedViews
|
|
210
|
+
views={saved}
|
|
211
|
+
onSave={(snapshot) =>
|
|
212
|
+
setSaved((prev) => [
|
|
213
|
+
...prev,
|
|
214
|
+
{ id: `view-${prev.length + 1}`, label: `View ${prev.length + 1}`, snapshot },
|
|
215
|
+
])
|
|
216
|
+
}
|
|
217
|
+
/>
|
|
218
|
+
<DataViews.Panel.Columns />
|
|
219
|
+
<DataViews.Panel.Sort />
|
|
220
|
+
</DataViews.Panel.Tab>
|
|
221
|
+
|
|
222
|
+
<DataViews.Panel.Tab value="filters" label="Filters" icon={<Filter />}>
|
|
223
|
+
{/* The controls are FormBuilder fields — the same Select and Slider any form in
|
|
224
|
+
this library uses. The <FormBuilder> itself lives inside Filters; you write only
|
|
225
|
+
its fields, and Filters reads each one's name, label and bounds to learn what
|
|
226
|
+
it is. */}
|
|
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
|
+
```
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: DataViews example — Tree → custom UI
|
|
3
|
+
description: Every custom-UI seam of the tree: `renderNode`, `paneRows`, a custom cell, card and tab, `paneActions`, and a whole-pane override.
|
|
4
|
+
group: examples
|
|
5
|
+
component: DataViews
|
|
6
|
+
keywords: [data-views, example, examples, tree, custom]
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# DataViews example — Tree → custom UI
|
|
10
|
+
|
|
11
|
+
Every custom-UI seam of the tree: `renderNode`, `paneRows`, a custom cell, card and tab, `paneActions`, and a whole-pane override.
|
|
12
|
+
|
|
13
|
+
Complete and runnable — this is the page itself, not an excerpt. In the monorepo it lives at `apps/app/data-views/tree-custom/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 { Clock, Filter, Settings } from "lucide-react";
|
|
23
|
+
import { Badge } from "@/components/Badge";
|
|
24
|
+
import { Button } from "@/components/Button";
|
|
25
|
+
import {
|
|
26
|
+
Cell,
|
|
27
|
+
DataViews,
|
|
28
|
+
emptyQuery,
|
|
29
|
+
queryToParams,
|
|
30
|
+
useActiveRow,
|
|
31
|
+
useDataViewsData,
|
|
32
|
+
type SavedView,
|
|
33
|
+
} from "@/components/DataViews";
|
|
34
|
+
import { FormBuilder } from "@/components/FormBuilder";
|
|
35
|
+
import type {
|
|
36
|
+
DataViewsQuery,
|
|
37
|
+
FieldConfig,
|
|
38
|
+
Row,
|
|
39
|
+
TreeNode,
|
|
40
|
+
} from "@/utils/dataViews/types";
|
|
41
|
+
|
|
42
|
+
// ─── Data ─────────────────────────────────────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
interface Order extends Row {
|
|
45
|
+
id: number;
|
|
46
|
+
customer: { name: string };
|
|
47
|
+
brand: { name: string };
|
|
48
|
+
status: "Pending" | "Shipped" | "Delivered";
|
|
49
|
+
priority: "High" | "Medium" | "Low";
|
|
50
|
+
total: number;
|
|
51
|
+
createdAt: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const FIELDS: FieldConfig[] = [
|
|
55
|
+
{ path: "id", label: "Order #", type: "number" },
|
|
56
|
+
{ path: "customer.name", label: "Customer", type: "text" },
|
|
57
|
+
{ path: "brand.name", label: "Brand", type: "text" },
|
|
58
|
+
{ path: "status", label: "Status", type: "enum-badge", variants: { Pending: "yellow", Shipped: "blue", Delivered: "green" } },
|
|
59
|
+
{ path: "priority", label: "Priority", type: "enum-badge", variants: { High: "redOrange", Medium: "purple", Low: "gray" } },
|
|
60
|
+
{ path: "total", label: "Total", type: "currency", currency: "USD" },
|
|
61
|
+
{ path: "createdAt", label: "Created", type: "date-format", dateFormat: "YYYY-MM-DD" },
|
|
62
|
+
];
|
|
63
|
+
|
|
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
|
+
async function fetchOrders(q: DataViewsQuery): Promise<{ rows: Order[]; total: number }> {
|
|
80
|
+
const params = queryToParams(q);
|
|
81
|
+
const res = await fetch(`/api/orders?${params}`);
|
|
82
|
+
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
|
|
83
|
+
return res.json();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A tree of **brands**, each holding its orders — the shape that makes the pane worth
|
|
88
|
+
* customising. A branch is a synthetic node: it stands for a brand, and the row it carries is
|
|
89
|
+
* only there so the rail has something to label it with.
|
|
90
|
+
*/
|
|
91
|
+
const nodesByBrand = (rows: readonly Order[]): TreeNode[] => {
|
|
92
|
+
const brands = [...new Set(rows.map((row) => row.brand?.name).filter(Boolean))];
|
|
93
|
+
return brands.map((brand) => {
|
|
94
|
+
const mine = rows.filter((row) => row.brand?.name === brand);
|
|
95
|
+
return {
|
|
96
|
+
id: `brand:${brand}`,
|
|
97
|
+
// The branch borrows its first order's row so `labelPath` has a value to read. It is not
|
|
98
|
+
// one of the orders — which is exactly why `paneRows` below never lists it.
|
|
99
|
+
row: { ...mine[0], id: `brand:${brand}`, customer: { name: brand } } as Row,
|
|
100
|
+
depth: 0,
|
|
101
|
+
children: mine.map((row) => ({ id: String(row.id), row, depth: 1, children: [] })),
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// ─── Custom UI ────────────────────────────────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
/** A card of your own. `renderPaneCard` replaces `DataViewCard` outright — this is plain markup. */
|
|
109
|
+
function OrderCard({ row }: { row: Row }) {
|
|
110
|
+
const order = row as Order;
|
|
111
|
+
return (
|
|
112
|
+
<article className="border-border-presentation-global-primary bg-background-presentation-form-base flex flex-col gap-2 rounded-[12px] border p-4">
|
|
113
|
+
<div className="flex items-start justify-between gap-2">
|
|
114
|
+
<div className="min-w-0">
|
|
115
|
+
<div className="typography-body-small-medium text-content-presentation-global-tertiary">
|
|
116
|
+
Order #{order.id}
|
|
117
|
+
</div>
|
|
118
|
+
<div className="typography-headers-medium-semibold text-content-presentation-global-primary truncate">
|
|
119
|
+
{order.customer?.name}
|
|
120
|
+
</div>
|
|
121
|
+
</div>
|
|
122
|
+
<Badge
|
|
123
|
+
label={order.status}
|
|
124
|
+
color={order.status === "Delivered" ? "green" : order.status === "Shipped" ? "blue" : "yellow"}
|
|
125
|
+
badgeStyle="subtle"
|
|
126
|
+
showIcon={false}
|
|
127
|
+
/>
|
|
128
|
+
</div>
|
|
129
|
+
{/* A bar is the sort of thing a card can show and a table cell cannot. */}
|
|
130
|
+
<div className="bg-background-presentation-action-disabled h-[6px] w-full overflow-hidden rounded-full">
|
|
131
|
+
<div
|
|
132
|
+
className="bg-background-presentation-state-information-primary h-full rounded-full"
|
|
133
|
+
style={{ width: `${Math.min(100, (order.total / 15000) * 100)}%` }}
|
|
134
|
+
/>
|
|
135
|
+
</div>
|
|
136
|
+
<div className="typography-body-medium-semibold text-content-presentation-global-primary">
|
|
137
|
+
${order.total?.toLocaleString()}
|
|
138
|
+
</div>
|
|
139
|
+
</article>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A tab of your own. It reads `useDataViewsData()` like any other part — and because a tab sits
|
|
145
|
+
* inside the pane's scope, the `rows` it gets are the **selected node's**, already narrowed by
|
|
146
|
+
* `paneRows`. No prop threading, no second copy of the row set.
|
|
147
|
+
*/
|
|
148
|
+
function Timeline() {
|
|
149
|
+
const { rows } = useDataViewsData();
|
|
150
|
+
const ordered = [...rows].sort(
|
|
151
|
+
(a, b) => new Date(String(a.createdAt)).getTime() - new Date(String(b.createdAt)).getTime(),
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<ol className="flex flex-col gap-0 p-6">
|
|
156
|
+
{ordered.map((row, i) => {
|
|
157
|
+
const order = row as Order;
|
|
158
|
+
return (
|
|
159
|
+
<li key={order.id} className="flex gap-3">
|
|
160
|
+
<div className="flex flex-col items-center">
|
|
161
|
+
<span className="bg-background-presentation-state-information-primary mt-[6px] size-[10px] shrink-0 rounded-full" />
|
|
162
|
+
{i < ordered.length - 1 && (
|
|
163
|
+
<span className="bg-border-presentation-global-primary w-px flex-1" />
|
|
164
|
+
)}
|
|
165
|
+
</div>
|
|
166
|
+
<div className="flex-1 pb-5">
|
|
167
|
+
<div className="typography-body-small-regular text-content-presentation-global-tertiary">
|
|
168
|
+
{order.createdAt}
|
|
169
|
+
</div>
|
|
170
|
+
<div className="typography-body-medium-semibold text-content-presentation-global-primary">
|
|
171
|
+
Order #{order.id} · {order.customer?.name}
|
|
172
|
+
</div>
|
|
173
|
+
<div className="typography-body-small-regular text-content-presentation-global-secondary">
|
|
174
|
+
${order.total?.toLocaleString()} · {order.status}
|
|
175
|
+
</div>
|
|
176
|
+
</div>
|
|
177
|
+
</li>
|
|
178
|
+
);
|
|
179
|
+
})}
|
|
180
|
+
</ol>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A pane of your own — anything that is not a tab replaces the built-in pane entirely, switch
|
|
186
|
+
* and all.
|
|
187
|
+
*
|
|
188
|
+
* `useActiveRow()` resolves whatever the rail has selected, and returns nothing when that is a
|
|
189
|
+
* synthetic branch rather than a row: a brand node has no order behind it, so say so rather than
|
|
190
|
+
* rendering an empty shell.
|
|
191
|
+
*/
|
|
192
|
+
function BrandBrief() {
|
|
193
|
+
const row = useActiveRow();
|
|
194
|
+
const { visibleFields } = useDataViewsData();
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
<div className="flex h-full flex-col gap-3 p-6">
|
|
198
|
+
<h3 className="typography-headers-large-semibold text-content-presentation-global-primary">
|
|
199
|
+
A pane of my own
|
|
200
|
+
</h3>
|
|
201
|
+
{row ? (
|
|
202
|
+
<dl className="flex flex-col gap-2">
|
|
203
|
+
{visibleFields.map((field, i) => (
|
|
204
|
+
<div key={`${field.path}-${i}`} className="flex items-center justify-between gap-4">
|
|
205
|
+
<dt className="typography-body-small-regular text-content-presentation-global-tertiary">
|
|
206
|
+
{field.label ?? field.path}
|
|
207
|
+
</dt>
|
|
208
|
+
<dd className="typography-body-medium-semibold text-content-presentation-global-primary">
|
|
209
|
+
<Cell field={field} row={row} />
|
|
210
|
+
</dd>
|
|
211
|
+
</div>
|
|
212
|
+
))}
|
|
213
|
+
</dl>
|
|
214
|
+
) : (
|
|
215
|
+
<p className="typography-body-medium-regular text-content-presentation-global-tertiary">
|
|
216
|
+
Pick an order — a brand is a branch, not a row.
|
|
217
|
+
</p>
|
|
218
|
+
)}
|
|
219
|
+
</div>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ─── Page ─────────────────────────────────────────────────────────────────────
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Custom UI in the tree, one seam at a time.
|
|
227
|
+
*
|
|
228
|
+
* The tree hands you five places to put your own markup, and this page uses every one of them:
|
|
229
|
+
* `renderNode` for the rail, `paneRows` for what the pane lists, `paneTable.renderCell` for a
|
|
230
|
+
* cell, `renderPaneCard` for a card, `paneActions` for the pane's header — and `children` when
|
|
231
|
+
* you want none of it and would rather write the pane yourself.
|
|
232
|
+
*/
|
|
233
|
+
export default function TreeCustomExample() {
|
|
234
|
+
const [query, setQuery] = useState(emptyQuery());
|
|
235
|
+
const [saved, setSaved] = useState<SavedView[]>([]);
|
|
236
|
+
const [ownPane, setOwnPane] = useState(false);
|
|
237
|
+
|
|
238
|
+
const { data, isPending, fetchNextPage, isFetchingNextPage } = useInfiniteQuery({
|
|
239
|
+
queryKey: ["tree-custom-orders", { ...query, page: undefined }],
|
|
240
|
+
queryFn: ({ pageParam }) => fetchOrders({ ...query, page: pageParam }),
|
|
241
|
+
initialPageParam: 1,
|
|
242
|
+
getNextPageParam: (last, pages) => {
|
|
243
|
+
const loaded = pages.reduce((n, page) => n + page.rows.length, 0);
|
|
244
|
+
return loaded < last.total ? pages.length + 1 : undefined;
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const rows = useMemo(() => data?.pages.flatMap((page) => page.rows) ?? [], [data]);
|
|
249
|
+
const total = data?.pages[0]?.total ?? 0;
|
|
250
|
+
const nodes = useMemo(() => nodesByBrand(rows), [rows]);
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
<div className="flex h-full min-h-0 flex-col p-4">
|
|
254
|
+
<DataViews
|
|
255
|
+
rows={rows}
|
|
256
|
+
fields={FIELDS}
|
|
257
|
+
total={total}
|
|
258
|
+
loading={isPending}
|
|
259
|
+
onLoadMore={fetchNextPage}
|
|
260
|
+
loadingMore={isFetchingNextPage}
|
|
261
|
+
onQueryChange={setQuery}
|
|
262
|
+
className="h-full"
|
|
263
|
+
>
|
|
264
|
+
<DataViews.Header title="Brands">
|
|
265
|
+
<DataViews.ViewSwitch />
|
|
266
|
+
<DataViews.Search />
|
|
267
|
+
<DataViews.Actions>
|
|
268
|
+
<Button variant="BluColStyle" size="M" onClick={() => setOwnPane(!ownPane)}>
|
|
269
|
+
{ownPane ? "Built-in pane" : "Write my own pane"}
|
|
270
|
+
</Button>
|
|
271
|
+
</DataViews.Actions>
|
|
272
|
+
<DataViews.PanelToggle />
|
|
273
|
+
</DataViews.Header>
|
|
274
|
+
|
|
275
|
+
<DataViews.Tree
|
|
276
|
+
nodes={nodes}
|
|
277
|
+
labelPath="customer.name"
|
|
278
|
+
// ① The rail. `renderNode` returns *parts*, not markup — `TreeFolder` owns the row.
|
|
279
|
+
renderNode={({ node, row }) =>
|
|
280
|
+
node.children.length > 0
|
|
281
|
+
? {
|
|
282
|
+
// `TreeFolder` already prints the child count, so the name carries what it
|
|
283
|
+
// cannot: what the brand is worth.
|
|
284
|
+
name: `${(row as Order).customer?.name} · $${node.children
|
|
285
|
+
.reduce((sum, child) => sum + Number(child.row.total ?? 0), 0)
|
|
286
|
+
.toLocaleString()}`,
|
|
287
|
+
icon: <i className="ri-price-tag-3-line" />,
|
|
288
|
+
}
|
|
289
|
+
: {
|
|
290
|
+
meta: (
|
|
291
|
+
<Badge
|
|
292
|
+
label={String(row.status)}
|
|
293
|
+
color={row.status === "Delivered" ? "green" : row.status === "Shipped" ? "blue" : "yellow"}
|
|
294
|
+
badgeStyle="subtle"
|
|
295
|
+
size="S"
|
|
296
|
+
showIcon={false}
|
|
297
|
+
/>
|
|
298
|
+
),
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// ② What the pane lists. The default is every descendant; this one drops the synthetic
|
|
302
|
+
// brand branch and sorts by value, which is also how you make the pane's order
|
|
303
|
+
// self-contained — the table's own headers sort by re-querying the dataset.
|
|
304
|
+
paneRows={(node) =>
|
|
305
|
+
(node.children.length > 0 ? node.children.map((child) => child.row) : [node.row])
|
|
306
|
+
.filter((row) => typeof row.id === "number")
|
|
307
|
+
.slice()
|
|
308
|
+
.sort((a, b) => Number(b.total) - Number(a.total))
|
|
309
|
+
}
|
|
310
|
+
// ③ Your markup in the pane's header, beside the tab switch.
|
|
311
|
+
paneActions={
|
|
312
|
+
<Button variant="BluColStyle" size="M">
|
|
313
|
+
New order
|
|
314
|
+
</Button>
|
|
315
|
+
}
|
|
316
|
+
>
|
|
317
|
+
{/* ④ The pane's tabs are children, the same bargain as the component's own views: the
|
|
318
|
+
switch shows exactly what is rendered here. Pass none and you get these two anyway. */}
|
|
319
|
+
{ownPane ? (
|
|
320
|
+
// ⑦ …and anything that is not a tab is the pane itself — switch, header and all.
|
|
321
|
+
<BrandBrief />
|
|
322
|
+
) : (
|
|
323
|
+
<>
|
|
324
|
+
<DataViews.Tree.Table
|
|
325
|
+
selectable
|
|
326
|
+
// A cell of your own. Return `undefined` for the fields you have no opinion
|
|
327
|
+
// about and the default cell paints them.
|
|
328
|
+
renderCell={({ field, row }) =>
|
|
329
|
+
field.path === "total" ? (
|
|
330
|
+
<span className="flex w-full items-center gap-2">
|
|
331
|
+
<span className="bg-background-presentation-action-disabled h-[6px] flex-1 overflow-hidden rounded-full">
|
|
332
|
+
<span
|
|
333
|
+
className="bg-background-presentation-state-information-primary block h-full rounded-full"
|
|
334
|
+
style={{ width: `${Math.min(100, (Number(row.total) / 15000) * 100)}%` }}
|
|
335
|
+
/>
|
|
336
|
+
</span>
|
|
337
|
+
<span className="typography-body-small-medium shrink-0">
|
|
338
|
+
${Number(row.total).toLocaleString()}
|
|
339
|
+
</span>
|
|
340
|
+
</span>
|
|
341
|
+
) : undefined
|
|
342
|
+
}
|
|
343
|
+
/>
|
|
344
|
+
{/* ⑤ A card of your own in cards mode. */}
|
|
345
|
+
<DataViews.Tree.Cards renderCard={({ row }) => <OrderCard row={row} />} />
|
|
346
|
+
{/* ⑥ A tab that is entirely yours — no default content, just markup. */}
|
|
347
|
+
<DataViews.Tree.Tab value="timeline" label="Timeline" icon={<Clock />}>
|
|
348
|
+
<Timeline />
|
|
349
|
+
</DataViews.Tree.Tab>
|
|
350
|
+
</>
|
|
351
|
+
)}
|
|
352
|
+
</DataViews.Tree>
|
|
353
|
+
|
|
354
|
+
<DataViews.Panel>
|
|
355
|
+
<DataViews.Panel.Tab value="config" label="Config." icon={<Settings />}>
|
|
356
|
+
<DataViews.Panel.SavedViews
|
|
357
|
+
views={saved}
|
|
358
|
+
onSave={(snapshot) =>
|
|
359
|
+
setSaved((prev) => [
|
|
360
|
+
...prev,
|
|
361
|
+
{ id: `view-${prev.length + 1}`, label: `View ${prev.length + 1}`, snapshot },
|
|
362
|
+
])
|
|
363
|
+
}
|
|
364
|
+
/>
|
|
365
|
+
<DataViews.Panel.Columns />
|
|
366
|
+
<DataViews.Panel.Sort />
|
|
367
|
+
</DataViews.Panel.Tab>
|
|
368
|
+
|
|
369
|
+
<DataViews.Panel.Tab value="filters" label="Filters" icon={<Filter />}>
|
|
370
|
+
<DataViews.Filters title={null} className="border-b-0 p-0">
|
|
371
|
+
<FormBuilder.CheckboxGroup name="status" label="Status" options={STATUS_OPTIONS} />
|
|
372
|
+
<FormBuilder.RadioList name="priority" label="Priority" options={PRIORITY_OPTIONS} />
|
|
373
|
+
<FormBuilder.SearchableSelect
|
|
374
|
+
name="customer.name"
|
|
375
|
+
label="Customer"
|
|
376
|
+
options={CUSTOMER_OPTIONS}
|
|
377
|
+
/>
|
|
378
|
+
<FormBuilder.MultiSelect name="brand.name" label="Brand" options={BRAND_OPTIONS} />
|
|
379
|
+
<FormBuilder.Slider name="total" label="Total" range min={0} max={15000} step={100} />
|
|
380
|
+
<FormBuilder.DateRange name="createdAt" label="Created" />
|
|
381
|
+
</DataViews.Filters>
|
|
382
|
+
</DataViews.Panel.Tab>
|
|
383
|
+
</DataViews.Panel>
|
|
384
|
+
</DataViews>
|
|
385
|
+
</div>
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
```
|