@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 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
@@ -583,6 +583,7 @@ function SchemaField({ field, value, onChange }) {
583
583
  annotation: field.range,
584
584
  className: span,
585
585
  children: field.kind === "json" ? /* @__PURE__ */ jsx(Textarea, {
586
+ mono: true,
586
587
  rows: 2,
587
588
  "aria-label": field.label,
588
589
  placeholder: field.placeholder,
@@ -620,7 +621,7 @@ function SchemaField({ field, value, onChange }) {
620
621
  * Python configs these forms are for do not have them.
621
622
  */
622
623
  const INDEX = /^(0|[1-9]\d*)$/;
623
- function isRecord$1(value) {
624
+ function isRecord$2(value) {
624
625
  return typeof value === "object" && value !== null && !Array.isArray(value);
625
626
  }
626
627
  /**
@@ -652,7 +653,7 @@ function joinPath(parent, key) {
652
653
  function getAtPath(value, path) {
653
654
  let current = value;
654
655
  for (const segment of splitPath(path)) if (Array.isArray(current)) current = INDEX.test(segment) ? current[Number(segment)] : void 0;
655
- 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];
656
657
  else return;
657
658
  return current;
658
659
  }
@@ -660,7 +661,7 @@ function setIn(value, segments, depth, next) {
660
661
  const segment = segments[depth];
661
662
  if (segment === void 0) return next;
662
663
  const isIndex = INDEX.test(segment);
663
- if (Array.isArray(value) || !isRecord$1(value) && isIndex) {
664
+ if (Array.isArray(value) || !isRecord$2(value) && isIndex) {
664
665
  if (!isIndex) return value;
665
666
  const list = Array.isArray(value) ? value : [];
666
667
  const index = Number(segment);
@@ -671,7 +672,7 @@ function setIn(value, segments, depth, next) {
671
672
  copy[index] = updated;
672
673
  return copy;
673
674
  }
674
- const record = isRecord$1(value) ? value : {};
675
+ const record = isRecord$2(value) ? value : {};
675
676
  const has = Object.hasOwn(record, segment);
676
677
  const updated = setIn(has ? record[segment] : void 0, segments, depth + 1, next);
677
678
  if (updated === void 0) {
@@ -734,7 +735,7 @@ const MAX_DEPTH = 8;
734
735
  function isPrimitive(value) {
735
736
  return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
736
737
  }
737
- function isRecord(value) {
738
+ function isRecord$1(value) {
738
739
  return typeof value === "object" && value !== null && !Array.isArray(value);
739
740
  }
740
741
  function fieldsOf(schema, omit) {
@@ -898,7 +899,7 @@ function shapeOf(node, root = node) {
898
899
  * value that names none (absent, not an object, an unknown tag).
899
900
  */
900
901
  function activeTagged(shape, value) {
901
- const tag = isRecord(value) ? value[shape.discriminator] : void 0;
902
+ const tag = isRecord$1(value) ? value[shape.discriminator] : void 0;
902
903
  return typeof tag === "string" ? shape.variants.find((variant) => variant.tag === tag) : void 0;
903
904
  }
904
905
  /**
@@ -911,7 +912,7 @@ function activeTagged(shape, value) {
911
912
  */
912
913
  function activeExternal(shape, value) {
913
914
  if (typeof value === "string") return shape.variants.find((variant) => variant.payload === void 0 && variant.tag === value);
914
- if (!isRecord(value)) return void 0;
915
+ if (!isRecord$1(value)) return void 0;
915
916
  return shape.variants.find((variant) => variant.payload !== void 0 && Object.hasOwn(value, variant.tag));
916
917
  }
917
918
  function seedOf(schema, root, depth, notNull) {
@@ -965,7 +966,7 @@ function seedNumber(schema, integer) {
965
966
  function seedTagged(shape, variant, root, depth) {
966
967
  const body = seedOf(variant.schema, root, depth + 1, false);
967
968
  return {
968
- ...isRecord(body) ? body : {},
969
+ ...isRecord$1(body) ? body : {},
969
970
  [shape.discriminator]: variant.tag
970
971
  };
971
972
  }
@@ -1147,6 +1148,76 @@ function fieldUiAt(ui, path) {
1147
1148
  };
1148
1149
  }
1149
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
1150
1221
  //#region src/api/firstParagraph.ts
1151
1222
  /**
1152
1223
  * Descriptions are Markdown (Rustdoc comments, docstrings) and the form shows them as plain
@@ -1262,6 +1333,107 @@ function useChrome(meta, range = null) {
1262
1333
  };
1263
1334
  }
1264
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
1265
1437
  //#region src/api/numberSpec.ts
1266
1438
  /**
1267
1439
  * Collect a number field's rules.
@@ -1343,6 +1515,7 @@ function NumberLeaf({ meta }) {
1343
1515
  const value = getAtPath(form.value, meta.path);
1344
1516
  const spec = numberSpec(meta.schema, meta.ui);
1345
1517
  const chrome = useChrome(meta, rangeText(spec));
1518
+ const extras = useFieldExtras(meta);
1346
1519
  const [error, setError] = useState();
1347
1520
  const fallback = meta.schema.default;
1348
1521
  const restore = meta.ui.restoreDefaultOnClear ?? true;
@@ -1362,9 +1535,9 @@ function NumberLeaf({ meta }) {
1362
1535
  label: meta.label,
1363
1536
  required: marked(meta),
1364
1537
  annotation: chrome.annotation,
1365
- description: chrome.description,
1366
- error,
1367
- 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),
1368
1541
  children: /* @__PURE__ */ jsx(NumberInput, {
1369
1542
  "aria-label": meta.label,
1370
1543
  disabled: form.disabled,
@@ -1392,13 +1565,15 @@ function TextLeaf({ meta }) {
1392
1565
  const form = useForm();
1393
1566
  const value = getAtPath(form.value, meta.path);
1394
1567
  const chrome = useChrome(meta);
1568
+ const extras = useFieldExtras(meta);
1395
1569
  const fallback = meta.schema.default;
1396
1570
  return /* @__PURE__ */ jsx(Field, {
1397
1571
  label: meta.label,
1398
1572
  required: marked(meta),
1399
1573
  annotation: chrome.annotation,
1400
- description: chrome.description,
1401
- 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),
1402
1577
  children: /* @__PURE__ */ jsx(Input, {
1403
1578
  className: "font-mono",
1404
1579
  autoComplete: "off",
@@ -1442,6 +1617,7 @@ function EnumLeaf({ meta, options }) {
1442
1617
  const dense = use(DenseContext);
1443
1618
  const value = getAtPath(form.value, meta.path);
1444
1619
  const chrome = useChrome(meta);
1620
+ const extras = useFieldExtras(meta);
1445
1621
  const shown = options.map((option) => ({
1446
1622
  value: String(option.value),
1447
1623
  label: meta.ui.enumLabels?.[String(option.value)] ?? option.label
@@ -1458,8 +1634,9 @@ function EnumLeaf({ meta, options }) {
1458
1634
  label: meta.label,
1459
1635
  required: marked(meta),
1460
1636
  annotation: chrome.annotation,
1461
- description,
1462
- 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),
1463
1640
  children: /* @__PURE__ */ jsx(Chooser, {
1464
1641
  label: meta.label,
1465
1642
  options: shown,
@@ -1483,15 +1660,16 @@ function BooleanLeaf({ meta }) {
1483
1660
  const form = useForm();
1484
1661
  const value = getAtPath(form.value, meta.path);
1485
1662
  const chrome = useChrome(meta);
1663
+ const extras = useFieldExtras(meta);
1486
1664
  const props = {
1487
1665
  checked: value === true,
1488
1666
  onCheckedChange: (next) => form.setValue(meta.path, next),
1489
1667
  label: meta.label,
1490
- description: chrome.description,
1668
+ description: withNotes(withNotes(chrome.description, extras.error ? /* @__PURE__ */ jsx(ErrorText, { text: extras.error }) : void 0), extras.notes),
1491
1669
  disabled: form.disabled
1492
1670
  };
1493
1671
  return /* @__PURE__ */ jsxs("div", {
1494
- 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),
1495
1673
  children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...props }) : /* @__PURE__ */ jsx(Switch, { ...props }), chrome.hint !== null && /* @__PURE__ */ jsx("span", {
1496
1674
  className: "mt-0.5",
1497
1675
  children: chrome.hint
@@ -1512,6 +1690,7 @@ function JsonLeaf({ meta }) {
1512
1690
  const form = useForm();
1513
1691
  const value = getAtPath(form.value, meta.path);
1514
1692
  const chrome = useChrome(meta);
1693
+ const extras = useFieldExtras(meta);
1515
1694
  const canonical = JSON.stringify(value) ?? "";
1516
1695
  const [draft, setDraft] = useState(() => ({
1517
1696
  text: pretty(value),
@@ -1560,10 +1739,11 @@ function JsonLeaf({ meta }) {
1560
1739
  label: meta.label,
1561
1740
  required: marked(meta),
1562
1741
  annotation: chrome.annotation,
1563
- description: chrome.description,
1564
- error: draft.error,
1565
- 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),
1566
1745
  children: /* @__PURE__ */ jsx(Textarea, {
1746
+ mono: true,
1567
1747
  rows: Math.min(Math.max(draft.text.split("\n").length, 3), 12),
1568
1748
  spellCheck: false,
1569
1749
  "aria-label": meta.label,
@@ -1594,6 +1774,7 @@ function StringListLeaf({ meta, minItems, maxItems }) {
1594
1774
  const form = useForm();
1595
1775
  const value = getAtPath(form.value, meta.path);
1596
1776
  const chrome = useChrome(meta);
1777
+ const extras = useFieldExtras(meta);
1597
1778
  const raw = Array.isArray(value) ? value : [];
1598
1779
  const [rows, setRows] = useState(() => syncIds({
1599
1780
  ids: [],
@@ -1609,8 +1790,9 @@ function StringListLeaf({ meta, minItems, maxItems }) {
1609
1790
  label: meta.label,
1610
1791
  required: marked(meta),
1611
1792
  annotation: chrome.annotation,
1612
- description: chrome.description,
1613
- 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),
1614
1796
  children: /* @__PURE__ */ jsxs("div", {
1615
1797
  className: "flex flex-col gap-1.5",
1616
1798
  children: [raw.map((entry, index) => /* @__PURE__ */ jsxs("div", {
@@ -1805,12 +1987,14 @@ function Frame({ meta, bare, children }) {
1805
1987
  const dense = use(DenseContext);
1806
1988
  const density = useDensity();
1807
1989
  const chrome = useChrome(meta);
1990
+ const extras = useFieldExtras(meta);
1808
1991
  if (bare) return /* @__PURE__ */ jsx("div", {
1809
1992
  className: "flex min-w-0 flex-col gap-3",
1810
1993
  children
1811
1994
  });
1812
1995
  return /* @__PURE__ */ jsxs("fieldset", {
1813
- 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),
1814
1998
  children: [
1815
1999
  /* @__PURE__ */ jsx("legend", {
1816
2000
  className: "px-1",
@@ -1823,6 +2007,11 @@ function Frame({ meta, bare, children }) {
1823
2007
  className: "text-xs leading-snug text-fg-muted",
1824
2008
  children: chrome.description
1825
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
+ }),
1826
2015
  children
1827
2016
  ]
1828
2017
  });
@@ -1954,17 +2143,18 @@ function TupleNode({ meta, shape, bare }) {
1954
2143
  function NullableNode({ meta, shape }) {
1955
2144
  const form = useForm();
1956
2145
  const chrome = useChrome(meta);
2146
+ const extras = useFieldExtras(meta);
1957
2147
  const value = getAtPath(form.value, meta.path);
1958
2148
  const set = value !== null && value !== void 0;
1959
2149
  const props = {
1960
2150
  checked: set,
1961
2151
  label: meta.label,
1962
- description: chrome.description,
2152
+ description: withNotes(withNotes(chrome.description, extras.error ? /* @__PURE__ */ jsx(ErrorText, { text: extras.error }) : void 0), extras.notes),
1963
2153
  disabled: form.disabled,
1964
2154
  onCheckedChange: (next) => form.setValue(meta.path, next ? seedNotNull(meta.node, form.schema) : null)
1965
2155
  };
1966
2156
  return /* @__PURE__ */ jsxs("div", {
1967
- 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),
1968
2158
  children: [/* @__PURE__ */ jsxs("div", {
1969
2159
  className: "flex items-start gap-1.5",
1970
2160
  children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...props }) : /* @__PURE__ */ jsx(Switch, { ...props }), chrome.hint !== null && /* @__PURE__ */ jsx("span", {
@@ -2021,8 +2211,12 @@ function useValue(value, seed, onValueChange) {
2021
2211
  * The root is a `div` carrying `data-columns` and, while disabled, `data-disabled`. Wraps itself in a
2022
2212
  * `TooltipProvider`, so it needs none above it.
2023
2213
  */
2024
- 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 }) {
2025
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]);
2026
2220
  const state = {
2027
2221
  schema,
2028
2222
  value: current,
@@ -2034,9 +2228,16 @@ function SchemaValueForm({ schema, value, defaultValue, onValueChange, ui = NO_U
2034
2228
  disabled,
2035
2229
  columns,
2036
2230
  renderField,
2037
- 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
2038
2234
  };
2039
2235
  const root = shapeOf(schema, schema);
2236
+ const raw = messages?.[""];
2237
+ const formMessage = typeof raw === "string" ? {
2238
+ severity: "error",
2239
+ text: raw
2240
+ } : raw;
2040
2241
  return /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsx(FormContext, {
2041
2242
  value: state,
2042
2243
  children: /* @__PURE__ */ jsxs("div", {
@@ -2044,16 +2245,25 @@ function SchemaValueForm({ schema, value, defaultValue, onValueChange, ui = NO_U
2044
2245
  "data-columns": columns,
2045
2246
  "data-disabled": disabled ? "" : void 0,
2046
2247
  className: cn("flex min-w-0 flex-col gap-5", className),
2047
- children: [root.kind === "object" && (ui.groups ?? []).map((group) => /* @__PURE__ */ jsx(GroupBlock, { group }, group.id)), root.kind === "object" ? /* @__PURE__ */ jsx(ObjectBody, {
2048
- path: "",
2049
- fields: root.fields
2050
- }) : /* @__PURE__ */ jsx(FieldNode, {
2051
- path: "",
2052
- name: schema.title ?? "value",
2053
- node: schema,
2054
- required: true,
2055
- bare: true
2056
- })]
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
+ ]
2057
2267
  })
2058
2268
  }) });
2059
2269
  }
@@ -2091,6 +2301,6 @@ function GroupBlock({ group }) {
2091
2301
  });
2092
2302
  }
2093
2303
  //#endregion
2094
- 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 };
2095
2305
 
2096
2306
  //# sourceMappingURL=index.js.map