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,67 @@
|
|
|
1
|
+
import type { Path, Row } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Split cache. Bounded on purpose — the previous implementation used an unbounded module-level
|
|
5
|
+
* `Map`, which in a long-lived app grows for every distinct path string it ever sees. Paths come
|
|
6
|
+
* from a fixed field config in practice, so a small cap costs nothing and removes the leak.
|
|
7
|
+
*/
|
|
8
|
+
const CACHE_LIMIT = 512;
|
|
9
|
+
const cache = new Map<string, string[]>();
|
|
10
|
+
|
|
11
|
+
function split(path: Path): string[] {
|
|
12
|
+
const hit = cache.get(path);
|
|
13
|
+
if (hit) return hit;
|
|
14
|
+
const parts = path.split(".");
|
|
15
|
+
if (cache.size >= CACHE_LIMIT) cache.clear();
|
|
16
|
+
cache.set(path, parts);
|
|
17
|
+
return parts;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Read a dotted path off a row. Returns `undefined` for any missing link in the chain. */
|
|
21
|
+
export function getByPath(obj: unknown, path: Path | undefined | null): unknown {
|
|
22
|
+
if (obj == null || path == null || path === "") return undefined;
|
|
23
|
+
if (typeof obj !== "object") return undefined;
|
|
24
|
+
let cur: unknown = obj;
|
|
25
|
+
for (const key of split(path)) {
|
|
26
|
+
if (cur == null) return undefined;
|
|
27
|
+
cur = (cur as Record<string, unknown>)[key];
|
|
28
|
+
}
|
|
29
|
+
return cur;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Read a path and coerce to string for display/compare. `undefined`/`null` become `""`. */
|
|
33
|
+
export function getString(obj: unknown, path: Path | undefined | null): string {
|
|
34
|
+
const v = getByPath(obj, path);
|
|
35
|
+
return v == null ? "" : String(v);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `"created_at"` / `"createdAt"` → `"Created At"`. Uses the last segment of a dotted path. */
|
|
39
|
+
export function formatPathLabel(path: Path): string {
|
|
40
|
+
if (!path) return "";
|
|
41
|
+
const tail = path.includes(".") ? path.split(".").pop()! : path;
|
|
42
|
+
|
|
43
|
+
if (tail.includes("_")) {
|
|
44
|
+
return tail
|
|
45
|
+
.split("_")
|
|
46
|
+
.filter(Boolean)
|
|
47
|
+
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
|
|
48
|
+
.join(" ");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return tail
|
|
52
|
+
.replace(/([A-Z])/g, " $1")
|
|
53
|
+
.replace(/^./, (s) => s.toUpperCase())
|
|
54
|
+
.trim();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The default row identity: the first of `row.id`, `row._id`, `row.uuid`, else the array index.
|
|
59
|
+
*
|
|
60
|
+
* The previous implementation fell back through three candidates ending in the array index, so
|
|
61
|
+
* selection and drag-and-drop both broke the moment rows were reordered. Pass `getRowId` when
|
|
62
|
+
* your id lives elsewhere.
|
|
63
|
+
*/
|
|
64
|
+
export function defaultGetRowId(row: Row, index: number): string {
|
|
65
|
+
const id = row.id ?? row._id ?? row.uuid;
|
|
66
|
+
return id == null ? String(index) : String(id);
|
|
67
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { DataViewsQuery, FilterState, Sort } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The wire format for a `DataViewsQuery`, both directions.
|
|
5
|
+
*
|
|
6
|
+
* A query is written in the browser and read on the server, and the two have to agree exactly.
|
|
7
|
+
* Keeping the encoder and the decoder in one file is what makes that true by construction —
|
|
8
|
+
* split across a page and a route handler, they drift the first time either side gains a field.
|
|
9
|
+
*
|
|
10
|
+
* No React here, like its neighbours `path.ts` and `types.ts`, so a route handler can import it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** A query nobody has touched yet: everything, first page. */
|
|
14
|
+
export function emptyQuery(overrides?: Partial<DataViewsQuery>): DataViewsQuery {
|
|
15
|
+
return { search: "", filters: {}, sort: null, page: 1, pageSize: 10, ...overrides };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Browser → wire.
|
|
20
|
+
*
|
|
21
|
+
* `filters` goes as JSON because it is a nested structure, not a flat list of values. `sort` goes
|
|
22
|
+
* as `total:desc` — one parameter rather than two, since neither half means anything alone.
|
|
23
|
+
*
|
|
24
|
+
* Extra parameters an endpoint of your own needs are yours to append:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* const params = queryToParams(query);
|
|
28
|
+
* params.set("shape", "wide");
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function queryToParams(query: DataViewsQuery): URLSearchParams {
|
|
32
|
+
return new URLSearchParams({
|
|
33
|
+
search: query.search,
|
|
34
|
+
filters: JSON.stringify(query.filters),
|
|
35
|
+
sort: query.sort ? `${query.sort.path}:${query.sort.direction}` : "",
|
|
36
|
+
page: String(query.page),
|
|
37
|
+
pageSize: String(query.pageSize),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Wire → server.
|
|
43
|
+
*
|
|
44
|
+
* Malformed `filters` is treated as "no filters" rather than a 400: a filter you cannot parse
|
|
45
|
+
* should not take the page down. `pageSize` is clamped, because it arrives from the client and a
|
|
46
|
+
* request for a million rows is a request to fall over.
|
|
47
|
+
*/
|
|
48
|
+
export function parseQuery(url: URL): DataViewsQuery {
|
|
49
|
+
const params = url.searchParams;
|
|
50
|
+
|
|
51
|
+
let filters: FilterState = {};
|
|
52
|
+
const raw = params.get("filters");
|
|
53
|
+
if (raw) {
|
|
54
|
+
try {
|
|
55
|
+
filters = JSON.parse(raw) as FilterState;
|
|
56
|
+
} catch {
|
|
57
|
+
filters = {};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const [sortPath, sortDir] = (params.get("sort") ?? "").split(":");
|
|
62
|
+
const sort: Sort = sortPath
|
|
63
|
+
? { path: sortPath, direction: sortDir === "desc" ? "desc" : "asc" }
|
|
64
|
+
: null;
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
search: params.get("search") ?? "",
|
|
68
|
+
filters,
|
|
69
|
+
sort,
|
|
70
|
+
page: Math.max(1, Number(params.get("page")) || 1),
|
|
71
|
+
pageSize: Math.min(500, Math.max(1, Number(params.get("pageSize")) || 10)),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The vocabulary DataViews speaks — nothing more.
|
|
3
|
+
*
|
|
4
|
+
* DataViews does no data work: it never filters, searches, sorts, groups, or infers a schema.
|
|
5
|
+
* Those are the app's job, almost always server-side. What is described here is only what the
|
|
6
|
+
* component needs in order to **paint** rows and to **emit intent** back to you:
|
|
7
|
+
* `FilterState` and `Sort` are things it hands you so you can go and query, not things it applies.
|
|
8
|
+
*
|
|
9
|
+
* These types live in the utils layer so the dependency runs one way — the component imports its
|
|
10
|
+
* vocabulary from here, and nothing here imports React or any component.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { ReactNode } from "react";
|
|
14
|
+
|
|
15
|
+
/** A row is any record. DataViews never assumes a shape beyond the id resolved by `getRowId`. */
|
|
16
|
+
export type Row = Record<string, unknown>;
|
|
17
|
+
|
|
18
|
+
/** A dotted accessor into a row, e.g. `"customer.name"`. */
|
|
19
|
+
export type Path = string;
|
|
20
|
+
|
|
21
|
+
export type FieldType =
|
|
22
|
+
| "text"
|
|
23
|
+
| "number"
|
|
24
|
+
| "date"
|
|
25
|
+
| "boolean"
|
|
26
|
+
| "hidden"
|
|
27
|
+
| "enum-badge"
|
|
28
|
+
| "badge-array"
|
|
29
|
+
| "currency"
|
|
30
|
+
| "number-format"
|
|
31
|
+
| "progress-bar"
|
|
32
|
+
| "star-rating"
|
|
33
|
+
| "icon-text"
|
|
34
|
+
| "two-line"
|
|
35
|
+
| "avatar"
|
|
36
|
+
| "link"
|
|
37
|
+
| "image"
|
|
38
|
+
| "date-format";
|
|
39
|
+
|
|
40
|
+
export type BadgeVariant =
|
|
41
|
+
| "green"
|
|
42
|
+
| "greenLight"
|
|
43
|
+
| "cocktailGreen"
|
|
44
|
+
| "yellow"
|
|
45
|
+
| "redOrange"
|
|
46
|
+
| "redLight"
|
|
47
|
+
| "rose"
|
|
48
|
+
| "purple"
|
|
49
|
+
| "bluePurple"
|
|
50
|
+
| "blue"
|
|
51
|
+
| "navy"
|
|
52
|
+
| "gray"
|
|
53
|
+
| "highlight";
|
|
54
|
+
|
|
55
|
+
/** Palette keys for a board column pill. */
|
|
56
|
+
export type ColumnColor = "gray" | "purple" | "orange" | "blue" | "green" | "red";
|
|
57
|
+
|
|
58
|
+
export type CurrencyOptions = {
|
|
59
|
+
symbol?: string;
|
|
60
|
+
locale?: string;
|
|
61
|
+
decimals?: number;
|
|
62
|
+
code?: string;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** How one field is read and painted. You author these — nothing is inferred from the data. */
|
|
66
|
+
export type FieldConfig = {
|
|
67
|
+
path: Path;
|
|
68
|
+
label?: string;
|
|
69
|
+
type?: FieldType;
|
|
70
|
+
visible?: boolean;
|
|
71
|
+
|
|
72
|
+
/** Value → badge colour, for `enum-badge` / `badge-array`. */
|
|
73
|
+
variants?: Record<string, BadgeVariant>;
|
|
74
|
+
defaultVariant?: BadgeVariant;
|
|
75
|
+
variant?: BadgeVariant;
|
|
76
|
+
|
|
77
|
+
limit?: number;
|
|
78
|
+
currency?: string | CurrencyOptions;
|
|
79
|
+
format?: Intl.NumberFormatOptions;
|
|
80
|
+
thresholds?: [number, number];
|
|
81
|
+
max?: number;
|
|
82
|
+
icon?: string;
|
|
83
|
+
iconPosition?: "before" | "after";
|
|
84
|
+
secondaryPath?: Path;
|
|
85
|
+
linkType?: "mailto" | "tel" | "url";
|
|
86
|
+
fallbackPath?: Path;
|
|
87
|
+
dateFormat?: string | Intl.DateTimeFormatOptions;
|
|
88
|
+
trueLabel?: string;
|
|
89
|
+
falseLabel?: string;
|
|
90
|
+
trueVariant?: BadgeVariant;
|
|
91
|
+
falseVariant?: BadgeVariant;
|
|
92
|
+
|
|
93
|
+
/** Paint this field yourself. Wins over `type`. */
|
|
94
|
+
render?: (value: unknown, row: Row) => ReactNode;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** A column as the user has arranged it — the projection the config panel edits. */
|
|
98
|
+
export type ColumnState = {
|
|
99
|
+
path: Path;
|
|
100
|
+
label: string;
|
|
101
|
+
visible: boolean;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
type SortDirection = "asc" | "desc";
|
|
105
|
+
/** Which column the header shows as sorted. Emitted on click; the component never re-orders rows. */
|
|
106
|
+
export type Sort = { path: Path; direction: SortDirection } | null;
|
|
107
|
+
|
|
108
|
+
type NumericRange = { kind: "number"; min?: number; max?: number };
|
|
109
|
+
type DateRange = { kind: "date"; from?: string; to?: string };
|
|
110
|
+
type RangeValue = NumericRange | DateRange;
|
|
111
|
+
/** A categorical selection, or a range. */
|
|
112
|
+
export type FilterValue = string[] | RangeValue;
|
|
113
|
+
/** Keyed by field path. */
|
|
114
|
+
export type FilterState = Record<Path, FilterValue>;
|
|
115
|
+
|
|
116
|
+
/** A one-click shortcut on a numeric filter, e.g. "Under 500". */
|
|
117
|
+
export type NumberPreset = { label: string; min?: number; max?: number };
|
|
118
|
+
/** A one-click shortcut on a date filter, e.g. "Last 30 days". */
|
|
119
|
+
export type DatePreset = { label: string; from?: string; to?: string };
|
|
120
|
+
/** `Filters.Presets` takes both shapes; each control renders the ones it understands. */
|
|
121
|
+
export type Preset = NumberPreset | DatePreset;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Everything the user asked for, in one object.
|
|
125
|
+
*
|
|
126
|
+
* This is the whole contract between `DataViews` and your data layer. The component collects it —
|
|
127
|
+
* a search typed in the header, filters set in the rail, a column header clicked, a page turned —
|
|
128
|
+
* and hands it over whole; going and fetching the matching rows is your half.
|
|
129
|
+
*
|
|
130
|
+
* It is one object rather than five callbacks because it is one question. A query with a new
|
|
131
|
+
* filter and a stale page number is not a query anyone meant to ask, and keeping the parts
|
|
132
|
+
* together is what lets the component keep them consistent — changing a filter resets `page` to 1,
|
|
133
|
+
* because a different result set has no page 4 to stay on.
|
|
134
|
+
*/
|
|
135
|
+
export interface DataViewsQuery {
|
|
136
|
+
search: string;
|
|
137
|
+
filters: FilterState;
|
|
138
|
+
sort: Sort;
|
|
139
|
+
/** 1-based. */
|
|
140
|
+
page: number;
|
|
141
|
+
pageSize: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** How a filter maps between its control's value and `FilterState`. */
|
|
145
|
+
type FilterKind = "choice" | "number" | "date" | "text";
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* What `DataViews.Filters` learned about one of its children.
|
|
149
|
+
*
|
|
150
|
+
* You never write these: filters are authored as `FormBuilder` fields, and `Filters` reads each
|
|
151
|
+
* child's `name`, `label` and bounds off the element to build this. It exists because the mapping
|
|
152
|
+
* to `FilterState` needs to know a slider from a multi-select — a `[0, 500]` is a numeric range,
|
|
153
|
+
* while `["a", "b"]` is a selection, and the two are indistinguishable once they are just values.
|
|
154
|
+
*/
|
|
155
|
+
export type FilterFieldDescriptor = {
|
|
156
|
+
path: Path;
|
|
157
|
+
label?: string;
|
|
158
|
+
kind: FilterKind;
|
|
159
|
+
/** Single-valued choice (`FormBuilder.Select`) rather than multi (`.MultiSelect`). */
|
|
160
|
+
single?: boolean;
|
|
161
|
+
/** Slider bounds, read from the child. A slider resting on them emits no filter at all. */
|
|
162
|
+
min?: number;
|
|
163
|
+
max?: number;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export type TreeNode = {
|
|
167
|
+
id: string;
|
|
168
|
+
row: Row;
|
|
169
|
+
children: TreeNode[];
|
|
170
|
+
depth: number;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/** Emitted by a board or tree drag. The component never applies it. */
|
|
174
|
+
export type MoveIntent = {
|
|
175
|
+
id: string;
|
|
176
|
+
from: string | null;
|
|
177
|
+
to: string | null;
|
|
178
|
+
index?: number;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/** A pre-grouped column for the board view. You build these; the component only paints them. */
|
|
182
|
+
export type RowGroup = {
|
|
183
|
+
id: string;
|
|
184
|
+
label: string;
|
|
185
|
+
color?: ColumnColor;
|
|
186
|
+
rows: Row[];
|
|
187
|
+
};
|
package/docs/components/card.md
CHANGED
|
@@ -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`
|