@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.
@@ -1,230 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.isGroupControl = exports.isSchemaControl = exports.fieldForControl = exports.controlForField = exports.renderControl = exports.controlTitle = exports.fieldDisplayName = exports.findField = exports.findCompoundField = exports.findScalarField = exports.elementValueForField = exports.defaultValueForField = exports.defaultValueForFields = exports.applyDefaultForField = exports.applyDefaultValues = exports.isCompoundField = exports.isScalarField = exports.useFormRendererComponents = exports.FormRendererComponentsContext = void 0;
27
- const types_1 = require("./types");
28
- const react_1 = __importStar(require("react"));
29
- const core_1 = require("@react-typed-forms/core");
30
- exports.FormRendererComponentsContext = (0, react_1.createContext)(undefined);
31
- function useFormRendererComponents() {
32
- const c = (0, react_1.useContext)(exports.FormRendererComponentsContext);
33
- if (!c) {
34
- throw "Need to use FormRendererComponentContext.Provider";
35
- }
36
- return c;
37
- }
38
- exports.useFormRendererComponents = useFormRendererComponents;
39
- function isScalarField(sf) {
40
- return sf.schemaType === types_1.SchemaFieldType.Scalar;
41
- }
42
- exports.isScalarField = isScalarField;
43
- function isCompoundField(sf) {
44
- return sf.schemaType === types_1.SchemaFieldType.Compound;
45
- }
46
- exports.isCompoundField = isCompoundField;
47
- function applyDefaultValues(v, fields) {
48
- if (!v)
49
- return defaultValueForFields(fields);
50
- const applyValue = fields.filter((x) => x.schemaType === types_1.SchemaFieldType.Compound || !(x.field in v));
51
- if (!applyValue.length)
52
- return v;
53
- const out = Object.assign({}, v);
54
- applyValue.forEach((x) => {
55
- out[x.field] =
56
- x.field in v
57
- ? applyDefaultForField(v[x.field], x, fields)
58
- : defaultValueForField(x);
59
- });
60
- return out;
61
- }
62
- exports.applyDefaultValues = applyDefaultValues;
63
- function applyDefaultForField(v, field, parent, notElement) {
64
- var _a;
65
- if (field.collection && !notElement) {
66
- return ((_a = v) !== null && _a !== void 0 ? _a : []).map((x) => applyDefaultForField(x, field, parent, true));
67
- }
68
- if (isCompoundField(field)) {
69
- if (!v && !field.required)
70
- return v;
71
- return applyDefaultValues(v, field.treeChildren ? parent : field.children);
72
- }
73
- return defaultValueForField(field);
74
- }
75
- exports.applyDefaultForField = applyDefaultForField;
76
- function defaultValueForFields(fields) {
77
- return Object.fromEntries(fields.map((x) => [x.field, defaultValueForField(x)]));
78
- }
79
- exports.defaultValueForFields = defaultValueForFields;
80
- function defaultValueForField(sf) {
81
- if (isCompoundField(sf)) {
82
- return sf.required
83
- ? sf.collection
84
- ? []
85
- : defaultValueForFields(sf.children)
86
- : undefined;
87
- }
88
- if (sf.collection)
89
- return [];
90
- return sf.defaultValue;
91
- }
92
- exports.defaultValueForField = defaultValueForField;
93
- function elementValueForField(sf) {
94
- if (isCompoundField(sf)) {
95
- return defaultValueForFields(sf.children);
96
- }
97
- return sf.defaultValue;
98
- }
99
- exports.elementValueForField = elementValueForField;
100
- function findScalarField(fields, field) {
101
- return findField(fields, field);
102
- }
103
- exports.findScalarField = findScalarField;
104
- function findCompoundField(fields, field) {
105
- return findField(fields, field);
106
- }
107
- exports.findCompoundField = findCompoundField;
108
- function findField(fields, field) {
109
- return fields.find((x) => x.field === field);
110
- }
111
- exports.findField = findField;
112
- function fieldDisplayName(sf) {
113
- return sf.displayName ? sf.displayName : sf.field;
114
- }
115
- exports.fieldDisplayName = fieldDisplayName;
116
- function controlTitle(title, field) {
117
- return title ? title : fieldDisplayName(field);
118
- }
119
- exports.controlTitle = controlTitle;
120
- function renderControl(definition, formState, hooks, key, wrapChild) {
121
- const { fields } = formState;
122
- switch (definition.type) {
123
- case types_1.ControlDefinitionType.Data:
124
- const def = definition;
125
- const fieldData = findScalarField(fields, def.field);
126
- if (!fieldData)
127
- return react_1.default.createElement("h1", null,
128
- "No schema field for: ",
129
- def.field);
130
- return (react_1.default.createElement(DataRenderer, { key: key, wrapElem: wrapElem, formState: formState, hooks: hooks, controlDef: def, fieldData: fieldData }));
131
- case types_1.ControlDefinitionType.Group:
132
- return (react_1.default.createElement(GroupRenderer, { key: key, hooks: hooks, groupDef: definition, formState: formState, wrapElem: wrapElem }));
133
- case types_1.ControlDefinitionType.Display:
134
- return (react_1.default.createElement(DisplayRenderer, { key: key, hooks: hooks, formState: formState, wrapElem: wrapElem, displayDef: definition }));
135
- case types_1.ControlDefinitionType.Action:
136
- return (react_1.default.createElement(ActionRenderer, { key: key, hooks: hooks, formState: formState, wrapElem: wrapElem, actionDef: definition }));
137
- default:
138
- return react_1.default.createElement("h1", null,
139
- "Unknown control: ",
140
- definition.type);
141
- }
142
- function wrapElem(e) {
143
- var _a;
144
- return (_a = wrapChild === null || wrapChild === void 0 ? void 0 : wrapChild(key, e)) !== null && _a !== void 0 ? _a : e;
145
- }
146
- }
147
- exports.renderControl = renderControl;
148
- function DataRenderer({ hooks, formState, controlDef, wrapElem, fieldData, }) {
149
- var _a, _b;
150
- const renderer = useFormRendererComponents();
151
- const props = hooks.useDataProperties(formState, controlDef, fieldData);
152
- const scalarControl = (_a = formState.data.fields[fieldData.field]) !== null && _a !== void 0 ? _a : (0, core_1.newControl)(undefined);
153
- (0, core_1.useControlEffect)(() => scalarControl.value, (v) => {
154
- if (props.defaultValue && !v) {
155
- scalarControl.value = props.defaultValue;
156
- }
157
- }, true);
158
- if (!props.visible) {
159
- return react_1.default.createElement(react_1.default.Fragment, null);
160
- }
161
- const scalarProps = {
162
- formEditState: formState,
163
- field: fieldData,
164
- definition: controlDef,
165
- properties: props,
166
- };
167
- return wrapElem(((_b = props.customRender) !== null && _b !== void 0 ? _b : renderer.renderData)(scalarProps, scalarControl, false, renderer));
168
- }
169
- function ActionRenderer({ hooks, formState, wrapElem, actionDef, }) {
170
- const { renderAction } = useFormRendererComponents();
171
- const actionControlProperties = hooks.useActionProperties(formState, actionDef);
172
- if (!actionControlProperties.visible) {
173
- return react_1.default.createElement(react_1.default.Fragment, null);
174
- }
175
- return wrapElem(renderAction({ definition: actionDef, properties: actionControlProperties }));
176
- }
177
- function GroupRenderer({ hooks, formState, groupDef, wrapElem, }) {
178
- const renderers = useFormRendererComponents();
179
- const groupProps = hooks.useGroupProperties(formState, groupDef, hooks);
180
- if (!groupProps.visible) {
181
- return react_1.default.createElement(react_1.default.Fragment, null);
182
- }
183
- const compoundField = groupDef.compoundField
184
- ? findCompoundField(formState.fields, groupDef.compoundField)
185
- : undefined;
186
- if (compoundField) {
187
- return wrapElem(renderers.renderCompound({
188
- definition: groupDef,
189
- field: compoundField,
190
- properties: groupProps,
191
- renderChild: (k, c, data, wrapChild) => renderControl(c, Object.assign(Object.assign({}, formState), { fields: compoundField.children, data }), groupProps.hooks, k, wrapChild),
192
- }, formState.data.fields[compoundField.field], renderers));
193
- }
194
- return wrapElem(renderers.renderGroup({
195
- definition: groupDef,
196
- childCount: groupDef.children.length,
197
- properties: groupProps,
198
- renderChild: (c, wrapChild) => renderControl(groupDef.children[c], formState, groupProps.hooks, c, wrapChild),
199
- }));
200
- }
201
- function DisplayRenderer({ hooks, wrapElem, formState, displayDef, }) {
202
- const { renderDisplay } = useFormRendererComponents();
203
- const displayProps = hooks.useDisplayProperties(formState, displayDef);
204
- if (!displayProps.visible) {
205
- return react_1.default.createElement(react_1.default.Fragment, null);
206
- }
207
- return wrapElem(renderDisplay({ definition: displayDef, properties: displayProps }));
208
- }
209
- function controlForField(field, formState) {
210
- var _a;
211
- const refField = findField(formState.fields, field);
212
- return ((_a = (refField && formState.data.fields[refField.field])) !== null && _a !== void 0 ? _a : (0, core_1.newControl)(undefined));
213
- }
214
- exports.controlForField = controlForField;
215
- function fieldForControl(c) {
216
- return isSchemaControl(c)
217
- ? c.field
218
- : isGroupControl(c)
219
- ? c.compoundField
220
- : undefined;
221
- }
222
- exports.fieldForControl = fieldForControl;
223
- function isSchemaControl(c) {
224
- return c.type === types_1.ControlDefinitionType.Data;
225
- }
226
- exports.isSchemaControl = isSchemaControl;
227
- function isGroupControl(c) {
228
- return c.type === types_1.ControlDefinitionType.Group;
229
- }
230
- exports.isGroupControl = isGroupControl;
package/lib/hooks.js DELETED
@@ -1,93 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createFormEditHooks = exports.defaultExpressionHook = exports.getOptionsForScalarField = exports.getDefaultScalarControlProperties = exports.useIsControlVisible = exports.useDefaultValue = void 0;
4
- const types_1 = require("./types");
5
- const controlRender_1 = require("./controlRender");
6
- const react_1 = require("react");
7
- const core_1 = require("@react-typed-forms/core");
8
- function useDefaultValue(definition, field, formState, useExpression) {
9
- var _a;
10
- const valueExpression = (_a = definition.dynamic) === null || _a === void 0 ? void 0 : _a.find((x) => x.type === types_1.DynamicPropertyType.DefaultValue);
11
- if (valueExpression) {
12
- return useExpression(valueExpression.expr, formState);
13
- }
14
- return field.defaultValue;
15
- }
16
- exports.useDefaultValue = useDefaultValue;
17
- function useIsControlVisible(definition, formState, useExpression) {
18
- var _a, _b;
19
- const visibleExpression = (_a = definition.dynamic) === null || _a === void 0 ? void 0 : _a.find((x) => x.type === types_1.DynamicPropertyType.Visible);
20
- if (visibleExpression && visibleExpression.expr) {
21
- return Boolean(useExpression(visibleExpression.expr, formState));
22
- }
23
- const schemaFields = formState.fields;
24
- const { typeControl, compoundField } = (0, react_1.useMemo)(() => {
25
- var _a, _b;
26
- const typeField = schemaFields.find((x) => (0, controlRender_1.isScalarField)(x) && x.isTypeField);
27
- const typeControl = ((_b = (typeField &&
28
- ((_a = formState.data.fields) === null || _a === void 0 ? void 0 : _a[typeField.field]))) !== null && _b !== void 0 ? _b : (0, core_1.newControl)(undefined));
29
- const compoundField = (0, controlRender_1.isGroupControl)(definition) && definition.compoundField
30
- ? formState.data.fields[definition.compoundField]
31
- : undefined;
32
- return { typeControl, compoundField };
33
- }, [schemaFields, formState.data]);
34
- const fieldName = (0, controlRender_1.fieldForControl)(definition);
35
- const onlyForTypes = (_b = (fieldName ? (0, controlRender_1.findField)(schemaFields, fieldName) : undefined)) === null || _b === void 0 ? void 0 : _b.onlyForTypes;
36
- return (0, core_1.useControlValue)(() => (!compoundField || compoundField.value != null) &&
37
- (!onlyForTypes ||
38
- onlyForTypes.length === 0 ||
39
- Boolean(typeControl.value && onlyForTypes.includes(typeControl.value))));
40
- }
41
- exports.useIsControlVisible = useIsControlVisible;
42
- function getDefaultScalarControlProperties(control, field, visible, defaultValue, readonly) {
43
- var _a, _b;
44
- return {
45
- defaultValue,
46
- options: getOptionsForScalarField(field),
47
- required: (_a = control.required) !== null && _a !== void 0 ? _a : false,
48
- visible,
49
- readonly: (_b = readonly !== null && readonly !== void 0 ? readonly : control.readonly) !== null && _b !== void 0 ? _b : false,
50
- };
51
- }
52
- exports.getDefaultScalarControlProperties = getDefaultScalarControlProperties;
53
- function getOptionsForScalarField(field) {
54
- var _a, _b;
55
- const opts = (_a = field.restrictions) === null || _a === void 0 ? void 0 : _a.options;
56
- if ((_b = opts === null || opts === void 0 ? void 0 : opts.length) !== null && _b !== void 0 ? _b : 0 > 0) {
57
- return opts;
58
- }
59
- return undefined;
60
- }
61
- exports.getOptionsForScalarField = getOptionsForScalarField;
62
- const defaultExpressionHook = (expr, formState) => {
63
- switch (expr.type) {
64
- case types_1.ExpressionType.FieldValue:
65
- const fvExpr = expr;
66
- return (0, core_1.useControlValue)(() => (0, controlRender_1.controlForField)(fvExpr.field, formState).value === fvExpr.value);
67
- default:
68
- return undefined;
69
- }
70
- };
71
- exports.defaultExpressionHook = defaultExpressionHook;
72
- function createFormEditHooks(useExpression) {
73
- return {
74
- useDataProperties(formState, definition, field) {
75
- const visible = useIsControlVisible(definition, formState, useExpression);
76
- const defaultValue = useDefaultValue(definition, field, formState, useExpression);
77
- return getDefaultScalarControlProperties(definition, field, visible, defaultValue, formState.readonly);
78
- },
79
- useDisplayProperties: (fs, definition) => {
80
- const visible = useIsControlVisible(definition, fs, useExpression);
81
- return { visible };
82
- },
83
- useGroupProperties: (fs, definition, hooks) => {
84
- const visible = useIsControlVisible(definition, fs, useExpression);
85
- return { visible, hooks };
86
- },
87
- useActionProperties(formState, definition) {
88
- const visible = useIsControlVisible(definition, formState, useExpression);
89
- return { visible, onClick: () => { } };
90
- },
91
- };
92
- }
93
- exports.createFormEditHooks = createFormEditHooks;
@@ -1,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.defaultCompoundField = exports.defaultScalarField = exports.compoundField = exports.boolField = exports.intField = exports.makeCompoundField = exports.makeScalarField = exports.withScalarOptions = exports.stringOptionsField = exports.stringField = exports.buildSchema = void 0;
4
- const types_1 = require("./types");
5
- function buildSchema(def) {
6
- return Object.entries(def).map((x) => x[1](x[0]));
7
- }
8
- exports.buildSchema = buildSchema;
9
- function stringField(displayName, options) {
10
- return makeScalarField(Object.assign({ type: types_1.FieldType.String, displayName }, options));
11
- }
12
- exports.stringField = stringField;
13
- function stringOptionsField(displayName, ...options) {
14
- return makeScalarField({
15
- type: types_1.FieldType.String,
16
- displayName,
17
- restrictions: { options },
18
- });
19
- }
20
- exports.stringOptionsField = stringOptionsField;
21
- function withScalarOptions(options, v) {
22
- return (n) => (Object.assign(Object.assign({}, v(n)), options));
23
- }
24
- exports.withScalarOptions = withScalarOptions;
25
- function makeScalarField(options) {
26
- return (n) => (Object.assign(Object.assign({}, defaultScalarField(n, n)), options));
27
- }
28
- exports.makeScalarField = makeScalarField;
29
- function makeCompoundField(options) {
30
- return (n) => (Object.assign(Object.assign({}, defaultCompoundField(n, n, false)), options));
31
- }
32
- exports.makeCompoundField = makeCompoundField;
33
- function intField(displayName, options) {
34
- return makeScalarField(Object.assign({ type: types_1.FieldType.Int, displayName }, options));
35
- }
36
- exports.intField = intField;
37
- function boolField(displayName, options) {
38
- return makeScalarField(Object.assign({ type: types_1.FieldType.Bool, displayName }, options));
39
- }
40
- exports.boolField = boolField;
41
- function compoundField(displayName, fields, other) {
42
- return (field) => (Object.assign(Object.assign(Object.assign({}, defaultCompoundField(field, displayName, false)), other), { children: fields }));
43
- }
44
- exports.compoundField = compoundField;
45
- function defaultScalarField(field, displayName) {
46
- return {
47
- restrictions: {
48
- options: [],
49
- },
50
- tags: [],
51
- field,
52
- displayName,
53
- type: types_1.FieldType.String,
54
- collection: false,
55
- searchable: false,
56
- schemaType: types_1.SchemaFieldType.Scalar,
57
- system: false,
58
- entityRefType: "",
59
- parentField: "",
60
- required: false,
61
- defaultValue: undefined,
62
- onlyForTypes: [],
63
- isTypeField: false,
64
- };
65
- }
66
- exports.defaultScalarField = defaultScalarField;
67
- function defaultCompoundField(field, displayName, collection) {
68
- return {
69
- tags: [],
70
- field,
71
- displayName,
72
- type: types_1.FieldType.Compound,
73
- collection,
74
- schemaType: types_1.SchemaFieldType.Compound,
75
- system: false,
76
- treeChildren: false,
77
- children: [],
78
- onlyForTypes: [],
79
- required: true,
80
- };
81
- }
82
- exports.defaultCompoundField = defaultCompoundField;
package/lib/types.js DELETED
@@ -1,73 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DisplayDataType = exports.GroupRenderType = exports.SyncTextType = exports.DataRenderType = exports.ControlAdornmentType = exports.ExpressionType = exports.DynamicPropertyType = exports.ControlDefinitionType = exports.FieldType = exports.SchemaFieldType = void 0;
4
- var SchemaFieldType;
5
- (function (SchemaFieldType) {
6
- SchemaFieldType["Scalar"] = "Scalar";
7
- SchemaFieldType["Compound"] = "Compound";
8
- })(SchemaFieldType = exports.SchemaFieldType || (exports.SchemaFieldType = {}));
9
- var FieldType;
10
- (function (FieldType) {
11
- FieldType["String"] = "String";
12
- FieldType["Bool"] = "Bool";
13
- FieldType["Int"] = "Int";
14
- FieldType["Date"] = "Date";
15
- FieldType["DateTime"] = "DateTime";
16
- FieldType["Double"] = "Double";
17
- FieldType["EntityRef"] = "EntityRef";
18
- FieldType["Compound"] = "Compound";
19
- FieldType["AutoId"] = "AutoId";
20
- FieldType["Image"] = "Image";
21
- })(FieldType = exports.FieldType || (exports.FieldType = {}));
22
- var ControlDefinitionType;
23
- (function (ControlDefinitionType) {
24
- ControlDefinitionType["Data"] = "Data";
25
- ControlDefinitionType["Group"] = "Group";
26
- ControlDefinitionType["Display"] = "Display";
27
- ControlDefinitionType["Action"] = "Action";
28
- })(ControlDefinitionType = exports.ControlDefinitionType || (exports.ControlDefinitionType = {}));
29
- var DynamicPropertyType;
30
- (function (DynamicPropertyType) {
31
- DynamicPropertyType["Visible"] = "Visible";
32
- DynamicPropertyType["DefaultValue"] = "DefaultValue";
33
- })(DynamicPropertyType = exports.DynamicPropertyType || (exports.DynamicPropertyType = {}));
34
- var ExpressionType;
35
- (function (ExpressionType) {
36
- ExpressionType["Jsonata"] = "Jsonata";
37
- ExpressionType["FieldValue"] = "FieldValue";
38
- ExpressionType["UserMatch"] = "UserMatch";
39
- })(ExpressionType = exports.ExpressionType || (exports.ExpressionType = {}));
40
- var ControlAdornmentType;
41
- (function (ControlAdornmentType) {
42
- ControlAdornmentType["Tooltip"] = "Tooltip";
43
- ControlAdornmentType["Accordion"] = "Accordion";
44
- })(ControlAdornmentType = exports.ControlAdornmentType || (exports.ControlAdornmentType = {}));
45
- var DataRenderType;
46
- (function (DataRenderType) {
47
- DataRenderType["Standard"] = "Standard";
48
- DataRenderType["Radio"] = "Radio";
49
- DataRenderType["HtmlEditor"] = "HtmlEditor";
50
- DataRenderType["IconList"] = "IconList";
51
- DataRenderType["CheckList"] = "CheckList";
52
- DataRenderType["UserSelection"] = "UserSelection";
53
- DataRenderType["Synchronised"] = "Synchronised";
54
- DataRenderType["IconSelector"] = "IconSelector";
55
- DataRenderType["DateTime"] = "DateTime";
56
- })(DataRenderType = exports.DataRenderType || (exports.DataRenderType = {}));
57
- var SyncTextType;
58
- (function (SyncTextType) {
59
- SyncTextType["Camel"] = "Camel";
60
- SyncTextType["Snake"] = "Snake";
61
- SyncTextType["Pascal"] = "Pascal";
62
- })(SyncTextType = exports.SyncTextType || (exports.SyncTextType = {}));
63
- var GroupRenderType;
64
- (function (GroupRenderType) {
65
- GroupRenderType["Standard"] = "Standard";
66
- GroupRenderType["Grid"] = "Grid";
67
- GroupRenderType["GroupElement"] = "GroupElement";
68
- })(GroupRenderType = exports.GroupRenderType || (exports.GroupRenderType = {}));
69
- var DisplayDataType;
70
- (function (DisplayDataType) {
71
- DisplayDataType["Text"] = "Text";
72
- DisplayDataType["Html"] = "Html";
73
- })(DisplayDataType = exports.DisplayDataType || (exports.DisplayDataType = {}));
package/schemas.build.log DELETED
@@ -1,2 +0,0 @@
1
- Invoking: rimraf ./lib/ && tsc
2
-
package/src/hooks.ts DELETED
@@ -1,167 +0,0 @@
1
- import {
2
- ActionControlDefinition,
3
- DataControlDefinition,
4
- DynamicPropertyType,
5
- EntityExpression,
6
- ExpressionType,
7
- FieldOption,
8
- FieldValueExpression,
9
- ScalarField,
10
- ControlDefinition,
11
- } from "./types";
12
- import {
13
- ActionControlProperties,
14
- controlForField,
15
- DataControlProperties,
16
- fieldForControl,
17
- findField,
18
- FormEditHooks,
19
- FormEditState,
20
- isGroupControl,
21
- isScalarField,
22
- } from "./controlRender";
23
- import { useMemo } from "react";
24
- import { Control, newControl, useControlValue } from "@react-typed-forms/core";
25
-
26
- export type ExpressionHook = (
27
- expr: EntityExpression,
28
- formState: FormEditState
29
- ) => any;
30
- export function useDefaultValue(
31
- definition: DataControlDefinition,
32
- field: ScalarField,
33
- formState: FormEditState,
34
- useExpression: ExpressionHook
35
- ) {
36
- const valueExpression = definition.dynamic?.find(
37
- (x) => x.type === DynamicPropertyType.DefaultValue
38
- );
39
- if (valueExpression) {
40
- return useExpression(valueExpression.expr, formState);
41
- }
42
- return field.defaultValue;
43
- }
44
-
45
- export function useIsControlVisible(
46
- definition: ControlDefinition,
47
- formState: FormEditState,
48
- useExpression: ExpressionHook
49
- ) {
50
- const visibleExpression = definition.dynamic?.find(
51
- (x) => x.type === DynamicPropertyType.Visible
52
- );
53
- if (visibleExpression && visibleExpression.expr) {
54
- return Boolean(useExpression(visibleExpression.expr, formState));
55
- }
56
- const schemaFields = formState.fields;
57
-
58
- const { typeControl, compoundField } = useMemo(() => {
59
- const typeField = schemaFields.find(
60
- (x) => isScalarField(x) && x.isTypeField
61
- ) as ScalarField | undefined;
62
-
63
- const typeControl = ((typeField &&
64
- formState.data.fields?.[typeField.field]) ??
65
- newControl(undefined)) as Control<string | undefined>;
66
- const compoundField =
67
- isGroupControl(definition) && definition.compoundField
68
- ? formState.data.fields[definition.compoundField]
69
- : undefined;
70
- return { typeControl, compoundField };
71
- }, [schemaFields, formState.data]);
72
-
73
- const fieldName = fieldForControl(definition);
74
- const onlyForTypes = (
75
- fieldName ? findField(schemaFields, fieldName) : undefined
76
- )?.onlyForTypes;
77
-
78
- return useControlValue(
79
- () =>
80
- (!compoundField || compoundField.value != null) &&
81
- (!onlyForTypes ||
82
- onlyForTypes.length === 0 ||
83
- Boolean(typeControl.value && onlyForTypes.includes(typeControl.value)))
84
- );
85
- }
86
- export function getDefaultScalarControlProperties(
87
- control: DataControlDefinition,
88
- field: ScalarField,
89
- visible: boolean,
90
- defaultValue: any,
91
- readonly?: boolean
92
- ): DataControlProperties {
93
- return {
94
- defaultValue,
95
- options: getOptionsForScalarField(field),
96
- required: control.required ?? false,
97
- visible,
98
- readonly: readonly ?? control.readonly ?? false,
99
- };
100
- }
101
-
102
- export function getOptionsForScalarField(
103
- field: ScalarField
104
- ): FieldOption[] | undefined {
105
- const opts = field.restrictions?.options;
106
- if (opts?.length ?? 0 > 0) {
107
- return opts;
108
- }
109
- return undefined;
110
- }
111
-
112
- export const defaultExpressionHook: ExpressionHook = (
113
- expr: EntityExpression,
114
- formState: FormEditState
115
- ) => {
116
- switch (expr.type) {
117
- case ExpressionType.FieldValue:
118
- const fvExpr = expr as FieldValueExpression;
119
- return useControlValue(
120
- () => controlForField(fvExpr.field, formState).value === fvExpr.value
121
- );
122
- default:
123
- return undefined;
124
- }
125
- };
126
-
127
- export function createFormEditHooks(
128
- useExpression: ExpressionHook
129
- ): FormEditHooks {
130
- return {
131
- useDataProperties(
132
- formState: FormEditState,
133
- definition: DataControlDefinition,
134
- field: ScalarField
135
- ): DataControlProperties {
136
- const visible = useIsControlVisible(definition, formState, useExpression);
137
- const defaultValue = useDefaultValue(
138
- definition,
139
- field,
140
- formState,
141
- useExpression
142
- );
143
- return getDefaultScalarControlProperties(
144
- definition,
145
- field,
146
- visible,
147
- defaultValue,
148
- formState.readonly
149
- );
150
- },
151
- useDisplayProperties: (fs, definition) => {
152
- const visible = useIsControlVisible(definition, fs, useExpression);
153
- return { visible };
154
- },
155
- useGroupProperties: (fs, definition, hooks) => {
156
- const visible = useIsControlVisible(definition, fs, useExpression);
157
- return { visible, hooks };
158
- },
159
- useActionProperties(
160
- formState: FormEditState,
161
- definition: ActionControlDefinition
162
- ): ActionControlProperties {
163
- const visible = useIsControlVisible(definition, formState, useExpression);
164
- return { visible, onClick: () => {} };
165
- },
166
- };
167
- }