@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 CHANGED
@@ -31,6 +31,24 @@ helpers for building your own: `defaultValueForSchema`, `resolveSchema`, `shapeO
31
31
  <SchemaValueForm schema={schema} value={config} onValueChange={setConfig} ui={ui} columns={1} />
32
32
  ```
33
33
 
34
+ **Against a baseline**: `baseline` is a value to compare with (the defaults, a previous run).
35
+ - A field that differs gets a rule in the gutter and a note under the control: "changed", what it was, and a Reset
36
+ that writes the baseline's value back through `onValueChange`.
37
+ - A struct, a variant or a tuple row says how many leaves under it changed, with one Reset for the block.
38
+ - A key missing on one side differs from a key present on the other; arrays of equal length compare by index, others as a whole.
39
+ - `diffPaths(value, baseline)` is the same comparison as a list of dot paths.
40
+
41
+ **Messages**: `messages` attaches text to fields by dot path (`solver.max_iters`, `cameras.0.id`), for validation
42
+ that comes from elsewhere, such as a backend.
43
+ - A bare string, or `{ severity: "error", text }`, is announced as an alert and marks the control invalid.
44
+ - `{ severity: "warning", text }` is shown under the field without blocking.
45
+ - The path `""` is the form as a whole, shown above it.
46
+ - A path that no visible field has shows nowhere; list those yourself.
47
+
48
+ ```tsx
49
+ <SchemaValueForm schema={schema} value={config} onValueChange={setConfig} baseline={defaults} messages={{ "solver.max_iters": "Must be at most 1000" }} />
50
+ ```
51
+
34
52
  ## License
35
53
 
36
54
  Licensed under either of
