@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 +56 -2
- package/dist/index.js +121 -19
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/api/uiSchema.ts +62 -0
- package/src/components/SchemaValueForm.tsx +28 -13
- package/src/components/valueForm/chrome.tsx +34 -5
- package/src/components/valueForm/context.ts +5 -1
- package/src/components/valueForm/leaves.tsx +47 -5
- package/src/components/valueForm/nodes.tsx +9 -9
- package/src/index.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitavision/forms",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.5",
|
|
4
4
|
"description": "JSON Schema (draft 2020-12, schemars output) rendered as a vitavision options form.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"react-dom": "^19.2.0"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@vitavision/ui": "^0.14.
|
|
50
|
+
"@vitavision/ui": "^0.14.3"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@storybook/react-vite": "10.6.0",
|
package/src/api/uiSchema.ts
CHANGED
|
@@ -12,6 +12,14 @@ import type { ReactNode } from "react";
|
|
|
12
12
|
|
|
13
13
|
import type { JsonSchema } from "./schemaNode";
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Where a field's label goes. `top` stacks the label over the control. `side` is the
|
|
17
|
+
* property-sheet row: the label in a fixed column on the left, the control on the right,
|
|
18
|
+
* about 32 px tall, with the description behind the hint icon. Objects, unions, tuples and
|
|
19
|
+
* lists keep their label above and span the full width either way.
|
|
20
|
+
*/
|
|
21
|
+
export type LabelPlacement = "top" | "side";
|
|
22
|
+
|
|
15
23
|
/** A titled set of fields, rendered together. */
|
|
16
24
|
export interface UiGroup {
|
|
17
25
|
/** Stable id, used for keys and test hooks. */
|
|
@@ -28,6 +36,16 @@ export interface UiGroup {
|
|
|
28
36
|
collapsible?: boolean | undefined;
|
|
29
37
|
/** Whether a collapsible group starts open. Defaults to closed. */
|
|
30
38
|
defaultOpen?: boolean | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Lay the group's fields out on one row, each with its label above its control, instead
|
|
41
|
+
* of the form's grid: for a few small numbers that read as one setting (rows × columns ×
|
|
42
|
+
* square size; iterations and tolerances). Give each field a short `label` in `fields`
|
|
43
|
+
* of the `UiSchema`. A row has no room for descriptions or range notes, so they are
|
|
44
|
+
* dropped, and a field that is not a leaf (an object, a list) should stay out of it.
|
|
45
|
+
*/
|
|
46
|
+
inline?: boolean | undefined;
|
|
47
|
+
/** This group's label placement, over the form's. */
|
|
48
|
+
labelPlacement?: LabelPlacement | undefined;
|
|
31
49
|
/** A line of explanation under the heading. */
|
|
32
50
|
hint?: ReactNode;
|
|
33
51
|
}
|
|
@@ -61,6 +79,17 @@ export interface FieldUi {
|
|
|
61
79
|
* `{ brown_conrady5: "Brown-Conrady" }`.
|
|
62
80
|
*/
|
|
63
81
|
enumLabels?: Record<string, string> | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* For an object (or a union's variant): the order of its fields, by key. Keys not listed
|
|
84
|
+
* follow in schema order, and keys that do not exist are ignored. Use it where the
|
|
85
|
+
* schema's own order is not the one to read in (a schema whose keys arrive sorted).
|
|
86
|
+
*/
|
|
87
|
+
order?: string[] | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* For an object: lay its fields out on one row with their labels above, like a group's
|
|
90
|
+
* `inline`. The object keeps its frame and label.
|
|
91
|
+
*/
|
|
92
|
+
inline?: boolean | undefined;
|
|
64
93
|
/** Columns the field takes in the grid. Containers and lists default to the full row. */
|
|
65
94
|
span?: 1 | 2 | undefined;
|
|
66
95
|
/**
|
|
@@ -105,6 +134,23 @@ export interface UiSchema {
|
|
|
105
134
|
groups?: UiGroup[] | undefined;
|
|
106
135
|
/** Per-field presentation, by dot path. */
|
|
107
136
|
fields?: Record<string, FieldUi> | undefined;
|
|
137
|
+
/**
|
|
138
|
+
* The order of the root object's ungrouped fields, by key. Keys not listed follow in
|
|
139
|
+
* schema order. (Fields inside a group follow the group's `fields`; a nested object's
|
|
140
|
+
* order is `FieldUi.order`.)
|
|
141
|
+
*/
|
|
142
|
+
order?: string[] | undefined;
|
|
143
|
+
/**
|
|
144
|
+
* Where labels go, for the whole form: `top` (the default) or `side`. A group can
|
|
145
|
+
* override it, and so can the form's prop, which wins over this.
|
|
146
|
+
*/
|
|
147
|
+
labelPlacement?: LabelPlacement | undefined;
|
|
148
|
+
/**
|
|
149
|
+
* Where descriptions go for every field the app did not say otherwise about (see
|
|
150
|
+
* `FieldUi.descriptionAs`). Defaults to `hint`, the same in both placements: a long
|
|
151
|
+
* description is a tooltip, never a line in the form.
|
|
152
|
+
*/
|
|
153
|
+
descriptionAs?: "hint" | "inline" | "none" | undefined;
|
|
108
154
|
}
|
|
109
155
|
|
|
110
156
|
/** What `renderField` is given for a field. */
|
|
@@ -146,3 +192,19 @@ export function fieldUiAt(ui: UiSchema | undefined, path: string): FieldUi {
|
|
|
146
192
|
if (general === undefined) return exact ?? NO_UI;
|
|
147
193
|
return exact === undefined ? general : { ...general, ...exact };
|
|
148
194
|
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Put the keys an app listed first, in its order, and the rest after them in their own.
|
|
198
|
+
*
|
|
199
|
+
* @param fields - Items with a `key`, in schema order.
|
|
200
|
+
* @param order - The keys to bring forward; unknown keys are ignored.
|
|
201
|
+
* @returns A new array when `order` changes anything, otherwise `fields` itself.
|
|
202
|
+
*/
|
|
203
|
+
export function orderFields<T extends { key: string }>(fields: T[], order: readonly string[] | undefined): T[] {
|
|
204
|
+
if (order === undefined || order.length === 0) return fields;
|
|
205
|
+
const rank = new Map<string, number>();
|
|
206
|
+
for (const key of order) if (!rank.has(key)) rank.set(key, rank.size);
|
|
207
|
+
const listed = fields.filter((field) => rank.has(field.key)).sort((a, b) => (rank.get(a.key) ?? 0) - (rank.get(b.key) ?? 0));
|
|
208
|
+
const rest = fields.filter((field) => !rank.has(field.key));
|
|
209
|
+
return [...listed, ...rest];
|
|
210
|
+
}
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { Disclosure, Section, TooltipProvider, cn } from "@vitavision/ui";
|
|
13
|
-
import { useMemo, useState, type Ref } from "react";
|
|
13
|
+
import { useMemo, useState, type CSSProperties, type Ref } from "react";
|
|
14
14
|
|
|
15
15
|
import type { JsonSchema } from "../api/schemaNode";
|
|
16
16
|
import { defaultValueForSchema, fieldAt, shapeOf } from "../api/schemaValue";
|
|
17
|
-
import { fieldUiAt, type RenderField, type UiGroup, type UiSchema } from "../api/uiSchema";
|
|
17
|
+
import { fieldUiAt, type LabelPlacement, type RenderField, type UiGroup, type UiSchema } from "../api/uiSchema";
|
|
18
18
|
import { changeCounts, diffPaths } from "../api/valueDiff";
|
|
19
19
|
import { setAtPath } from "../api/valuePath";
|
|
20
|
-
import { Grid } from "./valueForm/chrome";
|
|
20
|
+
import { Grid, InlineRow } from "./valueForm/chrome";
|
|
21
21
|
import { FormContext, useForm, type FieldMessage, type FormState } from "./valueForm/context";
|
|
22
22
|
import { FieldNode, ObjectBody } from "./valueForm/nodes";
|
|
23
23
|
|
|
@@ -68,6 +68,15 @@ export interface SchemaValueFormProps {
|
|
|
68
68
|
messages?: FieldMessages | undefined;
|
|
69
69
|
/** `2` (the default) puts fields side by side from `sm:` up; `1` is one column, for a narrow rail. */
|
|
70
70
|
columns?: 1 | 2 | undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Where labels go: `top` (the default) stacks each label over its control; `side` is the
|
|
73
|
+
* property-sheet row, the label in a fixed column on the left (truncated, the full text
|
|
74
|
+
* in its `title`) and the control on the right, about 32 px tall, with the description
|
|
75
|
+
* behind the hint icon. Containers, unions, tuples and lists keep their label above
|
|
76
|
+
* either way. Wins over `ui.labelPlacement`; a group's own `labelPlacement` wins over
|
|
77
|
+
* both for its fields. Works with both `columns`.
|
|
78
|
+
*/
|
|
79
|
+
labelPlacement?: LabelPlacement | undefined;
|
|
71
80
|
/** Blocks every control. The value is still shown. */
|
|
72
81
|
disabled?: boolean | undefined;
|
|
73
82
|
/**
|
|
@@ -126,6 +135,7 @@ export function SchemaValueForm({
|
|
|
126
135
|
messages,
|
|
127
136
|
ui = NO_UI,
|
|
128
137
|
columns = 2,
|
|
138
|
+
labelPlacement,
|
|
129
139
|
disabled = false,
|
|
130
140
|
renderField,
|
|
131
141
|
className,
|
|
@@ -153,6 +163,8 @@ export function SchemaValueForm({
|
|
|
153
163
|
ui,
|
|
154
164
|
disabled,
|
|
155
165
|
columns,
|
|
166
|
+
labelPlacement: labelPlacement ?? ui.labelPlacement ?? "top",
|
|
167
|
+
descriptionAs: ui.descriptionAs ?? "hint",
|
|
156
168
|
renderField,
|
|
157
169
|
claimed,
|
|
158
170
|
baseline: compared,
|
|
@@ -169,7 +181,9 @@ export function SchemaValueForm({
|
|
|
169
181
|
<div
|
|
170
182
|
ref={ref}
|
|
171
183
|
data-columns={columns}
|
|
184
|
+
data-label-placement={state.labelPlacement}
|
|
172
185
|
data-disabled={disabled ? "" : undefined}
|
|
186
|
+
style={{ "--field-label-w": columns === 2 ? "7.5rem" : "9rem" } as CSSProperties}
|
|
173
187
|
className={cn("flex min-w-0 flex-col gap-5", className)}
|
|
174
188
|
>
|
|
175
189
|
{formMessage !== undefined && (
|
|
@@ -195,7 +209,8 @@ export function SchemaValueForm({
|
|
|
195
209
|
|
|
196
210
|
/** A `UiGroup`: its fields under a heading, folded away if the app said so. */
|
|
197
211
|
function GroupBlock({ group }: { group: UiGroup }) {
|
|
198
|
-
const
|
|
212
|
+
const form = useForm();
|
|
213
|
+
const { schema, value, ui } = form;
|
|
199
214
|
const fields = group.fields.flatMap((path) => {
|
|
200
215
|
const info = fieldAt(schema, path, schema, value);
|
|
201
216
|
if (info === undefined || fieldUiAt(ui, path).hidden === true) return [];
|
|
@@ -203,25 +218,25 @@ function GroupBlock({ group }: { group: UiGroup }) {
|
|
|
203
218
|
});
|
|
204
219
|
if (fields.length === 0) return null;
|
|
205
220
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
221
|
+
// The listed order is the rendered order: `fields` is rendered as given, never re-sorted by the schema.
|
|
222
|
+
const cells = fields.map(({ path, info }) => (
|
|
223
|
+
<FieldNode key={path} path={path} name={path.split(".").at(-1) ?? path} node={info.schema} required={info.required} />
|
|
224
|
+
));
|
|
225
|
+
const body = group.inline === true ? <InlineRow>{cells}</InlineRow> : <Grid>{cells}</Grid>;
|
|
226
|
+
const scoped =
|
|
227
|
+
group.labelPlacement === undefined ? body : <FormContext value={{ ...form, labelPlacement: group.labelPlacement }}>{body}</FormContext>;
|
|
213
228
|
|
|
214
229
|
if (group.collapsible === true) {
|
|
215
230
|
return (
|
|
216
231
|
<Disclosure summary={group.title} count={fields.length} defaultOpen={group.defaultOpen ?? false}>
|
|
217
232
|
{group.hint !== undefined && <p className="mb-3 text-xs text-fg-muted">{group.hint}</p>}
|
|
218
|
-
{
|
|
233
|
+
{scoped}
|
|
219
234
|
</Disclosure>
|
|
220
235
|
);
|
|
221
236
|
}
|
|
222
237
|
return (
|
|
223
238
|
<Section title={group.title} hint={group.hint}>
|
|
224
|
-
{
|
|
239
|
+
{scoped}
|
|
225
240
|
</Section>
|
|
226
241
|
);
|
|
227
242
|
}
|
|
@@ -9,7 +9,7 @@ import { use, type ReactNode } from "react";
|
|
|
9
9
|
|
|
10
10
|
import { firstParagraph } from "../../api/firstParagraph";
|
|
11
11
|
import type { JsonSchema } from "../../api/schemaNode";
|
|
12
|
-
import type { FieldUi } from "../../api/uiSchema";
|
|
12
|
+
import type { FieldUi, LabelPlacement } from "../../api/uiSchema";
|
|
13
13
|
import { DenseContext, useForm } from "./context";
|
|
14
14
|
|
|
15
15
|
/** Everything a control needs to know about its field. */
|
|
@@ -45,7 +45,7 @@ export function spanClass(columns: 1 | 2, ui: FieldUi, fallback: 1 | 2): string
|
|
|
45
45
|
|
|
46
46
|
/** The field grid: two columns on `sm:` (or one), or an auto-fit row inside a tuple. */
|
|
47
47
|
export function Grid({ children }: { children: ReactNode }) {
|
|
48
|
-
const { columns } = useForm();
|
|
48
|
+
const { columns, labelPlacement } = useForm();
|
|
49
49
|
const dense = use(DenseContext);
|
|
50
50
|
const density = useDensity();
|
|
51
51
|
return (
|
|
@@ -54,7 +54,12 @@ export function Grid({ children }: { children: ReactNode }) {
|
|
|
54
54
|
"grid min-w-0",
|
|
55
55
|
dense
|
|
56
56
|
? "grid-cols-[repeat(auto-fit,minmax(5.5rem,1fr))] gap-2"
|
|
57
|
-
: cn(
|
|
57
|
+
: cn(
|
|
58
|
+
labelPlacement === "side"
|
|
59
|
+
? byDensity(density, "gap-x-6 gap-y-2", "gap-x-5 gap-y-1.5")
|
|
60
|
+
: byDensity(density, "gap-x-5 gap-y-4", "gap-x-4 gap-y-2.5"),
|
|
61
|
+
columns === 2 ? "grid-cols-1 sm:grid-cols-2" : "grid-cols-1",
|
|
62
|
+
),
|
|
58
63
|
)}
|
|
59
64
|
>
|
|
60
65
|
{children}
|
|
@@ -62,6 +67,27 @@ export function Grid({ children }: { children: ReactNode }) {
|
|
|
62
67
|
);
|
|
63
68
|
}
|
|
64
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Fields on one row, each labelled from above: the layout for a few small numbers that read
|
|
72
|
+
* as one setting. Cells drop descriptions and range notes (see `DenseContext`).
|
|
73
|
+
*/
|
|
74
|
+
export function InlineRow({ children }: { children: ReactNode }) {
|
|
75
|
+
return (
|
|
76
|
+
<DenseContext value>
|
|
77
|
+
<Grid>{children}</Grid>
|
|
78
|
+
</DenseContext>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Where this field's label goes: the form's (or its group's) placement, except inside a row
|
|
84
|
+
* of cells, which always label from above because a cell has no room for a label column.
|
|
85
|
+
*/
|
|
86
|
+
export function usePlacement(): LabelPlacement {
|
|
87
|
+
const { labelPlacement } = useForm();
|
|
88
|
+
return use(DenseContext) ? "top" : labelPlacement;
|
|
89
|
+
}
|
|
90
|
+
|
|
65
91
|
function present(node: ReactNode): boolean {
|
|
66
92
|
return node !== undefined && node !== null && node !== false && node !== "";
|
|
67
93
|
}
|
|
@@ -80,13 +106,16 @@ export function useChrome(
|
|
|
80
106
|
range: string | null = null,
|
|
81
107
|
): { hint: ReactNode; description: ReactNode; annotation: ReactNode } {
|
|
82
108
|
const dense = use(DenseContext);
|
|
83
|
-
const
|
|
109
|
+
const side = usePlacement() === "side";
|
|
110
|
+
const { descriptionAs } = useForm();
|
|
111
|
+
const mode = meta.ui.descriptionAs ?? descriptionAs;
|
|
84
112
|
const text: ReactNode = meta.ui.hint ?? firstParagraph(meta.schema.description);
|
|
85
113
|
const shown = !dense && present(text);
|
|
86
114
|
|
|
87
115
|
const hint = shown && mode === "hint" ? <InfoHint label={`About ${meta.label}`}>{text}</InfoHint> : null;
|
|
88
116
|
const description = shown && mode === "inline" ? text : undefined;
|
|
89
|
-
|
|
117
|
+
// A side row's label column has no room for a range: the hint and an error carry it.
|
|
118
|
+
const note = dense || side ? null : range;
|
|
90
119
|
const annotation =
|
|
91
120
|
note !== null || hint !== null ? (
|
|
92
121
|
<span className="inline-flex items-center gap-1.5">
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { createContext, use } from "react";
|
|
9
9
|
|
|
10
10
|
import type { JsonSchema } from "../../api/schemaNode";
|
|
11
|
-
import type { RenderField, UiSchema } from "../../api/uiSchema";
|
|
11
|
+
import type { LabelPlacement, RenderField, UiSchema } from "../../api/uiSchema";
|
|
12
12
|
|
|
13
13
|
/** A message the app attaches to a field, such as a backend's verdict on a value. */
|
|
14
14
|
export interface FieldMessage {
|
|
@@ -46,6 +46,10 @@ export interface FormState {
|
|
|
46
46
|
baseline: BaselineState | null;
|
|
47
47
|
/** The app's messages, by dot path (`""` is the form as a whole). */
|
|
48
48
|
messages: Readonly<Record<string, FieldMessage | string>> | undefined;
|
|
49
|
+
/** Where labels go here: the form's, or the enclosing group's. */
|
|
50
|
+
labelPlacement: LabelPlacement;
|
|
51
|
+
/** The form's default for where descriptions go. */
|
|
52
|
+
descriptionAs: "hint" | "inline" | "none";
|
|
49
53
|
/** Paths a group has claimed, so the ungrouped remainder skips them. */
|
|
50
54
|
claimed: ReadonlySet<string>;
|
|
51
55
|
}
|
|
@@ -27,7 +27,7 @@ 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
31
|
import { ErrorText, useFieldExtras, withNotes } from "./changes";
|
|
32
32
|
import { DenseContext, useForm } from "./context";
|
|
33
33
|
|
|
@@ -55,6 +55,7 @@ export function NumberLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
55
55
|
const value = getAtPath(form.value, meta.path);
|
|
56
56
|
const spec = numberSpec(meta.schema, meta.ui);
|
|
57
57
|
const chrome = useChrome(meta, rangeText(spec));
|
|
58
|
+
const placement = usePlacement();
|
|
58
59
|
const extras = useFieldExtras(meta);
|
|
59
60
|
const [error, setError] = useState<string | undefined>();
|
|
60
61
|
|
|
@@ -77,6 +78,7 @@ export function NumberLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
77
78
|
|
|
78
79
|
return (
|
|
79
80
|
<Field
|
|
81
|
+
layout={placement}
|
|
80
82
|
label={meta.label}
|
|
81
83
|
required={marked(meta)}
|
|
82
84
|
annotation={chrome.annotation}
|
|
@@ -115,11 +117,13 @@ export function TextLeaf({ meta }: { meta: FieldMeta }) {
|
|
|
115
117
|
const form = useForm();
|
|
116
118
|
const value = getAtPath(form.value, meta.path);
|
|
117
119
|
const chrome = useChrome(meta);
|
|
120
|
+
const placement = usePlacement();
|
|
118
121
|
const extras = useFieldExtras(meta);
|
|
119
122
|
const fallback = meta.schema.default;
|
|
120
123
|
|
|
121
124
|
return (
|
|
122
125
|
<Field
|
|
126
|
+
layout={placement}
|
|
123
127
|
label={meta.label}
|
|
124
128
|
required={marked(meta)}
|
|
125
129
|
annotation={chrome.annotation}
|
|
@@ -189,6 +193,7 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
|
|
|
189
193
|
const dense = use(DenseContext);
|
|
190
194
|
const value = getAtPath(form.value, meta.path);
|
|
191
195
|
const chrome = useChrome(meta);
|
|
196
|
+
const placement = usePlacement();
|
|
192
197
|
const extras = useFieldExtras(meta);
|
|
193
198
|
|
|
194
199
|
const shown = options.map((option) => ({
|
|
@@ -212,6 +217,7 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
|
|
|
212
217
|
|
|
213
218
|
return (
|
|
214
219
|
<Field
|
|
220
|
+
layout={placement}
|
|
215
221
|
as="group"
|
|
216
222
|
label={meta.label}
|
|
217
223
|
required={marked(meta)}
|
|
@@ -240,28 +246,64 @@ export function EnumLeaf({ meta, options }: { meta: FieldMeta; options: EnumOpti
|
|
|
240
246
|
);
|
|
241
247
|
}
|
|
242
248
|
|
|
243
|
-
/**
|
|
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
|
+
*/
|
|
244
253
|
export function BooleanLeaf({ meta }: { meta: FieldMeta }) {
|
|
245
254
|
const form = useForm();
|
|
246
255
|
const value = getAtPath(form.value, meta.path);
|
|
247
256
|
const chrome = useChrome(meta);
|
|
248
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);
|
|
249
260
|
const props = {
|
|
250
261
|
checked: value === true,
|
|
251
262
|
onCheckedChange: (next: boolean) => form.setValue(meta.path, next),
|
|
252
|
-
label: meta.label,
|
|
253
|
-
description: withNotes(withNotes(chrome.description, extras.error ? <ErrorText text={extras.error} /> : undefined), extras.notes),
|
|
254
263
|
disabled: form.disabled,
|
|
255
264
|
};
|
|
256
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 };
|
|
257
295
|
return (
|
|
258
296
|
<div className={cn("flex min-w-0 items-start gap-1.5", spanClass(form.columns, meta.ui, 1), extras.className)}>
|
|
259
|
-
{meta.ui.widget === "checkbox" ? <Checkbox {...
|
|
297
|
+
{meta.ui.widget === "checkbox" ? <Checkbox {...rich} /> : <Switch {...rich} />}
|
|
260
298
|
{chrome.hint !== null && <span className="mt-0.5">{chrome.hint}</span>}
|
|
261
299
|
</div>
|
|
262
300
|
);
|
|
263
301
|
}
|
|
264
302
|
|
|
303
|
+
function present(node: ReactNode): boolean {
|
|
304
|
+
return node !== undefined && node !== null && node !== false && node !== "";
|
|
305
|
+
}
|
|
306
|
+
|
|
265
307
|
function pretty(value: unknown): string {
|
|
266
308
|
return value === undefined ? "" : JSON.stringify(value, null, 2);
|
|
267
309
|
}
|
|
@@ -13,9 +13,9 @@ 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
19
|
import { ErrorText, useFieldExtras, withNotes } from "./changes";
|
|
20
20
|
import { Chooser, BooleanLeaf, EnumLeaf, JsonLeaf, NumberLeaf, StringListLeaf, TextLeaf } from "./leaves";
|
|
21
21
|
import { DenseContext, useForm } from "./context";
|
|
@@ -145,15 +145,15 @@ function useVisible(path: string, fields: FieldInfo[]): FieldInfo[] {
|
|
|
145
145
|
*/
|
|
146
146
|
export function ObjectBody({ path, fields }: { path: string; fields: FieldInfo[] }) {
|
|
147
147
|
const visible = useVisible(path, fields);
|
|
148
|
+
const form = useForm();
|
|
148
149
|
if (visible.length === 0) return null;
|
|
149
150
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
);
|
|
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>;
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
/** The frame of a composite field: its label and hint over its content. `bare` drops the frame. */
|
package/src/index.ts
CHANGED
|
@@ -47,6 +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
51
|
export { diffPaths } from "./api/valueDiff";
|
|
52
52
|
export { getAtPath, setAtPath } from "./api/valuePath";
|