@react-typed-forms/schemas 1.0.0-dev.24 → 1.0.0-dev.26
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/.rush/temp/operation/build/state.json +1 -1
- package/.rush/temp/shrinkwrap-deps.json +163 -172
- package/lib/controlBuilder.d.ts +3 -1
- package/lib/hooks.d.ts +1 -1
- package/lib/index.js +44 -20
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/controlBuilder.ts +21 -0
- package/src/hooks.tsx +36 -18
package/package.json
CHANGED
package/src/controlBuilder.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
ControlDefinition,
|
|
2
3
|
ControlDefinitionType,
|
|
3
4
|
DataControlDefinition,
|
|
4
5
|
DisplayControlDefinition,
|
|
@@ -8,6 +9,7 @@ import {
|
|
|
8
9
|
EntityExpression,
|
|
9
10
|
ExpressionType,
|
|
10
11
|
FieldValueExpression,
|
|
12
|
+
GroupedControlsDefinition,
|
|
11
13
|
HtmlDisplay,
|
|
12
14
|
JsonataExpression,
|
|
13
15
|
TextDisplay,
|
|
@@ -43,6 +45,9 @@ export function htmlDisplayControl(
|
|
|
43
45
|
};
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
export function dynamicDefaultValue(expr: EntityExpression): DynamicProperty {
|
|
49
|
+
return { type: DynamicPropertyType.DefaultValue, expr };
|
|
50
|
+
}
|
|
46
51
|
export function visibility(expr: EntityExpression): DynamicProperty {
|
|
47
52
|
return { type: DynamicPropertyType.Visible, expr };
|
|
48
53
|
}
|
|
@@ -53,3 +58,19 @@ export function fieldEqExpr(field: string, value: any): FieldValueExpression {
|
|
|
53
58
|
export function jsonataExpr(expression: string): JsonataExpression {
|
|
54
59
|
return { type: ExpressionType.Jsonata, expression };
|
|
55
60
|
}
|
|
61
|
+
|
|
62
|
+
export function compoundControl(
|
|
63
|
+
compoundField: string,
|
|
64
|
+
title: string | undefined,
|
|
65
|
+
children: ControlDefinition[],
|
|
66
|
+
options?: Partial<GroupedControlsDefinition>,
|
|
67
|
+
): GroupedControlsDefinition {
|
|
68
|
+
return {
|
|
69
|
+
type: ControlDefinitionType.Group,
|
|
70
|
+
compoundField,
|
|
71
|
+
children,
|
|
72
|
+
title,
|
|
73
|
+
groupOptions: { type: "Standard", hideTitle: !title },
|
|
74
|
+
...options,
|
|
75
|
+
};
|
|
76
|
+
}
|
package/src/hooks.tsx
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
FieldValueExpression,
|
|
14
14
|
GroupedControlsDefinition,
|
|
15
15
|
GroupRenderType,
|
|
16
|
+
isDataControlDefinition,
|
|
16
17
|
JsonataExpression,
|
|
17
18
|
SchemaField,
|
|
18
19
|
SchemaValidator,
|
|
@@ -51,12 +52,12 @@ import {
|
|
|
51
52
|
elementValueForField,
|
|
52
53
|
findCompoundField,
|
|
53
54
|
findField,
|
|
54
|
-
isGroupControl,
|
|
55
55
|
isScalarField,
|
|
56
56
|
} from "./util";
|
|
57
|
+
import { FieldType } from "./types";
|
|
57
58
|
|
|
58
59
|
export function useDefaultValue(
|
|
59
|
-
definition:
|
|
60
|
+
definition: ControlDefinition,
|
|
60
61
|
field: SchemaField,
|
|
61
62
|
formState: FormEditState,
|
|
62
63
|
hooks: SchemaHooks,
|
|
@@ -67,7 +68,11 @@ export function useDefaultValue(
|
|
|
67
68
|
if (valueExpression) {
|
|
68
69
|
return hooks.useExpression(valueExpression.expr, formState).value;
|
|
69
70
|
}
|
|
70
|
-
return
|
|
71
|
+
return (
|
|
72
|
+
(isDataControlDefinition(definition)
|
|
73
|
+
? definition.defaultValue
|
|
74
|
+
: undefined) ?? field.defaultValue
|
|
75
|
+
);
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
export function useIsControlVisible(
|
|
@@ -90,30 +95,29 @@ export function useIsControlVisible(
|
|
|
90
95
|
}
|
|
91
96
|
const schemaFields = formState.fields;
|
|
92
97
|
|
|
93
|
-
const
|
|
98
|
+
const typeControl = useMemo(() => {
|
|
94
99
|
const typeField = schemaFields.find(
|
|
95
100
|
(x) => isScalarField(x) && x.isTypeField,
|
|
96
101
|
) as SchemaField | undefined;
|
|
97
102
|
|
|
98
|
-
|
|
99
|
-
formState.data.fields?.[typeField.field]) ??
|
|
103
|
+
return ((typeField && formState.data.fields?.[typeField.field]) ??
|
|
100
104
|
newControl(undefined)) as Control<string | undefined>;
|
|
101
|
-
const compoundField =
|
|
102
|
-
isGroupControl(definition) && definition.compoundField
|
|
103
|
-
? formState.data.fields[definition.compoundField]
|
|
104
|
-
: undefined;
|
|
105
|
-
return { typeControl, compoundField };
|
|
106
105
|
}, [schemaFields, formState.data]);
|
|
107
106
|
|
|
108
107
|
const fieldName = fieldForControl(definition);
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
108
|
+
const schemaField = fieldName
|
|
109
|
+
? findField(schemaFields, fieldName)
|
|
110
|
+
: undefined;
|
|
111
|
+
const isSingleCompoundField =
|
|
112
|
+
schemaField &&
|
|
113
|
+
schemaField.type === FieldType.Compound &&
|
|
114
|
+
!schemaField.collection;
|
|
115
|
+
const onlyForTypes = schemaField?.onlyForTypes ?? [];
|
|
116
|
+
const canChange = Boolean(isSingleCompoundField || onlyForTypes.length);
|
|
113
117
|
const value =
|
|
114
|
-
(!
|
|
115
|
-
|
|
116
|
-
|
|
118
|
+
(!isSingleCompoundField ||
|
|
119
|
+
formState.data.fields[fieldName!].value != null) &&
|
|
120
|
+
(!onlyForTypes.length ||
|
|
117
121
|
Boolean(typeControl.value && onlyForTypes.includes(typeControl.value)));
|
|
118
122
|
return { value, canChange };
|
|
119
123
|
}
|
|
@@ -331,6 +335,20 @@ export function createFormEditHooks(schemaHooks: SchemaHooks): FormEditHooks {
|
|
|
331
335
|
const field = definition.compoundField
|
|
332
336
|
? findCompoundField(fs.fields, definition.compoundField)
|
|
333
337
|
: undefined;
|
|
338
|
+
|
|
339
|
+
const defaultValue =
|
|
340
|
+
field && useDefaultValue(definition, field, fs, schemaHooks);
|
|
341
|
+
const dataControl = field && fs.data.fields[field.field];
|
|
342
|
+
const isVisible = visible.value && !fs.invisible;
|
|
343
|
+
|
|
344
|
+
useEffect(() => {
|
|
345
|
+
if (!dataControl) return;
|
|
346
|
+
if (isVisible === false) dataControl.value = null;
|
|
347
|
+
else if (dataControl.current.value == null) {
|
|
348
|
+
dataControl.value = defaultValue;
|
|
349
|
+
}
|
|
350
|
+
}, [dataControl, isVisible, defaultValue]);
|
|
351
|
+
|
|
334
352
|
const newFs: RenderControlOptions = {
|
|
335
353
|
...fs,
|
|
336
354
|
fields: field ? field.children : fs.fields,
|