@vitavision/forms 0.8.4 → 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/dist/index.d.ts CHANGED
@@ -271,6 +271,13 @@ export declare function resolveRef(ref: string, root: JsonSchema): JsonSchema |
271
271
  export declare function resolveSchema<T extends JsonSchema>(node: T, root?: JsonSchema): ResolvedSchema<T>;
272
272
  //#endregion
273
273
  //#region src/api/uiSchema.d.ts
274
+ /**
275
+ * Where a field's label goes. `top` stacks the label over the control. `side` is the
276
+ * property-sheet row: the label in a fixed column on the left, the control on the right,
277
+ * about 32 px tall, with the description behind the hint icon. Objects, unions, tuples and
278
+ * lists keep their label above and span the full width either way.
279
+ */
280
+ type LabelPlacement = "top" | "side";
274
281
  /** A titled set of fields, rendered together. */
275
282
  interface UiGroup {
276
283
  /** Stable id, used for keys and test hooks. */
@@ -287,6 +294,16 @@ interface UiGroup {
287
294
  collapsible?: boolean | undefined;
288
295
  /** Whether a collapsible group starts open. Defaults to closed. */
289
296
  defaultOpen?: boolean | undefined;
297
+ /**
298
+ * Lay the group's fields out on one row, each with its label above its control, instead
299
+ * of the form's grid: for a few small numbers that read as one setting (rows × columns ×
300
+ * square size; iterations and tolerances). Give each field a short `label` in `fields`
301
+ * of the `UiSchema`. A row has no room for descriptions or range notes, so they are
302
+ * dropped, and a field that is not a leaf (an object, a list) should stay out of it.
303
+ */
304
+ inline?: boolean | undefined;
305
+ /** This group's label placement, over the form's. */
306
+ labelPlacement?: LabelPlacement | undefined;
290
307
  /** A line of explanation under the heading. */
291
308
  hint?: ReactNode;
292
309
  }
@@ -318,6 +335,17 @@ interface FieldUi {
318
335
  * `{ brown_conrady5: "Brown-Conrady" }`.
319
336
  */
320
337
  enumLabels?: Record<string, string> | undefined;
338
+ /**
339
+ * For an object (or a union's variant): the order of its fields, by key. Keys not listed
340
+ * follow in schema order, and keys that do not exist are ignored. Use it where the
341
+ * schema's own order is not the one to read in (a schema whose keys arrive sorted).
342
+ */
343
+ order?: string[] | undefined;
344
+ /**
345
+ * For an object: lay its fields out on one row with their labels above, like a group's
346
+ * `inline`. The object keeps its frame and label.
347
+ */
348
+ inline?: boolean | undefined;
321
349
  /** Columns the field takes in the grid. Containers and lists default to the full row. */
322
350
  span?: 1 | 2 | undefined;
323
351
  /**
@@ -361,6 +389,23 @@ interface UiSchema {
361
389
  groups?: UiGroup[] | undefined;
362
390
  /** Per-field presentation, by dot path. */
363
391
  fields?: Record<string, FieldUi> | undefined;
392
+ /**
393
+ * The order of the root object's ungrouped fields, by key. Keys not listed follow in
394
+ * schema order. (Fields inside a group follow the group's `fields`; a nested object's
395
+ * order is `FieldUi.order`.)
396
+ */
397
+ order?: string[] | undefined;
398
+ /**
399
+ * Where labels go, for the whole form: `top` (the default) or `side`. A group can
400
+ * override it, and so can the form's prop, which wins over this.
401
+ */
402
+ labelPlacement?: LabelPlacement | undefined;
403
+ /**
404
+ * Where descriptions go for every field the app did not say otherwise about (see
405
+ * `FieldUi.descriptionAs`). Defaults to `hint`, the same in both placements: a long
406
+ * description is a tooltip, never a line in the form.
407
+ */
408
+ descriptionAs?: "hint" | "inline" | "none" | undefined;
364
409
  }
365
410
  /** What `renderField` is given for a field. */
366
411
  interface RenderFieldContext {
@@ -433,6 +478,15 @@ interface SchemaValueFormProps {
433
478
  messages?: FieldMessages | undefined;
434
479
  /** `2` (the default) puts fields side by side from `sm:` up; `1` is one column, for a narrow rail. */
435
480
  columns?: 1 | 2 | undefined;
481
+ /**
482
+ * Where labels go: `top` (the default) stacks each label over its control; `side` is the
483
+ * property-sheet row, the label in a fixed column on the left (truncated, the full text
484
+ * in its `title`) and the control on the right, about 32 px tall, with the description
485
+ * behind the hint icon. Containers, unions, tuples and lists keep their label above
486
+ * either way. Wins over `ui.labelPlacement`; a group's own `labelPlacement` wins over
487
+ * both for its fields. Works with both `columns`.
488
+ */
489
+ labelPlacement?: LabelPlacement | undefined;
436
490
  /** Blocks every control. The value is still shown. */
437
491
  disabled?: boolean | undefined;
438
492
  /**
@@ -464,7 +518,7 @@ interface SchemaValueFormProps {
464
518
  * The root is a `div` carrying `data-columns` and, while disabled, `data-disabled`. Wraps itself in a
465
519
  * `TooltipProvider`, so it needs none above it.
466
520
  */
467
- export declare function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline, messages, ui, columns, disabled, renderField, className, ref }: SchemaValueFormProps): import("react").JSX.Element;
521
+ export declare function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline, messages, ui, columns, labelPlacement, disabled, renderField, className, ref }: SchemaValueFormProps): import("react").JSX.Element;
468
522
  //#endregion
