@vitavision/forms 0.6.0
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/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +32 -0
- package/dist/index.d.ts +170 -0
- package/dist/index.js +435 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
- package/src/api/schemaForm.ts +459 -0
- package/src/components/SchemaForm.tsx +216 -0
- package/src/index.ts +27 -0
- package/src/styles.css +7 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { Disclosure, Field, Input, NumberInput, SegmentedControl, Select, Switch, Textarea } from "@vitavision/ui";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
//#region src/api/schemaForm.ts
|
|
4
|
+
const DEFS_PREFIX = "#/$defs/";
|
|
5
|
+
/**
|
|
6
|
+
* Follow a `$ref` into `$defs`.
|
|
7
|
+
*
|
|
8
|
+
* The merge order matters and is the opposite of the obvious one. Pydantic writes the
|
|
9
|
+
* field's `default` and `description` *beside* the `$ref`, while the target carries the
|
|
10
|
+
* enum's own `title` and sometimes its own docstring as a `description`. The field's
|
|
11
|
+
* statement about itself wins; the type's is the fallback.
|
|
12
|
+
*/
|
|
13
|
+
function deref(node, defs) {
|
|
14
|
+
if (node.$ref === void 0) return node;
|
|
15
|
+
if (!node.$ref.startsWith(DEFS_PREFIX)) return node;
|
|
16
|
+
const target = defs?.[node.$ref.slice(8)];
|
|
17
|
+
if (target === void 0) return node;
|
|
18
|
+
const rest = { ...node };
|
|
19
|
+
delete rest.$ref;
|
|
20
|
+
return {
|
|
21
|
+
...target,
|
|
22
|
+
...rest
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Look through pydantic's `str | None` encoding to the type that carries the meaning.
|
|
27
|
+
*
|
|
28
|
+
* An optional string arrives as `anyOf: [{type: "string"}, {type: "null"}]`, and treating
|
|
29
|
+
* that as an unrecognised node would give every nullable option a JSON textarea.
|
|
30
|
+
*/
|
|
31
|
+
function unwrap(node) {
|
|
32
|
+
if (!node.anyOf) return node;
|
|
33
|
+
const meaningful = node.anyOf.filter((branch) => branch.type !== "null");
|
|
34
|
+
return meaningful.length === 1 ? {
|
|
35
|
+
...node,
|
|
36
|
+
...meaningful[0]
|
|
37
|
+
} : node;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The node that says what this field *is*.
|
|
41
|
+
*
|
|
42
|
+
* Dereferenced on both sides of the unwrap because either can hold the `$ref`: a plain
|
|
43
|
+
* `ColorMode` is a bare `$ref`, while an `Optional[ColorMode]` hides one inside `anyOf`.
|
|
44
|
+
*/
|
|
45
|
+
function resolve(node, defs) {
|
|
46
|
+
return deref(unwrap(deref(node, defs)), defs);
|
|
47
|
+
}
|
|
48
|
+
function kindOf(resolved) {
|
|
49
|
+
if (Array.isArray(resolved.enum) && resolved.enum.length > 0) return resolved.enum.length <= 3 ? "choice-inline" : "choice";
|
|
50
|
+
if (resolved.type === "boolean") return "boolean";
|
|
51
|
+
if (resolved.type === "integer" || resolved.type === "number") return "number";
|
|
52
|
+
if (resolved.type === "string") return "text";
|
|
53
|
+
if (resolved.type === "array" && unwrap(resolved.items ?? {}).type === "string") return "string-list";
|
|
54
|
+
return "json";
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Which options a caller has to think about, and which already have an answer.
|
|
58
|
+
*
|
|
59
|
+
* The schema says this without anyone maintaining a second list: a field whose default is
|
|
60
|
+
* *empty* — `[]`, `null`, `""`, or absent entirely — is one nothing can do anything useful
|
|
61
|
+
* with until it is filled in. A field with a real default already works, and someone
|
|
62
|
+
* filling in the form should not have to read past nine vocabulary options to answer the
|
|
63
|
+
* question that actually needs an answer.
|
|
64
|
+
*/
|
|
65
|
+
function isAdvanced(node) {
|
|
66
|
+
const fallback = node.default;
|
|
67
|
+
if (fallback === void 0 || fallback === null) return false;
|
|
68
|
+
if (Array.isArray(fallback)) return fallback.length > 0;
|
|
69
|
+
if (fallback === "") return false;
|
|
70
|
+
if (typeof fallback === "object") return Object.keys(fallback).length > 0;
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A schema that marks its decisions says so with `x-primary`; one that does not is read by
|
|
75
|
+
* its defaults. The guess works for an adapter, whose open questions have empty defaults,
|
|
76
|
+
* and fails for a model's hyperparameters, where *every* field has a working default and the
|
|
77
|
+
* two that matter are indistinguishable from the fifteen that do not.
|
|
78
|
+
*
|
|
79
|
+
* Read from the raw node first: a `$ref` field carries its extras beside the reference.
|
|
80
|
+
*/
|
|
81
|
+
function isPrimary(node, resolved) {
|
|
82
|
+
const hint = node["x-primary"] ?? resolved["x-primary"];
|
|
83
|
+
if (hint !== void 0) return hint;
|
|
84
|
+
return !isAdvanced(resolved);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Humanise `normal_dirs` into `Normal dirs`.
|
|
88
|
+
*
|
|
89
|
+
* Pydantic emits a `title` for every field whether or not anyone wrote one, and its
|
|
90
|
+
* generated form is Title Case — `Csv Path`, `Defect Type From Dir` — which reads like a
|
|
91
|
+
* spreadsheet header rather than a form label. So a title is used only when it is *not*
|
|
92
|
+
* the one pydantic would have generated, which is exactly when a human chose it.
|
|
93
|
+
*
|
|
94
|
+
* Read from the *unresolved* node on purpose. Dereferencing pulls in the enum class's own
|
|
95
|
+
* title, so a resolved `color` field would be labelled "ColorMode".
|
|
96
|
+
*/
|
|
97
|
+
function labelFor(name, node) {
|
|
98
|
+
const words = name.split("_");
|
|
99
|
+
const generated = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
|
100
|
+
if (node.title && node.title !== generated) return node.title;
|
|
101
|
+
const spaced = words.join(" ");
|
|
102
|
+
return spaced.charAt(0).toUpperCase() + spaced.slice(1);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* A value as a person reads it: scalars as themselves, anything structured as JSON rather
|
|
106
|
+
* than `[object Object]`.
|
|
107
|
+
*/
|
|
108
|
+
function displayValue(value) {
|
|
109
|
+
if (value === null || value === void 0) return "";
|
|
110
|
+
if (typeof value === "string") return value;
|
|
111
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") return value.toString();
|
|
112
|
+
return JSON.stringify(value) ?? "";
|
|
113
|
+
}
|
|
114
|
+
function placeholderFor(kind, fallback) {
|
|
115
|
+
if (kind === "string-list") return Array.isArray(fallback) && fallback.length > 0 ? fallback.join(", ") : "a, b, c";
|
|
116
|
+
if (kind === "json") return "{}";
|
|
117
|
+
return displayValue(fallback);
|
|
118
|
+
}
|
|
119
|
+
/** `rgb` reads better as `Rgb` in a picker, but `bilinear` must not become `Bi Linear`. */
|
|
120
|
+
function optionLabel(value) {
|
|
121
|
+
const text = String(value);
|
|
122
|
+
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
123
|
+
}
|
|
124
|
+
function optionsFor(resolved, kind) {
|
|
125
|
+
if (kind !== "choice" && kind !== "choice-inline") return [];
|
|
126
|
+
return (resolved.enum ?? []).map((value) => ({
|
|
127
|
+
value: String(value),
|
|
128
|
+
label: optionLabel(value)
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
/** The bounds as prose, keeping the distinction the HTML control cannot express. */
|
|
132
|
+
function rangeOf(resolved) {
|
|
133
|
+
const parts = [];
|
|
134
|
+
if (resolved.minimum !== void 0) parts.push(`≥ ${resolved.minimum}`);
|
|
135
|
+
else if (resolved.exclusiveMinimum !== void 0) parts.push(`> ${resolved.exclusiveMinimum}`);
|
|
136
|
+
if (resolved.maximum !== void 0) parts.push(`≤ ${resolved.maximum}`);
|
|
137
|
+
else if (resolved.exclusiveMaximum !== void 0) parts.push(`< ${resolved.exclusiveMaximum}`);
|
|
138
|
+
return parts.length > 0 ? parts.join(", ") : null;
|
|
139
|
+
}
|
|
140
|
+
function stepOf(resolved) {
|
|
141
|
+
if (resolved.multipleOf !== void 0) return resolved.multipleOf;
|
|
142
|
+
if (resolved.type === "integer") return 1;
|
|
143
|
+
return "any";
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Derive the form's fields from an options schema, in schema order.
|
|
147
|
+
*
|
|
148
|
+
* Resolves `$ref`s into `$defs`, looks through `anyOf: [T, null]`, picks a control per node
|
|
149
|
+
* (see `FieldKind`), and folds a field under "Advanced" when it is optional and its default
|
|
150
|
+
* already works — unless the schema says otherwise with `"x-primary"`.
|
|
151
|
+
*
|
|
152
|
+
* @param schema - The model's JSON Schema (draft 2020-12, as pydantic or schemars emit it).
|
|
153
|
+
* @returns One `FieldSpec` per property; empty for a model with no options.
|
|
154
|
+
*/
|
|
155
|
+
function describeFields(schema) {
|
|
156
|
+
const required = new Set(schema.required ?? []);
|
|
157
|
+
const defs = schema.$defs;
|
|
158
|
+
return Object.entries(schema.properties ?? {}).map(([name, node]) => {
|
|
159
|
+
const resolved = resolve(node, defs);
|
|
160
|
+
const kind = kindOf(resolved);
|
|
161
|
+
const numeric = (kind === "choice" || kind === "choice-inline") && (resolved.type === "integer" || resolved.type === "number");
|
|
162
|
+
return {
|
|
163
|
+
name,
|
|
164
|
+
label: labelFor(name, node),
|
|
165
|
+
description: node.description ?? resolved.description ?? null,
|
|
166
|
+
kind,
|
|
167
|
+
required: required.has(name),
|
|
168
|
+
fallback: resolved.default,
|
|
169
|
+
placeholder: placeholderFor(kind, resolved.default),
|
|
170
|
+
advanced: !required.has(name) && !isPrimary(node, resolved),
|
|
171
|
+
options: optionsFor(resolved, kind),
|
|
172
|
+
numeric,
|
|
173
|
+
...kind === "number" ? {
|
|
174
|
+
min: resolved.minimum ?? resolved.exclusiveMinimum,
|
|
175
|
+
max: resolved.maximum ?? resolved.exclusiveMaximum,
|
|
176
|
+
step: stepOf(resolved)
|
|
177
|
+
} : {},
|
|
178
|
+
range: kind === "number" ? rangeOf(resolved) : null
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The controls' starting state.
|
|
184
|
+
*
|
|
185
|
+
* Text, list and choice controls start **empty even when the schema has a default**, and
|
|
186
|
+
* that is deliberate: a pre-filled box says "this is your value" when what is true is "this
|
|
187
|
+
* is what happens if you say nothing". Pre-filling would also mean every run sent every
|
|
188
|
+
* default back, so a later change to a default would silently not reach anyone who had
|
|
189
|
+
* once opened the form. The default is shown as the placeholder instead — and for a
|
|
190
|
+
* segmented control, as the segment already highlighted.
|
|
191
|
+
*
|
|
192
|
+
* Booleans are the exception — a checkbox has no empty state, so it starts at the
|
|
193
|
+
* default and is always sent.
|
|
194
|
+
*/
|
|
195
|
+
function initialValues(fields) {
|
|
196
|
+
const values = {};
|
|
197
|
+
for (const field of fields) values[field.name] = field.kind === "boolean" ? field.fallback === true : "";
|
|
198
|
+
return values;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Build the options object to send.
|
|
202
|
+
*
|
|
203
|
+
* An untouched control contributes **nothing**, so the receiving side's own default
|
|
204
|
+
* applies and stays the single definition of it. That is what keeps a default from
|
|
205
|
+
* existing twice, once here and once wherever it is enforced, with the two free to drift.
|
|
206
|
+
*/
|
|
207
|
+
function toOptions(fields, values) {
|
|
208
|
+
const options = {};
|
|
209
|
+
for (const field of fields) {
|
|
210
|
+
const raw = values[field.name];
|
|
211
|
+
if (field.kind === "boolean") {
|
|
212
|
+
options[field.name] = raw === true;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const text = typeof raw === "string" ? raw.trim() : "";
|
|
216
|
+
if (text === "") continue;
|
|
217
|
+
switch (field.kind) {
|
|
218
|
+
case "string-list":
|
|
219
|
+
options[field.name] = text.split(",").map((item) => item.trim()).filter(Boolean);
|
|
220
|
+
break;
|
|
221
|
+
case "number": {
|
|
222
|
+
const parsed = Number(text);
|
|
223
|
+
if (Number.isFinite(parsed)) options[field.name] = parsed;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
case "choice":
|
|
227
|
+
case "choice-inline": {
|
|
228
|
+
if (!field.numeric) {
|
|
229
|
+
options[field.name] = text;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
const parsed = Number(text);
|
|
233
|
+
if (Number.isFinite(parsed)) options[field.name] = parsed;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
case "json":
|
|
237
|
+
try {
|
|
238
|
+
options[field.name] = JSON.parse(text);
|
|
239
|
+
} catch {}
|
|
240
|
+
break;
|
|
241
|
+
default: options[field.name] = text;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return options;
|
|
245
|
+
}
|
|
246
|
+
/** How many options the operator has actually changed, for the tab that hides them. */
|
|
247
|
+
function overrideCount(fields, values) {
|
|
248
|
+
const initial = initialValues(fields);
|
|
249
|
+
return fields.filter((field) => values[field.name] !== initial[field.name]).length;
|
|
250
|
+
}
|
|
251
|
+
/** Which JSON fields do not parse, so the form can say so before the run starts. */
|
|
252
|
+
function jsonErrors(fields, values) {
|
|
253
|
+
return fields.filter((field) => field.kind === "json").filter((field) => {
|
|
254
|
+
const raw = values[field.name];
|
|
255
|
+
if (typeof raw !== "string" || raw.trim() === "") return false;
|
|
256
|
+
try {
|
|
257
|
+
JSON.parse(raw);
|
|
258
|
+
return false;
|
|
259
|
+
} catch {
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
}).map((field) => field.label);
|
|
263
|
+
}
|
|
264
|
+
/** Required fields the operator has not filled in. Blocks the run. */
|
|
265
|
+
function missingRequired(fields, values) {
|
|
266
|
+
return fields.filter((field) => field.required).filter((field) => {
|
|
267
|
+
const raw = values[field.name];
|
|
268
|
+
if (typeof raw === "boolean") return false;
|
|
269
|
+
return (raw ?? "").trim() === "";
|
|
270
|
+
}).map((field) => field.label);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Fields whose typed value is outside the bounds the schema states.
|
|
274
|
+
*
|
|
275
|
+
* Native `min`/`max` stop the spinner and mark the input invalid, but nothing stops a
|
|
276
|
+
* paste, so the check is repeated here where it can block the button.
|
|
277
|
+
*/
|
|
278
|
+
function outOfRange(fields, values) {
|
|
279
|
+
return fields.filter((field) => field.kind === "number").filter((field) => {
|
|
280
|
+
const raw = values[field.name];
|
|
281
|
+
if (typeof raw !== "string" || raw.trim() === "") return false;
|
|
282
|
+
const parsed = Number(raw);
|
|
283
|
+
if (!Number.isFinite(parsed)) return true;
|
|
284
|
+
if (field.min !== void 0 && parsed < field.min) return true;
|
|
285
|
+
if (field.max !== void 0 && parsed > field.max) return true;
|
|
286
|
+
return false;
|
|
287
|
+
}).map((field) => field.label);
|
|
288
|
+
}
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/components/SchemaForm.tsx
|
|
291
|
+
/**
|
|
292
|
+
* An options form, generated from a JSON Schema.
|
|
293
|
+
*
|
|
294
|
+
* Adding an adapter, a model, or a measurement method costs a config module and a registry
|
|
295
|
+
* entry, and *nothing here* — that is the point of generating the form from the schema
|
|
296
|
+
* instead of hardcoding it. Every decision about which control a field deserves lives in
|
|
297
|
+
* `api/schemaForm.ts`; this file only renders.
|
|
298
|
+
*
|
|
299
|
+
* Two behaviours are worth knowing while reading the markup:
|
|
300
|
+
*
|
|
301
|
+
* * An empty control means **unset**, and the caller's own default applies. The default
|
|
302
|
+
* is shown — as a placeholder, as the pre-highlighted segment, or as the picker's
|
|
303
|
+
* leading entry — so a reader can see what happens if they say nothing, without that
|
|
304
|
+
* value being claimed as their choice.
|
|
305
|
+
* * A field the schema requires is marked, and the caller blocks the run on it.
|
|
306
|
+
*
|
|
307
|
+
* The layout is a two-column grid: a form of eight numeric parameters, each a full-width
|
|
308
|
+
* control over a full-width description, runs to screens of scrolling for a form that
|
|
309
|
+
* often needs nothing touched at all. Fields whose value is genuinely wide — a list, a
|
|
310
|
+
* JSON object, a statement with a switch — still span both columns.
|
|
311
|
+
*/
|
|
312
|
+
/**
|
|
313
|
+
* Which kinds are worth a full row. Everything else pairs up.
|
|
314
|
+
*
|
|
315
|
+
* A JSON object needs the width to be editable at all, and a switch reads as a sentence
|
|
316
|
+
* with its explanation beside it. A comma-separated list does *not* qualify: three of them
|
|
317
|
+
* in a row can run as tall as the layout this rule exists to avoid.
|
|
318
|
+
*/
|
|
319
|
+
const WIDE = /* @__PURE__ */ new Set(["json", "boolean"]);
|
|
320
|
+
/**
|
|
321
|
+
* An options form rendered from `describeFields(schema)`: primary fields first, the rest
|
|
322
|
+
* folded under "Advanced", each with the control its schema node calls for.
|
|
323
|
+
*
|
|
324
|
+
* Controlled: `values` holds the raw control values (strings, or booleans for switches),
|
|
325
|
+
* and an empty string means "unset — the schema's default applies". Turn them into the
|
|
326
|
+
* options to send with `toOptions`, and gate a run on `missingRequired`, `outOfRange` and
|
|
327
|
+
* `jsonErrors`.
|
|
328
|
+
*/
|
|
329
|
+
function SchemaForm({ fields, values, onChange }) {
|
|
330
|
+
if (fields.length === 0) return /* @__PURE__ */ jsx("p", {
|
|
331
|
+
className: "text-sm text-fg-muted",
|
|
332
|
+
children: "This plugin takes no options."
|
|
333
|
+
});
|
|
334
|
+
const set = (name, value) => onChange({
|
|
335
|
+
...values,
|
|
336
|
+
[name]: value
|
|
337
|
+
});
|
|
338
|
+
const render = (field) => /* @__PURE__ */ jsx(SchemaField, {
|
|
339
|
+
field,
|
|
340
|
+
value: values[field.name] ?? "",
|
|
341
|
+
onChange: (value) => set(field.name, value)
|
|
342
|
+
}, field.name);
|
|
343
|
+
const primary = fields.filter((field) => !field.advanced);
|
|
344
|
+
const advanced = fields.filter((field) => field.advanced);
|
|
345
|
+
if (primary.length === 0) return /* @__PURE__ */ jsx(Grid, { children: advanced.map(render) });
|
|
346
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
347
|
+
className: "flex flex-col gap-4",
|
|
348
|
+
children: [/* @__PURE__ */ jsx(Grid, { children: primary.map(render) }), advanced.length > 0 && /* @__PURE__ */ jsx(Disclosure, {
|
|
349
|
+
summary: "Advanced",
|
|
350
|
+
count: advanced.length,
|
|
351
|
+
children: /* @__PURE__ */ jsx(Grid, { children: advanced.map(render) })
|
|
352
|
+
})]
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
function Grid({ children }) {
|
|
356
|
+
return /* @__PURE__ */ jsx("div", {
|
|
357
|
+
className: "grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2",
|
|
358
|
+
children
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
function SchemaField({ field, value, onChange }) {
|
|
362
|
+
const span = WIDE.has(field.kind) ? "sm:col-span-2" : void 0;
|
|
363
|
+
if (field.kind === "boolean") return /* @__PURE__ */ jsx("div", {
|
|
364
|
+
className: span,
|
|
365
|
+
children: /* @__PURE__ */ jsx(Switch, {
|
|
366
|
+
checked: value === true,
|
|
367
|
+
onCheckedChange: onChange,
|
|
368
|
+
label: field.label,
|
|
369
|
+
description: field.description
|
|
370
|
+
})
|
|
371
|
+
});
|
|
372
|
+
const text = typeof value === "string" ? value : "";
|
|
373
|
+
const fallback = field.fallback === void 0 || field.fallback === null ? void 0 : displayValue(field.fallback);
|
|
374
|
+
if (field.kind === "choice-inline") return /* @__PURE__ */ jsx(Field, {
|
|
375
|
+
as: "group",
|
|
376
|
+
label: field.label,
|
|
377
|
+
description: field.description,
|
|
378
|
+
required: field.required,
|
|
379
|
+
className: span,
|
|
380
|
+
children: /* @__PURE__ */ jsx(SegmentedControl, {
|
|
381
|
+
"aria-label": field.label,
|
|
382
|
+
value: text,
|
|
383
|
+
defaultValue: fallback,
|
|
384
|
+
options: field.options,
|
|
385
|
+
onValueChange: onChange
|
|
386
|
+
})
|
|
387
|
+
});
|
|
388
|
+
if (field.kind === "choice") return /* @__PURE__ */ jsx(Field, {
|
|
389
|
+
as: "group",
|
|
390
|
+
label: field.label,
|
|
391
|
+
description: field.description,
|
|
392
|
+
required: field.required,
|
|
393
|
+
className: span,
|
|
394
|
+
children: /* @__PURE__ */ jsx(Select, {
|
|
395
|
+
"aria-label": field.label,
|
|
396
|
+
value: text,
|
|
397
|
+
onValueChange: onChange,
|
|
398
|
+
options: field.options,
|
|
399
|
+
placeholder: fallback === void 0 ? "Choose…" : `Default · ${fallback}`,
|
|
400
|
+
unsetLabel: fallback === void 0 ? void 0 : `Default · ${fallback}`
|
|
401
|
+
})
|
|
402
|
+
});
|
|
403
|
+
return /* @__PURE__ */ jsx(Field, {
|
|
404
|
+
label: field.label,
|
|
405
|
+
description: field.kind === "string-list" ? [field.description, "Comma-separated."].filter(Boolean).join(" ") : field.description,
|
|
406
|
+
required: field.required,
|
|
407
|
+
annotation: field.range,
|
|
408
|
+
className: span,
|
|
409
|
+
children: field.kind === "json" ? /* @__PURE__ */ jsx(Textarea, {
|
|
410
|
+
rows: 2,
|
|
411
|
+
"aria-label": field.label,
|
|
412
|
+
placeholder: field.placeholder,
|
|
413
|
+
value: text,
|
|
414
|
+
onChange: (event) => onChange(event.target.value)
|
|
415
|
+
}) : field.kind === "number" ? /* @__PURE__ */ jsx(NumberInput, {
|
|
416
|
+
"aria-label": field.label,
|
|
417
|
+
placeholder: field.placeholder,
|
|
418
|
+
min: field.min,
|
|
419
|
+
max: field.max,
|
|
420
|
+
step: field.step,
|
|
421
|
+
value: text,
|
|
422
|
+
onChange: (event) => onChange(event.target.value)
|
|
423
|
+
}) : /* @__PURE__ */ jsx(Input, {
|
|
424
|
+
className: "font-mono",
|
|
425
|
+
"aria-label": field.label,
|
|
426
|
+
placeholder: field.placeholder,
|
|
427
|
+
value: text,
|
|
428
|
+
onChange: (event) => onChange(event.target.value)
|
|
429
|
+
})
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
//#endregion
|
|
433
|
+
export { SchemaForm, describeFields, initialValues, jsonErrors, missingRequired, outOfRange, overrideCount, toOptions };
|
|
434
|
+
|
|
435
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/api/schemaForm.ts","../src/components/SchemaForm.tsx"],"sourcesContent":["/**\n * Turning a JSON Schema into a form, and a form back into options.\n *\n * An options model — a plugin's config, an algorithm's parameters — is meant to *drive*\n * the UI form rather than have the form hardcoded and the schema's defaults merely hoped\n * to agree with it. That stops being survivable the moment an option is *required* and\n * nobody can type it.\n *\n * The logic lives here rather than in the component because it is where the decisions\n * are: what control a schema node deserves, what an empty box means, and when a value is\n * worth sending at all. Rendering is the easy half.\n *\n * Only the shapes pydantic (or an equivalent JSON Schema generator) actually emits are\n * handled. A node this does not recognise becomes a JSON textarea rather than\n * disappearing — a caller can always express what the model accepts, even where the form\n * has nothing prettier to offer.\n *\n * ## Two things worth reading before touching `kindOf`\n *\n * `enum` has to be tested *before* `type`: `Literal[\"small\", \"medium\"]` emits `enum`\n * *alongside* `type: \"string\"`, and testing type first turns a closed set into a free text\n * box whose placeholder is the only hint of what is accepted. A `StrEnum` is worse —\n * pydantic emits it as `{\"$ref\": \"#/$defs/ColorMode\"}` with the default and description as\n * siblings, and `$ref` is not in the type at all, so it falls through every branch to a\n * JSON textarea unless `$defs` is resolved first.\n *\n * Numeric bounds are the same story: a field can carry `minimum`/`maximum` in the schema,\n * and a control that ignores them turns an out-of-range value into a backend rejection\n * reported nowhere near the field that caused it.\n */\n\n/** The subset of JSON Schema that pydantic (or an equivalent generator) emits for an options model. */\nexport type SchemaNode = {\n type?: string;\n title?: string;\n description?: string;\n default?: unknown;\n items?: SchemaNode;\n anyOf?: SchemaNode[];\n additionalProperties?: SchemaNode | boolean;\n /** A nested model's own fields. Recognised only so such a node is not mistaken for one\n * of the shapes above; it still renders as a JSON textarea. */\n properties?: Record<string, SchemaNode>;\n enum?: unknown[];\n $ref?: string;\n minimum?: number;\n maximum?: number;\n exclusiveMinimum?: number;\n exclusiveMaximum?: number;\n multipleOf?: number;\n /** The schema's own answer to \"does the caller have to think about this?\" — pydantic's\n * `json_schema_extra={\"x-primary\": True}`. Overrides the default-based guess in both\n * directions; a required field is primary regardless. */\n \"x-primary\"?: boolean;\n};\n\n/**\n * An options model's JSON Schema, as pydantic or schemars emits it for a whole model: the\n * top-level object whose `properties` become the form's fields.\n */\nexport type OptionsSchema = {\n properties?: Record<string, SchemaNode>;\n required?: string[];\n /** Where an `Enum` or a nested model lands. Pydantic never inlines those. */\n $defs?: Record<string, SchemaNode>;\n};\n\n/** Which control a field is rendered as — decided from its schema node by `describeFields`. */\nexport type FieldKind =\n | \"text\"\n | \"number\"\n | \"boolean\"\n | \"string-list\"\n | \"json\"\n /** A closed set, few enough to show all at once. */\n | \"choice-inline\"\n /** A closed set, too many to show at once. */\n | \"choice\";\n\n/** One entry of a closed set: the value as sent (stringified) and the label as shown. */\nexport type ChoiceOption = { value: string; label: string };\n\n/**\n * Everything the form needs to render one field, derived once from the schema by\n * `describeFields` so the component makes no decisions of its own.\n */\nexport type FieldSpec = {\n name: string;\n label: string;\n description: string | null;\n kind: FieldKind;\n required: boolean;\n /** Rendered into the empty control as a hint, never submitted on the caller's behalf. */\n placeholder: string;\n /** What applies when the field is left empty. Shown, not sent. */\n fallback: unknown;\n /** Behind a disclosure: there is already a working answer for this one. */\n advanced: boolean;\n /** The closed set, in schema order. Empty for every kind but the two choices. */\n options: ChoiceOption[];\n /** True when a choice's values are numbers, so the chosen one is sent as one. */\n numeric: boolean;\n /** Bounds the control can enforce. `\"any\"` step frees a float from the default of 1. */\n min?: number;\n max?: number;\n step?: number | \"any\" | undefined;\n /** The bounds as a reader should see them, exclusive ones included. */\n range: string | null;\n};\n\nconst DEFS_PREFIX = \"#/$defs/\";\n\n/**\n * Follow a `$ref` into `$defs`.\n *\n * The merge order matters and is the opposite of the obvious one. Pydantic writes the\n * field's `default` and `description` *beside* the `$ref`, while the target carries the\n * enum's own `title` and sometimes its own docstring as a `description`. The field's\n * statement about itself wins; the type's is the fallback.\n */\nfunction deref(node: SchemaNode, defs: Record<string, SchemaNode> | undefined): SchemaNode {\n if (node.$ref === undefined) return node;\n if (!node.$ref.startsWith(DEFS_PREFIX)) return node;\n\n const target = defs?.[node.$ref.slice(DEFS_PREFIX.length)];\n if (target === undefined) return node;\n\n const rest: SchemaNode = { ...node };\n delete rest.$ref;\n return { ...target, ...rest };\n}\n\n/**\n * Look through pydantic's `str | None` encoding to the type that carries the meaning.\n *\n * An optional string arrives as `anyOf: [{type: \"string\"}, {type: \"null\"}]`, and treating\n * that as an unrecognised node would give every nullable option a JSON textarea.\n */\nfunction unwrap(node: SchemaNode): SchemaNode {\n if (!node.anyOf) return node;\n const meaningful = node.anyOf.filter((branch) => branch.type !== \"null\");\n return meaningful.length === 1 ? { ...node, ...meaningful[0] } : node;\n}\n\n/**\n * The node that says what this field *is*.\n *\n * Dereferenced on both sides of the unwrap because either can hold the `$ref`: a plain\n * `ColorMode` is a bare `$ref`, while an `Optional[ColorMode]` hides one inside `anyOf`.\n */\nfunction resolve(node: SchemaNode, defs: Record<string, SchemaNode> | undefined): SchemaNode {\n return deref(unwrap(deref(node, defs)), defs);\n}\n\nfunction kindOf(resolved: SchemaNode): FieldKind {\n // Before `type`, always: a `Literal` carries both, and the closed set is the stronger\n // statement of the two.\n if (Array.isArray(resolved.enum) && resolved.enum.length > 0) {\n return resolved.enum.length <= 3 ? \"choice-inline\" : \"choice\";\n }\n if (resolved.type === \"boolean\") return \"boolean\";\n if (resolved.type === \"integer\" || resolved.type === \"number\") return \"number\";\n if (resolved.type === \"string\") return \"text\";\n if (resolved.type === \"array\" && unwrap(resolved.items ?? {}).type === \"string\") {\n return \"string-list\";\n }\n return \"json\";\n}\n\n/**\n * Which options a caller has to think about, and which already have an answer.\n *\n * The schema says this without anyone maintaining a second list: a field whose default is\n * *empty* — `[]`, `null`, `\"\"`, or absent entirely — is one nothing can do anything useful\n * with until it is filled in. A field with a real default already works, and someone\n * filling in the form should not have to read past nine vocabulary options to answer the\n * question that actually needs an answer.\n */\nfunction isAdvanced(node: SchemaNode): boolean {\n const fallback = node.default;\n if (fallback === undefined || fallback === null) return false;\n if (Array.isArray(fallback)) return fallback.length > 0;\n if (fallback === \"\") return false;\n if (typeof fallback === \"object\") return Object.keys(fallback).length > 0;\n return true;\n}\n\n/**\n * A schema that marks its decisions says so with `x-primary`; one that does not is read by\n * its defaults. The guess works for an adapter, whose open questions have empty defaults,\n * and fails for a model's hyperparameters, where *every* field has a working default and the\n * two that matter are indistinguishable from the fifteen that do not.\n *\n * Read from the raw node first: a `$ref` field carries its extras beside the reference.\n */\nfunction isPrimary(node: SchemaNode, resolved: SchemaNode): boolean {\n const hint = node[\"x-primary\"] ?? resolved[\"x-primary\"];\n if (hint !== undefined) return hint;\n return !isAdvanced(resolved);\n}\n\n/** Raw control state: what the operator chose, not what will be sent. */\nexport type RawValues = Record<string, string | boolean>;\n\n/**\n * Humanise `normal_dirs` into `Normal dirs`.\n *\n * Pydantic emits a `title` for every field whether or not anyone wrote one, and its\n * generated form is Title Case — `Csv Path`, `Defect Type From Dir` — which reads like a\n * spreadsheet header rather than a form label. So a title is used only when it is *not*\n * the one pydantic would have generated, which is exactly when a human chose it.\n *\n * Read from the *unresolved* node on purpose. Dereferencing pulls in the enum class's own\n * title, so a resolved `color` field would be labelled \"ColorMode\".\n */\nfunction labelFor(name: string, node: SchemaNode): string {\n const words = name.split(\"_\");\n const generated = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(\" \");\n if (node.title && node.title !== generated) return node.title;\n\n const spaced = words.join(\" \");\n return spaced.charAt(0).toUpperCase() + spaced.slice(1);\n}\n\n/**\n * A value as a person reads it: scalars as themselves, anything structured as JSON rather\n * than `[object Object]`.\n */\nexport function displayValue(value: unknown): string {\n if (value === null || value === undefined) return \"\";\n if (typeof value === \"string\") return value;\n if (typeof value === \"number\" || typeof value === \"boolean\" || typeof value === \"bigint\") return value.toString();\n return JSON.stringify(value) ?? \"\";\n}\n\nfunction placeholderFor(kind: FieldKind, fallback: unknown): string {\n if (kind === \"string-list\") {\n return Array.isArray(fallback) && fallback.length > 0 ? fallback.join(\", \") : \"a, b, c\";\n }\n if (kind === \"json\") return \"{}\";\n return displayValue(fallback);\n}\n\n/** `rgb` reads better as `Rgb` in a picker, but `bilinear` must not become `Bi Linear`. */\nfunction optionLabel(value: unknown): string {\n const text = String(value);\n return text.charAt(0).toUpperCase() + text.slice(1);\n}\n\nfunction optionsFor(resolved: SchemaNode, kind: FieldKind): ChoiceOption[] {\n if (kind !== \"choice\" && kind !== \"choice-inline\") return [];\n return (resolved.enum ?? []).map((value) => ({\n value: String(value),\n label: optionLabel(value),\n }));\n}\n\n/** The bounds as prose, keeping the distinction the HTML control cannot express. */\nfunction rangeOf(resolved: SchemaNode): string | null {\n const parts: string[] = [];\n if (resolved.minimum !== undefined) parts.push(`≥ ${resolved.minimum}`);\n else if (resolved.exclusiveMinimum !== undefined) parts.push(`> ${resolved.exclusiveMinimum}`);\n if (resolved.maximum !== undefined) parts.push(`≤ ${resolved.maximum}`);\n else if (resolved.exclusiveMaximum !== undefined) parts.push(`< ${resolved.exclusiveMaximum}`);\n return parts.length > 0 ? parts.join(\", \") : null;\n}\n\nfunction stepOf(resolved: SchemaNode): number | \"any\" | undefined {\n if (resolved.multipleOf !== undefined) return resolved.multipleOf;\n if (resolved.type === \"integer\") return 1;\n // A number input steps by 1 unless told otherwise, which marks a learning rate of 0.0001\n // invalid and refuses to submit it.\n return \"any\";\n}\n\n/**\n * Derive the form's fields from an options schema, in schema order.\n *\n * Resolves `$ref`s into `$defs`, looks through `anyOf: [T, null]`, picks a control per node\n * (see `FieldKind`), and folds a field under \"Advanced\" when it is optional and its default\n * already works — unless the schema says otherwise with `\"x-primary\"`.\n *\n * @param schema - The model's JSON Schema (draft 2020-12, as pydantic or schemars emit it).\n * @returns One `FieldSpec` per property; empty for a model with no options.\n */\nexport function describeFields(schema: OptionsSchema): FieldSpec[] {\n const required = new Set(schema.required ?? []);\n const defs = schema.$defs;\n\n return Object.entries(schema.properties ?? {}).map(([name, node]) => {\n const resolved = resolve(node, defs);\n const kind = kindOf(resolved);\n const numeric =\n (kind === \"choice\" || kind === \"choice-inline\") &&\n (resolved.type === \"integer\" || resolved.type === \"number\");\n\n return {\n name,\n // From the raw node: the resolved one carries the enum type's title, not the field's.\n label: labelFor(name, node),\n description: node.description ?? resolved.description ?? null,\n kind,\n required: required.has(name),\n fallback: resolved.default,\n placeholder: placeholderFor(kind, resolved.default),\n advanced: !required.has(name) && !isPrimary(node, resolved),\n options: optionsFor(resolved, kind),\n numeric,\n ...(kind === \"number\"\n ? {\n min: resolved.minimum ?? resolved.exclusiveMinimum,\n max: resolved.maximum ?? resolved.exclusiveMaximum,\n step: stepOf(resolved),\n }\n : {}),\n range: kind === \"number\" ? rangeOf(resolved) : null,\n };\n });\n}\n\n/**\n * The controls' starting state.\n *\n * Text, list and choice controls start **empty even when the schema has a default**, and\n * that is deliberate: a pre-filled box says \"this is your value\" when what is true is \"this\n * is what happens if you say nothing\". Pre-filling would also mean every run sent every\n * default back, so a later change to a default would silently not reach anyone who had\n * once opened the form. The default is shown as the placeholder instead — and for a\n * segmented control, as the segment already highlighted.\n *\n * Booleans are the exception — a checkbox has no empty state, so it starts at the\n * default and is always sent.\n */\nexport function initialValues(fields: FieldSpec[]): RawValues {\n const values: RawValues = {};\n for (const field of fields) {\n values[field.name] = field.kind === \"boolean\" ? field.fallback === true : \"\";\n }\n return values;\n}\n\n/**\n * Build the options object to send.\n *\n * An untouched control contributes **nothing**, so the receiving side's own default\n * applies and stays the single definition of it. That is what keeps a default from\n * existing twice, once here and once wherever it is enforced, with the two free to drift.\n */\nexport function toOptions(fields: FieldSpec[], values: RawValues): Record<string, unknown> {\n const options: Record<string, unknown> = {};\n\n for (const field of fields) {\n const raw = values[field.name];\n\n if (field.kind === \"boolean\") {\n // Always sent: a checkbox cannot express \"unset\", so leaving it out would make its\n // rendered state and the value in force disagree whenever the default is `true`.\n options[field.name] = raw === true;\n continue;\n }\n\n const text = typeof raw === \"string\" ? raw.trim() : \"\";\n if (text === \"\") continue;\n\n switch (field.kind) {\n case \"string-list\":\n options[field.name] = text\n .split(\",\")\n .map((item) => item.trim())\n .filter(Boolean);\n break;\n case \"number\": {\n const parsed = Number(text);\n if (Number.isFinite(parsed)) options[field.name] = parsed;\n break;\n }\n case \"choice\":\n case \"choice-inline\": {\n // `Literal[1, 2, 3]` is a closed set whose members are numbers; the control can\n // only hand back a string, so the schema's own type decides what is sent.\n if (!field.numeric) {\n options[field.name] = text;\n break;\n }\n const parsed = Number(text);\n if (Number.isFinite(parsed)) options[field.name] = parsed;\n break;\n }\n case \"json\":\n try {\n options[field.name] = JSON.parse(text);\n } catch {\n // Left out rather than sent as a string: the receiving side would reject it with\n // a less useful message than the one `jsonErrors` puts next to the field.\n }\n break;\n default:\n options[field.name] = text;\n }\n }\n\n return options;\n}\n\n/** How many options the operator has actually changed, for the tab that hides them. */\nexport function overrideCount(fields: FieldSpec[], values: RawValues): number {\n const initial = initialValues(fields);\n return fields.filter((field) => values[field.name] !== initial[field.name]).length;\n}\n\n/** Which JSON fields do not parse, so the form can say so before the run starts. */\nexport function jsonErrors(fields: FieldSpec[], values: RawValues): string[] {\n return fields\n .filter((field) => field.kind === \"json\")\n .filter((field) => {\n const raw = values[field.name];\n if (typeof raw !== \"string\" || raw.trim() === \"\") return false;\n try {\n JSON.parse(raw);\n return false;\n } catch {\n return true;\n }\n })\n .map((field) => field.label);\n}\n\n/** Required fields the operator has not filled in. Blocks the run. */\nexport function missingRequired(fields: FieldSpec[], values: RawValues): string[] {\n return fields\n .filter((field) => field.required)\n .filter((field) => {\n const raw = values[field.name];\n if (typeof raw === \"boolean\") return false;\n return (raw ?? \"\").trim() === \"\";\n })\n .map((field) => field.label);\n}\n\n/**\n * Fields whose typed value is outside the bounds the schema states.\n *\n * Native `min`/`max` stop the spinner and mark the input invalid, but nothing stops a\n * paste, so the check is repeated here where it can block the button.\n */\nexport function outOfRange(fields: FieldSpec[], values: RawValues): string[] {\n return fields\n .filter((field) => field.kind === \"number\")\n .filter((field) => {\n const raw = values[field.name];\n if (typeof raw !== \"string\" || raw.trim() === \"\") return false;\n const parsed = Number(raw);\n if (!Number.isFinite(parsed)) return true;\n if (field.min !== undefined && parsed < field.min) return true;\n if (field.max !== undefined && parsed > field.max) return true;\n return false;\n })\n .map((field) => field.label);\n}\n","/**\n * An options form, generated from a JSON Schema.\n *\n * Adding an adapter, a model, or a measurement method costs a config module and a registry\n * entry, and *nothing here* — that is the point of generating the form from the schema\n * instead of hardcoding it. Every decision about which control a field deserves lives in\n * `api/schemaForm.ts`; this file only renders.\n *\n * Two behaviours are worth knowing while reading the markup:\n *\n * * An empty control means **unset**, and the caller's own default applies. The default\n * is shown — as a placeholder, as the pre-highlighted segment, or as the picker's\n * leading entry — so a reader can see what happens if they say nothing, without that\n * value being claimed as their choice.\n * * A field the schema requires is marked, and the caller blocks the run on it.\n *\n * The layout is a two-column grid: a form of eight numeric parameters, each a full-width\n * control over a full-width description, runs to screens of scrolling for a form that\n * often needs nothing touched at all. Fields whose value is genuinely wide — a list, a\n * JSON object, a statement with a switch — still span both columns.\n */\n\nimport {\n Disclosure,\n Field,\n NumberInput,\n SegmentedControl,\n Select,\n Switch,\n Textarea,\n Input,\n} from \"@vitavision/ui\";\nimport { displayValue, type FieldSpec, type RawValues } from \"../api/schemaForm\";\n\n/**\n * Which kinds are worth a full row. Everything else pairs up.\n *\n * A JSON object needs the width to be editable at all, and a switch reads as a sentence\n * with its explanation beside it. A comma-separated list does *not* qualify: three of them\n * in a row can run as tall as the layout this rule exists to avoid.\n */\nconst WIDE: ReadonlySet<FieldSpec[\"kind\"]> = new Set([\"json\", \"boolean\"]);\n\n/**\n * An options form rendered from `describeFields(schema)`: primary fields first, the rest\n * folded under \"Advanced\", each with the control its schema node calls for.\n *\n * Controlled: `values` holds the raw control values (strings, or booleans for switches),\n * and an empty string means \"unset — the schema's default applies\". Turn them into the\n * options to send with `toOptions`, and gate a run on `missingRequired`, `outOfRange` and\n * `jsonErrors`.\n */\nexport function SchemaForm({\n fields,\n values,\n onChange,\n}: {\n fields: FieldSpec[];\n values: RawValues;\n onChange: (next: RawValues) => void;\n}) {\n if (fields.length === 0) {\n return <p className=\"text-sm text-fg-muted\">This plugin takes no options.</p>;\n }\n\n const set = (name: string, value: string | boolean) => onChange({ ...values, [name]: value });\n const render = (field: FieldSpec) => (\n <SchemaField\n key={field.name}\n field={field}\n value={values[field.name] ?? \"\"}\n onChange={(value) => set(field.name, value)}\n />\n );\n\n const primary = fields.filter((field) => !field.advanced);\n const advanced = fields.filter((field) => field.advanced);\n\n // Where nothing is required — the normal shape of a model's own hyperparameters, as\n // against an adapter's \"which folder holds the good images\" — there is nothing to fold\n // away from. Hiding every field behind a disclosure would leave a box containing only a\n // disclosure, so they simply render.\n if (primary.length === 0) {\n return <Grid>{advanced.map(render)}</Grid>;\n }\n\n return (\n <div className=\"flex flex-col gap-4\">\n <Grid>{primary.map(render)}</Grid>\n {advanced.length > 0 && (\n <Disclosure summary=\"Advanced\" count={advanced.length}>\n <Grid>{advanced.map(render)}</Grid>\n </Disclosure>\n )}\n </div>\n );\n}\n\nfunction Grid({ children }: { children: React.ReactNode }) {\n return <div className=\"grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2\">{children}</div>;\n}\n\nfunction SchemaField({\n field,\n value,\n onChange,\n}: {\n field: FieldSpec;\n value: string | boolean;\n onChange: (value: string | boolean) => void;\n}) {\n const span = WIDE.has(field.kind) ? \"sm:col-span-2\" : undefined;\n\n // A switch reads as a statement and carries its own label, rather than sitting under one.\n // It is also the one control with no \"unset\" position.\n if (field.kind === \"boolean\") {\n return (\n <div className={span}>\n <Switch\n checked={value === true}\n onCheckedChange={onChange}\n label={field.label}\n description={field.description}\n />\n </div>\n );\n }\n\n const text = typeof value === \"string\" ? value : \"\";\n const fallback = field.fallback === undefined || field.fallback === null\n ? undefined\n : displayValue(field.fallback);\n\n if (field.kind === \"choice-inline\") {\n return (\n <Field\n as=\"group\"\n label={field.label}\n description={field.description}\n required={field.required}\n className={span}\n >\n <SegmentedControl\n aria-label={field.label}\n value={text}\n defaultValue={fallback}\n options={field.options}\n onValueChange={onChange}\n />\n </Field>\n );\n }\n\n if (field.kind === \"choice\") {\n return (\n <Field\n as=\"group\"\n label={field.label}\n description={field.description}\n required={field.required}\n className={span}\n >\n <Select\n aria-label={field.label}\n value={text}\n onValueChange={onChange}\n options={field.options}\n placeholder={fallback === undefined ? \"Choose…\" : `Default · ${fallback}`}\n unsetLabel={fallback === undefined ? undefined : `Default · ${fallback}`}\n />\n </Field>\n );\n }\n\n return (\n <Field\n label={field.label}\n description={\n field.kind === \"string-list\"\n ? [field.description, \"Comma-separated.\"].filter(Boolean).join(\" \")\n : field.description\n }\n required={field.required}\n annotation={field.range}\n className={span}\n >\n {field.kind === \"json\" ? (\n <Textarea\n rows={2}\n aria-label={field.label}\n placeholder={field.placeholder}\n value={text}\n onChange={(event) => onChange(event.target.value)}\n />\n ) : field.kind === \"number\" ? (\n <NumberInput\n aria-label={field.label}\n placeholder={field.placeholder}\n min={field.min}\n max={field.max}\n step={field.step}\n value={text}\n onChange={(event) => onChange(event.target.value)}\n />\n ) : (\n <Input\n className=\"font-mono\"\n aria-label={field.label}\n placeholder={field.placeholder}\n value={text}\n onChange={(event) => onChange(event.target.value)}\n />\n )}\n </Field>\n );\n}\n"],"mappings":";;;AA8GA,MAAM,cAAc;;;;;;;;;AAUpB,SAAS,MAAM,MAAkB,MAA0D;CACzF,IAAI,KAAK,SAAS,KAAA,GAAW,OAAO;CACpC,IAAI,CAAC,KAAK,KAAK,WAAW,WAAW,GAAG,OAAO;CAE/C,MAAM,SAAS,OAAO,KAAK,KAAK,MAAM,CAAkB;CACxD,IAAI,WAAW,KAAA,GAAW,OAAO;CAEjC,MAAM,OAAmB,EAAE,GAAG,KAAK;CACnC,OAAO,KAAK;CACZ,OAAO;EAAE,GAAG;EAAQ,GAAG;CAAK;AAC9B;;;;;;;AAQA,SAAS,OAAO,MAA8B;CAC5C,IAAI,CAAC,KAAK,OAAO,OAAO;CACxB,MAAM,aAAa,KAAK,MAAM,QAAQ,WAAW,OAAO,SAAS,MAAM;CACvE,OA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GAClB,OAAO;GACP,cAAc;GACd,SAAS,MAAM;GACf,eAAe;EAChB,CAAA;CACI,CAAA;CAIX,IAAI,MAAM,SAAS,UACjB,OACE,oBAAC,OAAD;EACE,IAAG;EACH,OAAO,MAAM;EACb,aAAa,MAAM;EACnB,UAAU,MAAM;EAChB,WAAW;EAEX,UAAA,oBAAC,QAAD;GACE,cAAY,MAAM;GAClB,OAAO;GACP,eAAe;GACf,SAAS,MAAM;GACf,aAAa,aAAa,KAAA,IAAY,YAAY,aAAa;GAC/D,YAAY,aAAa,KAAA,IAAY,KAAA,IAAY,aAAa;EAC/D,CAAA;CACI,CAAA;CAIX,OACE,oBAAC,OAAD;EACE,OAAO,MAAM;EACb,aACE,MAAM,SAAS,gBACX,CAAC,MAAM,aAAa,kBAAkB,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG,IAChE,MAAM;EAEZ,UAAU,MAAM;EAChB,YAAY,MAAM;EAClB,WAAW;EAEV,UAAA,MAAM,SAAS,SACd,oBAAC,UAAD;GACE,MAAM;GACN,cAAY,MAAM;GAClB,aAAa,MAAM;GACnB,OAAO;GACP,WAAW,UAAU,SAAS,MAAM,OAAO,KAAK;EACjD,CAAA,IACC,MAAM,SAAS,WACjB,oBAAC,aAAD;GACE,cAAY,MAAM;GAClB,aAAa,MAAM;GACnB,KAAK,MAAM;GACX,KAAK,MAAM;GACX,MAAM,MAAM;GACZ,OAAO;GACP,WAAW,UAAU,SAAS,MAAM,OAAO,KAAK;EACjD,CAAA,IAED,oBAAC,OAAD;GACE,WAAU;GACV,cAAY,MAAM;GAClB,aAAa,MAAM;GACnB,OAAO;GACP,WAAW,UAAU,SAAS,MAAM,OAAO,KAAK;EACjD,CAAA;CAEE,CAAA;AAEX"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vitavision/forms",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "JSON Schema (draft 2020-12, schemars output) rendered as a vitavision options form.",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/VitalyVorobyev/lab-ui.git",
|
|
9
|
+
"directory": "packages/forms"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/VitalyVorobyev/lab-ui/tree/main/packages/forms#readme",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"sideEffects": [
|
|
14
|
+
"*.css"
|
|
15
|
+
],
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"!src/**/*.test.*",
|
|
20
|
+
"!src/**/*.stories.*",
|
|
21
|
+
"LICENSE-MIT",
|
|
22
|
+
"LICENSE-APACHE"
|
|
23
|
+
],
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"@vitavision/source": "./src/index.ts",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./styles.css": "./src/styles.css",
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsdown",
|
|
38
|
+
"typecheck": "tsc --noEmit",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"api": "api-extractor run --local",
|
|
41
|
+
"coverage": "vitest run --coverage",
|
|
42
|
+
"api:check": "api-extractor run"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": "^19.2.0",
|
|
46
|
+
"react-dom": "^19.2.0"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@vitavision/ui": "^0.6.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@storybook/react-vite": "10.6.0",
|
|
53
|
+
"storybook": "10.6.0"
|
|
54
|
+
}
|
|
55
|
+
}
|