package/dist/index.d.ts CHANGED
@@ -381,7 +381,18 @@ interface RenderFieldContext {
381
381
  */
382
382
  type RenderField = (context: RenderFieldContext) => ReactNode;
383
383
  //#endregion
384
+ //#region src/components/valueForm/context.d.ts
385
+ /** A message the app attaches to a field, such as a backend's verdict on a value. */
386
+ interface FieldMessage {
387
+ /** `error` is announced as an alert and marks the control invalid; `warning` is shown without blocking. */
388
+ severity: "error" | "warning";
389
+ /** What to tell the person. */
390
+ text: string;
391
+ }
392
+ //#endregion
384
393
  //#region src/components/SchemaValueForm.d.ts
394
+ /** Messages from the app, by dot path (`""` is the form as a whole). A bare string is an error. */
395
+ type FieldMessages = Readonly<Record<string, FieldMessage | string>>;
385
396
  /** Props of `SchemaValueForm`. */
386
397
  interface SchemaValueFormProps {
387
398
  /**
@@ -403,6 +414,23 @@ interface SchemaValueFormProps {
403
414
  onValueChange?: ((next: unknown) => void) | undefined;
404
415
  /** Layout the schema cannot carry: groups, labels, units, widgets. */
405
416
  ui?: UiSchema | undefined;
417
+ /**
418
+ * The value to compare with, such as the defaults or a previous run. A field whose value
419
+ * differs from it is marked "changed" (a rule in the gutter and a note under the control
420
+ * saying what it was), with a Reset that writes the baseline's value back through
421
+ * `onValueChange`; a struct, a variant or a tuple shows how many leaves under it changed
422
+ * and one Reset for them all. A key missing on one side differs from a key present on the
423
+ * other; arrays of equal length compare by index, others as a whole. Without it, nothing is marked.
424
+ */
425
+ baseline?: unknown;
426
+ /**
427
+ * Messages to show on fields, by dot path (`solver.max_iters`, `cameras.0.id`, the paths
428
+ * `getAtPath` reads), for validation that comes from elsewhere, such as a backend. An `error`
429
+ * (or a bare string) is announced as an alert and marks the control invalid; a `warning`
430
+ * is shown under it without blocking. A path of `""` shows above the form. A path that no
431
+ * visible field has shows nowhere: list those yourself.
432
+ */
433
+ messages?: FieldMessages | undefined;
406
434
  /** `2` (the default) puts fields side by side from `sm:` up; `1` is one column, for a narrow rail. */
407
435
  columns?: 1 | 2 | undefined;
408
436
  /** Blocks every control. The value is still shown. */
@@ -436,7 +464,7 @@ interface SchemaValueFormProps {
436
464
  * The root is a `div` carrying `data-columns` and, while disabled, `data-disabled`. Wraps itself in a
437
465
  * `TooltipProvider`, so it needs none above it.
438
466
  */
439
- export declare function SchemaValueForm({ schema, value, defaultValue, onValueChange, ui, columns, disabled, renderField, className, ref }: SchemaValueFormProps): import("react").JSX.Element;
467
+ export declare function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline, messages, ui, columns, disabled, renderField, className, ref }: SchemaValueFormProps): import("react").JSX.Element;
440
468
  //#endregion
441
469
  //#region src/api/firstParagraph.d.ts
442
470
  /**
@@ -600,6 +628,27 @@ export declare function fieldAt(schema: JsonSchema, path: string, root?: JsonSch
600
628
  */
601
629
  export declare function fieldsAt(schema: JsonSchema, path: string, root?: JsonSchema, value?: unknown): FieldInfo[];
602
630
  //#endregion
631
+ //#region src/api/valueDiff.d.ts
632
+ /**
633
+ * Where one JSON value differs from another, by dot path: what a form marks as "changed from
634
+ * the baseline".
635
+ *
636
+ * Paths are the ones `getAtPath` reads. A value and its baseline are compared structurally:
637
+ * objects by key (a key missing on one side differs from a key present on the other, even
638
+ * if the present value is `null`), arrays by index when they have the same length, and by
639
+ * the array as a whole when they do not (its elements have no shared identity to match).
640
+ */
641
+ /**
642
+ * The paths at which `current` differs from `baseline`, deepest first-found, in key order.
643
+ *
644
+ * @param current - The value.
645
+ * @param baseline - What it is compared with.
646
+ * @param path - The path of both, for a recursive call; `""` is the root.
647
+ * @returns Each differing path once; `[""]` when the roots are different kinds of value.
648
+ * Empty when the values are equal.
649
+ */
650
+ export declare function diffPaths(current: unknown, baseline: unknown, path?: string): string[];
651
+ //#endregion
603
652
  //#region src/api/valuePath.d.ts
604
653
  /**
605
654
  * Read the value at a path.
@@ -624,5 +673,5 @@ export declare function getAtPath(value: unknown, path: string): unknown;
624
673
  */
625
674
  export declare function setAtPath(value: unknown, path: string, next: unknown): unknown;
626
675
  //#endregion
627
- export type { ChoiceOption, EnumOption, EnumValue, ExternalVariant, FieldInfo, FieldKind, FieldSpec, FieldUi, FieldWidget, JsonSchema, OptionsSchema, RawValues, RenderField, RenderFieldContext, ResolvedSchema, SchemaNode, SchemaShape, SchemaValueFormProps, TaggedVariant, UiGroup, UiSchema };
676
+ export type { ChoiceOption, EnumOption, EnumValue, ExternalVariant, FieldInfo, FieldKind, FieldMessage, FieldMessages, FieldSpec, FieldUi, FieldWidget, JsonSchema, OptionsSchema, RawValues, RenderField, RenderFieldContext, ResolvedSchema, SchemaNode, SchemaShape, SchemaValueFormProps, TaggedVariant, UiGroup, UiSchema };
628
677
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Button, Checkbox, Disclosure, Field, InfoHint, Input, NumberInput, Section, SegmentedControl, Select, Switch, Textarea, TooltipProvider, byDensity, cn, parseNumber, useDensity } from "@vitavision/ui";
1
+ import { Button, Checkbox, Disclosure, Field, InfoHint, Input, NumberInput, Section, SegmentedControl, Select, Switch, Textarea, TooltipProvider, byDensity, cn, focusRing, parseNumber, useDensity } from "@vitavision/ui";
2
2
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
3
  import { createContext, use, useMemo, useState } from "react";
4
4
  //#region src/api/schemaNode.ts