469
523
  //#region src/api/firstParagraph.d.ts
470
524
  /**
@@ -673,5 +727,5 @@ export declare function getAtPath(value: unknown, path: string): unknown;
673
727
  */
674
728
  export declare function setAtPath(value: unknown, path: string, next: unknown): unknown;
675
729
  //#endregion
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 };
730
+ export type { ChoiceOption, EnumOption, EnumValue, ExternalVariant, FieldInfo, FieldKind, FieldMessage, FieldMessages, FieldSpec, FieldUi, FieldWidget, JsonSchema, LabelPlacement, OptionsSchema, RawValues, RenderField, RenderFieldContext, ResolvedSchema, SchemaNode, SchemaShape, SchemaValueFormProps, TaggedVariant, UiGroup, UiSchema };
677
731
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1147,6 +1147,21 @@ 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
+ }
1150
1165
  //#endregion
1151
1166
  //#region src/api/valueDiff.ts
1152
1167
  /**
@@ -1292,15 +1307,33 @@ function spanClass(columns, ui, fallback) {
1292
1307
  }
1293
1308
  /** The field grid: two columns on `sm:` (or one), or an auto-fit row inside a tuple. */
1294
1309
  function Grid({ children }) {
1295
- const { columns } = useForm();
1310
+ const { columns, labelPlacement } = useForm();
1296
1311
  const dense = use(DenseContext);
1297
1312
  const density = useDensity();
1298
1313
  return /* @__PURE__ */ jsx("div", {
1299
- 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")),
1300
1315
  children
1301
1316
  });
1302
1317
  }
1303
- function present(node) {
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) {
1304
1337
  return node !== void 0 && node !== null && node !== false && node !== "";
1305
1338
  }
1306
1339
  /**
@@ -1314,15 +1347,17 @@ function present(node) {
1314
1347
  */
