@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 +18 -0
- package/dist/index.d.ts +51 -2
- package/dist/index.js +249 -39
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/schemaForm.ts +1 -3
- package/src/api/schemaValue.ts +2 -9
- package/src/api/uiSchema.ts +1 -10
- package/src/api/valueDiff.ts +79 -0
- package/src/components/SchemaForm.tsx +10 -60
- package/src/components/SchemaValueForm.tsx +44 -8
- package/src/components/valueForm/changes.tsx +137 -0
- package/src/components/valueForm/chrome.tsx +1 -4
- package/src/components/valueForm/context.ts +20 -0
- package/src/components/valueForm/leaves.tsx +36 -25
- package/src/components/valueForm/nodes.tsx +11 -14
- package/src/index.ts +2 -1
|
@@ -28,6 +28,7 @@ import { defaultValueForSchema } from "../../api/schemaValue";
|
|
|
28
28
|
import { getAtPath } from "../../api/valuePath";
|
|
29
29
|
import type { FieldWidget } from "../../api/uiSchema";
|
|
30
30
|
import { useChrome, 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,7 @@ 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 extras = useFieldExtras(meta);
|
|
57
59
|
const [error, setError] = useState<string | undefined>();
|
|
58
60
|
|
|
59
61
|
const fallback = meta.schema.default;
|
|
@@ -78,9 +80,9 @@ export function NumberLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
78
80
|
label={meta.label}
|
|
79
81
|
required={marked(meta)}
|
|
80
82
|
annotation={chrome.annotation}
|
|
81
|
-
description={chrome.description}
|
|
82
|
-
error={error}
|
|
83
|
-
className={spanClass(form.columns, meta.ui, 1)}
|
|
83
|
+
description={withNotes(chrome.description, extras.notes)}
|
|
84
|
+
error={error ?? extras.error}
|
|
85
|
+
className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
|
|
84
86
|
>
|
|
85
87
|
<NumberInput
|
|
86
88
|
aria-label={meta.label}
|
|
@@ -113,6 +115,7 @@ export function TextLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
113
115
|
const form = useForm();
|
|
114
116
|
const value = getAtPath(form.value, meta.path);
|
|
115
117
|
const chrome = useChrome(meta);
|
|
118
|
+
const extras = useFieldExtras(meta);
|
|
116
119
|
const fallback = meta.schema.default;
|
|
117
120
|
|
|
118
121
|
return (
|
|
@@ -120,8 +123,9 @@ export function TextLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
120
123
|
label={meta.label}
|
|
121
124
|
required={marked(meta)}
|
|
122
125
|
annotation={chrome.annotation}
|
|
123
|
-
description={chrome.description}
|
|
124
|
-
|
|
126
|
+
description={withNotes(chrome.description, extras.notes)}
|
|
127
|
+
error={extras.error}
|
|
128
|
+
className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
|
|
125
129
|
>
|
|
126
130
|
<Input
|
|
127
131
|
className="font-mono"
|
|
@@ -165,14 +169,7 @@ export function Chooser({
|
|
|
165
169
|
const size = byDensity(useDensity(), "md", "sm");
|
|
166
170
|
const strip = widget === "segmented" || (widget !== "select" && options.length <= 3 && unsetLabel === undefined);
|
|
167
171
|
return strip ? (
|
|
168
|
-
<SegmentedControl
|
|
169
|
-
aria-label={label}
|
|
170
|
-
size={size}
|
|
171
|
-
disabled={disabled}
|
|
172
|
-
value={value}
|
|
173
|
-
options={options}
|
|
174
|
-
onValueChange={onChoose}
|
|
175
|
-
/>
|
|
172
|
+
<SegmentedControl aria-label={label} size={size} disabled={disabled} value={value} options={options} onValueChange={onChoose} />
|
|
176
173
|
) : (
|
|
177
174
|
<Select
|
|
178
175
|
aria-label={label}
|
|
@@ -192,6 +189,7 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
|
|
|
192
189
|
const dense = use(DenseContext);
|
|
193
190
|
const value = getAtPath(form.value, meta.path);
|
|
194
191
|
const chrome = useChrome(meta);
|
|
192
|
+
const extras = useFieldExtras(meta);
|
|
195
193
|
|
|
196
194
|
const shown = options.map((option) => ({
|
|
197
195
|
value: String(option.value),
|
|
@@ -200,8 +198,7 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
|
|
|
200
198
|
// An absent key is its schema default, so that is the option shown as chosen.
|
|
201
199
|
const selected = options.find((option) => option.value === (value === undefined ? meta.schema.default : value));
|
|
202
200
|
// The chosen variant's own sentence ("No distortion; no parameter block.") under the strip.
|
|
203
|
-
const explained =
|
|
204
|
-
!dense && meta.ui.descriptionAs !== "none" ? firstParagraph(selected?.description) : "";
|
|
201
|
+
const explained = !dense && meta.ui.descriptionAs !== "none" ? firstParagraph(selected?.description) : "";
|
|
205
202
|
const description: ReactNode =
|
|
206
203
|
explained !== "" && chrome.description !== undefined ? (
|
|
207
204
|
<>
|
|
@@ -219,8 +216,9 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
|
|
|
219
216
|
label={meta.label}
|
|
220
217
|
required={marked(meta)}
|
|
221
218
|
annotation={chrome.annotation}
|
|
222
|
-
description={description}
|
|
223
|
-
|
|
219
|
+
description={withNotes(description, extras.notes)}
|
|
220
|
+
error={extras.error}
|
|
221
|
+
className={cn(spanClass(form.columns, meta.ui, 1), extras.className)}
|
|
224
222
|
>
|
|
225
223
|
<Chooser
|
|
226
224
|
label={meta.label}
|
|
@@ -247,16 +245,17 @@ export function BooleanLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
247
245
|
const form = useForm();
|
|
248
246
|
const value = getAtPath(form.value, meta.path);
|
|
249
247
|
const chrome = useChrome(meta);
|
|
248
|
+
const extras = useFieldExtras(meta);
|
|
250
249
|
const props = {
|
|
251
250
|
checked: value === true,
|
|
252
251
|
onCheckedChange: (next: boolean) => form.setValue(meta.path, next),
|
|
253
252
|
label: meta.label,
|
|
254
|
-
description: chrome.description,
|
|
253
|
+
description: withNotes(withNotes(chrome.description, extras.error ? <ErrorText text={extras.error} /> : undefined), extras.notes),
|
|
255
254
|
disabled: form.disabled,
|
|
256
255
|
};
|
|
257
256
|
|
|
258
257
|
return (
|
|
259
|
-
<div className={cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1))}>
|
|
258
|
+
<div className={cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1), extras.className)}>
|
|
260
259
|
{meta.ui.widget === "checkbox" ? <Checkbox {...props} /> : <Switch {...props} />}
|
|
261
260
|
{chrome.hint !== null && <span className="mt-0.5">{chrome.hint}</span>}
|
|
262
261
|
</div>
|
|
@@ -278,6 +277,7 @@ export function JsonLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
278
277
|
const form = useForm();
|
|
279
278
|
const value = getAtPath(form.value, meta.path);
|
|
280
279
|
const chrome = useChrome(meta);
|
|
280
|
+
const extras = useFieldExtras(meta);
|
|
281
281
|
const canonical = JSON.stringify(value) ?? "";
|
|
282
282
|
const [draft, setDraft] = useState<{ text: string; error: string | undefined; base: string }>(() => ({
|
|
283
283
|
text: pretty(value),
|
|
@@ -310,11 +310,12 @@ export function JsonLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
310
310
|
label={meta.label}
|
|
311
311
|
required={marked(meta)}
|
|
312
312
|
annotation={chrome.annotation}
|
|
313
|
-
description={chrome.description}
|
|
314
|
-
error={draft.error}
|
|
315
|
-
className={spanClass(form.columns, meta.ui, 2)}
|
|
313
|
+
description={withNotes(chrome.description, extras.notes)}
|
|
314
|
+
error={draft.error ?? extras.error}
|
|
315
|
+
className={cn(spanClass(form.columns, meta.ui, 2), extras.className)}
|
|
316
316
|
>
|
|
317
317
|
<Textarea
|
|
318
|
+
mono
|
|
318
319
|
rows={Math.min(Math.max(draft.text.split("\n").length, 3), 12)}
|
|
319
320
|
spellCheck={false}
|
|
320
321
|
aria-label={meta.label}
|
|
@@ -346,10 +347,19 @@ function syncIds(rows: RowIds, length: number): RowIds {
|
|
|
346
347
|
* Each row has an id of its own, so removing the second of three keeps the focus and the
|
|
347
348
|
* cursor of the others instead of re-keying them by position.
|
|
348
349
|
*/
|
|
349
|
-
export function StringListLeaf({
|
|
350
|
+
export function StringListLeaf({
|
|
351
|
+
meta,
|
|
352
|
+
minItems,
|
|
353
|
+
maxItems,
|
|
354
|
+
}: {
|
|
355
|
+
meta: FieldMeta;
|
|
356
|
+
minItems?: number | undefined;
|
|
357
|
+
maxItems?: number | undefined;
|
|
358
|
+
}) {
|
|
350
359
|
const form = useForm();
|
|
351
360
|
const value = getAtPath(form.value, meta.path);
|
|
352
361
|
const chrome = useChrome(meta);
|
|
362
|
+
const extras = useFieldExtras(meta);
|
|
353
363
|
const raw: unknown[] = Array.isArray(value) ? (value as unknown[]) : [];
|
|
354
364
|
const [rows, setRows] = useState<RowIds>(() => syncIds({ ids: [], next: 0 }, raw.length));
|
|
355
365
|
const synced = syncIds(rows, raw.length);
|
|
@@ -365,8 +375,9 @@ export function StringListLeaf({ meta, minItems, maxItems }: { meta: FieldMeta;
|
|
|
365
375
|
label={meta.label}
|
|
366
376
|
required={marked(meta)}
|
|
367
377
|
annotation={chrome.annotation}
|
|
368
|
-
description={chrome.description}
|
|
369
|
-
|
|
378
|
+
description={withNotes(chrome.description, extras.notes)}
|
|
379
|
+
error={extras.error}
|
|
380
|
+
className={cn(spanClass(form.columns, meta.ui, 2), extras.className)}
|
|
370
381
|
>
|
|
371
382
|
<div className="flex flex-col gap-1.5">
|
|
372
383
|
{raw.map((entry, index) => (
|
|
@@ -12,18 +12,11 @@ import { use, type ReactNode } from "react";
|
|
|
12
12
|
|
|
13
13
|
import { firstParagraph } from "../../api/firstParagraph";
|
|
14
14
|
import { labelFor, resolveSchema, type JsonSchema } from "../../api/schemaNode";
|
|
15
|
-
import {
|
|
16
|
-
activeExternal,
|
|
17
|
-
activeTagged,
|
|
18
|
-
seedNotNull,
|
|
19
|
-
seedVariant,
|
|
20
|
-
shapeOf,
|
|
21
|
-
type FieldInfo,
|
|
22
|
-
type SchemaShape,
|
|
23
|
-
} from "../../api/schemaValue";
|
|
15
|
+
import { activeExternal, activeTagged, seedNotNull, seedVariant, shapeOf, type FieldInfo, type SchemaShape } from "../../api/schemaValue";
|
|
24
16
|
import { fieldUiAt } from "../../api/uiSchema";
|
|
25
17
|
import { getAtPath, joinPath } from "../../api/valuePath";
|
|
26
18
|
import { Grid, spanClass, useChrome, type FieldMeta } from "./chrome";
|
|
19
|
+
import { ErrorText, useFieldExtras, withNotes } from "./changes";
|
|
27
20
|
import { Chooser, BooleanLeaf, EnumLeaf, JsonLeaf, NumberLeaf, StringListLeaf, TextLeaf } from "./leaves";
|
|
28
21
|
import { DenseContext, useForm } from "./context";
|
|
29
22
|
|
|
@@ -56,9 +49,7 @@ function applyWidget(shape: SchemaShape, meta: FieldMeta, value: unknown): Schem
|
|
|
56
49
|
case "string-list":
|
|
57
50
|
return meta.schema.type === "array" ? { kind: "string-list", item: items ?? { type: "string" } } : shape;
|
|
58
51
|
case "tuple-row":
|
|
59
|
-
return items !== undefined && Array.isArray(value)
|
|
60
|
-
? { kind: "tuple", items: (value as unknown[]).map(() => items) }
|
|
61
|
-
: shape;
|
|
52
|
+
return items !== undefined && Array.isArray(value) ? { kind: "tuple", items: (value as unknown[]).map(() => items) } : shape;
|
|
62
53
|
default:
|
|
63
54
|
return shape;
|
|
64
55
|
}
|
|
@@ -171,14 +162,17 @@ function Frame({ meta, bare, children }: { meta: FieldMeta; bare: boolean; child
|
|
|
171
162
|
const dense = use(DenseContext);
|
|
172
163
|
const density = useDensity();
|
|
173
164
|
const chrome = useChrome(meta);
|
|
165
|
+
const extras = useFieldExtras(meta);
|
|
174
166
|
if (bare) return <div className="flex min-w-0 flex-col gap-3">{children}</div>;
|
|
175
167
|
|
|
176
168
|
return (
|
|
177
169
|
<fieldset
|
|
170
|
+
data-changed={extras.changed ? "" : undefined}
|
|
178
171
|
className={cn(
|
|
179
172
|
"flex min-w-0 flex-col gap-3 rounded-panel border border-line",
|
|
180
173
|
byDensity(density, "p-3", "p-2"),
|
|
181
174
|
!dense && spanClass(form.columns, meta.ui, 2),
|
|
175
|
+
extras.className,
|
|
182
176
|
)}
|
|
183
177
|
>
|
|
184
178
|
<legend className="px-1">
|
|
@@ -188,6 +182,8 @@ function Frame({ meta, bare, children }: { meta: FieldMeta; bare: boolean; child
|
|
|
188
182
|
</span>
|
|
189
183
|
</legend>
|
|
190
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>}
|
|
191
187
|
{children}
|
|
192
188
|
</fieldset>
|
|
193
189
|
);
|
|
@@ -305,18 +301,19 @@ function TupleNode({ meta, shape, bare }: { meta: FieldMeta; shape: TupleShape;
|
|
|
305
301
|
function NullableNode({ meta, shape }: { meta: FieldMeta; shape: SchemaShape }) {
|
|
306
302
|
const form = useForm();
|
|
307
303
|
const chrome = useChrome(meta);
|
|
304
|
+
const extras = useFieldExtras(meta);
|
|
308
305
|
const value = getAtPath(form.value, meta.path);
|
|
309
306
|
const set = value !== null && value !== undefined;
|
|
310
307
|
const props = {
|
|
311
308
|
checked: set,
|
|
312
309
|
label: meta.label,
|
|
313
|
-
description: chrome.description,
|
|
310
|
+
description: withNotes(withNotes(chrome.description, extras.error ? <ErrorText text={extras.error} /> : undefined), extras.notes),
|
|
314
311
|
disabled: form.disabled,
|
|
315
312
|
onCheckedChange: (next: boolean) => form.setValue(meta.path, next ? seedNotNull(meta.node, form.schema) : null),
|
|
316
313
|
};
|
|
317
314
|
|
|
318
315
|
return (
|
|
319
|
-
<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)}>
|
|
320
317
|
<div className="flex items-start gap-1.5">
|
|
321
318
|
{meta.ui.widget === "checkbox" ? <Checkbox {...props} /> : <Switch {...props} />}
|
|
322
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,
|
|
@@ -48,4 +48,5 @@ export {
|
|
|
48
48
|
type TaggedVariant,
|
|
49
49
|
} from "./api/schemaValue";
|
|
50
50
|
export type { FieldUi, FieldWidget, RenderField, RenderFieldContext, UiGroup, UiSchema } from "./api/uiSchema";
|
|
51
|
+
export { diffPaths } from "./api/valueDiff";
|
|
51
52
|
export { getAtPath, setAtPath } from "./api/valuePath";
|