@react-typed-forms/schemas 3.0.0-dev.99 → 4.0.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.
@@ -1,498 +1,594 @@
1
+ import React, {
2
+ FC,
3
+ Fragment,
4
+ Key,
5
+ ReactNode,
6
+ useCallback,
7
+ useRef,
8
+ } from "react";
1
9
  import {
2
- ActionControlDefinition,
3
- AnyControlDefinition,
4
- CompoundField,
10
+ addElement,
11
+ Control,
12
+ newControl,
13
+ removeElement,
14
+ useComponentTracking,
15
+ useComputed,
16
+ useControl,
17
+ useControlEffect,
18
+ } from "@react-typed-forms/core";
19
+ import {
20
+ AdornmentPlacement,
21
+ ControlAdornment,
5
22
  ControlDefinition,
6
- ControlDefinitionType,
7
23
  DataControlDefinition,
8
- DisplayControlDefinition,
24
+ DisplayData,
9
25
  FieldOption,
10
- GroupedControlsDefinition,
11
- ScalarField,
26
+ GroupRenderOptions,
27
+ isActionControlsDefinition,
28
+ isDataControlDefinition,
29
+ isDisplayControlsDefinition,
30
+ isGroupControlsDefinition,
31
+ RenderOptions,
12
32
  SchemaField,
13
- SchemaFieldType,
14
33
  } from "./types";
15
- import React, { createContext, Key, ReactElement, useContext } from "react";
16
34
  import {
17
- Control,
18
- ControlChange,
19
- newControl,
20
- useControlEffect,
21
- } from "@react-typed-forms/core";
22
-
23
- export interface FormEditHooks {
24
- useDataProperties(
25
- formState: FormEditState,
26
- definition: DataControlDefinition,
27
- field: ScalarField
28
- ): DataControlProperties;
29
- useGroupProperties(
30
- formState: FormEditState,
31
- definition: GroupedControlsDefinition,
32
- currentHooks: FormEditHooks
33
- ): GroupControlProperties;
34
- useDisplayProperties(
35
- formState: FormEditState,
36
- definition: DisplayControlDefinition
37
- ): DisplayControlProperties;
38
- useActionProperties(
39
- formState: FormEditState,
40
- definition: ActionControlDefinition
41
- ): ActionControlProperties;
42
- }
43
-
44
- export interface DataControlProperties {
45
- readonly: boolean;
46
- visible: boolean;
47
- options: FieldOption[] | undefined;
48
- defaultValue: any;
49
- required: boolean;
50
- customRender?: (
35
+ ControlGroupContext,
36
+ elementValueForField,
37
+ fieldDisplayName,
38
+ findField,
39
+ isCompoundField,
40
+ useUpdatedRef,
41
+ } from "./util";
42
+ import { dataControl } from "./controlBuilder";
43
+ import { useEvalDefaultValueHook, useEvalVisibilityHook } from "./hooks";
44
+ import { useValidationHook } from "./validators";
45
+
46
+ export interface FormRenderer {
47
+ renderData: (
51
48
  props: DataRendererProps,
52
- control: Control<any>
53
- ) => ReactElement;
49
+ asArray: (() => ReactNode) | undefined,
50
+ ) => (layout: ControlLayoutProps) => ControlLayoutProps;
51
+ renderGroup: (props: GroupRendererProps) => ReactNode;
52
+ renderDisplay: (props: DisplayRendererProps) => ReactNode;
53
+ renderAction: (props: ActionRendererProps) => ReactNode;
54
+ renderArray: (props: ArrayRendererProps) => ReactNode;
55
+ renderAdornment: (props: AdornmentProps) => AdornmentRenderer;
56
+ renderLabel: (
57
+ props: LabelRendererProps,
58
+ labelStart: ReactNode,
59
+ labelEnd: ReactNode,
60
+ ) => ReactNode;
61
+ renderLayout: (props: ControlLayoutProps) => ReactNode;
62
+ renderVisibility: (
63
+ control: Control<Visibility | undefined>,
64
+ children: () => ReactNode,
65
+ ) => ReactNode;
54
66
  }
55
67
 
