@vitavision/forms 0.8.3 → 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 +247 -39
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/valueDiff.ts +79 -0
- package/src/components/SchemaValueForm.tsx +43 -1
- package/src/components/valueForm/changes.tsx +137 -0
- package/src/components/valueForm/context.ts +20 -0
- package/src/components/valueForm/leaves.tsx +24 -14
- package/src/components/valueForm/nodes.tsx +9 -2
- 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.14.
|
|
50
|
+
"@vitavision/ui": "^0.14.2"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@storybook/react-vite": "10.6.0",
|
|
@@ -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
|
+
}
|
|
@@ -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} />
|
|
@@ -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
|
+
}
|
|
@@ -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
|
}
|
|
@@ -28,6 +28,7 @@ import { defaultValueForSchema } from "../../api/schemaValue";
|
|
|
28
28
|
import { getAtPath } from "../../api/valuePath";
|
|
29
29
|
import type { FieldWidget } from "../../api/uiSchema";
|
|
30
30
|
import { useChrome, spanClass, type FieldMeta } from "./chrome";
|
|
31
|
+
import { ErrorText, useFieldExtras, withNotes } from "./changes";
|
|
31
32
|
import { DenseContext, useForm } from "./context";
|
|
32
33
|
|
|
33
34
|
/** `required` as the marker shows it: a nullable field is never mandatory. */
|
|
@@ -54,6 +55,7 @@ export function NumberLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
54
55
|
const value = getAtPath(form.value, meta.path);
|
|
55
56
|
const spec = numberSpec(meta.schema, meta.ui);
|
|
56
57
|
const chrome = useChrome(meta, rangeText(spec));
|
|
58
|
+
const extras = useFieldExtras(meta);
|
|
57
59
|
const [error, setError] = useState<string | undefined>();
|
|
58
60
|
|
|
59
61
|
const fallback = meta.schema.default;
|
|
@@ -78,9 +80,9 @@ export function NumberLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
78
80
|
label={meta.label}
|
|
79
81
|
required={marked(meta)}
|
|
80
82
|
annotation={chrome.annotation}
|
|
81
|
-
description={chrome.description}
|
|
82
|
-
error={error}
|
|
83
|
-
className={spanClass(form.columns, meta.ui, 1)}
|
|
83
|
+
description={withNotes(chrome.description, extras.notes)}
|
|
84
|
+
error={error ?? extras.error}
|
|
85
|
+
className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
|
|
84
86
|
>
|
|
85
87
|
<NumberInput
|
|
86
88
|
aria-label={meta.label}
|
|
@@ -113,6 +115,7 @@ export function TextLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
113
115
|
const form = useForm();
|
|
114
116
|
const value = getAtPath(form.value, meta.path);
|
|
115
117
|
const chrome = useChrome(meta);
|
|
118
|
+
const extras = useFieldExtras(meta);
|
|
116
119
|
const fallback = meta.schema.default;
|
|
117
120
|
|
|
118
121
|
return (
|
|
@@ -120,8 +123,9 @@ export function TextLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
120
123
|
label={meta.label}
|
|
121
124
|
required={marked(meta)}
|
|
122
125
|
annotation={chrome.annotation}
|
|
123
|
-
description={chrome.description}
|
|
124
|
-
|
|
126
|
+
description={withNotes(chrome.description, extras.notes)}
|
|
127
|
+
error={extras.error}
|
|
128
|
+
className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
|
|
125
129
|
>
|
|
126
130
|
<Input
|
|
127
131
|
className="font-mono"
|
|
@@ -185,6 +189,7 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
|
|
|
185
189
|
const dense = use(DenseContext);
|
|
186
190
|
const value = getAtPath(form.value, meta.path);
|
|
187
191
|
const chrome = useChrome(meta);
|
|
192
|
+
const extras = useFieldExtras(meta);
|
|
188
193
|
|
|
189
194
|
const shown = options.map((option) => ({
|
|
190
195
|
value: String(option.value),
|
|
@@ -211,8 +216,9 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
|
|
|
211
216
|
label={meta.label}
|
|
212
217
|
required={marked(meta)}
|
|
213
218
|
annotation={chrome.annotation}
|
|
214
|
-
description={description}
|
|
215
|
-
|
|
219
|
+
description={withNotes(description, extras.notes)}
|
|
220
|
+
error={extras.error}
|
|
221
|
+
className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
|
|
216
222
|
>
|
|
217
223
|
<Chooser
|
|
218
224
|
label={meta.label}
|
|
@@ -239,16 +245,17 @@ export function BooleanLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
239
245
|
const form = useForm();
|
|
240
246
|
const value = getAtPath(form.value, meta.path);
|
|
241
247
|
const chrome = useChrome(meta);
|
|
248
|
+
const extras = useFieldExtras(meta);
|
|
242
249
|
const props = {
|
|
243
250
|
checked: value === true,
|
|
244
251
|
onCheckedChange: (next: boolean) => form.setValue(meta.path, next),
|
|
245
252
|
label: meta.label,
|
|
246
|
-
description: chrome.description,
|
|
253
|
+
description: withNotes(withNotes(chrome.description, extras.error ? <ErrorText text={extras.error} /> : undefined), extras.notes),
|
|
247
254
|
disabled: form.disabled,
|
|
248
255
|
};
|
|
249
256
|
|
|
250
257
|
return (
|
|
251
|
-
<div className={cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1))}>
|
|
258
|
+
<div className={cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1), extras.className)}>
|
|
252
259
|
{meta.ui.widget === "checkbox" ? <Checkbox {...props} /> : <Switch {...props} />}
|
|
253
260
|
{chrome.hint !== null && <span className="mt-0.5">{chrome.hint}</span>}
|
|
254
261
|
</div>
|
|
@@ -270,6 +277,7 @@ export function JsonLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
270
277
|
const form = useForm();
|
|
271
278
|
const value = getAtPath(form.value, meta.path);
|
|
272
279
|
const chrome = useChrome(meta);
|
|
280
|
+
const extras = useFieldExtras(meta);
|
|
273
281
|
const canonical = JSON.stringify(value) ?? "";
|
|
274
282
|
const [draft, setDraft] = useState<{ text: string; error: string | undefined; base: string }>(() => ({
|
|
275
283
|
text: pretty(value),
|
|
@@ -302,9 +310,9 @@ export function JsonLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
302
310
|
label={meta.label}
|
|
303
311
|
required={marked(meta)}
|
|
304
312
|
annotation={chrome.annotation}
|
|
305
|
-
description={chrome.description}
|
|
306
|
-
error={draft.error}
|
|
307
|
-
className={spanClass(form.columns, meta.ui, 2)}
|
|
313
|
+
description={withNotes(chrome.description, extras.notes)}
|
|
314
|
+
error={draft.error ?? extras.error}
|
|
315
|
+
className={cn(spanClass(form.columns, meta.ui, 2), extras.className)}
|
|
308
316
|
>
|
|
309
317
|
<Textarea
|
|
310
318
|
mono
|
|
@@ -351,6 +359,7 @@ export function StringListLeaf({
|
|
|
351
359
|
const form = useForm();
|
|
352
360
|
const value = getAtPath(form.value, meta.path);
|
|
353
361
|
const chrome = useChrome(meta);
|
|
362
|
+
const extras = useFieldExtras(meta);
|
|
354
363
|
const raw: unknown[] = Array.isArray(value) ? (value as unknown[]) : [];
|
|
355
364
|
const [rows, setRows] = useState<RowIds>(() => syncIds({ ids: [], next: 0 }, raw.length));
|
|
356
365
|
const synced = syncIds(rows, raw.length);
|
|
@@ -366,8 +375,9 @@ export function StringListLeaf({
|
|
|
366
375
|
label={meta.label}
|
|
367
376
|
required={marked(meta)}
|
|
368
377
|
annotation={chrome.annotation}
|
|
369
|
-
description={chrome.description}
|
|
370
|
-
|
|
378
|
+
description={withNotes(chrome.description, extras.notes)}
|
|
379
|
+
error={extras.error}
|
|
380
|
+
className={cn(spanClass(form.columns, meta.ui, 2), extras.className)}
|
|
371
381
|
>
|
|
372
382
|
<div className="flex flex-col gap-1.5">
|
|
373
383
|
{raw.map((entry, index) => (
|
|
@@ -16,6 +16,7 @@ import { activeExternal, activeTagged, seedNotNull, seedVariant, shapeOf, type F
|
|
|
16
16
|
import { fieldUiAt } from "../../api/uiSchema";
|
|
17
17
|
import { getAtPath, joinPath } from "../../api/valuePath";
|
|
18
18
|
import { Grid, spanClass, useChrome, type FieldMeta } from "./chrome";
|
|
19
|
+
import { ErrorText, useFieldExtras, withNotes } from "./changes";
|
|
19
20
|
import { Chooser, BooleanLeaf, EnumLeaf, JsonLeaf, NumberLeaf, StringListLeaf, TextLeaf } from "./leaves";
|
|
20
21
|
import { DenseContext, useForm } from "./context";
|
|
21
22
|
|
|
@@ -161,14 +162,17 @@ function Frame({ meta, bare, children }: { meta: FieldMeta; bare: boolean; child
|
|
|
161
162
|
const dense = use(DenseContext);
|
|
162
163
|
const density = useDensity();
|
|
163
164
|
const chrome = useChrome(meta);
|
|
165
|
+
const extras = useFieldExtras(meta);
|
|
164
166
|
if (bare) return <div className="flex min-w-0 flex-col gap-3">{children}</div>;
|
|
165
167
|
|
|
166
168
|
return (
|
|
167
169
|
<fieldset
|
|
170
|
+
data-changed={extras.changed ? "" : undefined}
|
|
168
171
|
className={cn(
|
|
169
172
|
"flex min-w-0 flex-col gap-3 rounded-panel border border-line",
|
|
170
173
|
byDensity(density, "p-3", "p-2"),
|
|
171
174
|
!dense && spanClass(form.columns, meta.ui, 2),
|
|
175
|
+
extras.className,
|
|
172
176
|
)}
|
|
173
177
|
>
|
|
174
178
|
<legend className="px-1">
|
|
@@ -178,6 +182,8 @@ function Frame({ meta, bare, children }: { meta: FieldMeta; bare: boolean; child
|
|
|
178
182
|
</span>
|
|
179
183
|
</legend>
|
|
180
184
|
{chrome.description !== undefined && <p className="text-xs leading-snug text-fg-muted">{chrome.description}</p>}
|
|
185
|
+
{extras.error !== undefined && <ErrorText text={extras.error} />}
|
|
186
|
+
{extras.notes !== undefined && <div className="text-xs">{extras.notes}</div>}
|
|
181
187
|
{children}
|
|
182
188
|
</fieldset>
|
|
183
189
|
);
|
|
@@ -295,18 +301,19 @@ function TupleNode({ meta, shape, bare }: { meta: FieldMeta; shape: TupleShape;
|
|
|
295
301
|
function NullableNode({ meta, shape }: { meta: FieldMeta; shape: SchemaShape }) {
|
|
296
302
|
const form = useForm();
|
|
297
303
|
const chrome = useChrome(meta);
|
|
304
|
+
const extras = useFieldExtras(meta);
|
|
298
305
|
const value = getAtPath(form.value, meta.path);
|
|
299
306
|
const set = value !== null && value !== undefined;
|
|
300
307
|
const props = {
|
|
301
308
|
checked: set,
|
|
302
309
|
label: meta.label,
|
|
303
|
-
description: chrome.description,
|
|
310
|
+
description: withNotes(withNotes(chrome.description, extras.error ? <ErrorText text={extras.error} /> : undefined), extras.notes),
|
|
304
311
|
disabled: form.disabled,
|
|
305
312
|
onCheckedChange: (next: boolean) => form.setValue(meta.path, next ? seedNotNull(meta.node, form.schema) : null),
|
|
306
313
|
};
|
|
307
314
|
|
|
308
315
|
return (
|
|
309
|
-
<div className={cn("flex min-w-0 flex-col gap-3", spanClass(form.columns, meta.ui, 2))}>
|
|
316
|
+
<div className={cn("flex min-w-0 flex-col gap-3", spanClass(form.columns, meta.ui, 2), extras.className)}>
|
|
310
317
|
<div className="flex items-start gap-1.5">
|
|
311
318
|
{meta.ui.widget === "checkbox" ? <Checkbox {...props} /> : <Switch {...props} />}
|
|
312
319
|
{chrome.hint !== null && <span className="mt-0.5">{chrome.hint}</span>}
|
package/src/index.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
export { SchemaForm } from "./components/SchemaForm";
|
|
18
|
-
export { SchemaValueForm, type SchemaValueFormProps } from "./components/SchemaValueForm";
|
|
18
|
+
export { SchemaValueForm, type FieldMessage, type FieldMessages, type SchemaValueFormProps } from "./components/SchemaValueForm";
|
|
19
19
|
|
|
20
20
|
export {
|
|
21
21
|
describeFields,
|
|
@@ -48,4 +48,5 @@ export {
|
|
|
48
48
|
type TaggedVariant,
|
|
49
49
|
} from "./api/schemaValue";
|
|
50
50
|
export type { FieldUi, FieldWidget, RenderField, RenderFieldContext, UiGroup, UiSchema } from "./api/uiSchema";
|
|
51
|
+
export { diffPaths } from "./api/valueDiff";
|
|
51
52
|
export { getAtPath, setAtPath } from "./api/valuePath";
|