@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.
@@ -27,7 +27,8 @@ import type { EnumOption } from "../../api/schemaValue";
27
27
  import { defaultValueForSchema } from "../../api/schemaValue";
28
28
  import { getAtPath } from "../../api/valuePath";
29
29
  import type { FieldWidget } from "../../api/uiSchema";
30
- import { useChrome, spanClass, type FieldMeta } from "./chrome";
30
+ import { useChrome, usePlacement, spanClass, type FieldMeta } from "./chrome";
31
+ import { ErrorText, useFieldExtras, withNotes } from "./changes";
31
32
  import { DenseContext, useForm } from "./context";
32
33
 
33
34
  /** `required` as the marker shows it: a nullable field is never mandatory. */
@@ -54,6 +55,8 @@ export function NumberLeaf({ meta }: { meta: FieldMeta }) {
54
55
  const value = getAtPath(form.value, meta.path);
55
56
  const spec = numberSpec(meta.schema, meta.ui);
56
57
  const chrome = useChrome(meta, rangeText(spec));
58
+ const placement = usePlacement();
59
+ const extras = useFieldExtras(meta);
57
60
  const [error, setError] = useState<string | undefined>();
58
61
 
59
62
  const fallback = meta.schema.default;
@@ -75,12 +78,13 @@ export function NumberLeaf({ meta }: { meta: FieldMeta }) {
75
78
 
76
79
  return (
77
80
  <Field
81
+ layout={placement}
78
82
  label={meta.label}
79
83
  required={marked(meta)}
80
84
  annotation={chrome.annotation}
81
- description={chrome.description}
82
- error={error}
83
- className={spanClass(form.columns, meta.ui, 1)}
85
+ description={withNotes(chrome.description, extras.notes)}
86
+ error={error ?? extras.error}
87
+ className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
84
88
  >
85
89
  <NumberInput
86
90
  aria-label={meta.label}
@@ -113,15 +117,19 @@ export function TextLeaf({ meta }: { meta: FieldMeta }) {
113
117
  const form = useForm();
114
118
  const value = getAtPath(form.value, meta.path);
115
119
  const chrome = useChrome(meta);
120
+ const placement = usePlacement();
121
+ const extras = useFieldExtras(meta);
116
122
  const fallback = meta.schema.default;
117
123
 
118
124
  return (
119
125
  <Field
126
+ layout={placement}
120
127
  label={meta.label}
121
128
  required={marked(meta)}
122
129
  annotation={chrome.annotation}
123
- description={chrome.description}
124
- className={spanClass(form.columns, meta.ui, 1)}
130
+ description={withNotes(chrome.description, extras.notes)}
131
+ error={extras.error}
132
+ className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
125
133
  >
126
134
  <Input
127
135
  className="font-mono"
@@ -185,6 +193,8 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
185
193
  const dense = use(DenseContext);
186
194
  const value = getAtPath(form.value, meta.path);
187
195
  const chrome = useChrome(meta);
196
+ const placement = usePlacement();
197
+ const extras = useFieldExtras(meta);
188
198
 
189
199
  const shown = options.map((option) => ({
190
200
  value: String(option.value),
@@ -207,12 +217,14 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
207
217
 
208
218
  return (
209
219
  <Field
220
+ layout={placement}
210
221
  as="group"
211
222
  label={meta.label}
212
223
  required={marked(meta)}
213
224
  annotation={chrome.annotation}
214
- description={description}
215
- className={spanClass(form.columns, meta.ui, 1)}
225
+ description={withNotes(description, extras.notes)}
226
+ error={extras.error}
227
+ className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
216
228
  >
217
229
  <Chooser
218
230
  label={meta.label}
@@ -234,27 +246,64 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
234
246
  );
235
247
  }
236
248
 
237
- /** A boolean: a switch (the default) or a checkbox. */
249
+ /**
250
+ * A boolean: a switch (the default) or a checkbox. In a side placement it is a property
251
+ * row: the label in the label column, the control right of it, the notes under the control.
252
+ */
238
253
  export function BooleanLeaf({ meta }: { meta: FieldMeta }) {
239
254
  const form = useForm();
240
255
  const value = getAtPath(form.value, meta.path);
241
256
  const chrome = useChrome(meta);
257
+ const extras = useFieldExtras(meta);
258
+ const side = usePlacement() === "side";
259
+ const notes = withNotes(withNotes(chrome.description, extras.error ? <ErrorText text={extras.error} /> : undefined), extras.notes);
242
260
  const props = {
243
261
  checked: value === true,
244
262
  onCheckedChange: (next: boolean) => form.setValue(meta.path, next),
245
- label: meta.label,
246
- description: chrome.description,
247
263
  disabled: form.disabled,
248
264
  };
249
265
 
266
+ if (side) {
267
+ // The visible label sits in the label column; the control keeps the name for assistive technology.
268
+ const named = <span className="sr-only">{meta.label}</span>;
269
+ return (
270
+ <div
271
+ data-layout="side"
272
+ className={cn(
273
+ "grid min-w-0 grid-cols-[var(--field-label-w,8.5rem)_minmax(0,1fr)] items-center gap-x-3 gap-y-0.5",
274
+ spanClass(form.columns, meta.ui, 1),
275
+ extras.className,
276
+ )}
277
+ >
278
+ <span className="flex min-w-0 items-center gap-1.5">
279
+ <span className="min-w-0 truncate text-xs font-medium text-fg" title={meta.label}>
280
+ {meta.label}
281
+ </span>
282
+ {chrome.hint}
283
+ </span>
284
+ {meta.ui.widget === "checkbox" ? (
285
+ <Checkbox {...props} aria-label={meta.label} className="justify-self-start" />
286
+ ) : (
287
+ <Switch {...props} label={named} className="justify-self-start" />
288
+ )}
289
+ {present(notes) && <div className="col-start-2 text-xs leading-snug text-fg-muted">{notes}</div>}
290
+ </div>
291
+ );
292
+ }
293
+
294
+ const rich = { ...props, label: meta.label, description: notes };
250
295
  return (
251
- <div className={cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1))}>
252
- {meta.ui.widget === "checkbox" ? <Checkbox {...props} /> : <Switch {...props} />}
296
+ <div className={cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1), extras.className)}>
297
+ {meta.ui.widget === "checkbox" ? <Checkbox {...rich} /> : <Switch {...rich} />}
253
298
  {chrome.hint !== null && <span className="mt-0.5">{chrome.hint}</span>}
254
299
  </div>
255
300
  );
256
301
  }
257
302
 
303
+ function present(node: ReactNode): boolean {
304
+ return node !== undefined && node !== null && node !== false && node !== "";
305
+ }
306
+
258
307
  function pretty(value: unknown): string {
259
308
  return value === undefined ? "" : JSON.stringify(value, null, 2);
260
309
  }
@@ -270,6 +319,7 @@ export function JsonLeaf({ meta }: { meta: FieldMeta }) {
270
319
  const form = useForm();
271
320
  const value = getAtPath(form.value, meta.path);
272
321
  const chrome = useChrome(meta);
322
+ const extras = useFieldExtras(meta);
273
323
  const canonical = JSON.stringify(value) ?? "";
274
324
  const [draft, setDraft] = useState<{ text: string; error: string | undefined; base: string }>(() => ({
275
325
  text: pretty(value),
@@ -302,9 +352,9 @@ export function JsonLeaf({ meta }: { meta: FieldMeta }) {
302
352
  label={meta.label}
303
353
  required={marked(meta)}
304
354
  annotation={chrome.annotation}
305
- description={chrome.description}
306
- error={draft.error}
307
- className={spanClass(form.columns, meta.ui, 2)}
355
+ description={withNotes(chrome.description, extras.notes)}
356
+ error={draft.error ?? extras.error}
357
+ className={cn(spanClass(form.columns, meta.ui, 2), extras.className)}
308
358
  >
309
359
  <Textarea
310
360
  mono
@@ -351,6 +401,7 @@ export function StringListLeaf({
351
401
  const form = useForm();
352
402
  const value = getAtPath(form.value, meta.path);
353
403
  const chrome = useChrome(meta);
404
+ const extras = useFieldExtras(meta);
354
405
  const raw: unknown[] = Array.isArray(value) ? (value as unknown[]) : [];
355
406
  const [rows, setRows] = useState<RowIds>(() => syncIds({ ids: [], next: 0 }, raw.length));
356
407
  const synced = syncIds(rows, raw.length);
@@ -366,8 +417,9 @@ export function StringListLeaf({
366
417
  label={meta.label}
367
418
  required={marked(meta)}
368
419
  annotation={chrome.annotation}
369
- description={chrome.description}
370
- className={spanClass(form.columns, meta.ui, 2)}
420
+ description={withNotes(chrome.description, extras.notes)}
421
+ error={extras.error}
422
+ className={cn(spanClass(form.columns, meta.ui, 2), extras.className)}
371
423
  >
372
424
  <div className="flex flex-col gap-1.5">
373
425
  {raw.map((entry, index) => (
@@ -13,9 +13,10 @@ import { use, type ReactNode } from "react";
13
13
  import { firstParagraph } from "../../api/firstParagraph";
14
14
  import { labelFor, resolveSchema, type JsonSchema } from "../../api/schemaNode";
15
15
  import { activeExternal, activeTagged, seedNotNull, seedVariant, shapeOf, type FieldInfo, type SchemaShape } from "../../api/schemaValue";
16
- import { fieldUiAt } from "../../api/uiSchema";
16
+ import { fieldUiAt, orderFields } from "../../api/uiSchema";
17
17
  import { getAtPath, joinPath } from "../../api/valuePath";
18
- import { Grid, spanClass, useChrome, type FieldMeta } from "./chrome";
18
+ import { Grid, InlineRow, spanClass, useChrome, type FieldMeta } from "./chrome";
19
+ import { ErrorText, useFieldExtras, withNotes } from "./changes";
19
20
  import { Chooser, BooleanLeaf, EnumLeaf, JsonLeaf, NumberLeaf, StringListLeaf, TextLeaf } from "./leaves";
20
21
  import { DenseContext, useForm } from "./context";
21
22
 
@@ -144,15 +145,15 @@ function useVisible(path: string, fields: FieldInfo[]): FieldInfo[] {
144
145
  */
145
146
  export function ObjectBody({ path, fields }: { path: string; fields: FieldInfo[] }) {
146
147
  const visible = useVisible(path, fields);
148
+ const form = useForm();
147
149
  if (visible.length === 0) return null;
148
150
 
149
- return (
150
- <Grid>
151
- {visible.map((field) => (
152
- <FieldNode key={field.key} path={joinPath(path, field.key)} name={field.key} node={field.schema} required={field.required} />
153
- ))}
154
- </Grid>
155
- );
151
+ const own = path === "" ? undefined : fieldUiAt(form.ui, path);
152
+ const cells = orderFields(visible, path === "" ? form.ui.order : own?.order).map((field) => (
153
+ <FieldNode key={field.key} path={joinPath(path, field.key)} name={field.key} node={field.schema} required={field.required} />
154
+ ));
155
+ // `inline` is for an object's own fields; the root has the groups' `inline` instead.
156
+ return own?.inline === true ? <InlineRow>{cells}</InlineRow> : <Grid>{cells}</Grid>;
156
157
  }
157
158
 
158
159
  /** The frame of a composite field: its label and hint over its content. `bare` drops the frame. */
@@ -161,14 +162,17 @@ function Frame({ meta, bare, children }: { meta: FieldMeta; bare: boolean; child
161
162
  const dense = use(DenseContext);
162
163
  const density = useDensity();
163
164
  const chrome = useChrome(meta);
165
+ const extras = useFieldExtras(meta);
164
166
  if (bare) return <div className="flex min-w-0 flex-col gap-3">{children}</div>;
165
167
 
166
168
  return (
167
169
  <fieldset
170
+ data-changed={extras.changed ? "" : undefined}
168
171
  className={cn(
169
172
  "flex min-w-0 flex-col gap-3 rounded-panel border border-line",
170
173
  byDensity(density, "p-3", "p-2"),
171
174
  !dense && spanClass(form.columns, meta.ui, 2),
175
+ extras.className,
172
176
  )}
173
177
  >
174
178
  <legend className="px-1">
@@ -178,6 +182,8 @@ function Frame({ meta, bare, children }: { meta: FieldMeta; bare: boolean; child
178
182
  </span>
179
183
  </legend>
180
184
  {chrome.description !== undefined && <p className="text-xs leading-snug text-fg-muted">{chrome.description}</p>}
185
+ {extras.error !== undefined && <ErrorText text={extras.error} />}
186
+ {extras.notes !== undefined && <div className="text-xs">{extras.notes}</div>}
181
187
  {children}
182
188
  </fieldset>
183
189
  );
@@ -295,18 +301,19 @@ function TupleNode({ meta, shape, bare }: { meta: FieldMeta; shape: TupleShape;
295
301
  function NullableNode({ meta, shape }: { meta: FieldMeta; shape: SchemaShape }) {
296
302
  const form = useForm();
297
303
  const chrome = useChrome(meta);
304
+ const extras = useFieldExtras(meta);
298
305
  const value = getAtPath(form.value, meta.path);
299
306
  const set = value !== null && value !== undefined;
300
307
  const props = {
301
308
  checked: set,
302
309
  label: meta.label,
303
- description: chrome.description,
310
+ description: withNotes(withNotes(chrome.description, extras.error ? <ErrorText text={extras.error} /> : undefined), extras.notes),
304
311
  disabled: form.disabled,
305
312
  onCheckedChange: (next: boolean) => form.setValue(meta.path, next ? seedNotNull(meta.node, form.schema) : null),
306
313
  };
307
314
 
308
315
  return (
309
- <div className={cn("flex min-w-0 flex-col gap-3", spanClass(form.columns, meta.ui, 2))}>
316
+ <div className={cn("flex min-w-0 flex-col gap-3", spanClass(form.columns, meta.ui, 2), extras.className)}>
310
317
  <div className="flex items-start gap-1.5">
311
318
  {meta.ui.widget === "checkbox" ? <Checkbox {...props} /> : <Switch {...props} />}
312
319
  {chrome.hint !== null && <span className="mt-0.5">{chrome.hint}</span>}
package/src/index.ts CHANGED
@@ -15,7 +15,7 @@
15
15
  */
16
16
 
17
17
  export { SchemaForm } from "./components/SchemaForm";
18
- export { SchemaValueForm, type SchemaValueFormProps } from "./components/SchemaValueForm";
18
+ export { SchemaValueForm, type FieldMessage, type FieldMessages, type SchemaValueFormProps } from "./components/SchemaValueForm";
19
19
 
20
20
  export {
21
21
  describeFields,
@@ -47,5 +47,6 @@ export {
47
47
  type SchemaShape,
48
48
  type TaggedVariant,
49
49
  } from "./api/schemaValue";
50
- export type { FieldUi, FieldWidget, RenderField, RenderFieldContext, UiGroup, UiSchema } from "./api/uiSchema";
50
+ export type { FieldUi, FieldWidget, LabelPlacement, RenderField, RenderFieldContext, UiGroup, UiSchema } from "./api/uiSchema";
51
+ export { diffPaths } from "./api/valueDiff";
51
52
  export { getAtPath, setAtPath } from "./api/valuePath";