56
- export interface GroupControlProperties {
57
- visible: boolean;
58
- hooks: FormEditHooks;
68
+ export interface DisplayRendererProps {
69
+ data: DisplayData;
59
70
  }
60
-
61
- export interface DisplayControlProperties {
62
- visible: boolean;
71
+ export interface AdornmentProps {
72
+ adornment: ControlAdornment;
63
73
  }
64
74
 
65
- export interface ActionControlProperties {
66
- visible: boolean;
67
- onClick: () => void;
68
- }
75
+ export const AppendAdornmentPriority = 0;
76
+ export const WrapAdornmentPriority = 1000;
69
77
 
70
- export interface ControlData {
71
- [field: string]: any;
78
+ export interface AdornmentRenderer {
79
+ apply(children: RenderedLayout): void;
80
+ adornment?: ControlAdornment;
81
+ priority: number;
72
82
  }
73
83
 
74
- export interface FormEditState {
75
- fields: SchemaField[];
76
- data: Control<ControlData>;
77
- readonly?: boolean;
84
+ export interface ArrayRendererProps {
85
+ addAction?: ActionRendererProps;
86
+ required: boolean;
87
+ removeAction?: (childIndex: number) => ActionRendererProps;
88
+ childCount: number;
89
+ renderChild: (childIndex: number) => ReactNode;
90
+ childKey: (childIndex: number) => Key;
78
91
  }
79
-
80
- export interface FormRendererComponents {
81
- renderData: (
82
- props: DataRendererProps,
83
- control: Control<any>,
84
- element: boolean,
85
- renderers: FormRendererComponents
86
- ) => ReactElement;
87
- renderCompound: (
88
- props: CompoundGroupRendererProps,
89
- control: Control<any>,
90
- renderers: FormRendererComponents
91
- ) => ReactElement;
92
- renderGroup: (props: GroupRendererProps) => ReactElement;
93
- renderDisplay: (props: DisplayRendererProps) => ReactElement;
94
- renderAction: (props: ActionRendererProps) => ReactElement;
92
+ export interface Visibility {
93
+ visible: boolean;
94
+ showing: boolean;
95
95
  }
96
96
 
97
- export const FormRendererComponentsContext = createContext<
98
- FormRendererComponents | undefined
99
- >(undefined);
100
-
101
- export function useFormRendererComponents() {
102
- const c = useContext(FormRendererComponentsContext);
103
- if (!c) {
104
- throw "Need to use FormRendererComponentContext.Provider";
105
- }
106
- return c;
97
+ export interface RenderedLayout {
98
+ labelStart?: ReactNode;
99
+ labelEnd?: ReactNode;
100
+ controlStart?: ReactNode;
101
+ controlEnd?: ReactNode;
102
+ label?: ReactNode;
103
+ children?: ReactNode;
107
104
  }
108
105
 
109
- export interface DisplayRendererProps {
110
- definition: DisplayControlDefinition;
111
- properties: DisplayControlProperties;
106
+ export interface ControlLayoutProps {
107
+ label?: LabelRendererProps;
108
+ errorControl?: Control<any>;
109
+ adornments?: AdornmentRenderer[];
110
+ children?: ReactNode;
111
+ processLayout?: (props: ControlLayoutProps) => ControlLayoutProps;
112
112
  }
113
113
 
114
- export interface ActionRendererProps {
115
- definition: ActionControlDefinition;
116
- properties: ActionControlProperties;
114
+ export enum LabelType {
115
+ Control,
116
+ Group,
117
117
  }
118
-
119
- export interface DataRendererProps {
120
- definition: DataControlDefinition;
121
- properties: DataControlProperties;
122
- field: ScalarField;
123
- formEditState?: FormEditState;
118
+ export interface LabelRendererProps {
119
+ type: LabelType;
120
+ hide?: boolean | null;
121
+ label: ReactNode;
122
+ required?: boolean | null;
123
+ forId?: string;
124
124
  }
125
-
126
125
  export interface GroupRendererProps {
127
- definition: Omit<GroupedControlsDefinition, "children">;
128
- properties: GroupControlProperties;
126
+ renderOptions: GroupRenderOptions;
129
127
  childCount: number;
130
- renderChild: (
131
- child: number,
132
- wrapChild: (key: Key, childElem: ReactElement) => ReactElement
133
- ) => ReactElement;
128
+ renderChild: (child: number) => ReactNode;
134
129
  }
135
130
 
136
- export interface CompoundGroupRendererProps {
137
- definition: GroupedControlsDefinition;
138
- field: CompoundField;
139
- properties: GroupControlProperties;
140
- renderChild: (
141
- key: Key,
142
- control: ControlDefinition,
143
- data: Control<{
144
- [field: string]: any;
145
- }>,
146
- wrapChild: (key: Key, childElem: ReactElement) => ReactElement
147
- ) => ReactElement;
131
+ export interface DataRendererProps {
132
+ renderOptions: RenderOptions;
133
+ field: SchemaField;
134
+ id: string;
135
+ control: Control<any>;
136
+ readonly: boolean;
137
+ required: boolean;
138
+ options: FieldOption[] | undefined | null;
139
+ hidden: boolean;
148
140
  }
149
141
 
150
- export function isScalarField(sf: SchemaField): sf is ScalarField {
151
- return sf.schemaType === SchemaFieldType.Scalar;
142
+ export interface ActionRendererProps {
143
+ actionId: string;
144
+ actionText: string;
145
+ onClick: () => void;
152
146
  }
153
147
 
154
- export function isCompoundField(sf: SchemaField): sf is CompoundField {
155
- return sf.schemaType === SchemaFieldType.Compound;
148
+ export interface ControlRenderProps {
149
+ control: Control<any>;
156
150
  }
157
151
 
158
- export type AnySchemaFields =
159
- | SchemaField
160
- | ScalarField
161
- | (Omit<CompoundField, "children"> & { children: AnySchemaFields[] });
162
-
163
- export function applyDefaultValues(
164
- v: { [k: string]: any } | undefined,
165
- fields: SchemaField[]
166
- ): any {
167
- if (!v) return defaultValueForFields(fields);
168
- const applyValue = fields.filter(
169
- (x) => x.schemaType === SchemaFieldType.Compound || !(x.field in v)
170
- );
171
- if (!applyValue.length) return v;
172
- const out = { ...v };
173
- applyValue.forEach((x) => {
174
- out[x.field] =
175
- x.field in v
176
- ? applyDefaultForField(v[x.field], x, fields)
177
- : defaultValueForField(x);
178
- });
179
- return out;
152
+ export interface FormContextOptions {
153
+ readonly?: boolean | null;
154
+ hidden?: boolean;
180
155
  }
181
156
 
182
- export function applyDefaultForField(
183
- v: any,
157
+ export type CreateDataProps = (
158
+ definition: DataControlDefinition,
184
159
  field: SchemaField,
185
- parent: SchemaField[],
186
- notElement?: boolean
187
- ): any {
188
- if (field.collection && !notElement) {
189
- return ((v as any[]) ?? []).map((x) =>
190
- applyDefaultForField(x, field, parent, true)
191
- );
192
- }
193
- if (isCompoundField(field)) {
194
- if (!v && !field.required) return v;
195
- return applyDefaultValues(v, field.treeChildren ? parent : field.children);
196
- }
197
- return defaultValueForField(field);
198
- }
199
-
200
- export function defaultValueForFields(fields: SchemaField[]): any {
201
- return Object.fromEntries(
202
- fields.map((x) => [x.field, defaultValueForField(x)])
203
- );
204
- }
205
-
206
- export function defaultValueForField(sf: SchemaField): any {
207
- if (isCompoundField(sf)) {
208
- return sf.required
209
- ? sf.collection
210
- ? []
211
- : defaultValueForFields(sf.children)
212
- : undefined;
213
- }
214
- if (sf.collection) return [];
215
- return (sf as ScalarField).defaultValue;
216
- }
217
-
218
- export function elementValueForField(sf: SchemaField): any {
219
- if (isCompoundField(sf)) {
220
- return defaultValueForFields(sf.children);
221
- }
222
- return (sf as ScalarField).defaultValue;
223
- }
224
-
225
- export function findScalarField(
226
- fields: SchemaField[],
227
- field: string
228
- ): ScalarField | undefined {
229
- return findField(fields, field) as ScalarField | undefined;
230
- }
231
-
232
- export function findCompoundField(
160
+ groupContext: ControlGroupContext,
161
+ control: Control<any>,
162
+ options: FormContextOptions,
163
+ ) => DataRendererProps;
164
+ export interface ControlRenderOptions extends FormContextOptions {
165
+ useDataHook?: (c: ControlDefinition) => CreateDataProps;
166
+ clearHidden?: boolean;
167
+ }
168
+ export function useControlRenderer(
169
+ definition: ControlDefinition,
233
170
  fields: SchemaField[],
234
- field: string
235
- ): CompoundField | undefined {
236
- return findField(fields, field) as CompoundField | undefined;
171
+ renderer: FormRenderer,
172
+ options: ControlRenderOptions = {},
173
+ ): FC<ControlRenderProps> {
174
+ const dataProps = options.useDataHook?.(definition) ?? defaultDataProps;
175
+
176
+ const schemaField = lookupSchemaField(definition, fields);
177
+ const useDefaultValue = useEvalDefaultValueHook(definition, schemaField);
178
+ const useIsVisible = useEvalVisibilityHook(definition, schemaField);
179
+ const useValidation = useValidationHook(definition);
180
+ const r = useUpdatedRef({ options, definition, fields, schemaField });
181
+
182
+ const Component = useCallback(
183
+ ({ control: parentControl }: ControlRenderProps) => {
184
+ const stopTracking = useComponentTracking();
185
+ try {
186
+ const { definition: c, options, fields, schemaField } = r.current;
187
+ const groupContext: ControlGroupContext = {
188
+ groupControl: parentControl,
189
+ fields,
190
+ };
191
+ const visibleControl = useIsVisible(groupContext);
192
+ const visible = visibleControl.current.value;
193
+ const visibility = useControl<Visibility | undefined>(() =>
194
+ visible != null
195
+ ? {
196
+ visible,
197
+ showing: visible,
198
+ }
199
+ : undefined,
200
+ );
201
+ useControlEffect(
202
+ () => visibleControl.value,
203
+ (visible) => {
204
+ if (visible != null)
205
+ visibility.setValue((ex) => ({
206
+ visible,
207
+ showing: ex ? ex.showing : visible,
208
+ }));
209
+ },
210
+ );
211
+
212
+ const defaultValueControl = useDefaultValue(groupContext);
213
+ const [control, childContext] = getControlData(
214
+ schemaField,
215
+ groupContext,
216
+ );
217
+ useControlEffect(
218
+ () => [
219
+ visibility.value,
220
+ defaultValueControl.value,
221
+ control,
222
+ parentControl.isNull,
223
+ ],
224
+ ([vc, dv, cd, pn]) => {
225
+ if (pn) {
226
+ parentControl.value = {};
227
+ }
228
+ if (vc && cd && vc.visible === vc.showing) {
229
+ if (!vc.visible) {
230
+ if (options.clearHidden) cd.value = undefined;
231
+ } else if (cd.value == null) {
232
+ cd.value = dv;
233
+ }
234
+ }
235
+ },
236
+ true,
237
+ );
238
+ const hidden = useComputed(
239
+ () => options.hidden || !visibility.fields?.showing.value,
240
+ ).value;
241
+ useValidation(control!, hidden, groupContext);
242
+ const myOptions =
243
+ options.hidden !== hidden ? { ...options, hidden } : options;
244
+ const childRenderers: FC<ControlRenderProps>[] =
245
+ c.children?.map((cd) =>
246
+ useControlRenderer(cd, childContext.fields, renderer, myOptions),
247
+ ) ?? [];
248
+ if (parentControl.isNull) return <></>;
249
+ const adornments =
250
+ definition.adornments?.map((x) =>
251
+ renderer.renderAdornment({ adornment: x }),
252
+ ) ?? [];
253
+ const labelAndChildren = renderControlLayout(
254
+ c,
255
+ renderer,
256
+ childRenderers.length,
257
+ (k, i, props) => {
258
+ const RenderChild = childRenderers[i];
259
+ return <RenderChild key={k} {...props} />;
260
+ },
261
+ dataProps,
262
+ myOptions,
263
+ groupContext,
264
+ control,
265
+ schemaField,
266
+ );
267
+ return renderer.renderVisibility(visibility, () =>
268
+ renderer.renderLayout({ ...labelAndChildren, adornments }),
269
+ );
270
+ } finally {
271
+ stopTracking();
272
+ }
273
+ },
274
+ [r, dataProps, useIsVisible, useDefaultValue, useValidation, renderer],
275
+ );
276
+ (Component as any).displayName = "RenderControl";
277
+ return Component;
237
278
  }
238
-
239
- export function findField(
279
+ export function lookupSchemaField(
280
+ c: ControlDefinition,
240
281
  fields: SchemaField[],
241
- field: string
242
282
  ): SchemaField | undefined {
243
- return fields.find((x) => x.field === field);
244
- }
245
-
246
- export function fieldDisplayName(sf: SchemaField): string {
247
- return sf.displayName ? sf.displayName : sf.field;
283
+ const fieldName = isGroupControlsDefinition(c)
284
+ ? c.compoundField
285
+ : isDataControlDefinition(c)
286
+ ? c.field
287
+ : undefined;
288
+ return fieldName ? findField(fields, fieldName) : undefined;
289
+ }
290
+ export function getControlData(
291
+ schemaField: SchemaField | undefined,
292
+ parentContext: ControlGroupContext,
293
+ ): [Control<any> | undefined, ControlGroupContext] {
294
+ const childControl: Control<any> | undefined = schemaField
295
+ ? parentContext.groupControl.fields?.[schemaField.field] ?? newControl({})
296
+ : undefined;
297
+ return [
298
+ childControl,
299
+ schemaField && isCompoundField(schemaField)
300
+ ? {
301
+ groupControl: childControl!,
302
+ fields: schemaField.children,
303
+ }
304
+ : parentContext,
305
+ ];
306
+ }
307
+
308
+ function renderArray(
309
+ renderer: FormRenderer,
310
+ noun: string,
311
+ field: SchemaField,
312
+ required: boolean,
313
+ controlArray: Control<any[] | undefined | null>,
314
+ renderChild: (elemIndex: number, control: Control<any>) => ReactNode,
315
+ ) {
316
+ const elems = controlArray.elements ?? [];
317
+ return renderer.renderArray({
318
+ childCount: elems.length,
319
+ required,
320
+ addAction: {
321
+ actionId: "add",
322
+ actionText: "Add " + noun,
323
+ onClick: () => addElement(controlArray, elementValueForField(field)),
324
+ },
325
+ childKey: (i) => elems[i].uniqueId,
326
+ removeAction: (i: number) => ({
327
+ actionId: "",
328
+ actionText: "Remove",
329
+ onClick: () => removeElement(controlArray, i),
330
+ }),
331
+ renderChild: (i) => renderChild(i, elems[i]),
332
+ });
248
333
  }
249
-
250
- export function controlTitle(title: string | undefined, field: SchemaField) {
251
- return title ? title : fieldDisplayName(field);
334
+ function groupProps(
335
+ renderOptions: GroupRenderOptions = { type: "Standard" },
336
+ childCount: number,
337
+ renderChild: ChildRenderer,
338
+ control: Control<any>,
339
+ ): GroupRendererProps {
340
+ return {
341
+ childCount,
342
+ renderChild: (i) => renderChild(i, i, { control }),
343
+ renderOptions,
344
+ };
252
345
  }
253
346
 
254
- export function renderControl(
255
- definition: AnyControlDefinition,
256
- formState: FormEditState,
257
- hooks: FormEditHooks,
258
- key: Key,
259
- wrapChild?: (key: Key, db: ReactElement) => ReactElement
260
- ): ReactElement {
261
- const { fields } = formState;
262
- switch (definition.type) {
263
- case ControlDefinitionType.Data:
264
- const def = definition as DataControlDefinition;
265
- const fieldData = findScalarField(fields, def.field);
266
- if (!fieldData) return <h1>No schema field for: {def.field}</h1>;
267
- return (
268
- <DataRenderer
269
- key={key}
270
- wrapElem={wrapElem}
271
- formState={formState}
272
- hooks={hooks}
273
- controlDef={def}
274
- fieldData={fieldData}
275
- />
276
- );
277
- case ControlDefinitionType.Group:
278
- return (
279
- <GroupRenderer
280
- key={key}
281
- hooks={hooks}
282
- groupDef={definition as GroupedControlsDefinition}
283
- formState={formState}
284
- wrapElem={wrapElem}
285
- />
286
- );
287
- case ControlDefinitionType.Display:
288
- return (
289
- <DisplayRenderer
290
- key={key}
291
- hooks={hooks}
292
- formState={formState}
293
- wrapElem={wrapElem}
294
- displayDef={definition as DisplayControlDefinition}
295
- />
296
- );
297
- case ControlDefinitionType.Action:
298
- return (
299
- <ActionRenderer
300
- key={key}
301
- hooks={hooks}
302
- formState={formState}
303
- wrapElem={wrapElem}
304
- actionDef={definition as ActionControlDefinition}
305
- />
347
+ export const defaultDataProps: CreateDataProps = (
348
+ definition,
349
+ field,
350
+ groupContext,
351
+ control,
352
+ options,
353
+ ) => {
354
+ return {
355
+ control,
356
+ field,
357
+ id: "c" + control.uniqueId,
358
+ options: (field.options?.length ?? 0) === 0 ? null : field.options,
359
+ readonly: options.readonly || !!definition.readonly,
360
+ renderOptions: definition.renderOptions ?? { type: "Standard" },
361
+ required: !!definition.required,
362
+ hidden: !!options.hidden,
363
+ };
364
+ };
365
+
366
+ export type ChildRenderer = (
367
+ k: Key,
368
+ childIndex: number,
369
+ props: ControlRenderProps,
370
+ ) => ReactNode;
371
+ export function renderControlLayout(
372
+ c: ControlDefinition,
373
+ renderer: FormRenderer,
374
+ childCount: number,
375
+ childRenderer: ChildRenderer,
376
+ dataProps: CreateDataProps,
377
+ dataOptions: FormContextOptions,
378
+ groupContext: ControlGroupContext,
379
+ childControl?: Control<any>,
380
+ schemaField?: SchemaField,
381
+ ): ControlLayoutProps {
382
+ if (isDataControlDefinition(c)) {
383
+ return renderData(c);
384
+ }
385
+ if (isGroupControlsDefinition(c)) {
386
+ if (c.compoundField) {
387
+ return renderData(
388
+ dataControl(c.compoundField, c.title, {
389
+ children: c.children,
390
+ hideTitle: c.groupOptions?.hideTitle,
391
+ }),
306
392
  );
307
- default:
308
- return <h1>Unknown control: {(definition as any).type}</h1>;
393
+ }
394
+ return {
395
+ children: renderer.renderGroup(
396
+ groupProps(
397
+ c.groupOptions,
398
+ childCount,
399
+ childRenderer,
400
+ groupContext.groupControl,
401
+ ),
402
+ ),
403
+ label: {
404
+ label: c.title,
405
+ type: LabelType.Group,
406
+ hide: c.groupOptions?.hideTitle,
407
+ },
408
+ };
309
409
  }
310
-
311
- function wrapElem(e: ReactElement): ReactElement {
312
- return wrapChild?.(key, e) ?? e;
410
+ if (isActionControlsDefinition(c)) {
411
+ return {
412
+ children: renderer.renderAction({
413
+ actionText: c.title ?? c.actionId,
414
+ actionId: c.actionId,
415
+ onClick: () => {},
416
+ }),
417
+ };
313
418
  }
314
- }
315
-
316
- function DataRenderer({
317
- hooks,
318
- formState,
319
- controlDef,
320
- wrapElem,
321
- fieldData,
322
- }: {
323
- hooks: FormEditHooks;
324
- controlDef: DataControlDefinition;
325
- formState: FormEditState;
326
- fieldData: ScalarField;
327
- wrapElem: (db: ReactElement) => ReactElement;
328
- }) {
329
- const renderer = useFormRendererComponents();
330
- const props = hooks.useDataProperties(formState, controlDef, fieldData);
331
- const scalarControl =
332
- formState.data.fields[fieldData.field] ?? newControl(undefined);
333
- useControlEffect(
334
- () => scalarControl.value,
335
- (v) => {
336
- if (props.defaultValue && !v) {
337
- scalarControl.value = props.defaultValue;
338
- }
339
- },
340
- true
341
- );
342
- if (!props.visible) {
343
- return <></>;
419
+ if (isDisplayControlsDefinition(c)) {
420
+ return { children: renderer.renderDisplay({ data: c.displayData ?? {} }) };
344
421
  }
345
- const scalarProps: DataRendererProps = {
346
- formEditState: formState,
347
- field: fieldData,
348
- definition: controlDef,
349
- properties: props,
350
- };
351
- return wrapElem(
352
- (props.customRender ?? renderer.renderData)(
353
- scalarProps,
354
- scalarControl,
355
- false,
356
- renderer
357
- )
358
- );
359
- }
360
-
361
- function ActionRenderer({
362
- hooks,
363
- formState,
364
- wrapElem,
365
- actionDef,
366
- }: {
367
- hooks: FormEditHooks;
368
- actionDef: ActionControlDefinition;
369
- formState: FormEditState;
370
- wrapElem: (db: ReactElement) => ReactElement;
371
- }) {
372
- const { renderAction } = useFormRendererComponents();
373
- const actionControlProperties = hooks.useActionProperties(
374
- formState,
375
- actionDef
376
- );
377
- if (!actionControlProperties.visible) {
378
- return <></>;
422
+ return {};
423
+
424
+ function renderData(c: DataControlDefinition) {
425
+ if (!schemaField) return { children: "No schema field for: " + c.field };
426
+ if (isCompoundField(schemaField)) {
427
+ const label: LabelRendererProps = {
428
+ hide: c.hideTitle,
429
+ label: controlTitle(c.title, schemaField),
430
+ type: schemaField.collection ? LabelType.Control : LabelType.Group,
431
+ };
432
+
433
+ if (schemaField.collection) {
434
+ return {
435
+ label,
436
+ children: renderArray(
437
+ renderer,
438
+ controlTitle(c.title, schemaField),
439
+ schemaField,
440
+ !!c.required,
441
+ childControl!,
442
+ compoundRenderer,
443
+ ),
444
+ errorControl: childControl,
445
+ };
446
+ }
447
+ return {
448
+ children: renderer.renderGroup(
449
+ groupProps(
450
+ { type: "Standard" },
451
+ childCount,
452
+ childRenderer,
453
+ childControl!,
454
+ ),
455
+ ),
456
+ label,
457
+ errorControl: childControl,
458
+ };
459
+ }
460
+ const props = dataProps(
461
+ c,
462
+ schemaField,
463
+ groupContext,
464
+ childControl!,
465
+ dataOptions,
466
+ );
467
+ const labelText = !c.hideTitle
468
+ ? controlTitle(c.title, schemaField)
469
+ : undefined;
470
+ return {
471
+ processLayout: renderer.renderData(
472
+ props,
473
+ schemaField.collection
474
+ ? () =>
475
+ renderArray(
476
+ renderer,
477
+ controlTitle(c.title, schemaField),
478
+ schemaField,
479
+ !!c.required,
480
+ childControl!,
481
+ scalarRenderer(props),
482
+ )
483
+ : undefined,
484
+ ),
485
+ label: {
486
+ type: LabelType.Control,
487
+ label: labelText,
488
+ forId: props.id,
489
+ required: c.required,
490
+ hide: c.hideTitle,
491
+ },
492
+ errorControl: childControl,
493
+ };
379
494
  }
380
495
 
381
- return wrapElem(
382
- renderAction({ definition: actionDef, properties: actionControlProperties })
383
- );
384
- }
385
-
386
- function GroupRenderer({
387
- hooks,
388
- formState,
389
- groupDef,
390
- wrapElem,
391
- }: {
392
- hooks: FormEditHooks;
393
- groupDef: GroupedControlsDefinition;
394
- formState: FormEditState;
395
- wrapElem: (db: ReactElement) => ReactElement;
396
- }) {
397
- const renderers = useFormRendererComponents();
398
-
399
- const groupProps = hooks.useGroupProperties(formState, groupDef, hooks);
400
- if (!groupProps.visible) {
401
- return <></>;
402
- }
403
- const compoundField = groupDef.compoundField
404
- ? findCompoundField(formState.fields, groupDef.compoundField)
405
- : undefined;
406
- if (compoundField) {
407
- return wrapElem(
408
- renderers.renderCompound(
409
- {
410
- definition: groupDef,
411
- field: compoundField,
412
- properties: groupProps,
413
- renderChild: (k, c, data, wrapChild) =>
414
- renderControl(
415
- c as AnyControlDefinition,
416
- {
417
- ...formState,
418
- fields: compoundField!.children,
419
- data,
420
- },
421
- groupProps.hooks,
422
- k,
423
- wrapChild
424
- ),
425
- },
426
- formState.data.fields[compoundField.field],
427
- renderers
428
- )
496
+ function compoundRenderer(i: number, control: Control<any>): ReactNode {
497
+ return (
498
+ <Fragment key={control.uniqueId}>
499
+ {renderer.renderGroup({
500
+ renderOptions: { type: "Standard", hideTitle: true },
501
+ childCount,
502
+ renderChild: (ci) => childRenderer(ci, ci, { control }),
503
+ })}
504
+ </Fragment>
429
505
  );
430
506
  }
431
- return wrapElem(
432
- renderers.renderGroup({
433
- definition: groupDef,
434
- childCount: groupDef.children.length,
435
- properties: groupProps,
436
- renderChild: (c, wrapChild) =>
437
- renderControl(
438
- groupDef.children[c],
439
- formState,
440
- groupProps.hooks,
441
- c,
442
- wrapChild
443
- ),
444
- })
445
- );
446
- }
447
-
448
- function DisplayRenderer({
449
- hooks,
450
- wrapElem,
451
- formState,
452
- displayDef,
453
- }: {
454
- hooks: FormEditHooks;
455
- displayDef: DisplayControlDefinition;
456
- formState: FormEditState;
457
- wrapElem: (db: ReactElement) => ReactElement;
458
- }) {
459
- const { renderDisplay } = useFormRendererComponents();
460
-
461
- const displayProps = hooks.useDisplayProperties(formState, displayDef);
462
- if (!displayProps.visible) {
463
- return <></>;
507
+ function scalarRenderer(
508
+ dataProps: DataRendererProps,
509
+ ): (i: number, control: Control<any>) => ReactNode {
510
+ return (i, control) => {
511
+ return (
512
+ <Fragment key={control.uniqueId}>
513
+ {
514
+ renderer.renderData({ ...dataProps, control }, undefined)({})
515
+ .children
516
+ }
517
+ </Fragment>
518
+ );
519
+ };
464
520
  }
465
- return wrapElem(
466
- renderDisplay({ definition: displayDef, properties: displayProps })
467
- );
468
- }
469
-
470
- export function controlForField(
471
- field: string,
472
- formState: FormEditState
473
- ): Control<any> {
474
- const refField = findField(formState.fields, field);
475
- return (
476
- (refField && formState.data.fields[refField.field]) ?? newControl(undefined)
477
- );
478
521
  }
479
522
 
480
- export function fieldForControl(c: ControlDefinition) {
481
- return isSchemaControl(c)
482
- ? c.field
483
- : isGroupControl(c)
484
- ? c.compoundField
485
- : undefined;
523
+ export function appendMarkup(
524
+ k: keyof RenderedLayout,
525
+ markup: ReactNode,
526
+ ): (layout: RenderedLayout) => void {
527
+ return (layout) =>
528
+ (layout[k] = (
529
+ <>
530
+ {layout[k]}
531
+ {markup}
532
+ </>
533
+ ));
534
+ }
535
+
536
+ export function wrapMarkup(
537
+ k: keyof RenderedLayout,
538
+ wrap: (ex: ReactNode) => ReactNode,
539
+ ): (layout: RenderedLayout) => void {
540
+ return (layout) => (layout[k] = wrap(layout[k]));
541
+ }
542
+
543
+ export function layoutKeyForPlacement(
544
+ pos: AdornmentPlacement,
545
+ ): keyof RenderedLayout {
546
+ switch (pos) {
547
+ case AdornmentPlacement.ControlEnd:
548
+ return "controlEnd";
549
+ case AdornmentPlacement.ControlStart:
550
+ return "controlStart";
551
+ case AdornmentPlacement.LabelStart:
552
+ return "labelStart";
553
+ case AdornmentPlacement.LabelEnd:
554
+ return "labelEnd";
555
+ }
486
556
  }
487
557
 
488
- export function isSchemaControl(
489
- c: ControlDefinition
490
- ): c is DataControlDefinition {
491
- return c.type === ControlDefinitionType.Data;
558
+ export function appendMarkupAt(
559
+ pos: AdornmentPlacement,
560
+ markup: ReactNode,
561
+ ): (layout: RenderedLayout) => void {
562
+ return appendMarkup(layoutKeyForPlacement(pos), markup);
563
+ }
564
+
565
+ export function wrapMarkupAt(
566
+ pos: AdornmentPlacement,
567
+ wrap: (ex: ReactNode) => ReactNode,
568
+ ): (layout: RenderedLayout) => void {
569
+ return wrapMarkup(layoutKeyForPlacement(pos), wrap);
570
+ }
571
+
572
+ export function renderLayoutParts(
573
+ props: ControlLayoutProps,
574
+ renderer: FormRenderer,
575
+ ): RenderedLayout {
576
+ const processed = props.processLayout?.(props) ?? props;
577
+ const layout: RenderedLayout = { children: processed.children };
578
+ (processed.adornments ?? [])
579
+ .sort((a, b) => a.priority - b.priority)
580
+ .forEach((x) => x.apply(layout));
581
+ const l = processed.label;
582
+ layout.label =
583
+ l && !l.hide
584
+ ? renderer.renderLabel(l, layout.labelStart, layout.labelEnd)
585
+ : undefined;
586
+ return layout;
492
587
  }
493
588
 
494
- export function isGroupControl(
495
- c: ControlDefinition
496
- ): c is GroupedControlsDefinition {
497
- return c.type === ControlDefinitionType.Group;
589
+ export function controlTitle(
590
+ title: string | undefined | null,
591
+ field: SchemaField,
592
+ ) {
593
+ return title ? title : fieldDisplayName(field);
498
594
  }