@vitavision/forms 0.8.3 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/index.d.ts +105 -2
- package/dist/index.js +365 -55
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/uiSchema.ts +62 -0
- package/src/api/valueDiff.ts +79 -0
- package/src/components/SchemaValueForm.tsx +71 -14
- package/src/components/valueForm/changes.tsx +137 -0
- package/src/components/valueForm/chrome.tsx +34 -5
- package/src/components/valueForm/context.ts +25 -1
- package/src/components/valueForm/leaves.tsx +70 -18
- package/src/components/valueForm/nodes.tsx +18 -11
- package/src/index.ts +3 -2
package/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$
|
|
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$
|
|
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$
|
|
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$
|
|
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
|
}
|
|
@@ -1147,6 +1147,91 @@ function fieldUiAt(ui, path) {
|
|
|
1147
1147
|
...exact
|
|
1148
1148
|
};
|
|
1149
1149
|
}
|
|
1150
|
+
/**
|
|
1151
|
+
* Put the keys an app listed first, in its order, and the rest after them in their own.
|
|
1152
|
+
*
|
|
1153
|
+
* @param fields - Items with a `key`, in schema order.
|
|
1154
|
+
* @param order - The keys to bring forward; unknown keys are ignored.
|
|
1155
|
+
* @returns A new array when `order` changes anything, otherwise `fields` itself.
|
|
1156
|
+
*/
|
|
1157
|
+
function orderFields(fields, order) {
|
|
1158
|
+
if (order === void 0 || order.length === 0) return fields;
|
|
1159
|
+
const rank = /* @__PURE__ */ new Map();
|
|
1160
|
+
for (const key of order) if (!rank.has(key)) rank.set(key, rank.size);
|
|
1161
|
+
const listed = fields.filter((field) => rank.has(field.key)).sort((a, b) => (rank.get(a.key) ?? 0) - (rank.get(b.key) ?? 0));
|
|
1162
|
+
const rest = fields.filter((field) => !rank.has(field.key));
|
|
1163
|
+
return [...listed, ...rest];
|
|
1164
|
+
}
|
|
1165
|
+
//#endregion
|
|
1166
|
+
//#region src/api/valueDiff.ts
|
|
1167
|
+
/**
|
|
1168
|
+
* Where one JSON value differs from another, by dot path: what a form marks as "changed from
|
|
1169
|
+
* the baseline".
|
|
1170
|
+
*
|
|
1171
|
+
* Paths are the ones `getAtPath` reads. A value and its baseline are compared structurally:
|
|
1172
|
+
* objects by key (a key missing on one side differs from a key present on the other, even
|
|
1173
|
+
* if the present value is `null`), arrays by index when they have the same length, and by
|
|
1174
|
+
* the array as a whole when they do not (its elements have no shared identity to match).
|
|
1175
|
+
*/
|
|
1176
|
+
function isRecord(value) {
|
|
1177
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1178
|
+
}
|
|
1179
|
+
/**
|
|
1180
|
+
* The paths at which `current` differs from `baseline`, deepest first-found, in key order.
|
|
1181
|
+
*
|
|
1182
|
+
* @param current - The value.
|
|
1183
|
+
* @param baseline - What it is compared with.
|
|
1184
|
+
* @param path - The path of both, for a recursive call; `""` is the root.
|
|
1185
|
+
* @returns Each differing path once; `[""]` when the roots are different kinds of value.
|
|
1186
|
+
* Empty when the values are equal.
|
|
1187
|
+
*/
|
|
1188
|
+
function diffPaths(current, baseline, path = "") {
|
|
1189
|
+
if (Object.is(current, baseline)) return [];
|
|
1190
|
+
if (isRecord(current) && isRecord(baseline)) return [.../* @__PURE__ */ new Set([...Object.keys(current), ...Object.keys(baseline)])].flatMap((key) => {
|
|
1191
|
+
const inCurrent = Object.hasOwn(current, key);
|
|
1192
|
+
const inBaseline = Object.hasOwn(baseline, key);
|
|
1193
|
+
const child = joinPath(path, key);
|
|
1194
|
+
return inCurrent && inBaseline ? diffPaths(current[key], baseline[key], child) : [child];
|
|
1195
|
+
});
|
|
1196
|
+
if (Array.isArray(current) && Array.isArray(baseline) && current.length === baseline.length) return current.flatMap((entry, index) => diffPaths(entry, baseline[index], joinPath(path, String(index))));
|
|
1197
|
+
return [path];
|
|
1198
|
+
}
|
|
1199
|
+
/**
|
|
1200
|
+
* How many differing leaves lie at or under each path.
|
|
1201
|
+
*
|
|
1202
|
+
* @param paths - From `diffPaths`.
|
|
1203
|
+
* @returns A map from a path, and each of its ancestors (the root as `""`), to the count of
|
|
1204
|
+
* differing paths at or beneath it.
|
|
1205
|
+
*/
|
|
1206
|
+
function changeCounts(paths) {
|
|
1207
|
+
const counts = /* @__PURE__ */ new Map();
|
|
1208
|
+
for (const path of paths) {
|
|
1209
|
+
let at = path;
|
|
1210
|
+
for (;;) {
|
|
1211
|
+
counts.set(at, (counts.get(at) ?? 0) + 1);
|
|
1212
|
+
if (at === "") break;
|
|
1213
|
+
const cut = at.lastIndexOf(".");
|
|
1214
|
+
at = cut < 0 ? "" : at.slice(0, cut);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
return counts;
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* A value as the "was ..." note shows it: short, one line.
|
|
1221
|
+
*
|
|
1222
|
+
* @param value - The baseline's value at a field.
|
|
1223
|
+
* @param limit - The longest text, in characters; a longer one is cut with an ellipsis. Defaults to 40.
|
|
1224
|
+
* @returns `unset` for `undefined`, `none` for `null`, `""` quoted, else the value or its JSON.
|
|
1225
|
+
*/
|
|
1226
|
+
function describeValue(value, limit = 40) {
|
|
1227
|
+
let text;
|
|
1228
|
+
if (value === void 0) text = "unset";
|
|
1229
|
+
else if (value === null) text = "none";
|
|
1230
|
+
else if (typeof value === "string") text = value === "" ? "\"\"" : value;
|
|
1231
|
+
else if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") text = value.toString();
|
|
1232
|
+
else text = JSON.stringify(value) ?? "";
|
|
1233
|
+
return text.length > limit ? `${text.slice(0, limit - 1)}…` : text;
|
|
1234
|
+
}
|
|
1150
1235
|
//#endregion
|
|
1151
1236
|
//#region src/api/firstParagraph.ts
|
|
1152
1237
|
/**
|
|
@@ -1222,15 +1307,33 @@ function spanClass(columns, ui, fallback) {
|
|
|
1222
1307
|
}
|
|
1223
1308
|
/** The field grid: two columns on `sm:` (or one), or an auto-fit row inside a tuple. */
|
|
1224
1309
|
function Grid({ children }) {
|
|
1225
|
-
const { columns } = useForm();
|
|
1310
|
+
const { columns, labelPlacement } = useForm();
|
|
1226
1311
|
const dense = use(DenseContext);
|
|
1227
1312
|
const density = useDensity();
|
|
1228
1313
|
return /* @__PURE__ */ jsx("div", {
|
|
1229
|
-
className: cn("grid min-w-0", dense ? "grid-cols-[repeat(auto-fit,minmax(5.5rem,1fr))] gap-2" : cn(byDensity(density, "gap-x-5 gap-y-4", "gap-x-4 gap-y-2.5"), columns === 2 ? "grid-cols-1 sm:grid-cols-2" : "grid-cols-1")),
|
|
1314
|
+
className: cn("grid min-w-0", dense ? "grid-cols-[repeat(auto-fit,minmax(5.5rem,1fr))] gap-2" : cn(labelPlacement === "side" ? byDensity(density, "gap-x-6 gap-y-2", "gap-x-5 gap-y-1.5") : byDensity(density, "gap-x-5 gap-y-4", "gap-x-4 gap-y-2.5"), columns === 2 ? "grid-cols-1 sm:grid-cols-2" : "grid-cols-1")),
|
|
1230
1315
|
children
|
|
1231
1316
|
});
|
|
1232
1317
|
}
|
|
1233
|
-
|
|
1318
|
+
/**
|
|
1319
|
+
* Fields on one row, each labelled from above: the layout for a few small numbers that read
|
|
1320
|
+
* as one setting. Cells drop descriptions and range notes (see `DenseContext`).
|
|
1321
|
+
*/
|
|
1322
|
+
function InlineRow({ children }) {
|
|
1323
|
+
return /* @__PURE__ */ jsx(DenseContext, {
|
|
1324
|
+
value: true,
|
|
1325
|
+
children: /* @__PURE__ */ jsx(Grid, { children })
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
* Where this field's label goes: the form's (or its group's) placement, except inside a row
|
|
1330
|
+
* of cells, which always label from above because a cell has no room for a label column.
|
|
1331
|
+
*/
|
|
1332
|
+
function usePlacement() {
|
|
1333
|
+
const { labelPlacement } = useForm();
|
|
1334
|
+
return use(DenseContext) ? "top" : labelPlacement;
|
|
1335
|
+
}
|
|
1336
|
+
function present$1(node) {
|
|
1234
1337
|
return node !== void 0 && node !== null && node !== false && node !== "";
|
|
1235
1338
|
}
|
|
1236
1339
|
/**
|
|
@@ -1244,15 +1347,17 @@ function present(node) {
|
|
|
1244
1347
|
*/
|
|
1245
1348
|
function useChrome(meta, range = null) {
|
|
1246
1349
|
const dense = use(DenseContext);
|
|
1247
|
-
const
|
|
1350
|
+
const side = usePlacement() === "side";
|
|
1351
|
+
const { descriptionAs } = useForm();
|
|
1352
|
+
const mode = meta.ui.descriptionAs ?? descriptionAs;
|
|
1248
1353
|
const text = meta.ui.hint ?? firstParagraph(meta.schema.description);
|
|
1249
|
-
const shown = !dense && present(text);
|
|
1354
|
+
const shown = !dense && present$1(text);
|
|
1250
1355
|
const hint = shown && mode === "hint" ? /* @__PURE__ */ jsx(InfoHint, {
|
|
1251
1356
|
label: `About ${meta.label}`,
|
|
1252
1357
|
children: text
|
|
1253
1358
|
}) : null;
|
|
1254
1359
|
const description = shown && mode === "inline" ? text : void 0;
|
|
1255
|
-
const note = dense ? null : range;
|
|
1360
|
+
const note = dense || side ? null : range;
|
|
1256
1361
|
return {
|
|
1257
1362
|
hint,
|
|
1258
1363
|
description,
|
|
@@ -1263,6 +1368,107 @@ function useChrome(meta, range = null) {
|
|
|
1263
1368
|
};
|
|
1264
1369
|
}
|
|
1265
1370
|
//#endregion
|
|
1371
|
+
//#region src/components/valueForm/changes.tsx
|
|
1372
|
+
/**
|
|
1373
|
+
* What a field shows beyond its control: whether it differs from the form's baseline (a rule
|
|
1374
|
+
* at its edge, a "changed" note that says what it was, and a reset), and the app's message
|
|
1375
|
+
* for it.
|
|
1376
|
+
*
|
|
1377
|
+
* The note sits under the control, outside the field's `<label>`: a button inside a label
|
|
1378
|
+
* would be part of the control's name. It is the field's description, so a reader hears it
|
|
1379
|
+
* with the control.
|
|
1380
|
+
*/
|
|
1381
|
+
/** 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. */
|
|
1382
|
+
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";
|
|
1383
|
+
/** A message as `{ severity, text }`: a bare string is an error. */
|
|
1384
|
+
function normalise(message) {
|
|
1385
|
+
if (message === void 0) return void 0;
|
|
1386
|
+
return typeof message === "string" ? {
|
|
1387
|
+
severity: "error",
|
|
1388
|
+
text: message
|
|
1389
|
+
} : message;
|
|
1390
|
+
}
|
|
1391
|
+
/**
|
|
1392
|
+
* The baseline comparison and the app's message for one field.
|
|
1393
|
+
*
|
|
1394
|
+
* @param meta - The field.
|
|
1395
|
+
* @returns Its extras; all empty for a form with no baseline and no message.
|
|
1396
|
+
*/
|
|
1397
|
+
function useFieldExtras(meta) {
|
|
1398
|
+
const form = useForm();
|
|
1399
|
+
const dense = use(DenseContext);
|
|
1400
|
+
const changes = form.baseline?.counts.get(meta.path) ?? 0;
|
|
1401
|
+
const message = normalise(form.messages?.[meta.path]);
|
|
1402
|
+
const warning = message?.severity === "warning" ? message.text : void 0;
|
|
1403
|
+
const notes = changes > 0 && !dense || warning !== void 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [warning !== void 0 && /* @__PURE__ */ jsx("span", {
|
|
1404
|
+
"data-message": "warning",
|
|
1405
|
+
className: "mt-0.5 block text-warn",
|
|
1406
|
+
children: warning
|
|
1407
|
+
}), changes > 0 && !dense && /* @__PURE__ */ jsx(ChangeNote, {
|
|
1408
|
+
meta,
|
|
1409
|
+
count: changes
|
|
1410
|
+
})] }) : void 0;
|
|
1411
|
+
return {
|
|
1412
|
+
className: changes > 0 ? CHANGED_RULE : void 0,
|
|
1413
|
+
changed: changes > 0,
|
|
1414
|
+
error: message?.severity === "error" ? message.text : void 0,
|
|
1415
|
+
notes
|
|
1416
|
+
};
|
|
1417
|
+
}
|
|
1418
|
+
/** An error message where the control has no error slot of its own (a switch). */
|
|
1419
|
+
function ErrorText({ text }) {
|
|
1420
|
+
return /* @__PURE__ */ jsx("span", {
|
|
1421
|
+
role: "alert",
|
|
1422
|
+
"data-message": "error",
|
|
1423
|
+
className: "block text-defect",
|
|
1424
|
+
children: text
|
|
1425
|
+
});
|
|
1426
|
+
}
|
|
1427
|
+
/** A description and the extras' notes, as one `Field` description (`undefined` when both are empty). */
|
|
1428
|
+
function withNotes(description, notes) {
|
|
1429
|
+
if (!(description !== void 0 && description !== null && description !== false && description !== "")) return notes;
|
|
1430
|
+
return notes === void 0 ? description : /* @__PURE__ */ jsxs(Fragment, { children: [description, notes] });
|
|
1431
|
+
}
|
|
1432
|
+
/** The "changed" marker with what the baseline had, and the button that puts it back. */
|
|
1433
|
+
function ChangeNote({ meta, count }) {
|
|
1434
|
+
const was = getAtPath(useForm().baseline?.value, meta.path);
|
|
1435
|
+
const composite = typeof was === "object" && was !== null;
|
|
1436
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
1437
|
+
"data-change-note": "",
|
|
1438
|
+
className: "mt-0.5 flex min-w-0 items-center gap-2 text-[11px] leading-snug",
|
|
1439
|
+
children: [
|
|
1440
|
+
/* @__PURE__ */ jsxs("span", {
|
|
1441
|
+
className: "inline-flex shrink-0 items-center gap-1 font-medium text-signal",
|
|
1442
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
1443
|
+
"aria-hidden": true,
|
|
1444
|
+
className: "size-1.5 rounded-full bg-signal"
|
|
1445
|
+
}), composite && count > 1 ? `${count} changed` : "changed"]
|
|
1446
|
+
}),
|
|
1447
|
+
!composite && /* @__PURE__ */ jsxs("span", {
|
|
1448
|
+
className: "min-w-0 truncate text-fg-muted",
|
|
1449
|
+
children: ["was ", /* @__PURE__ */ jsx("span", {
|
|
1450
|
+
className: "font-mono",
|
|
1451
|
+
children: describeValue(was)
|
|
1452
|
+
})]
|
|
1453
|
+
}),
|
|
1454
|
+
/* @__PURE__ */ jsx(ResetButton, { meta })
|
|
1455
|
+
]
|
|
1456
|
+
});
|
|
1457
|
+
}
|
|
1458
|
+
/** Puts a field (or a whole block) back to the baseline's value. */
|
|
1459
|
+
function ResetButton({ meta, className }) {
|
|
1460
|
+
const form = useForm();
|
|
1461
|
+
return /* @__PURE__ */ jsx("button", {
|
|
1462
|
+
type: "button",
|
|
1463
|
+
"data-reset": "",
|
|
1464
|
+
"aria-label": `Reset ${meta.label} to baseline`,
|
|
1465
|
+
disabled: form.disabled,
|
|
1466
|
+
onClick: () => form.setValue(meta.path, getAtPath(form.baseline?.value, meta.path)),
|
|
1467
|
+
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),
|
|
1468
|
+
children: "Reset"
|
|
1469
|
+
});
|
|
1470
|
+
}
|
|
1471
|
+
//#endregion
|
|
1266
1472
|
//#region src/api/numberSpec.ts
|
|
1267
1473
|
/**
|
|
1268
1474
|
* Collect a number field's rules.
|
|
@@ -1344,6 +1550,8 @@ function NumberLeaf({ meta }) {
|
|
|
1344
1550
|
const value = getAtPath(form.value, meta.path);
|
|
1345
1551
|
const spec = numberSpec(meta.schema, meta.ui);
|
|
1346
1552
|
const chrome = useChrome(meta, rangeText(spec));
|
|
1553
|
+
const placement = usePlacement();
|
|
1554
|
+
const extras = useFieldExtras(meta);
|
|
1347
1555
|
const [error, setError] = useState();
|
|
1348
1556
|
const fallback = meta.schema.default;
|
|
1349
1557
|
const restore = meta.ui.restoreDefaultOnClear ?? true;
|
|
@@ -1360,12 +1568,13 @@ function NumberLeaf({ meta }) {
|
|
|
1360
1568
|
else setError(parsed === null ? "Enter a number" : checkNumber(spec, parsed));
|
|
1361
1569
|
};
|
|
1362
1570
|
return /* @__PURE__ */ jsx(Field, {
|
|
1571
|
+
layout: placement,
|
|
1363
1572
|
label: meta.label,
|
|
1364
1573
|
required: marked(meta),
|
|
1365
1574
|
annotation: chrome.annotation,
|
|
1366
|
-
description: chrome.description,
|
|
1367
|
-
error,
|
|
1368
|
-
className: spanClass(form.columns, meta.ui, 1),
|
|
1575
|
+
description: withNotes(chrome.description, extras.notes),
|
|
1576
|
+
error: error ?? extras.error,
|
|
1577
|
+
className: cn(spanClass(form.columns, meta.ui, 1), extras.className),
|
|
1369
1578
|
children: /* @__PURE__ */ jsx(NumberInput, {
|
|
1370
1579
|
"aria-label": meta.label,
|
|
1371
1580
|
disabled: form.disabled,
|
|
@@ -1393,13 +1602,17 @@ function TextLeaf({ meta }) {
|
|
|
1393
1602
|
const form = useForm();
|
|
1394
1603
|
const value = getAtPath(form.value, meta.path);
|
|
1395
1604
|
const chrome = useChrome(meta);
|
|
1605
|
+
const placement = usePlacement();
|
|
1606
|
+
const extras = useFieldExtras(meta);
|
|
1396
1607
|
const fallback = meta.schema.default;
|
|
1397
1608
|
return /* @__PURE__ */ jsx(Field, {
|
|
1609
|
+
layout: placement,
|
|
1398
1610
|
label: meta.label,
|
|
1399
1611
|
required: marked(meta),
|
|
1400
1612
|
annotation: chrome.annotation,
|
|
1401
|
-
description: chrome.description,
|
|
1402
|
-
|
|
1613
|
+
description: withNotes(chrome.description, extras.notes),
|
|
1614
|
+
error: extras.error,
|
|
1615
|
+
className: cn(spanClass(form.columns, meta.ui, 1), extras.className),
|
|
1403
1616
|
children: /* @__PURE__ */ jsx(Input, {
|
|
1404
1617
|
className: "font-mono",
|
|
1405
1618
|
autoComplete: "off",
|
|
@@ -1443,6 +1656,8 @@ function EnumLeaf({ meta, options }) {
|
|
|
1443
1656
|
const dense = use(DenseContext);
|
|
1444
1657
|
const value = getAtPath(form.value, meta.path);
|
|
1445
1658
|
const chrome = useChrome(meta);
|
|
1659
|
+
const placement = usePlacement();
|
|
1660
|
+
const extras = useFieldExtras(meta);
|
|
1446
1661
|
const shown = options.map((option) => ({
|
|
1447
1662
|
value: String(option.value),
|
|
1448
1663
|
label: meta.ui.enumLabels?.[String(option.value)] ?? option.label
|
|
@@ -1455,12 +1670,14 @@ function EnumLeaf({ meta, options }) {
|
|
|
1455
1670
|
explained
|
|
1456
1671
|
] }) : explained !== "" ? explained : chrome.description;
|
|
1457
1672
|
return /* @__PURE__ */ jsx(Field, {
|
|
1673
|
+
layout: placement,
|
|
1458
1674
|
as: "group",
|
|
1459
1675
|
label: meta.label,
|
|
1460
1676
|
required: marked(meta),
|
|
1461
1677
|
annotation: chrome.annotation,
|
|
1462
|
-
description,
|
|
1463
|
-
|
|
1678
|
+
description: withNotes(description, extras.notes),
|
|
1679
|
+
error: extras.error,
|
|
1680
|
+
className: cn(spanClass(form.columns, meta.ui, 1), extras.className),
|
|
1464
1681
|
children: /* @__PURE__ */ jsx(Chooser, {
|
|
1465
1682
|
label: meta.label,
|
|
1466
1683
|
options: shown,
|
|
@@ -1479,26 +1696,71 @@ function EnumLeaf({ meta, options }) {
|
|
|
1479
1696
|
})
|
|
1480
1697
|
});
|
|
1481
1698
|
}
|
|
1482
|
-
/**
|
|
1699
|
+
/**
|
|
1700
|
+
* A boolean: a switch (the default) or a checkbox. In a side placement it is a property
|
|
1701
|
+
* row: the label in the label column, the control right of it, the notes under the control.
|
|
1702
|
+
*/
|
|
1483
1703
|
function BooleanLeaf({ meta }) {
|
|
1484
1704
|
const form = useForm();
|
|
1485
1705
|
const value = getAtPath(form.value, meta.path);
|
|
1486
1706
|
const chrome = useChrome(meta);
|
|
1707
|
+
const extras = useFieldExtras(meta);
|
|
1708
|
+
const side = usePlacement() === "side";
|
|
1709
|
+
const notes = withNotes(withNotes(chrome.description, extras.error ? /* @__PURE__ */ jsx(ErrorText, { text: extras.error }) : void 0), extras.notes);
|
|
1487
1710
|
const props = {
|
|
1488
1711
|
checked: value === true,
|
|
1489
1712
|
onCheckedChange: (next) => form.setValue(meta.path, next),
|
|
1490
|
-
label: meta.label,
|
|
1491
|
-
description: chrome.description,
|
|
1492
1713
|
disabled: form.disabled
|
|
1493
1714
|
};
|
|
1715
|
+
if (side) {
|
|
1716
|
+
const named = /* @__PURE__ */ jsx("span", {
|
|
1717
|
+
className: "sr-only",
|
|
1718
|
+
children: meta.label
|
|
1719
|
+
});
|
|
1720
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1721
|
+
"data-layout": "side",
|
|
1722
|
+
className: cn("grid min-w-0 grid-cols-[var(--field-label-w,8.5rem)_minmax(0,1fr)] items-center gap-x-3 gap-y-0.5", spanClass(form.columns, meta.ui, 1), extras.className),
|
|
1723
|
+
children: [
|
|
1724
|
+
/* @__PURE__ */ jsxs("span", {
|
|
1725
|
+
className: "flex min-w-0 items-center gap-1.5",
|
|
1726
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
1727
|
+
className: "min-w-0 truncate text-xs font-medium text-fg",
|
|
1728
|
+
title: meta.label,
|
|
1729
|
+
children: meta.label
|
|
1730
|
+
}), chrome.hint]
|
|
1731
|
+
}),
|
|
1732
|
+
meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, {
|
|
1733
|
+
...props,
|
|
1734
|
+
"aria-label": meta.label,
|
|
1735
|
+
className: "justify-self-start"
|
|
1736
|
+
}) : /* @__PURE__ */ jsx(Switch, {
|
|
1737
|
+
...props,
|
|
1738
|
+
label: named,
|
|
1739
|
+
className: "justify-self-start"
|
|
1740
|
+
}),
|
|
1741
|
+
present(notes) && /* @__PURE__ */ jsx("div", {
|
|
1742
|
+
className: "col-start-2 text-xs leading-snug text-fg-muted",
|
|
1743
|
+
children: notes
|
|
1744
|
+
})
|
|
1745
|
+
]
|
|
1746
|
+
});
|
|
1747
|
+
}
|
|
1748
|
+
const rich = {
|
|
1749
|
+
...props,
|
|
1750
|
+
label: meta.label,
|
|
1751
|
+
description: notes
|
|
1752
|
+
};
|
|
1494
1753
|
return /* @__PURE__ */ jsxs("div", {
|
|
1495
|
-
className: cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1)),
|
|
1496
|
-
children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...
|
|
1754
|
+
className: cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1), extras.className),
|
|
1755
|
+
children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...rich }) : /* @__PURE__ */ jsx(Switch, { ...rich }), chrome.hint !== null && /* @__PURE__ */ jsx("span", {
|
|
1497
1756
|
className: "mt-0.5",
|
|
1498
1757
|
children: chrome.hint
|
|
1499
1758
|
})]
|
|
1500
1759
|
});
|
|
1501
1760
|
}
|
|
1761
|
+
function present(node) {
|
|
1762
|
+
return node !== void 0 && node !== null && node !== false && node !== "";
|
|
1763
|
+
}
|
|
1502
1764
|
function pretty(value) {
|
|
1503
1765
|
return value === void 0 ? "" : JSON.stringify(value, null, 2);
|
|
1504
1766
|
}
|
|
@@ -1513,6 +1775,7 @@ function JsonLeaf({ meta }) {
|
|
|
1513
1775
|
const form = useForm();
|
|
1514
1776
|
const value = getAtPath(form.value, meta.path);
|
|
1515
1777
|
const chrome = useChrome(meta);
|
|
1778
|
+
const extras = useFieldExtras(meta);
|
|
1516
1779
|
const canonical = JSON.stringify(value) ?? "";
|
|
1517
1780
|
const [draft, setDraft] = useState(() => ({
|
|
1518
1781
|
text: pretty(value),
|
|
@@ -1561,9 +1824,9 @@ function JsonLeaf({ meta }) {
|
|
|
1561
1824
|
label: meta.label,
|
|
1562
1825
|
required: marked(meta),
|
|
1563
1826
|
annotation: chrome.annotation,
|
|
1564
|
-
description: chrome.description,
|
|
1565
|
-
error: draft.error,
|
|
1566
|
-
className: spanClass(form.columns, meta.ui, 2),
|
|
1827
|
+
description: withNotes(chrome.description, extras.notes),
|
|
1828
|
+
error: draft.error ?? extras.error,
|
|
1829
|
+
className: cn(spanClass(form.columns, meta.ui, 2), extras.className),
|
|
1567
1830
|
children: /* @__PURE__ */ jsx(Textarea, {
|
|
1568
1831
|
mono: true,
|
|
1569
1832
|
rows: Math.min(Math.max(draft.text.split("\n").length, 3), 12),
|
|
@@ -1596,6 +1859,7 @@ function StringListLeaf({ meta, minItems, maxItems }) {
|
|
|
1596
1859
|
const form = useForm();
|
|
1597
1860
|
const value = getAtPath(form.value, meta.path);
|
|
1598
1861
|
const chrome = useChrome(meta);
|
|
1862
|
+
const extras = useFieldExtras(meta);
|
|
1599
1863
|
const raw = Array.isArray(value) ? value : [];
|
|
1600
1864
|
const [rows, setRows] = useState(() => syncIds({
|
|
1601
1865
|
ids: [],
|
|
@@ -1611,8 +1875,9 @@ function StringListLeaf({ meta, minItems, maxItems }) {
|
|
|
1611
1875
|
label: meta.label,
|
|
1612
1876
|
required: marked(meta),
|
|
1613
1877
|
annotation: chrome.annotation,
|
|
1614
|
-
description: chrome.description,
|
|
1615
|
-
|
|
1878
|
+
description: withNotes(chrome.description, extras.notes),
|
|
1879
|
+
error: extras.error,
|
|
1880
|
+
className: cn(spanClass(form.columns, meta.ui, 2), extras.className),
|
|
1616
1881
|
children: /* @__PURE__ */ jsxs("div", {
|
|
1617
1882
|
className: "flex flex-col gap-1.5",
|
|
1618
1883
|
children: [raw.map((entry, index) => /* @__PURE__ */ jsxs("div", {
|
|
@@ -1793,13 +2058,16 @@ function useVisible(path, fields) {
|
|
|
1793
2058
|
*/
|
|
1794
2059
|
function ObjectBody({ path, fields }) {
|
|
1795
2060
|
const visible = useVisible(path, fields);
|
|
2061
|
+
const form = useForm();
|
|
1796
2062
|
if (visible.length === 0) return null;
|
|
1797
|
-
|
|
2063
|
+
const own = path === "" ? void 0 : fieldUiAt(form.ui, path);
|
|
2064
|
+
const cells = orderFields(visible, path === "" ? form.ui.order : own?.order).map((field) => /* @__PURE__ */ jsx(FieldNode, {
|
|
1798
2065
|
path: joinPath(path, field.key),
|
|
1799
2066
|
name: field.key,
|
|
1800
2067
|
node: field.schema,
|
|
1801
2068
|
required: field.required
|
|
1802
|
-
}, field.key))
|
|
2069
|
+
}, field.key));
|
|
2070
|
+
return own?.inline === true ? /* @__PURE__ */ jsx(InlineRow, { children: cells }) : /* @__PURE__ */ jsx(Grid, { children: cells });
|
|
1803
2071
|
}
|
|
1804
2072
|
/** The frame of a composite field: its label and hint over its content. `bare` drops the frame. */
|
|
1805
2073
|
function Frame({ meta, bare, children }) {
|
|
@@ -1807,12 +2075,14 @@ function Frame({ meta, bare, children }) {
|
|
|
1807
2075
|
const dense = use(DenseContext);
|
|
1808
2076
|
const density = useDensity();
|
|
1809
2077
|
const chrome = useChrome(meta);
|
|
2078
|
+
const extras = useFieldExtras(meta);
|
|
1810
2079
|
if (bare) return /* @__PURE__ */ jsx("div", {
|
|
1811
2080
|
className: "flex min-w-0 flex-col gap-3",
|
|
1812
2081
|
children
|
|
1813
2082
|
});
|
|
1814
2083
|
return /* @__PURE__ */ jsxs("fieldset", {
|
|
1815
|
-
|
|
2084
|
+
"data-changed": extras.changed ? "" : void 0,
|
|
2085
|
+
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
2086
|
children: [
|
|
1817
2087
|
/* @__PURE__ */ jsx("legend", {
|
|
1818
2088
|
className: "px-1",
|
|
@@ -1825,6 +2095,11 @@ function Frame({ meta, bare, children }) {
|
|
|
1825
2095
|
className: "text-xs leading-snug text-fg-muted",
|
|
1826
2096
|
children: chrome.description
|
|
1827
2097
|
}),
|
|
2098
|
+
extras.error !== void 0 && /* @__PURE__ */ jsx(ErrorText, { text: extras.error }),
|
|
2099
|
+
extras.notes !== void 0 && /* @__PURE__ */ jsx("div", {
|
|
2100
|
+
className: "text-xs",
|
|
2101
|
+
children: extras.notes
|
|
2102
|
+
}),
|
|
1828
2103
|
children
|
|
1829
2104
|
]
|
|
1830
2105
|
});
|
|
@@ -1956,17 +2231,18 @@ function TupleNode({ meta, shape, bare }) {
|
|
|
1956
2231
|
function NullableNode({ meta, shape }) {
|
|
1957
2232
|
const form = useForm();
|
|
1958
2233
|
const chrome = useChrome(meta);
|
|
2234
|
+
const extras = useFieldExtras(meta);
|
|
1959
2235
|
const value = getAtPath(form.value, meta.path);
|
|
1960
2236
|
const set = value !== null && value !== void 0;
|
|
1961
2237
|
const props = {
|
|
1962
2238
|
checked: set,
|
|
1963
2239
|
label: meta.label,
|
|
1964
|
-
description: chrome.description,
|
|
2240
|
+
description: withNotes(withNotes(chrome.description, extras.error ? /* @__PURE__ */ jsx(ErrorText, { text: extras.error }) : void 0), extras.notes),
|
|
1965
2241
|
disabled: form.disabled,
|
|
1966
2242
|
onCheckedChange: (next) => form.setValue(meta.path, next ? seedNotNull(meta.node, form.schema) : null)
|
|
1967
2243
|
};
|
|
1968
2244
|
return /* @__PURE__ */ jsxs("div", {
|
|
1969
|
-
className: cn("flex min-w-0 flex-col gap-3", spanClass(form.columns, meta.ui, 2)),
|
|
2245
|
+
className: cn("flex min-w-0 flex-col gap-3", spanClass(form.columns, meta.ui, 2), extras.className),
|
|
1970
2246
|
children: [/* @__PURE__ */ jsxs("div", {
|
|
1971
2247
|
className: "flex items-start gap-1.5",
|
|
1972
2248
|
children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...props }) : /* @__PURE__ */ jsx(Switch, { ...props }), chrome.hint !== null && /* @__PURE__ */ jsx("span", {
|
|
@@ -2023,8 +2299,13 @@ function useValue(value, seed, onValueChange) {
|
|
|
2023
2299
|
* The root is a `div` carrying `data-columns` and, while disabled, `data-disabled`. Wraps itself in a
|
|
2024
2300
|
* `TooltipProvider`, so it needs none above it.
|
|
2025
2301
|
*/
|
|
2026
|
-
function SchemaValueForm({ schema, value, defaultValue, onValueChange, ui = NO_UI, columns = 2, disabled = false, renderField, className, ref }) {
|
|
2302
|
+
function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline, messages, ui = NO_UI, columns = 2, labelPlacement, disabled = false, renderField, className, ref }) {
|
|
2027
2303
|
const [current, commit] = useValue(value, () => defaultValue !== void 0 ? defaultValue : defaultValueForSchema(schema), onValueChange);
|
|
2304
|
+
const compared = useMemo(() => baseline === void 0 ? null : {
|
|
2305
|
+
value: baseline,
|
|
2306
|
+
counts: changeCounts(diffPaths(current, baseline))
|
|
2307
|
+
}, [current, baseline]);
|
|
2308
|
+
const claimed = useMemo(() => new Set((ui.groups ?? []).flatMap((group) => group.fields)), [ui.groups]);
|
|
2028
2309
|
const state = {
|
|
2029
2310
|
schema,
|
|
2030
2311
|
value: current,
|
|
@@ -2035,33 +2316,54 @@ function SchemaValueForm({ schema, value, defaultValue, onValueChange, ui = NO_U
|
|
|
2035
2316
|
ui,
|
|
2036
2317
|
disabled,
|
|
2037
2318
|
columns,
|
|
2319
|
+
labelPlacement: labelPlacement ?? ui.labelPlacement ?? "top",
|
|
2320
|
+
descriptionAs: ui.descriptionAs ?? "hint",
|
|
2038
2321
|
renderField,
|
|
2039
|
-
claimed
|
|
2322
|
+
claimed,
|
|
2323
|
+
baseline: compared,
|
|
2324
|
+
messages
|
|
2040
2325
|
};
|
|
2041
2326
|
const root = shapeOf(schema, schema);
|
|
2327
|
+
const raw = messages?.[""];
|
|
2328
|
+
const formMessage = typeof raw === "string" ? {
|
|
2329
|
+
severity: "error",
|
|
2330
|
+
text: raw
|
|
2331
|
+
} : raw;
|
|
2042
2332
|
return /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsx(FormContext, {
|
|
2043
2333
|
value: state,
|
|
2044
2334
|
children: /* @__PURE__ */ jsxs("div", {
|
|
2045
2335
|
ref,
|
|
2046
2336
|
"data-columns": columns,
|
|
2337
|
+
"data-label-placement": state.labelPlacement,
|
|
2047
2338
|
"data-disabled": disabled ? "" : void 0,
|
|
2339
|
+
style: { "--field-label-w": columns === 2 ? "7.5rem" : "9rem" },
|
|
2048
2340
|
className: cn("flex min-w-0 flex-col gap-5", className),
|
|
2049
|
-
children: [
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2341
|
+
children: [
|
|
2342
|
+
formMessage !== void 0 && /* @__PURE__ */ jsx("p", {
|
|
2343
|
+
role: formMessage.severity === "error" ? "alert" : void 0,
|
|
2344
|
+
"data-message": formMessage.severity,
|
|
2345
|
+
className: cn("text-xs leading-snug", formMessage.severity === "error" ? "text-defect" : "text-warn"),
|
|
2346
|
+
children: formMessage.text
|
|
2347
|
+
}),
|
|
2348
|
+
root.kind === "object" && (ui.groups ?? []).map((group) => /* @__PURE__ */ jsx(GroupBlock, { group }, group.id)),
|
|
2349
|
+
root.kind === "object" ? /* @__PURE__ */ jsx(ObjectBody, {
|
|
2350
|
+
path: "",
|
|
2351
|
+
fields: root.fields
|
|
2352
|
+
}) : /* @__PURE__ */ jsx(FieldNode, {
|
|
2353
|
+
path: "",
|
|
2354
|
+
name: schema.title ?? "value",
|
|
2355
|
+
node: schema,
|
|
2356
|
+
required: true,
|
|
2357
|
+
bare: true
|
|
2358
|
+
})
|
|
2359
|
+
]
|
|
2059
2360
|
})
|
|
2060
2361
|
}) });
|
|
2061
2362
|
}
|
|
2062
2363
|
/** A `UiGroup`: its fields under a heading, folded away if the app said so. */
|
|
2063
2364
|
function GroupBlock({ group }) {
|
|
2064
|
-
const
|
|
2365
|
+
const form = useForm();
|
|
2366
|
+
const { schema, value, ui } = form;
|
|
2065
2367
|
const fields = group.fields.flatMap((path) => {
|
|
2066
2368
|
const info = fieldAt(schema, path, schema, value);
|
|
2067
2369
|
if (info === void 0 || fieldUiAt(ui, path).hidden === true) return [];
|
|
@@ -2071,12 +2373,20 @@ function GroupBlock({ group }) {
|
|
|
2071
2373
|
}];
|
|
2072
2374
|
});
|
|
2073
2375
|
if (fields.length === 0) return null;
|
|
2074
|
-
const
|
|
2376
|
+
const cells = fields.map(({ path, info }) => /* @__PURE__ */ jsx(FieldNode, {
|
|
2075
2377
|
path,
|
|
2076
2378
|
name: path.split(".").at(-1) ?? path,
|
|
2077
2379
|
node: info.schema,
|
|
2078
2380
|
required: info.required
|
|
2079
|
-
}, path))
|
|
2381
|
+
}, path));
|
|
2382
|
+
const body = group.inline === true ? /* @__PURE__ */ jsx(InlineRow, { children: cells }) : /* @__PURE__ */ jsx(Grid, { children: cells });
|
|
2383
|
+
const scoped = group.labelPlacement === void 0 ? body : /* @__PURE__ */ jsx(FormContext, {
|
|
2384
|
+
value: {
|
|
2385
|
+
...form,
|
|
2386
|
+
labelPlacement: group.labelPlacement
|
|
2387
|
+
},
|
|
2388
|
+
children: body
|
|
2389
|
+
});
|
|
2080
2390
|
if (group.collapsible === true) return /* @__PURE__ */ jsxs(Disclosure, {
|
|
2081
2391
|
summary: group.title,
|
|
2082
2392
|
count: fields.length,
|
|
@@ -2084,15 +2394,15 @@ function GroupBlock({ group }) {
|
|
|
2084
2394
|
children: [group.hint !== void 0 && /* @__PURE__ */ jsx("p", {
|
|
2085
2395
|
className: "mb-3 text-xs text-fg-muted",
|
|
2086
2396
|
children: group.hint
|
|
2087
|
-
}),
|
|
2397
|
+
}), scoped]
|
|
2088
2398
|
});
|
|
2089
2399
|
return /* @__PURE__ */ jsx(Section, {
|
|
2090
2400
|
title: group.title,
|
|
2091
2401
|
hint: group.hint,
|
|
2092
|
-
children:
|
|
2402
|
+
children: scoped
|
|
2093
2403
|
});
|
|
2094
2404
|
}
|
|
2095
2405
|
//#endregion
|
|
2096
|
-
export { SchemaForm, SchemaValueForm, defaultValueForSchema, describeFields, fieldAt, fieldsAt, firstParagraph, getAtPath, initialValues, jsonErrors, missingRequired, outOfRange, overrideCount, resolveRef, resolveSchema, setAtPath, shapeOf, toOptions };
|
|
2406
|
+
export { SchemaForm, SchemaValueForm, defaultValueForSchema, describeFields, diffPaths, fieldAt, fieldsAt, firstParagraph, getAtPath, initialValues, jsonErrors, missingRequired, outOfRange, overrideCount, resolveRef, resolveSchema, setAtPath, shapeOf, toOptions };
|
|
2097
2407
|
|
|
2098
2408
|
//# sourceMappingURL=index.js.map
|