@vitavision/forms 0.8.3 → 0.8.5
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/README.md +18 -0
- package/dist/index.d.ts +105 -2
- package/dist/index.js +365 -55
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/uiSchema.ts +62 -0
- package/src/api/valueDiff.ts +79 -0
- package/src/components/SchemaValueForm.tsx +71 -14
- package/src/components/valueForm/changes.tsx +137 -0
- package/src/components/valueForm/chrome.tsx +34 -5
- package/src/components/valueForm/context.ts +25 -1
- package/src/components/valueForm/leaves.tsx +70 -18
- package/src/components/valueForm/nodes.tsx +18 -11
- package/src/index.ts +3 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitavision/forms",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.5",
|
|
4
4
|
"description": "JSON Schema (draft 2020-12, schemars output) rendered as a vitavision options form.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"react-dom": "^19.2.0"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@vitavision/ui": "^0.14.
|
|
50
|
+
"@vitavision/ui": "^0.14.3"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@storybook/react-vite": "10.6.0",
|
package/src/api/uiSchema.ts
CHANGED
|
@@ -12,6 +12,14 @@ import type { ReactNode } from "react";
|
|
|
12
12
|
|
|
13
13
|
import type { JsonSchema } from "./schemaNode";
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Where a field's label goes. `top` stacks the label over the control. `side` is the
|
|
17
|
+
* property-sheet row: the label in a fixed column on the left, the control on the right,
|
|
18
|
+
* about 32 px tall, with the description behind the hint icon. Objects, unions, tuples and
|
|
19
|
+
* lists keep their label above and span the full width either way.
|
|
20
|
+
*/
|
|
21
|
+
export type LabelPlacement = "top" | "side";
|
|
22
|
+
|
|
15
23
|
/** A titled set of fields, rendered together. */
|
|
16
24
|
export interface UiGroup {
|
|
17
25
|
/** Stable id, used for keys and test hooks. */
|
|
@@ -28,6 +36,16 @@ export interface UiGroup {
|
|
|
28
36
|
collapsible?: boolean | undefined;
|
|
29
37
|
/** Whether a collapsible group starts open. Defaults to closed. */
|
|
30
38
|
defaultOpen?: boolean | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Lay the group's fields out on one row, each with its label above its control, instead
|
|
41
|
+
* of the form's grid: for a few small numbers that read as one setting (rows × columns ×
|
|
42
|
+
* square size; iterations and tolerances). Give each field a short `label` in `fields`
|
|
43
|
+
* of the `UiSchema`. A row has no room for descriptions or range notes, so they are
|
|
44
|
+
* dropped, and a field that is not a leaf (an object, a list) should stay out of it.
|
|
45
|
+
*/
|
|
46
|
+
inline?: boolean | undefined;
|
|
47
|
+
/** This group's label placement, over the form's. */
|
|
48
|
+
labelPlacement?: LabelPlacement | undefined;
|
|
31
49
|
/** A line of explanation under the heading. */
|
|
32
50
|
hint?: ReactNode;
|
|
33
51
|
}
|
|
@@ -61,6 +79,17 @@ export interface FieldUi {
|
|
|
61
79
|
* `{ brown_conrady5: "Brown-Conrady" }`.
|
|
62
80
|
*/
|
|
63
81
|
enumLabels?: Record<string, string> | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* For an object (or a union's variant): the order of its fields, by key. Keys not listed
|
|
84
|
+
* follow in schema order, and keys that do not exist are ignored. Use it where the
|
|
85
|
+
* schema's own order is not the one to read in (a schema whose keys arrive sorted).
|
|
86
|
+
*/
|
|
87
|
+
order?: string[] | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* For an object: lay its fields out on one row with their labels above, like a group's
|
|
90
|
+
* `inline`. The object keeps its frame and label.
|
|
91
|
+
*/
|
|
92
|
+
inline?: boolean | undefined;
|
|
64
93
|
/** Columns the field takes in the grid. Containers and lists default to the full row. */
|
|
65
94
|
span?: 1 | 2 | undefined;
|
|
66
95
|
/**
|
|
@@ -105,6 +134,23 @@ export interface UiSchema {
|
|
|
105
134
|
groups?: UiGroup[] | undefined;
|
|
106
135
|
/** Per-field presentation, by dot path. */
|
|
107
136
|
fields?: Record<string, FieldUi> | undefined;
|
|
137
|
+
/**
|
|
138
|
+
* The order of the root object's ungrouped fields, by key. Keys not listed follow in
|
|
139
|
+
* schema order. (Fields inside a group follow the group's `fields`; a nested object's
|
|
140
|
+
* order is `FieldUi.order`.)
|
|
141
|
+
*/
|
|
142
|
+
order?: string[] | undefined;
|
|
143
|
+
/**
|
|
144
|
+
* Where labels go, for the whole form: `top` (the default) or `side`. A group can
|
|
145
|
+
* override it, and so can the form's prop, which wins over this.
|
|
146
|
+
*/
|
|
147
|
+
labelPlacement?: LabelPlacement | undefined;
|
|
148
|
+
/**
|
|
149
|
+
* Where descriptions go for every field the app did not say otherwise about (see
|
|
150
|
+
* `FieldUi.descriptionAs`). Defaults to `hint`, the same in both placements: a long
|
|
151
|
+
* description is a tooltip, never a line in the form.
|
|
152
|
+
*/
|
|
153
|
+
descriptionAs?: "hint" | "inline" | "none" | undefined;
|
|
108
154
|
}
|
|
109
155
|
|
|
110
156
|
/** What `renderField` is given for a field. */
|
|
@@ -146,3 +192,19 @@ export function fieldUiAt(ui: UiSchema | undefined, path: string): FieldUi {
|
|
|
146
192
|
if (general === undefined) return exact ?? NO_UI;
|
|
147
193
|
return exact === undefined ? general : { ...general, ...exact };
|
|
148
194
|
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Put the keys an app listed first, in its order, and the rest after them in their own.
|
|
198
|
+
*
|
|
199
|
+
* @param fields - Items with a `key`, in schema order.
|
|
200
|
+
* @param order - The keys to bring forward; unknown keys are ignored.
|
|
201
|
+
* @returns A new array when `order` changes anything, otherwise `fields` itself.
|
|
202
|
+
*/
|
|
203
|
+
export function orderFields<T extends { key: string }>(fields: T[], order: readonly string[] | undefined): T[] {
|
|
204
|
+
if (order === undefined || order.length === 0) return fields;
|
|
205
|
+
const rank = new Map<string, number>();
|
|
206
|
+
for (const key of order) if (!rank.has(key)) rank.set(key, rank.size);
|
|
207
|
+
const listed = fields.filter((field) => rank.has(field.key)).sort((a, b) => (rank.get(a.key) ?? 0) - (rank.get(b.key) ?? 0));
|
|
208
|
+
const rest = fields.filter((field) => !rank.has(field.key));
|
|
209
|
+
return [...listed, ...rest];
|
|
210
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where one JSON value differs from another, by dot path: what a form marks as "changed from
|
|
3
|
+
* the baseline".
|
|
4
|
+
*
|
|
5
|
+
* Paths are the ones `getAtPath` reads. A value and its baseline are compared structurally:
|
|
6
|
+
* objects by key (a key missing on one side differs from a key present on the other, even
|
|
7
|
+
* if the present value is `null`), arrays by index when they have the same length, and by
|
|
8
|
+
* the array as a whole when they do not (its elements have no shared identity to match).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { joinPath } from "./valuePath";
|
|
12
|
+
|
|
13
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
14
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The paths at which `current` differs from `baseline`, deepest first-found, in key order.
|
|
19
|
+
*
|
|
20
|
+
* @param current - The value.
|
|
21
|
+
* @param baseline - What it is compared with.
|
|
22
|
+
* @param path - The path of both, for a recursive call; `""` is the root.
|
|
23
|
+
* @returns Each differing path once; `[""]` when the roots are different kinds of value.
|
|
24
|
+
* Empty when the values are equal.
|
|
25
|
+
*/
|
|
26
|
+
export function diffPaths(current: unknown, baseline: unknown, path = ""): string[] {
|
|
27
|
+
if (Object.is(current, baseline)) return [];
|
|
28
|
+
if (isRecord(current) && isRecord(baseline)) {
|
|
29
|
+
const keys = [...new Set([...Object.keys(current), ...Object.keys(baseline)])];
|
|
30
|
+
return keys.flatMap((key) => {
|
|
31
|
+
const inCurrent = Object.hasOwn(current, key);
|
|
32
|
+
const inBaseline = Object.hasOwn(baseline, key);
|
|
33
|
+
const child = joinPath(path, key);
|
|
34
|
+
return inCurrent && inBaseline ? diffPaths(current[key], baseline[key], child) : [child];
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (Array.isArray(current) && Array.isArray(baseline) && current.length === baseline.length) {
|
|
38
|
+
return current.flatMap((entry, index) => diffPaths(entry, baseline[index], joinPath(path, String(index))));
|
|
39
|
+
}
|
|
40
|
+
return [path];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* How many differing leaves lie at or under each path.
|
|
45
|
+
*
|
|
46
|
+
* @param paths - From `diffPaths`.
|
|
47
|
+
* @returns A map from a path, and each of its ancestors (the root as `""`), to the count of
|
|
48
|
+
* differing paths at or beneath it.
|
|
49
|
+
*/
|
|
50
|
+
export function changeCounts(paths: readonly string[]): Map<string, number> {
|
|
51
|
+
const counts = new Map<string, number>();
|
|
52
|
+
for (const path of paths) {
|
|
53
|
+
let at = path;
|
|
54
|
+
for (;;) {
|
|
55
|
+
counts.set(at, (counts.get(at) ?? 0) + 1);
|
|
56
|
+
if (at === "") break;
|
|
57
|
+
const cut = at.lastIndexOf(".");
|
|
58
|
+
at = cut < 0 ? "" : at.slice(0, cut);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return counts;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A value as the "was ..." note shows it: short, one line.
|
|
66
|
+
*
|
|
67
|
+
* @param value - The baseline's value at a field.
|
|
68
|
+
* @param limit - The longest text, in characters; a longer one is cut with an ellipsis. Defaults to 40.
|
|
69
|
+
* @returns `unset` for `undefined`, `none` for `null`, `""` quoted, else the value or its JSON.
|
|
70
|
+
*/
|
|
71
|
+
export function describeValue(value: unknown, limit = 40): string {
|
|
72
|
+
let text: string;
|
|
73
|
+
if (value === undefined) text = "unset";
|
|
74
|
+
else if (value === null) text = "none";
|
|
75
|
+
else if (typeof value === "string") text = value === "" ? '""' : value;
|
|
76
|
+
else if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") text = value.toString();
|
|
77
|
+
else text = JSON.stringify(value) ?? "";
|
|
78
|
+
return text.length > limit ? `${text.slice(0, limit - 1)}…` : text;
|
|
79
|
+
}
|
|
@@ -10,18 +10,24 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { Disclosure, Section, TooltipProvider, cn } from "@vitavision/ui";
|
|
13
|
-
import { useMemo, useState, type Ref } from "react";
|
|
13
|
+
import { useMemo, useState, type CSSProperties, type Ref } from "react";
|
|
14
14
|
|
|
15
15
|
import type { JsonSchema } from "../api/schemaNode";
|
|
16
16
|
import { defaultValueForSchema, fieldAt, shapeOf } from "../api/schemaValue";
|
|
17
|
-
import { fieldUiAt, type RenderField, type UiGroup, type UiSchema } from "../api/uiSchema";
|
|
17
|
+
import { fieldUiAt, type LabelPlacement, type RenderField, type UiGroup, type UiSchema } from "../api/uiSchema";
|
|
18
|
+
import { changeCounts, diffPaths } from "../api/valueDiff";
|
|
18
19
|
import { setAtPath } from "../api/valuePath";
|
|
19
|
-
import { Grid } from "./valueForm/chrome";
|
|
20
|
-
import { FormContext, useForm, type FormState } from "./valueForm/context";
|
|
20
|
+
import { Grid, InlineRow } from "./valueForm/chrome";
|
|
21
|
+
import { FormContext, useForm, type FieldMessage, type FormState } from "./valueForm/context";
|
|
21
22
|
import { FieldNode, ObjectBody } from "./valueForm/nodes";
|
|
22
23
|
|
|
23
24
|
const NO_UI: UiSchema = {};
|
|
24
25
|
|
|
26
|
+
/** Messages from the app, by dot path (`""` is the form as a whole). A bare string is an error. */
|
|
27
|
+
export type FieldMessages = Readonly<Record<string, FieldMessage | string>>;
|
|
28
|
+
|
|
29
|
+
export type { FieldMessage };
|
|
30
|
+
|
|
25
31
|
/** Props of `SchemaValueForm`. */
|
|
26
32
|
export interface SchemaValueFormProps {
|
|
27
33
|
/**
|
|
@@ -43,8 +49,34 @@ export interface SchemaValueFormProps {
|
|
|
43
49
|
onValueChange?: ((next: unknown) => void) | undefined;
|
|
44
50
|
/** Layout the schema cannot carry: groups, labels, units, widgets. */
|
|
45
51
|
ui?: UiSchema | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* The value to compare with, such as the defaults or a previous run. A field whose value
|
|
54
|
+
* differs from it is marked "changed" (a rule in the gutter and a note under the control
|
|
55
|
+
* saying what it was), with a Reset that writes the baseline's value back through
|
|
56
|
+
* `onValueChange`; a struct, a variant or a tuple shows how many leaves under it changed
|
|
57
|
+
* and one Reset for them all. A key missing on one side differs from a key present on the
|
|
58
|
+
* other; arrays of equal length compare by index, others as a whole. Without it, nothing is marked.
|
|
59
|
+
*/
|
|
60
|
+
baseline?: unknown;
|
|
61
|
+
/**
|
|
62
|
+
* Messages to show on fields, by dot path (`solver.max_iters`, `cameras.0.id`, the paths
|
|
63
|
+
* `getAtPath` reads), for validation that comes from elsewhere, such as a backend. An `error`
|
|
64
|
+
* (or a bare string) is announced as an alert and marks the control invalid; a `warning`
|
|
65
|
+
* is shown under it without blocking. A path of `""` shows above the form. A path that no
|
|
66
|
+
* visible field has shows nowhere: list those yourself.
|
|
67
|
+
*/
|
|
68
|
+
messages?: FieldMessages | undefined;
|
|
46
69
|
/** `2` (the default) puts fields side by side from `sm:` up; `1` is one column, for a narrow rail. */
|
|
47
70
|
columns?: 1 | 2 | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Where labels go: `top` (the default) stacks each label over its control; `side` is the
|
|
73
|
+
* property-sheet row, the label in a fixed column on the left (truncated, the full text
|
|
74
|
+
* in its `title`) and the control on the right, about 32 px tall, with the description
|
|
75
|
+
* behind the hint icon. Containers, unions, tuples and lists keep their label above
|
|
76
|
+
* either way. Wins over `ui.labelPlacement`; a group's own `labelPlacement` wins over
|
|
77
|
+
* both for its fields. Works with both `columns`.
|
|
78
|
+
*/
|
|
79
|
+
labelPlacement?: LabelPlacement | undefined;
|
|
48
80
|
/** Blocks every control. The value is still shown. */
|
|
49
81
|
disabled?: boolean | undefined;
|
|
50
82
|
/**
|
|
@@ -99,8 +131,11 @@ export function SchemaValueForm({
|
|
|
99
131
|
value,
|
|
100
132
|
defaultValue,
|
|
101
133
|
onValueChange,
|
|
134
|
+
baseline,
|
|
135
|
+
messages,
|
|
102
136
|
ui = NO_UI,
|
|
103
137
|
columns = 2,
|
|
138
|
+
labelPlacement,
|
|
104
139
|
disabled = false,
|
|
105
140
|
renderField,
|
|
106
141
|
className,
|
|
@@ -111,6 +146,10 @@ export function SchemaValueForm({
|
|
|
111
146
|
() => (defaultValue !== undefined ? defaultValue : defaultValueForSchema(schema)),
|
|
112
147
|
onValueChange,
|
|
113
148
|
);
|
|
149
|
+
const compared = useMemo(
|
|
150
|
+
() => (baseline === undefined ? null : { value: baseline, counts: changeCounts(diffPaths(current, baseline)) }),
|
|
151
|
+
[current, baseline],
|
|
152
|
+
);
|
|
114
153
|
const claimed = useMemo(() => new Set((ui.groups ?? []).flatMap((group) => group.fields)), [ui.groups]);
|
|
115
154
|
|
|
116
155
|
const state: FormState = {
|
|
@@ -124,11 +163,17 @@ export function SchemaValueForm({
|
|
|
124
163
|
ui,
|
|
125
164
|
disabled,
|
|
126
165
|
columns,
|
|
166
|
+
labelPlacement: labelPlacement ?? ui.labelPlacement ?? "top",
|
|
167
|
+
descriptionAs: ui.descriptionAs ?? "hint",
|
|
127
168
|
renderField,
|
|
128
169
|
claimed,
|
|
170
|
+
baseline: compared,
|
|
171
|
+
messages,
|
|
129
172
|
};
|
|
130
173
|
|
|
131
174
|
const root = shapeOf(schema, schema);
|
|
175
|
+
const raw = messages?.[""];
|
|
176
|
+
const formMessage: FieldMessage | undefined = typeof raw === "string" ? { severity: "error", text: raw } : raw;
|
|
132
177
|
|
|
133
178
|
return (
|
|
134
179
|
<TooltipProvider>
|
|
@@ -136,9 +181,20 @@ export function SchemaValueForm({
|
|
|
136
181
|
<div
|
|
137
182
|
ref={ref}
|
|
138
183
|
data-columns={columns}
|
|
184
|
+
data-label-placement={state.labelPlacement}
|
|
139
185
|
data-disabled={disabled ? "" : undefined}
|
|
186
|
+
style={{ "--field-label-w": columns === 2 ? "7.5rem" : "9rem" } as CSSProperties}
|
|
140
187
|
className={cn("flex min-w-0 flex-col gap-5", className)}
|
|
141
188
|
>
|
|
189
|
+
{formMessage !== undefined && (
|
|
190
|
+
<p
|
|
191
|
+
role={formMessage.severity === "error" ? "alert" : undefined}
|
|
192
|
+
data-message={formMessage.severity}
|
|
193
|
+
className={cn("text-xs leading-snug", formMessage.severity === "error" ? "text-defect" : "text-warn")}
|
|
194
|
+
>
|
|
195
|
+
{formMessage.text}
|
|
196
|
+
</p>
|
|
197
|
+
)}
|
|
142
198
|
{root.kind === "object" && (ui.groups ?? []).map((group) => <GroupBlock key={group.id} group={group} />)}
|
|
143
199
|
{root.kind === "object" ? (
|
|
144
200
|
<ObjectBody path="" fields={root.fields} />
|
|
@@ -153,7 +209,8 @@ export function SchemaValueForm({
|
|
|
153
209
|
|
|
154
210
|
/** A `UiGroup`: its fields under a heading, folded away if the app said so. */
|
|
155
211
|
function GroupBlock({ group }: { group: UiGroup }) {
|
|
156
|
-
const
|
|
212
|
+
const form = useForm();
|
|
213
|
+
const { schema, value, ui } = form;
|
|
157
214
|
const fields = group.fields.flatMap((path) => {
|
|
158
215
|
const info = fieldAt(schema, path, schema, value);
|
|
159
216
|
if (info === undefined || fieldUiAt(ui, path).hidden === true) return [];
|
|
@@ -161,25 +218,25 @@ function GroupBlock({ group }: { group: UiGroup }) {
|
|
|
161
218
|
});
|
|
162
219
|
if (fields.length === 0) return null;
|
|
163
220
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
221
|
+
// The listed order is the rendered order: `fields` is rendered as given, never re-sorted by the schema.
|
|
222
|
+
const cells = fields.map(({ path, info }) => (
|
|
223
|
+
<FieldNode key={path} path={path} name={path.split(".").at(-1) ?? path} node={info.schema} required={info.required} />
|
|
224
|
+
));
|
|
225
|
+
const body = group.inline === true ? <InlineRow>{cells}</InlineRow> : <Grid>{cells}</Grid>;
|
|
226
|
+
const scoped =
|
|
227
|
+
group.labelPlacement === undefined ? body : <FormContext value={{ ...form, labelPlacement: group.labelPlacement }}>{body}</FormContext>;
|
|
171
228
|
|
|
172
229
|
if (group.collapsible === true) {
|
|
173
230
|
return (
|
|
174
231
|
<Disclosure summary={group.title} count={fields.length} defaultOpen={group.defaultOpen ?? false}>
|
|
175
232
|
{group.hint !== undefined && <p className="mb-3 text-xs text-fg-muted">{group.hint}</p>}
|
|
176
|
-
{
|
|
233
|
+
{scoped}
|
|
177
234
|
</Disclosure>
|
|
178
235
|
);
|
|
179
236
|
}
|
|
180
237
|
return (
|
|
181
238
|
<Section title={group.title} hint={group.hint}>
|
|
182
|
-
{
|
|
239
|
+
{scoped}
|
|
183
240
|
</Section>
|
|
184
241
|
);
|
|
185
242
|
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a field shows beyond its control: whether it differs from the form's baseline (a rule
|
|
3
|
+
* at its edge, a "changed" note that says what it was, and a reset), and the app's message
|
|
4
|
+
* for it.
|
|
5
|
+
*
|
|
6
|
+
* The note sits under the control, outside the field's `<label>`: a button inside a label
|
|
7
|
+
* would be part of the control's name. It is the field's description, so a reader hears it
|
|
8
|
+
* with the control.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { cn, focusRing } from "@vitavision/ui";
|
|
12
|
+
import { use, type ReactNode } from "react";
|
|
13
|
+
|
|
14
|
+
import { describeValue } from "../../api/valueDiff";
|
|
15
|
+
import { getAtPath } from "../../api/valuePath";
|
|
16
|
+
import type { FieldMeta } from "./chrome";
|
|
17
|
+
import { DenseContext, useForm, type FieldMessage } from "./context";
|
|
18
|
+
|
|
19
|
+
/** A thin rule in the gutter beside a changed field: a scan cue that is not the only one, since the note says it in words. */
|
|
20
|
+
const CHANGED_RULE =
|
|
21
|
+
"relative before:absolute before:-left-2.5 before:top-0.5 before:bottom-0.5 before:w-0.5 before:rounded-full before:bg-signal";
|
|
22
|
+
|
|
23
|
+
/** A message as `{ severity, text }`: a bare string is an error. */
|
|
24
|
+
function normalise(message: FieldMessage | string | undefined): FieldMessage | undefined {
|
|
25
|
+
if (message === undefined) return undefined;
|
|
26
|
+
return typeof message === "string" ? { severity: "error", text: message } : message;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What a field adds to its `Field` or switch. */
|
|
30
|
+
export interface FieldExtras {
|
|
31
|
+
/** Classes for the field's root: the rule beside a changed field. */
|
|
32
|
+
className: string | undefined;
|
|
33
|
+
/** Whether the field, or anything under it, differs from the baseline. */
|
|
34
|
+
changed: boolean;
|
|
35
|
+
/** The app's error message for the field. */
|
|
36
|
+
error: string | undefined;
|
|
37
|
+
/** The changed note and the app's warning, to follow the field's description. */
|
|
38
|
+
notes: ReactNode;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The baseline comparison and the app's message for one field.
|
|
43
|
+
*
|
|
44
|
+
* @param meta - The field.
|
|
45
|
+
* @returns Its extras; all empty for a form with no baseline and no message.
|
|
46
|
+
*/
|
|
47
|
+
export function useFieldExtras(meta: FieldMeta): FieldExtras {
|
|
48
|
+
const form = useForm();
|
|
49
|
+
// A cell of a tuple row has no room for a note of its own: the rule marks it, and the row's frame carries the reset.
|
|
50
|
+
const dense = use(DenseContext);
|
|
51
|
+
const changes = form.baseline?.counts.get(meta.path) ?? 0;
|
|
52
|
+
const message = normalise(form.messages?.[meta.path]);
|
|
53
|
+
const warning = message?.severity === "warning" ? message.text : undefined;
|
|
54
|
+
const notes =
|
|
55
|
+
(changes > 0 && !dense) || warning !== undefined ? (
|
|
56
|
+
<>
|
|
57
|
+
{warning !== undefined && (
|
|
58
|
+
<span data-message="warning" className="mt-0.5 block text-warn">
|
|
59
|
+
{warning}
|
|
60
|
+
</span>
|
|
61
|
+
)}
|
|
62
|
+
{changes > 0 && !dense && <ChangeNote meta={meta} count={changes} />}
|
|
63
|
+
</>
|
|
64
|
+
) : undefined;
|
|
65
|
+
return {
|
|
66
|
+
className: changes > 0 ? CHANGED_RULE : undefined,
|
|
67
|
+
changed: changes > 0,
|
|
68
|
+
error: message?.severity === "error" ? message.text : undefined,
|
|
69
|
+
notes,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** An error message where the control has no error slot of its own (a switch). */
|
|
74
|
+
export function ErrorText({ text }: { text: string }) {
|
|
75
|
+
return (
|
|
76
|
+
<span role="alert" data-message="error" className="block text-defect">
|
|
77
|
+
{text}
|
|
78
|
+
</span>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A description and the extras' notes, as one `Field` description (`undefined` when both are empty). */
|
|
83
|
+
export function withNotes(description: ReactNode, notes: ReactNode): ReactNode {
|
|
84
|
+
const has = description !== undefined && description !== null && description !== false && description !== "";
|
|
85
|
+
if (!has) return notes;
|
|
86
|
+
return notes === undefined ? (
|
|
87
|
+
description
|
|
88
|
+
) : (
|
|
89
|
+
<>
|
|
90
|
+
{description}
|
|
91
|
+
{notes}
|
|
92
|
+
</>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The "changed" marker with what the baseline had, and the button that puts it back. */
|
|
97
|
+
export function ChangeNote({ meta, count }: { meta: FieldMeta; count: number }) {
|
|
98
|
+
const form = useForm();
|
|
99
|
+
const was = getAtPath(form.baseline?.value, meta.path);
|
|
100
|
+
const composite = typeof was === "object" && was !== null;
|
|
101
|
+
return (
|
|
102
|
+
<span data-change-note="" className="mt-0.5 flex min-w-0 items-center gap-2 text-[11px] leading-snug">
|
|
103
|
+
<span className="inline-flex shrink-0 items-center gap-1 font-medium text-signal">
|
|
104
|
+
<span aria-hidden className="size-1.5 rounded-full bg-signal" />
|
|
105
|
+
{composite && count > 1 ? `${count} changed` : "changed"}
|
|
106
|
+
</span>
|
|
107
|
+
{!composite && (
|
|
108
|
+
<span className="min-w-0 truncate text-fg-muted">
|
|
109
|
+
was <span className="font-mono">{describeValue(was)}</span>
|
|
110
|
+
</span>
|
|
111
|
+
)}
|
|
112
|
+
<ResetButton meta={meta} />
|
|
113
|
+
</span>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Puts a field (or a whole block) back to the baseline's value. */
|
|
118
|
+
export function ResetButton({ meta, className }: { meta: FieldMeta; className?: string | undefined }) {
|
|
119
|
+
const form = useForm();
|
|
120
|
+
return (
|
|
121
|
+
<button
|
|
122
|
+
type="button"
|
|
123
|
+
data-reset=""
|
|
124
|
+
aria-label={`Reset ${meta.label} to baseline`}
|
|
125
|
+
disabled={form.disabled}
|
|
126
|
+
onClick={() => form.setValue(meta.path, getAtPath(form.baseline?.value, meta.path))}
|
|
127
|
+
className={cn(
|
|
128
|
+
"-my-1 ml-auto shrink-0 cursor-pointer rounded-control px-1 py-1 font-medium text-fg-muted transition-colors",
|
|
129
|
+
"hover:text-fg disabled:cursor-not-allowed disabled:opacity-50",
|
|
130
|
+
focusRing,
|
|
131
|
+
className,
|
|
132
|
+
)}
|
|
133
|
+
>
|
|
134
|
+
Reset
|
|
135
|
+
</button>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
@@ -9,7 +9,7 @@ import { use, type ReactNode } from "react";
|
|
|
9
9
|
|
|
10
10
|
import { firstParagraph } from "../../api/firstParagraph";
|
|
11
11
|
import type { JsonSchema } from "../../api/schemaNode";
|
|
12
|
-
import type { FieldUi } from "../../api/uiSchema";
|
|
12
|
+
import type { FieldUi, LabelPlacement } from "../../api/uiSchema";
|
|
13
13
|
import { DenseContext, useForm } from "./context";
|
|
14
14
|
|
|
15
15
|
/** Everything a control needs to know about its field. */
|
|
@@ -45,7 +45,7 @@ export function spanClass(columns: 1 | 2, ui: FieldUi, fallback: 1 | 2): string
|
|
|
45
45
|
|
|
46
46
|
/** The field grid: two columns on `sm:` (or one), or an auto-fit row inside a tuple. */
|
|
47
47
|
export function Grid({ children }: { children: ReactNode }) {
|
|
48
|
-
const { columns } = useForm();
|
|
48
|
+
const { columns, labelPlacement } = useForm();
|
|
49
49
|
const dense = use(DenseContext);
|
|
50
50
|
const density = useDensity();
|
|
51
51
|
return (
|
|
@@ -54,7 +54,12 @@ export function Grid({ children }: { children: ReactNode }) {
|
|
|
54
54
|
"grid min-w-0",
|
|
55
55
|
dense
|
|
56
56
|
? "grid-cols-[repeat(auto-fit,minmax(5.5rem,1fr))] gap-2"
|
|
57
|
-
: cn(
|
|
57
|
+
: cn(
|
|
58
|
+
labelPlacement === "side"
|
|
59
|
+
? byDensity(density, "gap-x-6 gap-y-2", "gap-x-5 gap-y-1.5")
|
|
60
|
+
: byDensity(density, "gap-x-5 gap-y-4", "gap-x-4 gap-y-2.5"),
|
|
61
|
+
columns === 2 ? "grid-cols-1 sm:grid-cols-2" : "grid-cols-1",
|
|
62
|
+
),
|
|
58
63
|
)}
|
|
59
64
|
>
|
|
60
65
|
{children}
|
|
@@ -62,6 +67,27 @@ export function Grid({ children }: { children: ReactNode }) {
|
|
|
62
67
|
);
|
|
63
68
|
}
|
|
64
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Fields on one row, each labelled from above: the layout for a few small numbers that read
|
|
72
|
+
* as one setting. Cells drop descriptions and range notes (see `DenseContext`).
|
|
73
|
+
*/
|
|
74
|
+
export function InlineRow({ children }: { children: ReactNode }) {
|
|
75
|
+
return (
|
|
76
|
+
<DenseContext value>
|
|
77
|
+
<Grid>{children}</Grid>
|
|
78
|
+
</DenseContext>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Where this field's label goes: the form's (or its group's) placement, except inside a row
|
|
84
|
+
* of cells, which always label from above because a cell has no room for a label column.
|
|
85
|
+
*/
|
|
86
|
+
export function usePlacement(): LabelPlacement {
|
|
87
|
+
const { labelPlacement } = useForm();
|
|
88
|
+
return use(DenseContext) ? "top" : labelPlacement;
|
|
89
|
+
}
|
|
90
|
+
|
|
65
91
|
function present(node: ReactNode): boolean {
|
|
66
92
|
return node !== undefined && node !== null && node !== false && node !== "";
|
|
67
93
|
}
|
|
@@ -80,13 +106,16 @@ export function useChrome(
|
|
|
80
106
|
range: string | null = null,
|
|
81
107
|
): { hint: ReactNode; description: ReactNode; annotation: ReactNode } {
|
|
82
108
|
const dense = use(DenseContext);
|
|
83
|
-
const
|
|
109
|
+
const side = usePlacement() === "side";
|
|
110
|
+
const { descriptionAs } = useForm();
|
|
111
|
+
const mode = meta.ui.descriptionAs ?? descriptionAs;
|
|
84
112
|
const text: ReactNode = meta.ui.hint ?? firstParagraph(meta.schema.description);
|
|
85
113
|
const shown = !dense && present(text);
|
|
86
114
|
|
|
87
115
|
const hint = shown && mode === "hint" ? <InfoHint label={`About ${meta.label}`}>{text}</InfoHint> : null;
|
|
88
116
|
const description = shown && mode === "inline" ? text : undefined;
|
|
89
|
-
|
|
117
|
+
// A side row's label column has no room for a range: the hint and an error carry it.
|
|
118
|
+
const note = dense || side ? null : range;
|
|
90
119
|
const annotation =
|
|
91
120
|
note !== null || hint !== null ? (
|
|
92
121
|
<span className="inline-flex items-center gap-1.5">
|
|
@@ -8,7 +8,23 @@
|
|
|
8
8
|
import { createContext, use } from "react";
|
|
9
9
|
|
|
10
10
|
import type { JsonSchema } from "../../api/schemaNode";
|
|
11
|
-
import type { RenderField, UiSchema } from "../../api/uiSchema";
|
|
11
|
+
import type { LabelPlacement, RenderField, UiSchema } from "../../api/uiSchema";
|
|
12
|
+
|
|
13
|
+
/** A message the app attaches to a field, such as a backend's verdict on a value. */
|
|
14
|
+
export interface FieldMessage {
|
|
15
|
+
/** `error` is announced as an alert and marks the control invalid; `warning` is shown without blocking. */
|
|
16
|
+
severity: "error" | "warning";
|
|
17
|
+
/** What to tell the person. */
|
|
18
|
+
text: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The form's comparison with its baseline, computed once per value. */
|
|
22
|
+
export interface BaselineState {
|
|
23
|
+
/** The value the form compares with. */
|
|
24
|
+
value: unknown;
|
|
25
|
+
/** How many differing leaves lie at or under each path (see `changeCounts`). */
|
|
26
|
+
counts: ReadonlyMap<string, number>;
|
|
27
|
+
}
|
|
12
28
|
|
|
13
29
|
/** The form's state, for the nodes below it. */
|
|
14
30
|
export interface FormState {
|
|
@@ -26,6 +42,14 @@ export interface FormState {
|
|
|
26
42
|
columns: 1 | 2;
|
|
27
43
|
/** The app's escape hatch. */
|
|
28
44
|
renderField: RenderField | undefined;
|
|
45
|
+
/** The comparison with the baseline, or `null` when the form has none. */
|
|
46
|
+
baseline: BaselineState | null;
|
|
47
|
+
/** The app's messages, by dot path (`""` is the form as a whole). */
|
|
48
|
+
messages: Readonly<Record<string, FieldMessage | string>> | undefined;
|
|
49
|
+
/** Where labels go here: the form's, or the enclosing group's. */
|
|
50
|
+
labelPlacement: LabelPlacement;
|
|
51
|
+
/** The form's default for where descriptions go. */
|
|
52
|
+
descriptionAs: "hint" | "inline" | "none";
|
|
29
53
|
/** Paths a group has claimed, so the ungrouped remainder skips them. */
|
|
30
54
|
claimed: ReadonlySet<string>;
|
|
31
55
|
}
|