1315
1348
  function useChrome(meta, range = null) {
1316
1349
  const dense = use(DenseContext);
1317
- const mode = meta.ui.descriptionAs ?? "hint";
1350
+ const side = usePlacement() === "side";
1351
+ const { descriptionAs } = useForm();
1352
+ const mode = meta.ui.descriptionAs ?? descriptionAs;
1318
1353
  const text = meta.ui.hint ?? firstParagraph(meta.schema.description);
1319
- const shown = !dense && present(text);
1354
+ const shown = !dense && present$1(text);
1320
1355
  const hint = shown && mode === "hint" ? /* @__PURE__ */ jsx(InfoHint, {
1321
1356
  label: `About ${meta.label}`,
1322
1357
  children: text
1323
1358
  }) : null;
1324
1359
  const description = shown && mode === "inline" ? text : void 0;
1325
- const note = dense ? null : range;
1360
+ const note = dense || side ? null : range;
1326
1361
  return {
1327
1362
  hint,
1328
1363
  description,
@@ -1515,6 +1550,7 @@ function NumberLeaf({ meta }) {
1515
1550
  const value = getAtPath(form.value, meta.path);
1516
1551
  const spec = numberSpec(meta.schema, meta.ui);
1517
1552
  const chrome = useChrome(meta, rangeText(spec));
1553
+ const placement = usePlacement();
1518
1554
  const extras = useFieldExtras(meta);
1519
1555
  const [error, setError] = useState();
1520
1556
  const fallback = meta.schema.default;
@@ -1532,6 +1568,7 @@ function NumberLeaf({ meta }) {
1532
1568
  else setError(parsed === null ? "Enter a number" : checkNumber(spec, parsed));
1533
1569
  };
1534
1570
  return /* @__PURE__ */ jsx(Field, {
1571
+ layout: placement,
1535
1572
  label: meta.label,
1536
1573
  required: marked(meta),
1537
1574
  annotation: chrome.annotation,
@@ -1565,9 +1602,11 @@ function TextLeaf({ meta }) {
1565
1602
  const form = useForm();
1566
1603
  const value = getAtPath(form.value, meta.path);
1567
1604
  const chrome = useChrome(meta);
1605
+ const placement = usePlacement();
1568
1606
  const extras = useFieldExtras(meta);
1569
1607
  const fallback = meta.schema.default;
1570
1608
  return /* @__PURE__ */ jsx(Field, {
1609
+ layout: placement,
1571
1610
  label: meta.label,
1572
1611
  required: marked(meta),
1573
1612
  annotation: chrome.annotation,
@@ -1617,6 +1656,7 @@ function EnumLeaf({ meta, options }) {
1617
1656
  const dense = use(DenseContext);
1618
1657
  const value = getAtPath(form.value, meta.path);
1619
1658
  const chrome = useChrome(meta);
1659
+ const placement = usePlacement();
1620
1660
  const extras = useFieldExtras(meta);
1621
1661
  const shown = options.map((option) => ({
1622
1662
  value: String(option.value),
@@ -1630,6 +1670,7 @@ function EnumLeaf({ meta, options }) {
1630
1670
  explained
1631
1671
  ] }) : explained !== "" ? explained : chrome.description;
1632
1672
  return /* @__PURE__ */ jsx(Field, {
1673
+ layout: placement,
1633
1674
  as: "group",
1634
1675
  label: meta.label,
1635
1676
  required: marked(meta),
@@ -1655,27 +1696,71 @@ function EnumLeaf({ meta, options }) {
1655
1696
  })
1656
1697
  });
1657
1698
  }
1658
- /** A boolean: a switch (the default) or a checkbox. */
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
+ */
1659
1703
  function BooleanLeaf({ meta }) {
1660
1704
  const form = useForm();
1661
1705
  const value = getAtPath(form.value, meta.path);
1662
1706
  const chrome = useChrome(meta);
1663
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);
1664
1710
  const props = {
1665
1711
  checked: value === true,
1666
1712
  onCheckedChange: (next) => form.setValue(meta.path, next),
1667
- label: meta.label,
1668
- description: withNotes(withNotes(chrome.description, extras.error ? /* @__PURE__ */ jsx(ErrorText, { text: extras.error }) : void 0), extras.notes),
1669
1713
  disabled: form.disabled
1670
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
+ };
1671
1753
  return /* @__PURE__ */ jsxs("div", {
1672
1754
  className: cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1), extras.className),
1673
- children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...props }) : /* @__PURE__ */ jsx(Switch, { ...props }), chrome.hint !== null && /* @__PURE__ */ jsx("span", {
1755
+ children: [meta.ui.widget === "checkbox" ? /* @__PURE__ */ jsx(Checkbox, { ...rich }) : /* @__PURE__ */ jsx(Switch, { ...rich }), chrome.hint !== null && /* @__PURE__ */ jsx("span", {
1674
1756
  className: "mt-0.5",
1675
1757
  children: chrome.hint
1676
1758
  })]
1677
1759
  });
1678
1760
  }
1761
+ function present(node) {
1762
+ return node !== void 0 && node !== null && node !== false && node !== "";
1763
+ }
1679
1764
  function pretty(value) {
1680
1765
  return value === void 0 ? "" : JSON.stringify(value, null, 2);
1681
1766
  }
@@ -1973,13 +2058,16 @@ function useVisible(path, fields) {
1973
2058
  */
1974
2059
  function ObjectBody({ path, fields }) {
1975
2060
  const visible = useVisible(path, fields);
2061
+ const form = useForm();
1976
2062
  if (visible.length === 0) return null;
1977
- return /* @__PURE__ */ jsx(Grid, { children: visible.map((field) => /* @__PURE__ */ jsx(FieldNode, {
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, {
1978
2065
  path: joinPath(path, field.key),
1979
2066
  name: field.key,
1980
2067
  node: field.schema,
1981
2068
  required: field.required
1982
- }, field.key)) });
2069
+ }, field.key));
2070
+ return own?.inline === true ? /* @__PURE__ */ jsx(InlineRow, { children: cells }) : /* @__PURE__ */ jsx(Grid, { children: cells });
1983
2071
  }
1984
2072
  /** The frame of a composite field: its label and hint over its content. `bare` drops the frame. */
1985
2073
  function Frame({ meta, bare, children }) {
@@ -2211,12 +2299,13 @@ function useValue(value, seed, onValueChange) {
2211
2299
  * The root is a `div` carrying `data-columns` and, while disabled, `data-disabled`. Wraps itself in a
2212
2300
  * `TooltipProvider`, so it needs none above it.
2213
2301
  */
2214
- function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline, messages, 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 }) {
2215
2303
  const [current, commit] = useValue(value, () => defaultValue !== void 0 ? defaultValue : defaultValueForSchema(schema), onValueChange);
2216
2304
  const compared = useMemo(() => baseline === void 0 ? null : {
2217
2305
  value: baseline,
2218
2306
  counts: changeCounts(diffPaths(current, baseline))
2219
2307
  }, [current, baseline]);
2308
+ const claimed = useMemo(() => new Set((ui.groups ?? []).flatMap((group) => group.fields)), [ui.groups]);
2220
2309
  const state = {
2221
2310
  schema,
2222
2311
  value: current,
@@ -2227,8 +2316,10 @@ function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline,
2227
2316
  ui,
2228
2317
  disabled,
2229
2318
  columns,
2319
+ labelPlacement: labelPlacement ?? ui.labelPlacement ?? "top",
2320
+ descriptionAs: ui.descriptionAs ?? "hint",
2230
2321
  renderField,
2231
- claimed: useMemo(() => new Set((ui.groups ?? []).flatMap((group) => group.fields)), [ui.groups]),
2322
+ claimed,
2232
2323
  baseline: compared,
2233
2324
  messages
2234
2325
  };
@@ -2243,7 +2334,9 @@ function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline,
2243
2334
  children: /* @__PURE__ */ jsxs("div", {
2244
2335
  ref,
2245
2336
  "data-columns": columns,
2337
+ "data-label-placement": state.labelPlacement,
2246
2338
  "data-disabled": disabled ? "" : void 0,
2339
+ style: { "--field-label-w": columns === 2 ? "7.5rem" : "9rem" },
2247
2340
  className: cn("flex min-w-0 flex-col gap-5", className),
2248
2341
  children: [
2249
2342
  formMessage !== void 0 && /* @__PURE__ */ jsx("p", {
@@ -2269,7 +2362,8 @@ function SchemaValueForm({ schema, value, defaultValue, onValueChange, baseline,
2269
2362
  }
2270
2363
  /** A `UiGroup`: its fields under a heading, folded away if the app said so. */
2271
2364
  function GroupBlock({ group }) {
2272
- const { schema, value, ui } = useForm();
2365
+ const form = useForm();
2366
+ const { schema, value, ui } = form;
2273
2367
  const fields = group.fields.flatMap((path) => {
2274
2368
  const info = fieldAt(schema, path, schema, value);
2275
2369
  if (info === void 0 || fieldUiAt(ui, path).hidden === true) return [];
@@ -2279,12 +2373,20 @@ function GroupBlock({ group }) {
2279
2373
  }];
2280
2374
  });
2281
2375
  if (fields.length === 0) return null;
2282
- const body = /* @__PURE__ */ jsx(Grid, { children: fields.map(({ path, info }) => /* @__PURE__ */ jsx(FieldNode, {
2376
+ const cells = fields.map(({ path, info }) => /* @__PURE__ */ jsx(FieldNode, {
2283
2377
  path,
2284
2378
  name: path.split(".").at(-1) ?? path,
2285
2379
  node: info.schema,
2286
2380
  required: info.required
2287
- }, 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
+ });
2288
2390
  if (group.collapsible === true) return /* @__PURE__ */ jsxs(Disclosure, {
2289
2391
  summary: group.title,
2290
2392
  count: fields.length,
@@ -2292,12 +2394,12 @@ function GroupBlock({ group }) {
2292
2394
  children: [group.hint !== void 0 && /* @__PURE__ */ jsx("p", {
2293
2395
  className: "mb-3 text-xs text-fg-muted",
2294
2396
  children: group.hint
2295
- }), body]
2397
+ }), scoped]
2296
2398
  });
2297
2399
  return /* @__PURE__ */ jsx(Section, {
2298
2400
  title: group.title,
2299
2401
  hint: group.hint,
2300
- children: body
2402
+ children: scoped
2301
2403
  });
2302
2404
  }
2303
2405
  //#endregion