@vitavision/forms 0.8.1 → 0.8.4
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 +51 -2
- package/dist/index.js +249 -39
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/schemaForm.ts +1 -3
- package/src/api/schemaValue.ts +2 -9
- package/src/api/uiSchema.ts +1 -10
- package/src/api/valueDiff.ts +79 -0
- package/src/components/SchemaForm.tsx +10 -60
- package/src/components/SchemaValueForm.tsx +44 -8
- package/src/components/valueForm/changes.tsx +137 -0
- package/src/components/valueForm/chrome.tsx +1 -4
- package/src/components/valueForm/context.ts +20 -0
- package/src/components/valueForm/leaves.tsx +36 -25
- package/src/components/valueForm/nodes.tsx +11 -14
- package/src/index.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitavision/forms",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4",
|
|
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.
|
|
50
|
+
"@vitavision/ui": "^0.14.2"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@storybook/react-vite": "10.6.0",
|
package/src/api/schemaForm.ts
CHANGED
|
@@ -237,9 +237,7 @@ export function describeFields(schema: OptionsSchema): FieldSpec[] {
|
|
|
237
237
|
return Object.entries(schema.properties ?? {}).map(([name, node]) => {
|
|
238
238
|
const resolved = resolve(node, schema);
|
|
239
239
|
const kind = kindOf(resolved, schema);
|
|
240
|
-
const numeric =
|
|
241
|
-
(kind === "choice" || kind === "choice-inline") &&
|
|
242
|
-
(resolved.type === "integer" || resolved.type === "number");
|
|
240
|
+
const numeric = (kind === "choice" || kind === "choice-inline") && (resolved.type === "integer" || resolved.type === "number");
|
|
243
241
|
|
|
244
242
|
return {
|
|
245
243
|
name,
|
package/src/api/schemaValue.ts
CHANGED
|
@@ -219,9 +219,7 @@ function classify(schema: JsonSchema, root: JsonSchema): SchemaShape {
|
|
|
219
219
|
: { kind: "json" };
|
|
220
220
|
}
|
|
221
221
|
if (Object.hasOwn(schema, "const")) {
|
|
222
|
-
return isPrimitive(schema.const)
|
|
223
|
-
? { kind: "enum", options: [{ value: schema.const, label: String(schema.const) }] }
|
|
224
|
-
: { kind: "json" };
|
|
222
|
+
return isPrimitive(schema.const) ? { kind: "enum", options: [{ value: schema.const, label: String(schema.const) }] } : { kind: "json" };
|
|
225
223
|
}
|
|
226
224
|
|
|
227
225
|
const branches = schema.oneOf ?? schema.anyOf;
|
|
@@ -470,12 +468,7 @@ function findChild(shape: SchemaShape, segment: string, value: unknown): FieldIn
|
|
|
470
468
|
* @returns The field (its schema as written, and whether it is required), or `undefined`
|
|
471
469
|
* where the path leaves the schema.
|
|
472
470
|
*/
|
|
473
|
-
export function fieldAt(
|
|
474
|
-
schema: JsonSchema,
|
|
475
|
-
path: string,
|
|
476
|
-
root: JsonSchema = schema,
|
|
477
|
-
value?: unknown,
|
|
478
|
-
): FieldInfo | undefined {
|
|
471
|
+
export function fieldAt(schema: JsonSchema, path: string, root: JsonSchema = schema, value?: unknown): FieldInfo | undefined {
|
|
479
472
|
let info: FieldInfo = { key: "", schema, required: true };
|
|
480
473
|
let current = value;
|
|
481
474
|
for (const segment of splitPath(path)) {
|
package/src/api/uiSchema.ts
CHANGED
|
@@ -33,16 +33,7 @@ export interface UiGroup {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/** The widgets a field can be forced into with `FieldUi.widget`. */
|
|
36
|
-
export type FieldWidget =
|
|
37
|
-
| "number"
|
|
38
|
-
| "text"
|
|
39
|
-
| "select"
|
|
40
|
-
| "segmented"
|
|
41
|
-
| "checkbox"
|
|
42
|
-
| "switch"
|
|
43
|
-
| "json"
|
|
44
|
-
| "tuple-row"
|
|
45
|
-
| "string-list";
|
|
36
|
+
export type FieldWidget = "number" | "text" | "select" | "segmented" | "checkbox" | "switch" | "json" | "tuple-row" | "string-list";
|
|
46
37
|
|
|
47
38
|
/** Per-field presentation, looked up by the field's dot path. */
|
|
48
39
|
export interface FieldUi {
|
|
@@ -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
|
+
}
|
|
@@ -20,16 +20,7 @@
|
|
|
20
20
|
* JSON object, a statement with a switch — still span both columns.
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
|
-
import {
|
|
24
|
-
Disclosure,
|
|
25
|
-
Field,
|
|
26
|
-
NumberInput,
|
|
27
|
-
SegmentedControl,
|
|
28
|
-
Select,
|
|
29
|
-
Switch,
|
|
30
|
-
Textarea,
|
|
31
|
-
Input,
|
|
32
|
-
} from "@vitavision/ui";
|
|
23
|
+
import { Disclosure, Field, NumberInput, SegmentedControl, Select, Switch, Textarea, Input } from "@vitavision/ui";
|
|
33
24
|
import { displayValue, type FieldSpec, type RawValues } from "../api/schemaForm";
|
|
34
25
|
|
|
35
26
|
/**
|
|
@@ -50,27 +41,14 @@ const WIDE: ReadonlySet<FieldSpec["kind"]> = new Set(["json", "boolean"]);
|
|
|
50
41
|
* options to send with `toOptions`, and gate a run on `missingRequired`, `outOfRange` and
|
|
51
42
|
* `jsonErrors`.
|
|
52
43
|
*/
|
|
53
|
-
export function SchemaForm({
|
|
54
|
-
fields,
|
|
55
|
-
values,
|
|
56
|
-
onChange,
|
|
57
|
-
}: {
|
|
58
|
-
fields: FieldSpec[];
|
|
59
|
-
values: RawValues;
|
|
60
|
-
onChange: (next: RawValues) => void;
|
|
61
|
-
}) {
|
|
44
|
+
export function SchemaForm({ fields, values, onChange }: { fields: FieldSpec[]; values: RawValues; onChange: (next: RawValues) => void }) {
|
|
62
45
|
if (fields.length === 0) {
|
|
63
46
|
return <p className="text-sm text-fg-muted">This plugin takes no options.</p>;
|
|
64
47
|
}
|
|
65
48
|
|
|
66
49
|
const set = (name: string, value: string | boolean) => onChange({ ...values, [name]: value });
|
|
67
50
|
const render = (field: FieldSpec) => (
|
|
68
|
-
<SchemaField
|
|
69
|
-
key={field.name}
|
|
70
|
-
field={field}
|
|
71
|
-
value={values[field.name] ?? ""}
|
|
72
|
-
onChange={(value) => set(field.name, value)}
|
|
73
|
-
/>
|
|
51
|
+
<SchemaField key={field.name} field={field} value={values[field.name] ?? ""} onChange={(value) => set(field.name, value)} />
|
|
74
52
|
);
|
|
75
53
|
|
|
76
54
|
const primary = fields.filter((field) => !field.advanced);
|
|
@@ -116,50 +94,25 @@ function SchemaField({
|
|
|
116
94
|
if (field.kind === "boolean") {
|
|
117
95
|
return (
|
|
118
96
|
<div className={span}>
|
|
119
|
-
<Switch
|
|
120
|
-
checked={value === true}
|
|
121
|
-
onCheckedChange={onChange}
|
|
122
|
-
label={field.label}
|
|
123
|
-
description={field.description}
|
|
124
|
-
/>
|
|
97
|
+
<Switch checked={value === true} onCheckedChange={onChange} label={field.label} description={field.description} />
|
|
125
98
|
</div>
|
|
126
99
|
);
|
|
127
100
|
}
|
|
128
101
|
|
|
129
102
|
const text = typeof value === "string" ? value : "";
|
|
130
|
-
const fallback = field.fallback === undefined || field.fallback === null
|
|
131
|
-
? undefined
|
|
132
|
-
: displayValue(field.fallback);
|
|
103
|
+
const fallback = field.fallback === undefined || field.fallback === null ? undefined : displayValue(field.fallback);
|
|
133
104
|
|
|
134
105
|
if (field.kind === "choice-inline") {
|
|
135
106
|
return (
|
|
136
|
-
<Field
|
|
137
|
-
|
|
138
|
-
label={field.label}
|
|
139
|
-
description={field.description}
|
|
140
|
-
required={field.required}
|
|
141
|
-
className={span}
|
|
142
|
-
>
|
|
143
|
-
<SegmentedControl
|
|
144
|
-
aria-label={field.label}
|
|
145
|
-
value={text}
|
|
146
|
-
defaultValue={fallback}
|
|
147
|
-
options={field.options}
|
|
148
|
-
onValueChange={onChange}
|
|
149
|
-
/>
|
|
107
|
+
<Field as="group" label={field.label} description={field.description} required={field.required} className={span}>
|
|
108
|
+
<SegmentedControl aria-label={field.label} value={text} defaultValue={fallback} options={field.options} onValueChange={onChange} />
|
|
150
109
|
</Field>
|
|
151
110
|
);
|
|
152
111
|
}
|
|
153
112
|
|
|
154
113
|
if (field.kind === "choice") {
|
|
155
114
|
return (
|
|
156
|
-
<Field
|
|
157
|
-
as="group"
|
|
158
|
-
label={field.label}
|
|
159
|
-
description={field.description}
|
|
160
|
-
required={field.required}
|
|
161
|
-
className={span}
|
|
162
|
-
>
|
|
115
|
+
<Field as="group" label={field.label} description={field.description} required={field.required} className={span}>
|
|
163
116
|
<Select
|
|
164
117
|
aria-label={field.label}
|
|
165
118
|
value={text}
|
|
@@ -175,17 +128,14 @@ function SchemaField({
|
|
|
175
128
|
return (
|
|
176
129
|
<Field
|
|
177
130
|
label={field.label}
|
|
178
|
-
description={
|
|
179
|
-
field.kind === "string-list"
|
|
180
|
-
? [field.description, "Comma-separated."].filter(Boolean).join(" ")
|
|
181
|
-
: field.description
|
|
182
|
-
}
|
|
131
|
+
description={field.kind === "string-list" ? [field.description, "Comma-separated."].filter(Boolean).join(" ") : field.description}
|
|
183
132
|
required={field.required}
|
|
184
133
|
annotation={field.range}
|
|
185
134
|
className={span}
|
|
186
135
|
>
|
|
187
136
|
{field.kind === "json" ? (
|
|
188
137
|
<Textarea
|
|
138
|
+
mono
|
|
189
139
|
rows={2}
|
|
190
140
|
aria-label={field.label}
|
|
191
141
|
placeholder={field.placeholder}
|
|
@@ -15,13 +15,19 @@ import { useMemo, useState, type Ref } from "react";
|
|
|
15
15
|
import type { JsonSchema } from "../api/schemaNode";
|
|
16
16
|
import { defaultValueForSchema, fieldAt, shapeOf } from "../api/schemaValue";
|
|
17
17
|
import { fieldUiAt, type RenderField, type UiGroup, type UiSchema } from "../api/uiSchema";
|
|
18
|
+
import { changeCounts, diffPaths } from "../api/valueDiff";
|
|
18
19
|
import { setAtPath } from "../api/valuePath";
|
|
19
20
|
import { Grid } from "./valueForm/chrome";
|
|
20
|
-
import { FormContext, useForm, type FormState } from "./valueForm/context";
|
|
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,6 +49,23 @@ 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;
|
|
48
71
|
/** Blocks every control. The value is still shown. */
|
|
@@ -99,6 +122,8 @@ export function SchemaValueForm({
|
|
|
99
122
|
value,
|
|
100
123
|
defaultValue,
|
|
101
124
|
onValueChange,
|
|
125
|
+
baseline,
|
|
126
|
+
messages,
|
|
102
127
|
ui = NO_UI,
|
|
103
128
|
columns = 2,
|
|
104
129
|
disabled = false,
|
|
@@ -111,6 +136,10 @@ export function SchemaValueForm({
|
|
|
111
136
|
() => (defaultValue !== undefined ? defaultValue : defaultValueForSchema(schema)),
|
|
112
137
|
onValueChange,
|
|
113
138
|
);
|
|
139
|
+
const compared = useMemo(
|
|
140
|
+
() => (baseline === undefined ? null : { value: baseline, counts: changeCounts(diffPaths(current, baseline)) }),
|
|
141
|
+
[current, baseline],
|
|
142
|
+
);
|
|
114
143
|
const claimed = useMemo(() => new Set((ui.groups ?? []).flatMap((group) => group.fields)), [ui.groups]);
|
|
115
144
|
|
|
116
145
|
const state: FormState = {
|
|
@@ -126,9 +155,13 @@ export function SchemaValueForm({
|
|
|
126
155
|
columns,
|
|
127
156
|
renderField,
|
|
128
157
|
claimed,
|
|
158
|
+
baseline: compared,
|
|
159
|
+
messages,
|
|
129
160
|
};
|
|
130
161
|
|
|
131
162
|
const root = shapeOf(schema, schema);
|
|
163
|
+
const raw = messages?.[""];
|
|
164
|
+
const formMessage: FieldMessage | undefined = typeof raw === "string" ? { severity: "error", text: raw } : raw;
|
|
132
165
|
|
|
133
166
|
return (
|
|
134
167
|
<TooltipProvider>
|
|
@@ -139,6 +172,15 @@ export function SchemaValueForm({
|
|
|
139
172
|
data-disabled={disabled ? "" : undefined}
|
|
140
173
|
className={cn("flex min-w-0 flex-col gap-5", className)}
|
|
141
174
|
>
|
|
175
|
+
{formMessage !== undefined && (
|
|
176
|
+
<p
|
|
177
|
+
role={formMessage.severity === "error" ? "alert" : undefined}
|
|
178
|
+
data-message={formMessage.severity}
|
|
179
|
+
className={cn("text-xs leading-snug", formMessage.severity === "error" ? "text-defect" : "text-warn")}
|
|
180
|
+
>
|
|
181
|
+
{formMessage.text}
|
|
182
|
+
</p>
|
|
183
|
+
)}
|
|
142
184
|
{root.kind === "object" && (ui.groups ?? []).map((group) => <GroupBlock key={group.id} group={group} />)}
|
|
143
185
|
{root.kind === "object" ? (
|
|
144
186
|
<ObjectBody path="" fields={root.fields} />
|
|
@@ -164,13 +206,7 @@ function GroupBlock({ group }: { group: UiGroup }) {
|
|
|
164
206
|
const body = (
|
|
165
207
|
<Grid>
|
|
166
208
|
{fields.map(({ path, info }) => (
|
|
167
|
-
<FieldNode
|
|
168
|
-
key={path}
|
|
169
|
-
path={path}
|
|
170
|
-
name={path.split(".").at(-1) ?? path}
|
|
171
|
-
node={info.schema}
|
|
172
|
-
required={info.required}
|
|
173
|
-
/>
|
|
209
|
+
<FieldNode key={path} path={path} name={path.split(".").at(-1) ?? path} node={info.schema} required={info.required} />
|
|
174
210
|
))}
|
|
175
211
|
</Grid>
|
|
176
212
|
);
|
|
@@ -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
|
+
}
|
|
@@ -54,10 +54,7 @@ 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(
|
|
58
|
-
byDensity(density, "gap-x-5 gap-y-4", "gap-x-4 gap-y-2.5"),
|
|
59
|
-
columns === 2 ? "grid-cols-1 sm:grid-cols-2" : "grid-cols-1",
|
|
60
|
-
),
|
|
57
|
+
: cn(byDensity(density, "gap-x-5 gap-y-4", "gap-x-4 gap-y-2.5"), columns === 2 ? "grid-cols-1 sm:grid-cols-2" : "grid-cols-1"),
|
|
61
58
|
)}
|
|
62
59
|
>
|
|
63
60
|
{children}
|
|
@@ -10,6 +10,22 @@ import { createContext, use } from "react";
|
|
|
10
10
|
import type { JsonSchema } from "../../api/schemaNode";
|
|
11
11
|
import type { RenderField, UiSchema } from "../../api/uiSchema";
|
|
12
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
|
+
}
|
|
28
|
+
|
|
13
29
|
/** The form's state, for the nodes below it. */
|
|
14
30
|
export interface FormState {
|
|
15
31
|
/** The root schema, which also holds `$defs`. */
|
|
@@ -26,6 +42,10 @@ 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;
|
|
29
49
|
/** Paths a group has claimed, so the ungrouped remainder skips them. */
|
|
30
50
|
claimed: ReadonlySet<string>;
|
|
31
51
|
}
|