@react-typed-forms/schemas 3.0.0-dev.98 → 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.
- package/.babelrc +4 -0
- package/.rush/temp/operation/build/state.json +3 -0
- package/.rush/temp/operation/update-readme/state.json +3 -0
- package/.rush/temp/shrinkwrap-deps.json +581 -7
- package/README.md +292 -0
- package/lib/controlBuilder.d.ts +14 -0
- package/lib/controlRender.d.ts +96 -80
- package/lib/hooks.d.ts +9 -9
- package/lib/index.d.ts +5 -1
- package/lib/index.js +1835 -19
- package/lib/index.js.map +1 -0
- package/lib/renderers.d.ts +171 -0
- package/lib/schemaBuilder.d.ts +53 -70
- package/lib/tailwind.d.ts +2 -0
- package/lib/types.d.ts +108 -43
- package/lib/util.d.ts +35 -0
- package/lib/validators.d.ts +4 -0
- package/package.json +15 -8
- package/src/controlBuilder.ts +121 -0
- package/src/controlRender.tsx +533 -437
- package/src/hooks.tsx +153 -0
- package/src/index.ts +5 -1
- package/src/renderers.tsx +846 -0
- package/src/schemaBuilder.ts +45 -66
- package/src/tailwind.tsx +25 -0
- package/src/types.ts +164 -48
- package/src/util.ts +360 -0
- package/src/validators.ts +116 -0
- package/tsconfig.json +4 -3
- package/.rush/temp/package-deps_build.json +0 -13
- package/lib/controlRender.js +0 -230
- package/lib/hooks.js +0 -93
- package/lib/schemaBuilder.js +0 -82
- package/lib/types.js +0 -73
- package/schemas.build.log +0 -2
- package/src/hooks.ts +0 -167
package/src/controlRender.tsx
CHANGED
|
@@ -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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
24
|
+
DisplayData,
|
|
9
25
|
FieldOption,
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
53
|
-
) =>
|
|
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
|
|
57
|
-
|
|
58
|
-
hooks: FormEditHooks;
|
|
68
|
+
export interface DisplayRendererProps {
|
|
69
|
+
data: DisplayData;
|
|
59
70
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
visible: boolean;
|
|
71
|
+
export interface AdornmentProps {
|
|
72
|
+
adornment: ControlAdornment;
|
|
63
73
|
}
|
|
64
74
|
|
|
65
|
-
export
|
|
66
|
-
|
|
67
|
-
onClick: () => void;
|
|
68
|
-
}
|
|
75
|
+
export const AppendAdornmentPriority = 0;
|
|
76
|
+
export const WrapAdornmentPriority = 1000;
|
|
69
77
|
|
|
70
|
-
export interface
|
|
71
|
-
|
|
78
|
+
export interface AdornmentRenderer {
|
|
79
|
+
apply(children: RenderedLayout): void;
|
|
80
|
+
adornment?: ControlAdornment;
|
|
81
|
+
priority: number;
|
|
72
82
|
}
|
|
73
83
|
|
|
74
|
-
export interface
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
81
|
-
|
|
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
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
|
110
|
-
|
|
111
|
-
|
|
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
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
export enum LabelType {
|
|
115
|
+
Control,
|
|
116
|
+
Group,
|
|
117
117
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
|
|
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
|
|
137
|
-
|
|
138
|
-
field:
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
|
151
|
-
|
|
142
|
+
export interface ActionRendererProps {
|
|
143
|
+
actionId: string;
|
|
144
|
+
actionText: string;
|
|
145
|
+
onClick: () => void;
|
|
152
146
|
}
|
|
153
147
|
|
|
154
|
-
export
|
|
155
|
-
|
|
148
|
+
export interface ControlRenderProps {
|
|
149
|
+
control: Control<any>;
|
|
156
150
|
}
|
|
157
151
|
|
|
158
|
-
export
|
|
159
|
-
|
|
|
160
|
-
|
|
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
|
|
183
|
-
|
|
157
|
+
export type CreateDataProps = (
|
|
158
|
+
definition: DataControlDefinition,
|
|
184
159
|
field: SchemaField,
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
279
|
+
export function lookupSchemaField(
|
|
280
|
+
c: ControlDefinition,
|
|
240
281
|
fields: SchemaField[],
|
|
241
|
-
field: string
|
|
242
282
|
): SchemaField | undefined {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|
-
|
|
251
|
-
|
|
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
|
|
255
|
-
definition
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
)
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
|
|
308
|
-
|
|
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
|
-
|
|
312
|
-
|
|
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
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
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
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
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
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
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
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
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
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
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
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
589
|
+
export function controlTitle(
|
|
590
|
+
title: string | undefined | null,
|
|
591
|
+
field: SchemaField,
|
|
592
|
+
) {
|
|
593
|
+
return title ? title : fieldDisplayName(field);
|
|
498
594
|
}
|