@@ -621,7 +621,7 @@ function SchemaField({ field, value, onChange }) {
621
621
  * Python configs these forms are for do not have them.
622
622
  */
623
623
  const INDEX = /^(0|[1-9]\d*)$/;
624
- function isRecord$1(value) {
624
+ function isRecord$2(value) {
625
625
  return typeof value === "object" && value !== null && !Array.isArray(value);
626
626
  }
627
627
  /**
@@ -653,7 +653,7 @@ function joinPath(parent, key) {
653
653
  function getAtPath(value, path) {
654
654
  let current = value;
655
655
  for (const segment of splitPath(path)) if (Array.isArray(current)) current = INDEX.test(segment) ? current[Number(segment)] : void 0;
656
- else if (isRecord$1(current) && Object.hasOwn(current, segment)) current = current[segment];
656
+ else if (isRecord$2(current) && Object.hasOwn(current, segment)) current = current[segment];
657
657
  else return;
658
658
  return current;
659
659
  }
@@ -661,7 +661,7 @@ function setIn(value, segments, depth, next) {
661
661
  const segment = segments[depth];
662
662
  if (segment === void 0) return next;
663
663
  const isIndex = INDEX.test(segment);
664
- if (Array.isArray(value) || !isRecord$1(value) && isIndex) {
664
+ if (Array.isArray(value) || !isRecord$2(value) && isIndex) {
665
665
  if (!isIndex) return value;
666
666
  const list = Array.isArray(value) ? value : [];
667
667
  const index = Number(segment);
@@ -672,7 +672,7 @@ function setIn(value, segments, depth, next) {
672
672
  copy[index] = updated;
673
673
  return copy;
674
674
  }
675
- const record = isRecord$1(value) ? value : {};
675
+ const record = isRecord$2(value) ? value : {};
676
676
  const has = Object.hasOwn(record, segment);
677
677
  const updated = setIn(has ? record[segment] : void 0, segments, depth + 1, next);
678
678
  if (updated === void 0) {
@@ -735,7 +735,7 @@ const MAX_DEPTH = 8;
735
735
  function isPrimitive(value) {
736
736
  return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
737
737
  }
738
- function isRecord(value) {
738
+ function isRecord$1(value) {
739
739
  return typeof value === "object" && value !== null && !Array.isArray(value);
740
740
  }
741
741
  function fieldsOf(schema, omit) {
@@ -899,7 +899,7 @@ function shapeOf(node, root = node) {
899
899
  * value that names none (absent, not an object, an unknown tag).
900
900
  */
901
901
  function activeTagged(shape, value) {
902
- const tag = isRecord(value) ? value[shape.discriminator] : void 0;
902
+ const tag = isRecord$1(value) ? value[shape.discriminator] : void 0;
903
903
  return typeof tag === "string" ? shape.variants.find((variant) => variant.tag === tag) : void 0;
904
904
  }
905
905
  /**
@@ -912,7 +912,7 @@ function activeTagged(shape, value) {
912
912
  */
913
913
  function activeExternal(shape, value) {
914
914
  if (typeof value === "string") return shape.variants.find((variant) => variant.payload === void 0 && variant.tag === value);
915
- if (!isRecord(value)) return void 0;
915
+ if (!isRecord$1(value)) return void 0;
916
916
  return shape.variants.find((variant) => variant.payload !== void 0 && Object.hasOwn(value, variant.tag));
917
917
  }
918
918
  function seedOf(schema, root, depth, notNull) {
@@ -966,7 +966,7 @@ function seedNumber(schema, integer) {
966
966
  function seedTagged(shape, variant, root, depth) {
967
967
  const body = seedOf(variant.schema, root, depth + 1, false);
968
968
  return {
969
- ...isRecord(body) ? body : {},
969
+ ...isRecord$1(body) ? body : {},
970
970
  [shape.discriminator]: variant.tag
971
971
  };
972
972
  }
@@ -1148,6 +1148,76 @@ function fieldUiAt(ui, path) {
1148
1148
  };
1149
1149
  }
1150
1150
  //#endregion
1151
+ //#region src/api/valueDiff.ts
1152
+ /**
1153
+ * Where one JSON value differs from another, by dot path: what a form marks as "changed from
1154
+ * the baseline".
1155
+ *
1156
+ * Paths are the ones `getAtPath` reads. A value and its baseline are compared structurally:
1157
+ * objects by key (a key missing on one side differs from a key present on the other, even
1158
+ * if the present value is `null`), arrays by index when they have the same length, and by
1159
+ * the array as a whole when they do not (its elements have no shared identity to match).
1160
+ */
1161
+ function isRecord(value) {
1162
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1163
+ }
1164
+ /**
1165
+ * The paths at which `current` differs from `baseline`, deepest first-found, in key order.
1166
+ *
1167
+ * @param current - The value.
1168
+ * @param baseline - What it is compared with.
1169
+ * @param path - The path of both, for a recursive call; `""` is the root.
1170
+ * @returns Each differing path once; `[""]` when the roots are different kinds of value.
1171
+ * Empty when the values are equal.
1172
+ */
1173
+ function diffPaths(current, baseline, path = "") {
1174
+ if (Object.is(current, baseline)) return [];
1175
+ if (isRecord(current) && isRecord(baseline)) return [.../* @__PURE__ */ new Set([...Object.keys(current), ...Object.keys(baseline)])].flatMap((key) => {
1176
+ const inCurrent = Object.hasOwn(current, key);
1177
+ const inBaseline = Object.hasOwn(baseline, key);
1178
+ const child = joinPath(path, key);
1179
+ return inCurrent && inBaseline ? diffPaths(current[key], baseline[key], child) : [child];
1180
+ });
1181
+ if (Array.isArray(current) && Array.isArray(baseline) && current.length === baseline.length) return current.flatMap((entry, index) => diffPaths(entry, baseline[index], joinPath(path, String(index))));
1182
+ return [path];
1183
+ }
1184
+ /**
1185
+ * How many differing leaves lie at or under each path.
1186
+ *
1187
+ * @param paths - From `diffPaths`.
1188
+ * @returns A map from a path, and each of its ancestors (the root as `""`), to the count of
1189
+ * differing paths at or beneath it.
1190
+ */
1191
+ function changeCounts(paths) {
1192
+ const counts = /* @__PURE__ */ new Map();
1193
+ for (const path of paths) {
1194
+ let at = path;
1195
+ for (;;) {
1196
+ counts.set(at, (counts.get(at) ?? 0) + 1);
1197
+ if (at === "") break;
1198
+ const cut = at.lastIndexOf(".");
1199
+ at = cut < 0 ? "" : at.slice(0, cut);
1200
+ }
1201
+ }
1202
+ return counts;
1203
+ }
1204
+ /**
1205
+ * A value as the "was ..." note shows it: short, one line.
1206
+ *
1207
+ * @param value - The baseline's value at a field.
1208
+ * @param limit - The longest text, in characters; a longer one is cut with an ellipsis. Defaults to 40.
1209
+ * @returns `unset` for `undefined`, `none` for `null`, `""` quoted, else the value or its JSON.
1210
+ */
1211
+ function describeValue(value, limit = 40) {
1212
+ let text;
1213
+ if (value === void 0) text = "unset";
1214
+ else if (value === null) text = "none";
1215
+ else if (typeof value === "string") text = value === "" ? "\"\"" : value;
1216
+ else if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") text = value.toString();
1217
+ else text = JSON.stringify(value) ?? "";
1218
+ return text.length > limit ? `${text.slice(0, limit - 1)}…` : text;
1219
+ }
1220
+ //#endregion
1151
1221
  //#region src/api/firstParagraph.ts
1152
1222
  /**
1153
1223
  * Descriptions are Markdown (Rustdoc comments, docstrings) and the form shows them as plain
@@ -1263,6 +1333,107 @@ function useChrome(meta, range = null) {
1263
1333
  };
1264
1334
  }
1265
1335
  //#endregion
1336
+ //#region src/components/valueForm/changes.tsx
1337
+ /**
1338
+ * What a field shows beyond its control: whether it differs from the form's baseline (a rule
1339
+ * at its edge, a "changed" note that says what it was, and a reset), and the app's message
1340
+ * for it.
1341
+ *
1342
+ * The note sits under the control, outside the field's `<label>`: a button inside a label
1343
+ * would be part of the control's name. It is the field's description, so a reader hears it
1344
+ * with the control.
1345
+ */
1346
+ /** 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. */
1347
+ const CHANGED_RULE = "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";
1348
+ /** A message as `{ severity, text }`: a bare string is an error. */
1349
+ function normalise(message) {
1350
+ if (message === void 0) return void 0;
1351
+ return typeof message === "string" ? {
1352
+ severity: "error",
1353
+ text: message
1354
+ } : message;
1355
+ }
1356
+ /**
1357
+ * The baseline comparison and the app's message for one field.
1358
+ *
1359
+ * @param meta - The field.
1360
+ * @returns Its extras; all empty for a form with no baseline and no message.
1361
+ */
1362
+ function useFieldExtras(meta) {
1363
+ const form = useForm();
1364
+ const dense = use(DenseContext);
1365
+ const changes = form.baseline?.counts.get(meta.path) ?? 0;
1366
+ const message = normalise(form.messages?.[meta.path]);
1367
+ const warning = message?.severity === "warning" ? message.text : void 0;
1368
+ const notes = changes > 0 && !dense || warning !== void 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [warning !== void 0 && /* @__PURE__ */ jsx("span", {
1369
+ "data-message": "warning",
1370
+ className: "mt-0.5 block text-warn",
1371
+ children: warning
1372
+ }), changes > 0 && !dense && /* @__PURE__ */ jsx(ChangeNote, {
1373
+ meta,
1374
+ count: changes
1375
+ })] }) : void 0;
1376
+ return {
1377
+ className: changes > 0 ? CHANGED_RULE : void 0,
1378
+ changed: changes > 0,
1379
+ error: message?.severity === "error" ? message.text : void 0,
1380
+ notes
1381
+ };
1382
+ }
1383
+ /** An error message where the control has no error slot of its own (a switch). */
1384
+ function ErrorText({ text }) {
1385
+ return /* @__PURE__ */ jsx("span", {
1386
+ role: "alert",
1387
+ "data-message": "error",
1388
+ className: "block text-defect",
1389
+ children: text
1390
+ });
1391
+ }
1392
+ /** A description and the extras' notes, as one `Field` description (`undefined` when both are empty). */
1393
+ function withNotes(description, notes) {
1394
+ if (!(description !== void 0 && description !== null && description !== false && description !== "")) return notes;
1395
+ return notes === void 0 ? description : /* @__PURE__ */ jsxs(Fragment, { children: [description, notes] });
1396
+ }
1397
+ /** The "changed" marker with what the baseline had, and the button that puts it back. */
1398
+ function ChangeNote({ meta, count }) {
1399
+ const was = getAtPath(useForm().baseline?.value, meta.path);
1400
+ const composite = typeof was === "object" && was !== null;
1401
+ return /* @__PURE__ */ jsxs("span", {
1402
+ "data-change-note": "",
1403
+ className: "mt-0.5 flex min-w-0 items-center gap-2 text-[11px] leading-snug",
1404
+ children: [
1405
+ /* @__PURE__ */ jsxs("span", {
1406
+ className: "inline-flex shrink-0 items-center gap-1 font-medium text-signal",
1407
+ children: [/* @__PURE__ */ jsx("span", {
1408
+ "aria-hidden": true,
1409
+ className: "size-1.5 rounded-full bg-signal"
1410
+ }), composite && count > 1 ? `${count} changed` : "changed"]
1411
+ }),
1412
+ !composite && /* @__PURE__ */ jsxs("span", {
1413
+ className: "min-w-0 truncate text-fg-muted",
1414
+ children: ["was ", /* @__PURE__ */ jsx("span", {
1415
+ className: "font-mono",
1416
+ children: describeValue(was)
1417
+ })]
1418
+ }),
1419
+ /* @__PURE__ */ jsx(ResetButton, { meta })
1420
+ ]
1421
+ });
1422
+ }
1423
+ /** Puts a field (or a whole block) back to the baseline's value. */
1424
+ function ResetButton({ meta, className }) {
1425
+ const form = useForm();
1426
+ return /* @__PURE__ */ jsx("button", {
1427
+ type: "button",
1428
+ "data-reset": "",
1429
+ "aria-label": `Reset ${meta.label} to baseline`,
1430
+ disabled: form.disabled,
1431
+ onClick: () => form.setValue(meta.path, getAtPath(form.baseline?.value, meta.path)),
1432
+ className: cn("-my-1 ml-auto shrink-0 cursor-pointer rounded-control px-1 py-1 font-medium text-fg-muted transition-colors", "hover:text-fg disabled:cursor-not-allowed disabled:opacity-50", focusRing, className),
1433
+ children: "Reset"
1434
+ });
1435
+ }
1436
+ //#endregion
1266
1437
  //#region src/api/numberSpec.ts
1267
1438
  /**
1268
1439
  * Collect a number field's rules.
@@ -1344,6 +1515,7 @@ function NumberLeaf({ meta }) {
1344
1515
  const value = getAtPath(form.value, meta.path);
1345
1516
  const spec = numberSpec(meta.schema, meta.ui);
1346
1517
  const chrome = useChrome(meta, rangeText(spec));
1518
+ const extras = useFieldExtras(meta);
1347
1519
  const [error, setError] = useState();
1348
1520
  const fallback = meta.schema.default;
1349
1521
  const restore = meta.ui.restoreDefaultOnClear ?? true;
@@ -1363,9 +1535,9 @@ function NumberLeaf({ meta }) {
1363
1535
  label: meta.label,
1364
1536
  required: marked(meta),
1365
1537
  annotation: chrome.annotation,
1366
- description: chrome.description,
1367
- error,
1368
- className: spanClass(form.columns, meta.ui, 1),
1538
+ description: withNotes(chrome.description, extras.notes),
1539
+ error: error ?? extras.error,
1540
+ className: cn(spanClass(form.columns, meta.ui, 1), extras.className),
1369
1541
  children: /* @__PURE__ */ jsx(NumberInput, {
1370
1542
  "aria-label": meta.label,
1371
1543
  disabled: form.disabled,
@@ -1393,13 +1565,15 @@ function TextLeaf({ meta }) {
1393
1565
  const form = useForm();
1394
1566
  const value = getAtPath(form.value, meta.path);
1395
1567
  const chrome = useChrome(meta);
1568
+ const extras = useFieldExtras(meta);
1396
1569
  const fallback = meta.schema.default;
1397
1570
  return /* @__PURE__ */ jsx(Field, {
1398
1571
  label: meta.label,
1399
1572
  required: marked(meta),
1400
1573
  annotation: chrome.annotation,
1401
- description: chrome.description,
1402
- className: spanClass(form.columns, meta.ui, 1),
1574
+ description: withNotes(chrome.description, extras.notes),
1575
+ error: extras.error,
1576
+ className: cn(spanClass(form.columns, meta.ui, 1), extras.className),
1403
1577
  children: /* @__PURE__ */ jsx(Input, {
1404
1578
  className: "font-mono",
1405
1579
  autoComplete: "off",
@@ -1443,6 +1617,7 @@ function EnumLeaf({ meta, options }) {
1443
1617
  const dense = use(DenseContext);
1444
1618
  const value = getAtPath(form.value, meta.path);
1445
1619
  const chrome = useChrome(meta);
1620
+ const extras = useFieldExtras(meta);
1446
1621
  const shown = options.map((option) => ({
1447
1622
  value: String(option.value),
1448
1623
  label: meta.ui.enumLabels?.[String(option.value)] ?? option.label
@@ -1459,8 +1634,9 @@ function EnumLeaf({ meta, options }) {
1459
1634
  label: meta.label,
1460
1635
  required: marked(meta),
1461
1636
  annotation: chrome.annotation,
1462
- description,
1463
- className: spanClass(form.columns, meta.ui, 1),
1637
+ description: withNotes(description, extras.notes),
1638
+ error: extras.error,
1639
+ className: cn(spanClass(form.columns, meta.ui, 1), extras.className),
1464
1640
  children: /* @__PURE__ */ jsx(Chooser, {
1465
1641
  label: meta.label,
1466
1642
  options: shown,
@@ -1484,15 +1660,16 @@ function BooleanLeaf({ meta }) {
1484
1660
  const form = useForm();
1485
1661
  const value = getAtPath(form.value, meta.path);
1486
1662
  const chrome = useChrome(meta);
1663
+ const extras = useFieldExtras(meta);
1487
1664
  const props = {
1488
1665
  checked: value === true,
1489
1666
  onCheckedChange: (next) => form.setValue(meta.path, next),
1490
1667
  label: meta.label,
1491
- description: chrome.description,
1668
+ description: withNotes(withNotes(chrome.description, extras.error ? /* @__PURE__ */ jsx(ErrorText, { text: extras.error }) : void 0), extras.notes),
1492
1669
  disabled: form.disabled
1493
1670
  };
1494
1671
  return /* @__PURE__ */ jsxs("div", {
1495
- className: cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1)),
1672
+ className: cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1), extras.className),
1496
1673
  children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...props }) : /* @__PURE__ */ jsx(Switch, { ...props }), chrome.hint !== null && /* @__PURE__ */ jsx("span", {
1497
1674
  className: "mt-0.5",
1498
1675
  children: chrome.hint
@@ -1513,6 +1690,7 @@ function JsonLeaf({ meta }) {
1513
1690
  const form = useForm();
1514
1691
  const value = getAtPath(form.value, meta.path);
1515
1692
  const chrome = useChrome(meta);
1693
+ const extras = useFieldExtras(meta);
1516
1694
  const canonical = JSON.stringify(value) ?? "";
1517
1695
  const [draft, setDraft] = useState(() => ({
1518
1696
  text: pretty(value),
@@ -1561,9 +1739,9 @@ function JsonLeaf({ meta }) {
1561
1739
  label: meta.label,
1562
1740
  required: marked(meta),
1563
1741
  annotation: chrome.annotation,
1564
- description: chrome.description,
1565
- error: draft.error,
1566
- className: spanClass(form.columns, meta.ui, 2),
1742
+ description: withNotes(chrome.description, extras.notes),
1743
+ error: draft.error ?? extras.error,
1744
+ className: cn(spanClass(form.columns, meta.ui, 2), extras.className),
1567
1745
  children: /* @__PURE__ */ jsx(Textarea, {
1568
1746
  mono: true,
1569
1747
  rows: Math.min(Math.max(draft.text.split("\n").length, 3), 12),
@@ -1596,6 +1774,7 @@ function StringListLeaf({ meta, minItems, maxItems }) {
1596
1774
  const form = useForm();
1597
1775
  const value = getAtPath(form.value, meta.path);
1598
1776
  const chrome = useChrome(meta);
1777
+ const extras = useFieldExtras(meta);
1599
1778
  const raw = Array.isArray(value) ? value : [];
1600
1779
  const [rows, setRows] = useState(() => syncIds({
1601
1780
  ids: [],
@@ -1611,8 +1790,9 @@ function StringListLeaf({ meta, minItems, maxItems }) {
1611
1790
  label: meta.label,
1612
1791
  required: marked(meta),
1613
1792
  annotation: chrome.annotation,
1614
- description: chrome.description,
1615
- className: spanClass(form.columns, meta.ui, 2),
1793
+ description: withNotes(chrome.description, extras.notes),
1794
+ error: extras.error,
1795
+ className: cn(spanClass(form.columns, meta.ui, 2), extras.className),
1616
1796
  children: /* @__PURE__ */ jsxs("div", {
1617
1797
  className: "flex flex-col gap-1.5",
1618
1798
  children: [raw.map((entry, index) => /* @__PURE__ */ jsxs("div", {
@@ -1807,12 +1987,14 @@ function Frame({ meta, bare, children }) {
1807
1987
  const dense = use(DenseContext);
1808
1988
  const density = useDensity();
1809
1989
  const chrome = useChrome(meta);
1990
+ const extras = useFieldExtras(meta);
1810
1991
  if (bare) return /* @__PURE__ */ jsx("div", {
1811
1992
  className: "flex min-w-0 flex-col gap-3",
1812
1993
  children
1813
1994
  });
1814
1995
  return /* @__PURE__ */ jsxs("fieldset", {
1815
- className: cn("flex min-w-0 flex-col gap-3 rounded-panel border border-line", byDensity(density, "p-3", "p-2"), !dense && spanClass(form.columns, meta.ui, 2)),
1996
+ "data-changed": extras.changed ? "" : void 0,
1997
+ className: cn("flex min-w-0 flex-col gap-3 rounded-panel border border-line", byDensity(density, "p-3", "p-2"), !dense && spanClass(form.columns, meta.ui, 2), extras.className),
1816
1998
  children: [
1817
1999
  /* @__PURE__ */ jsx("legend", {
1818
2000
  className: "px-1",
@@ -1825,6 +2007,11 @@ function Frame({ meta, bare, children }) {
1825
2007
  className: "text-xs leading-snug text-fg-muted",
1826
2008
  children: chrome.description
1827
2009
  }),
2010
+ extras.error !== void 0 && /* @__PURE__ */ jsx(ErrorText, { text: extras.error }),
2011
+ extras.notes !== void 0 && /* @__PURE__ */ jsx("div", {
2012
+ className: "text-xs",
2013
+ children: extras.notes
2014
+ }),
1828
2015
  children
1829
2016
  ]
1830
2017
  });
@@ -1956,17 +2143,18 @@ function TupleNode({ meta, shape, bare }) {
1956
2143
  function NullableNode({ meta, shape }) {
1957
2144
  const form = useForm();
1958
2145
  const chrome = useChrome(meta);
2146
+ const extras = useFieldExtras(meta);
1959
2147
  const value = getAtPath(form.value, meta.path);
1960
2148
  const set = value !== null && value !== void 0;
1961
2149
  const props = {
1962
2150
  checked: set,
1963
2151
  label: meta.label,
1964
- description: chrome.description,
2152
+ description: withNotes(withNotes(chrome.description, extras.error ? /* @__PURE__ */ jsx(ErrorText, { text: extras.error }) : void 0), extras.notes),
1965
2153
  disabled: form.disabled,
1966
2154
  onCheckedChange: (next) => form.setValue(meta.path, next ? seedNotNull(meta.node, form.schema) : null)
1967
2155
  };
1968
2156
  return /* @__PURE__ */ jsxs("div", {
1969
- className: cn("flex min-w-0 flex-col gap-3", spanClass(form.columns, meta.ui, 2)),
2157
+ className: cn("flex min-w-0 flex-col gap-3", spanClass(form.columns, meta.ui, 2), extras.className),
1970
2158
  children: [/* @__PURE__ */ jsxs("div", {
1971
2159
  className: "flex items-start gap-1.5",
1972
2160
  children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...props }) : /* @__PURE__ */ jsx(Switch, { ...props }), chrome.hint !== null && /* @__PURE__ */ jsx("span", {
@@ -2023,8 +2211,12 @@ function useValue(value, seed, onValueChange) {
2023
2211
  * The root is a `div` carrying `data-columns` and, while disabled, `data-disabled`. Wraps itself in a
2024
2212
  * `TooltipProvider`, so it needs none above it.
2025
2213
  */
2026
- function SchemaValueForm({ schema, value, defaultValue, onValueChange, ui = NO_UI, columns = 2, disabled = false, renderField, className, ref }) {
2214
+ function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline, messages, ui = NO_UI, columns = 2, disabled = false, renderField, className, ref }) {
2027
2215
  const [current, commit] = useValue(value, () => defaultValue !== void 0 ? defaultValue : defaultValueForSchema(schema), onValueChange);
2216
+ const compared = useMemo(() => baseline === void 0 ? null : {
2217
+ value: baseline,
2218
+ counts: changeCounts(diffPaths(current, baseline))
2219
+ }, [current, baseline]);
2028
2220
  const state = {
2029
2221
  schema,
2030
2222
  value: current,
@@ -2036,9 +2228,16 @@ function SchemaValueForm({ schema, value, defaultValue, onValueChange, ui = NO_U
2036
2228
  disabled,
2037
2229
  columns,
2038
2230
  renderField,
2039
- claimed: useMemo(() => new Set((ui.groups ?? []).flatMap((group) => group.fields)), [ui.groups])
2231
+ claimed: useMemo(() => new Set((ui.groups ?? []).flatMap((group) => group.fields)), [ui.groups]),
2232
+ baseline: compared,
2233
+ messages
2040
2234
  };
2041
2235
  const root = shapeOf(schema, schema);
2236
+ const raw = messages?.[""];
2237
+ const formMessage = typeof raw === "string" ? {
2238
+ severity: "error",
2239
+ text: raw
2240
+ } : raw;
2042
2241
  return /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsx(FormContext, {
2043
2242
  value: state,
2044
2243
  children: /* @__PURE__ */ jsxs("div", {
@@ -2046,16 +2245,25 @@ function SchemaValueForm({ schema, value, defaultValue, onValueChange, ui = NO_U
2046
2245
  "data-columns": columns,
2047
2246
  "data-disabled": disabled ? "" : void 0,
2048
2247
  className: cn("flex min-w-0 flex-col gap-5", className),
2049
- children: [root.kind === "object" && (ui.groups ?? []).map((group) => /* @__PURE__ */ jsx(GroupBlock, { group }, group.id)), root.kind === "object" ? /* @__PURE__ */ jsx(ObjectBody, {
2050
- path: "",
2051
- fields: root.fields
2052
- }) : /* @__PURE__ */ jsx(FieldNode, {
2053
- path: "",
2054
- name: schema.title ?? "value",
2055
- node: schema,
2056
- required: true,
2057
- bare: true
2058
- })]
2248
+ children: [
2249
+ formMessage !== void 0 && /* @__PURE__ */ jsx("p", {
2250
+ role: formMessage.severity === "error" ? "alert" : void 0,
2251
+ "data-message": formMessage.severity,
2252
+ className: cn("text-xs leading-snug", formMessage.severity === "error" ? "text-defect" : "text-warn"),
2253
+ children: formMessage.text
2254
+ }),
2255
+ root.kind === "object" && (ui.groups ?? []).map((group) => /* @__PURE__ */ jsx(GroupBlock, { group }, group.id)),
2256
+ root.kind === "object" ? /* @__PURE__ */ jsx(ObjectBody, {
2257
+ path: "",
2258
+ fields: root.fields
2259
+ }) : /* @__PURE__ */ jsx(FieldNode, {
2260
+ path: "",
2261
+ name: schema.title ?? "value",
2262
+ node: schema,
2263
+ required: true,
2264
+ bare: true
2265
+ })
2266
+ ]
2059
2267
  })
2060
2268
  }) });
2061
2269
  }
@@ -2093,6 +2301,6 @@ function GroupBlock({ group }) {
2093
2301
  });
2094
2302
  }
2095
2303
  //#endregion
2096
- export { SchemaForm, SchemaValueForm, defaultValueForSchema, describeFields, fieldAt, fieldsAt, firstParagraph, getAtPath, initialValues, jsonErrors, missingRequired, outOfRange, overrideCount, resolveRef, resolveSchema, setAtPath, shapeOf, toOptions };
2304
+ export { SchemaForm, SchemaValueForm, defaultValueForSchema, describeFields, diffPaths, fieldAt, fieldsAt, firstParagraph, getAtPath, initialValues, jsonErrors, missingRequired, outOfRange, overrideCount, resolveRef, resolveSchema, setAtPath, shapeOf, toOptions };
2097
2305
 
2098
2306
  //# sourceMappingURL=index.js.map