@react-typed-forms/schemas 1.0.0-dev.2 → 1.0.0-dev.4
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/package-deps_build.json +5 -5
- package/lib/controlRender.d.ts +6 -6
- package/lib/controlRender.js +5 -26
- package/lib/hooks.d.ts +4 -3
- package/lib/hooks.js +13 -4
- package/lib/index.d.ts +3 -0
- package/lib/index.js +11 -0
- package/lib/schemaBuilder.d.ts +30 -30
- package/lib/types.d.ts +22 -22
- package/package.json +1 -1
- package/schemas.build.log +0 -2
- package/src/controlRender.tsx +11 -38
- package/src/hooks.ts +17 -6
- package/src/index.ts +10 -0
- package/src/types.ts +22 -22
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"files": {
|
|
3
3
|
"packages/schemas/.rush/temp/shrinkwrap-deps.json": "5ce6417fae765db5c61e400f851ecafe0ce756f0",
|
|
4
|
-
"packages/schemas/package.json": "
|
|
5
|
-
"packages/schemas/src/controlRender.tsx": "
|
|
6
|
-
"packages/schemas/src/hooks.ts": "
|
|
7
|
-
"packages/schemas/src/index.ts": "
|
|
4
|
+
"packages/schemas/package.json": "3fc4e26d218ceacae3452eccb3749fa9fc874c38",
|
|
5
|
+
"packages/schemas/src/controlRender.tsx": "5ce85ef4ee97307d9131d066655c133eb1b9bf48",
|
|
6
|
+
"packages/schemas/src/hooks.ts": "4eb4946f7b1c685e0b51cd011b4d39a1fdd6aa2e",
|
|
7
|
+
"packages/schemas/src/index.ts": "67f656f1d7f717bce7e933b9f4d7dc5dc50274dc",
|
|
8
8
|
"packages/schemas/src/schemaBuilder.ts": "bfe74d3fb1cc2a4d5226051a58e025e107c32b11",
|
|
9
|
-
"packages/schemas/src/types.ts": "
|
|
9
|
+
"packages/schemas/src/types.ts": "01bba36475e368ee273871358458caf60b36dad5",
|
|
10
10
|
"packages/schemas/tsconfig.json": "52e4f044fa580f4e0ad5830f3cfca574e2788372"
|
|
11
11
|
},
|
|
12
12
|
"arguments": "rimraf ./lib/ && tsc "
|
package/lib/controlRender.d.ts
CHANGED
|
@@ -8,12 +8,13 @@ export interface FormEditHooks {
|
|
|
8
8
|
useActionProperties(formState: FormEditState, definition: ActionControlDefinition): ActionControlProperties;
|
|
9
9
|
}
|
|
10
10
|
export interface DataControlProperties {
|
|
11
|
-
|
|
11
|
+
control: Control<any>;
|
|
12
12
|
visible: boolean;
|
|
13
|
-
|
|
13
|
+
readonly: boolean;
|
|
14
14
|
defaultValue: any;
|
|
15
15
|
required: boolean;
|
|
16
|
-
|
|
16
|
+
options: FieldOption[] | undefined | null;
|
|
17
|
+
customRender?: (props: DataRendererProps) => ReactElement;
|
|
17
18
|
}
|
|
18
19
|
export interface GroupControlProperties {
|
|
19
20
|
visible: boolean;
|
|
@@ -86,10 +87,9 @@ export declare function elementValueForField(sf: SchemaField): any;
|
|
|
86
87
|
export declare function findScalarField(fields: SchemaField[], field: string): SchemaField | undefined;
|
|
87
88
|
export declare function findCompoundField(fields: SchemaField[], field: string): CompoundField | undefined;
|
|
88
89
|
export declare function findField(fields: SchemaField[], field: string): SchemaField | undefined;
|
|
89
|
-
export declare function
|
|
90
|
-
export declare function controlTitle(title: string | undefined, field: SchemaField): string;
|
|
90
|
+
export declare function controlTitle(title: string | undefined | null, field: SchemaField): string;
|
|
91
91
|
export declare function renderControl(definition: AnyControlDefinition, formState: FormEditState, hooks: FormEditHooks, key: Key, wrapChild?: (key: Key, db: ReactElement) => ReactElement): ReactElement;
|
|
92
92
|
export declare function controlForField(field: string, formState: FormEditState): Control<any>;
|
|
93
|
-
export declare function fieldForControl(c: ControlDefinition): string | undefined;
|
|
93
|
+
export declare function fieldForControl(c: ControlDefinition): string | null | undefined;
|
|
94
94
|
export declare function isDataControl(c: ControlDefinition): c is DataControlDefinition;
|
|
95
95
|
export declare function isGroupControl(c: ControlDefinition): c is GroupedControlsDefinition;
|
package/lib/controlRender.js
CHANGED
|
@@ -23,10 +23,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.isGroupControl = exports.isDataControl = exports.fieldForControl = exports.controlForField = exports.renderControl = exports.controlTitle = exports.
|
|
26
|
+
exports.isGroupControl = exports.isDataControl = exports.fieldForControl = exports.controlForField = exports.renderControl = exports.controlTitle = 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
27
|
const types_1 = require("./types");
|
|
28
28
|
const react_1 = __importStar(require("react"));
|
|
29
29
|
const core_1 = require("@react-typed-forms/core");
|
|
30
|
+
const index_1 = require("./index");
|
|
30
31
|
exports.FormRendererComponentsContext = (0, react_1.createContext)(undefined);
|
|
31
32
|
function useFormRendererComponents() {
|
|
32
33
|
const c = (0, react_1.useContext)(exports.FormRendererComponentsContext);
|
|
@@ -109,12 +110,8 @@ function findField(fields, field) {
|
|
|
109
110
|
return fields.find((x) => x.field === field);
|
|
110
111
|
}
|
|
111
112
|
exports.findField = findField;
|
|
112
|
-
function fieldDisplayName(sf) {
|
|
113
|
-
return sf.displayName ? sf.displayName : sf.field;
|
|
114
|
-
}
|
|
115
|
-
exports.fieldDisplayName = fieldDisplayName;
|
|
116
113
|
function controlTitle(title, field) {
|
|
117
|
-
return title ? title : fieldDisplayName(field);
|
|
114
|
+
return title ? title : (0, index_1.fieldDisplayName)(field);
|
|
118
115
|
}
|
|
119
116
|
exports.controlTitle = controlTitle;
|
|
120
117
|
function renderControl(definition, formState, hooks, key, wrapChild) {
|
|
@@ -146,40 +143,25 @@ function renderControl(definition, formState, hooks, key, wrapChild) {
|
|
|
146
143
|
}
|
|
147
144
|
exports.renderControl = renderControl;
|
|
148
145
|
function DataRenderer({ hooks, formState, controlDef, wrapElem, fieldData, }) {
|
|
149
|
-
var _a
|
|
146
|
+
var _a;
|
|
150
147
|
const renderer = useFormRendererComponents();
|
|
151
148
|
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
149
|
const scalarProps = {
|
|
162
150
|
formEditState: formState,
|
|
163
151
|
field: fieldData,
|
|
164
152
|
definition: controlDef,
|
|
165
153
|
properties: props,
|
|
166
154
|
};
|
|
167
|
-
return wrapElem(((
|
|
155
|
+
return wrapElem(((_a = props.customRender) !== null && _a !== void 0 ? _a : renderer.renderData)(scalarProps, props.control, false, renderer));
|
|
168
156
|
}
|
|
169
157
|
function ActionRenderer({ hooks, formState, wrapElem, actionDef, }) {
|
|
170
158
|
const { renderAction } = useFormRendererComponents();
|
|
171
159
|
const actionControlProperties = hooks.useActionProperties(formState, actionDef);
|
|
172
|
-
if (!actionControlProperties.visible) {
|
|
173
|
-
return react_1.default.createElement(react_1.default.Fragment, null);
|
|
174
|
-
}
|
|
175
160
|
return wrapElem(renderAction({ definition: actionDef, properties: actionControlProperties }));
|
|
176
161
|
}
|
|
177
162
|
function GroupRenderer({ hooks, formState, groupDef, wrapElem, }) {
|
|
178
163
|
const renderers = useFormRendererComponents();
|
|
179
164
|
const groupProps = hooks.useGroupProperties(formState, groupDef, hooks);
|
|
180
|
-
if (!groupProps.visible) {
|
|
181
|
-
return react_1.default.createElement(react_1.default.Fragment, null);
|
|
182
|
-
}
|
|
183
165
|
const compoundField = groupDef.compoundField
|
|
184
166
|
? findCompoundField(formState.fields, groupDef.compoundField)
|
|
185
167
|
: undefined;
|
|
@@ -201,9 +183,6 @@ function GroupRenderer({ hooks, formState, groupDef, wrapElem, }) {
|
|
|
201
183
|
function DisplayRenderer({ hooks, wrapElem, formState, displayDef, }) {
|
|
202
184
|
const { renderDisplay } = useFormRendererComponents();
|
|
203
185
|
const displayProps = hooks.useDisplayProperties(formState, displayDef);
|
|
204
|
-
if (!displayProps.visible) {
|
|
205
|
-
return react_1.default.createElement(react_1.default.Fragment, null);
|
|
206
|
-
}
|
|
207
186
|
return wrapElem(renderDisplay({ definition: displayDef, properties: displayProps }));
|
|
208
187
|
}
|
|
209
188
|
function controlForField(field, formState) {
|
package/lib/hooks.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { DataControlDefinition, EntityExpression, FieldOption,
|
|
1
|
+
import { ControlDefinition, DataControlDefinition, EntityExpression, FieldOption, SchemaField } from "./types";
|
|
2
2
|
import { DataControlProperties, FormEditHooks, FormEditState } from "./controlRender";
|
|
3
|
+
import { Control } from "@react-typed-forms/core";
|
|
3
4
|
export type ExpressionHook = (expr: EntityExpression, formState: FormEditState) => any;
|
|
4
5
|
export declare function useDefaultValue(definition: DataControlDefinition, field: SchemaField, formState: FormEditState, useExpression: ExpressionHook): any;
|
|
5
6
|
export declare function useIsControlVisible(definition: ControlDefinition, formState: FormEditState, useExpression: ExpressionHook): boolean;
|
|
6
|
-
export declare function getDefaultScalarControlProperties(
|
|
7
|
-
export declare function getOptionsForScalarField(field: SchemaField): FieldOption[] | undefined;
|
|
7
|
+
export declare function getDefaultScalarControlProperties(definition: DataControlDefinition, field: SchemaField, visible: boolean, defaultValue: any, control: Control<any>, readonly?: boolean): DataControlProperties;
|
|
8
|
+
export declare function getOptionsForScalarField(field: SchemaField): FieldOption[] | undefined | null;
|
|
8
9
|
export declare const defaultExpressionHook: ExpressionHook;
|
|
9
10
|
export declare function createFormEditHooks(useExpression: ExpressionHook): FormEditHooks;
|
package/lib/hooks.js
CHANGED
|
@@ -39,14 +39,15 @@ function useIsControlVisible(definition, formState, useExpression) {
|
|
|
39
39
|
Boolean(typeControl.value && onlyForTypes.includes(typeControl.value))));
|
|
40
40
|
}
|
|
41
41
|
exports.useIsControlVisible = useIsControlVisible;
|
|
42
|
-
function getDefaultScalarControlProperties(
|
|
42
|
+
function getDefaultScalarControlProperties(definition, field, visible, defaultValue, control, readonly) {
|
|
43
43
|
var _a, _b;
|
|
44
44
|
return {
|
|
45
45
|
defaultValue,
|
|
46
46
|
options: getOptionsForScalarField(field),
|
|
47
|
-
required: (_a =
|
|
47
|
+
required: (_a = definition.required) !== null && _a !== void 0 ? _a : false,
|
|
48
48
|
visible,
|
|
49
|
-
readonly: (_b = readonly !== null && readonly !== void 0 ? readonly :
|
|
49
|
+
readonly: (_b = readonly !== null && readonly !== void 0 ? readonly : definition.readonly) !== null && _b !== void 0 ? _b : false,
|
|
50
|
+
control,
|
|
50
51
|
};
|
|
51
52
|
}
|
|
52
53
|
exports.getDefaultScalarControlProperties = getDefaultScalarControlProperties;
|
|
@@ -74,7 +75,15 @@ function createFormEditHooks(useExpression) {
|
|
|
74
75
|
useDataProperties(formState, definition, field) {
|
|
75
76
|
const visible = useIsControlVisible(definition, formState, useExpression);
|
|
76
77
|
const defaultValue = useDefaultValue(definition, field, formState, useExpression);
|
|
77
|
-
|
|
78
|
+
const scalarControl = formState.data.fields[field.field];
|
|
79
|
+
(0, react_1.useEffect)(() => {
|
|
80
|
+
if (!visible)
|
|
81
|
+
scalarControl.value = null;
|
|
82
|
+
else if (scalarControl.current.value == null) {
|
|
83
|
+
scalarControl.value = defaultValue;
|
|
84
|
+
}
|
|
85
|
+
}, [visible, defaultValue]);
|
|
86
|
+
return getDefaultScalarControlProperties(definition, field, visible, defaultValue, scalarControl, formState.readonly);
|
|
78
87
|
},
|
|
79
88
|
useDisplayProperties: (fs, definition) => {
|
|
80
89
|
const visible = useIsControlVisible(definition, fs, useExpression);
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { SchemaField } from "./types";
|
|
1
2
|
export * from "./types";
|
|
2
3
|
export * from "./schemaBuilder";
|
|
3
4
|
export * from "./controlRender";
|
|
4
5
|
export * from "./hooks";
|
|
6
|
+
export declare function fieldHasTag(field: SchemaField, tag: string): boolean;
|
|
7
|
+
export declare function fieldDisplayName(field: SchemaField): string;
|
package/lib/index.js
CHANGED
|
@@ -14,7 +14,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.fieldDisplayName = exports.fieldHasTag = void 0;
|
|
17
18
|
__exportStar(require("./types"), exports);
|
|
18
19
|
__exportStar(require("./schemaBuilder"), exports);
|
|
19
20
|
__exportStar(require("./controlRender"), exports);
|
|
20
21
|
__exportStar(require("./hooks"), exports);
|
|
22
|
+
function fieldHasTag(field, tag) {
|
|
23
|
+
var _a;
|
|
24
|
+
return Boolean((_a = field.tags) === null || _a === void 0 ? void 0 : _a.includes(tag));
|
|
25
|
+
}
|
|
26
|
+
exports.fieldHasTag = fieldHasTag;
|
|
27
|
+
function fieldDisplayName(field) {
|
|
28
|
+
var _a;
|
|
29
|
+
return (_a = field.displayName) !== null && _a !== void 0 ? _a : field.field;
|
|
30
|
+
}
|
|
31
|
+
exports.fieldDisplayName = fieldDisplayName;
|
package/lib/schemaBuilder.d.ts
CHANGED
|
@@ -18,17 +18,17 @@ export declare function buildSchema<T>(def: {
|
|
|
18
18
|
}): SchemaField[];
|
|
19
19
|
export declare function stringField(displayName: string, options?: Partial<Omit<SchemaField, "type">>): (name: string) => SchemaField & {
|
|
20
20
|
field?: string | undefined;
|
|
21
|
-
displayName: string;
|
|
22
|
-
tags?: string[] | undefined;
|
|
23
|
-
system?: boolean | undefined;
|
|
24
|
-
collection?: boolean | undefined;
|
|
25
|
-
onlyForTypes?: string[] | undefined;
|
|
26
|
-
required?: boolean | undefined;
|
|
21
|
+
displayName: string | null;
|
|
22
|
+
tags?: string[] | null | undefined;
|
|
23
|
+
system?: boolean | null | undefined;
|
|
24
|
+
collection?: boolean | null | undefined;
|
|
25
|
+
onlyForTypes?: string[] | null | undefined;
|
|
26
|
+
required?: boolean | null | undefined;
|
|
27
27
|
defaultValue?: any;
|
|
28
|
-
isTypeField?: boolean | undefined;
|
|
29
|
-
searchable?: boolean | undefined;
|
|
30
|
-
options?: FieldOption[] | undefined;
|
|
31
|
-
restrictions?: import("./types").SchemaRestrictions | undefined;
|
|
28
|
+
isTypeField?: boolean | null | undefined;
|
|
29
|
+
searchable?: boolean | null | undefined;
|
|
30
|
+
options?: FieldOption[] | null | undefined;
|
|
31
|
+
restrictions?: import("./types").SchemaRestrictions | null | undefined;
|
|
32
32
|
type: FieldType.String;
|
|
33
33
|
};
|
|
34
34
|
export declare function stringOptionsField(displayName: string, ...options: FieldOption[]): (name: string) => SchemaField & {
|
|
@@ -43,32 +43,32 @@ export declare function makeCompoundField<S extends Partial<CompoundField>>(opti
|
|
|
43
43
|
} & S;
|
|
44
44
|
export declare function intField(displayName: string, options?: Partial<Omit<SchemaField, "type">>): (name: string) => SchemaField & {
|
|
45
45
|
field?: string | undefined;
|
|
46
|
-
displayName: string;
|
|
47
|
-
tags?: string[] | undefined;
|
|
48
|
-
system?: boolean | undefined;
|
|
49
|
-
collection?: boolean | undefined;
|
|
50
|
-
onlyForTypes?: string[] | undefined;
|
|
51
|
-
required?: boolean | undefined;
|
|
46
|
+
displayName: string | null;
|
|
47
|
+
tags?: string[] | null | undefined;
|
|
48
|
+
system?: boolean | null | undefined;
|
|
49
|
+
collection?: boolean | null | undefined;
|
|
50
|
+
onlyForTypes?: string[] | null | undefined;
|
|
51
|
+
required?: boolean | null | undefined;
|
|
52
52
|
defaultValue?: any;
|
|
53
|
-
isTypeField?: boolean | undefined;
|
|
54
|
-
searchable?: boolean | undefined;
|
|
55
|
-
options?: FieldOption[] | undefined;
|
|
56
|
-
restrictions?: import("./types").SchemaRestrictions | undefined;
|
|
53
|
+
isTypeField?: boolean | null | undefined;
|
|
54
|
+
searchable?: boolean | null | undefined;
|
|
55
|
+
options?: FieldOption[] | null | undefined;
|
|
56
|
+
restrictions?: import("./types").SchemaRestrictions | null | undefined;
|
|
57
57
|
type: FieldType.Int;
|
|
58
58
|
};
|
|
59
59
|
export declare function boolField(displayName: string, options?: Partial<Omit<SchemaField, "type">>): (name: string) => SchemaField & {
|
|
60
60
|
field?: string | undefined;
|
|
61
|
-
displayName: string;
|
|
62
|
-
tags?: string[] | undefined;
|
|
63
|
-
system?: boolean | undefined;
|
|
64
|
-
collection?: boolean | undefined;
|
|
65
|
-
onlyForTypes?: string[] | undefined;
|
|
66
|
-
required?: boolean | undefined;
|
|
61
|
+
displayName: string | null;
|
|
62
|
+
tags?: string[] | null | undefined;
|
|
63
|
+
system?: boolean | null | undefined;
|
|
64
|
+
collection?: boolean | null | undefined;
|
|
65
|
+
onlyForTypes?: string[] | null | undefined;
|
|
66
|
+
required?: boolean | null | undefined;
|
|
67
67
|
defaultValue?: any;
|
|
68
|
-
isTypeField?: boolean | undefined;
|
|
69
|
-
searchable?: boolean | undefined;
|
|
70
|
-
options?: FieldOption[] | undefined;
|
|
71
|
-
restrictions?: import("./types").SchemaRestrictions | undefined;
|
|
68
|
+
isTypeField?: boolean | null | undefined;
|
|
69
|
+
searchable?: boolean | null | undefined;
|
|
70
|
+
options?: FieldOption[] | null | undefined;
|
|
71
|
+
restrictions?: import("./types").SchemaRestrictions | null | undefined;
|
|
72
72
|
type: FieldType.Bool;
|
|
73
73
|
};
|
|
74
74
|
export declare function compoundField<Other extends Partial<Omit<CompoundField, "type" | "schemaType">>>(displayName: string, fields: SchemaField[], other: Other): (name: string) => CompoundField & {
|
package/lib/types.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
export interface SchemaField {
|
|
2
2
|
type: string;
|
|
3
3
|
field: string;
|
|
4
|
-
displayName?: string;
|
|
5
|
-
tags?: string[];
|
|
6
|
-
system?: boolean;
|
|
7
|
-
collection?: boolean;
|
|
8
|
-
onlyForTypes?: string[];
|
|
9
|
-
required?: boolean;
|
|
4
|
+
displayName?: string | null;
|
|
5
|
+
tags?: string[] | null;
|
|
6
|
+
system?: boolean | null;
|
|
7
|
+
collection?: boolean | null;
|
|
8
|
+
onlyForTypes?: string[] | null;
|
|
9
|
+
required?: boolean | null;
|
|
10
10
|
defaultValue?: any;
|
|
11
|
-
isTypeField?: boolean;
|
|
12
|
-
searchable?: boolean;
|
|
13
|
-
options?: FieldOption[];
|
|
11
|
+
isTypeField?: boolean | null;
|
|
12
|
+
searchable?: boolean | null;
|
|
13
|
+
options?: FieldOption[] | null;
|
|
14
14
|
/**
|
|
15
15
|
* @deprecated Use options directly
|
|
16
16
|
*/
|
|
17
|
-
restrictions?: SchemaRestrictions | undefined;
|
|
17
|
+
restrictions?: SchemaRestrictions | undefined | null;
|
|
18
18
|
}
|
|
19
19
|
export declare enum FieldType {
|
|
20
20
|
String = "String",
|
|
@@ -34,7 +34,7 @@ export interface EntityRefField extends SchemaField {
|
|
|
34
34
|
parentField: string;
|
|
35
35
|
}
|
|
36
36
|
export interface SchemaRestrictions {
|
|
37
|
-
options
|
|
37
|
+
options?: FieldOption[] | null;
|
|
38
38
|
}
|
|
39
39
|
export interface FieldOption {
|
|
40
40
|
name: string;
|
|
@@ -48,9 +48,9 @@ export interface CompoundField extends SchemaField {
|
|
|
48
48
|
export type AnyControlDefinition = DataControlDefinition | GroupedControlsDefinition | ActionControlDefinition | DisplayControlDefinition;
|
|
49
49
|
export interface ControlDefinition {
|
|
50
50
|
type: string;
|
|
51
|
-
title?: string;
|
|
52
|
-
dynamic?: DynamicProperty[];
|
|
53
|
-
adornments?: ControlAdornment[];
|
|
51
|
+
title?: string | null;
|
|
52
|
+
dynamic?: DynamicProperty[] | null;
|
|
53
|
+
adornments?: ControlAdornment[] | null;
|
|
54
54
|
}
|
|
55
55
|
export declare enum ControlDefinitionType {
|
|
56
56
|
Data = "Data",
|
|
@@ -101,10 +101,10 @@ export interface AccordionAdornment extends ControlAdornment {
|
|
|
101
101
|
export interface DataControlDefinition extends ControlDefinition {
|
|
102
102
|
type: ControlDefinitionType.Data;
|
|
103
103
|
field: string;
|
|
104
|
-
required?: boolean;
|
|
105
|
-
renderOptions?: RenderOptions;
|
|
104
|
+
required?: boolean | null;
|
|
105
|
+
renderOptions?: RenderOptions | null;
|
|
106
106
|
defaultValue?: any;
|
|
107
|
-
readonly?: boolean;
|
|
107
|
+
readonly?: boolean | null;
|
|
108
108
|
}
|
|
109
109
|
export interface RenderOptions {
|
|
110
110
|
type: string;
|
|
@@ -128,14 +128,14 @@ export interface HtmlEditorRenderOptions extends RenderOptions {
|
|
|
128
128
|
allowImages: boolean;
|
|
129
129
|
}
|
|
130
130
|
export interface DateTimeRenderOptions extends RenderOptions {
|
|
131
|
-
format?: string;
|
|
131
|
+
format?: string | null;
|
|
132
132
|
}
|
|
133
133
|
export interface IconListRenderOptions extends RenderOptions {
|
|
134
134
|
iconMappings: IconMapping[];
|
|
135
135
|
}
|
|
136
136
|
export interface IconMapping {
|
|
137
137
|
value: string;
|
|
138
|
-
materialIcon
|
|
138
|
+
materialIcon?: string | null;
|
|
139
139
|
}
|
|
140
140
|
export interface CheckListRenderOptions extends RenderOptions {
|
|
141
141
|
}
|
|
@@ -157,12 +157,12 @@ export interface IconSelectionRenderOptions extends RenderOptions {
|
|
|
157
157
|
export interface GroupedControlsDefinition extends ControlDefinition {
|
|
158
158
|
type: ControlDefinitionType.Group;
|
|
159
159
|
children: AnyControlDefinition[];
|
|
160
|
-
compoundField?: string;
|
|
160
|
+
compoundField?: string | null;
|
|
161
161
|
groupOptions: GroupRenderOptions;
|
|
162
162
|
}
|
|
163
163
|
export interface GroupRenderOptions {
|
|
164
164
|
type: GroupRenderType;
|
|
165
|
-
hideTitle?: boolean;
|
|
165
|
+
hideTitle?: boolean | null;
|
|
166
166
|
}
|
|
167
167
|
export declare enum GroupRenderType {
|
|
168
168
|
Standard = "Standard",
|
|
@@ -175,7 +175,7 @@ export interface GroupElementRenderer extends GroupRenderOptions {
|
|
|
175
175
|
value: any;
|
|
176
176
|
}
|
|
177
177
|
export interface GridRenderer extends GroupRenderOptions {
|
|
178
|
-
columns
|
|
178
|
+
columns?: number | null;
|
|
179
179
|
}
|
|
180
180
|
export interface DisplayControlDefinition extends ControlDefinition {
|
|
181
181
|
type: ControlDefinitionType.Display;
|
package/package.json
CHANGED
package/schemas.build.log
CHANGED
package/src/controlRender.tsx
CHANGED
|
@@ -12,7 +12,8 @@ import {
|
|
|
12
12
|
SchemaField,
|
|
13
13
|
} from "./types";
|
|
14
14
|
import React, { createContext, Key, ReactElement, useContext } from "react";
|
|
15
|
-
import { Control, newControl
|
|
15
|
+
import { Control, newControl } from "@react-typed-forms/core";
|
|
16
|
+
import { fieldDisplayName } from "./index";
|
|
16
17
|
|
|
17
18
|
export interface FormEditHooks {
|
|
18
19
|
useDataProperties(
|
|
@@ -36,15 +37,13 @@ export interface FormEditHooks {
|
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
export interface DataControlProperties {
|
|
39
|
-
|
|
40
|
+
control: Control<any>;
|
|
40
41
|
visible: boolean;
|
|
41
|
-
|
|
42
|
+
readonly: boolean;
|
|
42
43
|
defaultValue: any;
|
|
43
44
|
required: boolean;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
control: Control<any>
|
|
47
|
-
) => ReactElement;
|
|
45
|
+
options: FieldOption[] | undefined | null;
|
|
46
|
+
customRender?: (props: DataRendererProps) => ReactElement;
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
export interface GroupControlProperties {
|
|
@@ -235,12 +234,10 @@ export function findField(
|
|
|
235
234
|
): SchemaField | undefined {
|
|
236
235
|
return fields.find((x) => x.field === field);
|
|
237
236
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
export function controlTitle(title: string | undefined, field: SchemaField) {
|
|
237
|
+
export function controlTitle(
|
|
238
|
+
title: string | undefined | null,
|
|
239
|
+
field: SchemaField
|
|
240
|
+
) {
|
|
244
241
|
return title ? title : fieldDisplayName(field);
|
|
245
242
|
}
|
|
246
243
|
|
|
@@ -321,20 +318,6 @@ function DataRenderer({
|
|
|
321
318
|
}) {
|
|
322
319
|
const renderer = useFormRendererComponents();
|
|
323
320
|
const props = hooks.useDataProperties(formState, controlDef, fieldData);
|
|
324
|
-
const scalarControl =
|
|
325
|
-
formState.data.fields[fieldData.field] ?? newControl(undefined);
|
|
326
|
-
useControlEffect(
|
|
327
|
-
() => scalarControl.value,
|
|
328
|
-
(v) => {
|
|
329
|
-
if (props.defaultValue && !v) {
|
|
330
|
-
scalarControl.value = props.defaultValue;
|
|
331
|
-
}
|
|
332
|
-
},
|
|
333
|
-
true
|
|
334
|
-
);
|
|
335
|
-
if (!props.visible) {
|
|
336
|
-
return <></>;
|
|
337
|
-
}
|
|
338
321
|
const scalarProps: DataRendererProps = {
|
|
339
322
|
formEditState: formState,
|
|
340
323
|
field: fieldData,
|
|
@@ -344,7 +327,7 @@ function DataRenderer({
|
|
|
344
327
|
return wrapElem(
|
|
345
328
|
(props.customRender ?? renderer.renderData)(
|
|
346
329
|
scalarProps,
|
|
347
|
-
|
|
330
|
+
props.control,
|
|
348
331
|
false,
|
|
349
332
|
renderer
|
|
350
333
|
)
|
|
@@ -367,10 +350,6 @@ function ActionRenderer({
|
|
|
367
350
|
formState,
|
|
368
351
|
actionDef
|
|
369
352
|
);
|
|
370
|
-
if (!actionControlProperties.visible) {
|
|
371
|
-
return <></>;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
353
|
return wrapElem(
|
|
375
354
|
renderAction({ definition: actionDef, properties: actionControlProperties })
|
|
376
355
|
);
|
|
@@ -390,9 +369,6 @@ function GroupRenderer({
|
|
|
390
369
|
const renderers = useFormRendererComponents();
|
|
391
370
|
|
|
392
371
|
const groupProps = hooks.useGroupProperties(formState, groupDef, hooks);
|
|
393
|
-
if (!groupProps.visible) {
|
|
394
|
-
return <></>;
|
|
395
|
-
}
|
|
396
372
|
const compoundField = groupDef.compoundField
|
|
397
373
|
? findCompoundField(formState.fields, groupDef.compoundField)
|
|
398
374
|
: undefined;
|
|
@@ -452,9 +428,6 @@ function DisplayRenderer({
|
|
|
452
428
|
const { renderDisplay } = useFormRendererComponents();
|
|
453
429
|
|
|
454
430
|
const displayProps = hooks.useDisplayProperties(formState, displayDef);
|
|
455
|
-
if (!displayProps.visible) {
|
|
456
|
-
return <></>;
|
|
457
|
-
}
|
|
458
431
|
return wrapElem(
|
|
459
432
|
renderDisplay({ definition: displayDef, properties: displayProps })
|
|
460
433
|
);
|
package/src/hooks.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ActionControlDefinition,
|
|
3
|
+
ControlDefinition,
|
|
3
4
|
DataControlDefinition,
|
|
4
5
|
DynamicPropertyType,
|
|
5
6
|
EntityExpression,
|
|
6
7
|
ExpressionType,
|
|
7
8
|
FieldOption,
|
|
8
9
|
FieldValueExpression,
|
|
9
|
-
ControlDefinition,
|
|
10
10
|
SchemaField,
|
|
11
11
|
} from "./types";
|
|
12
12
|
import {
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
isGroupControl,
|
|
21
21
|
isScalarField,
|
|
22
22
|
} from "./controlRender";
|
|
23
|
-
import { useMemo } from "react";
|
|
23
|
+
import { useEffect, useMemo } from "react";
|
|
24
24
|
import { Control, newControl } from "@react-typed-forms/core";
|
|
25
25
|
|
|
26
26
|
export type ExpressionHook = (
|
|
@@ -83,24 +83,26 @@ export function useIsControlVisible(
|
|
|
83
83
|
);
|
|
84
84
|
}
|
|
85
85
|
export function getDefaultScalarControlProperties(
|
|
86
|
-
|
|
86
|
+
definition: DataControlDefinition,
|
|
87
87
|
field: SchemaField,
|
|
88
88
|
visible: boolean,
|
|
89
89
|
defaultValue: any,
|
|
90
|
+
control: Control<any>,
|
|
90
91
|
readonly?: boolean
|
|
91
92
|
): DataControlProperties {
|
|
92
93
|
return {
|
|
93
94
|
defaultValue,
|
|
94
95
|
options: getOptionsForScalarField(field),
|
|
95
|
-
required:
|
|
96
|
+
required: definition.required ?? false,
|
|
96
97
|
visible,
|
|
97
|
-
readonly: readonly ??
|
|
98
|
+
readonly: readonly ?? definition.readonly ?? false,
|
|
99
|
+
control,
|
|
98
100
|
};
|
|
99
101
|
}
|
|
100
102
|
|
|
101
103
|
export function getOptionsForScalarField(
|
|
102
104
|
field: SchemaField
|
|
103
|
-
): FieldOption[] | undefined {
|
|
105
|
+
): FieldOption[] | undefined | null {
|
|
104
106
|
const opts = field.options ?? field.restrictions?.options;
|
|
105
107
|
if (opts?.length ?? 0 > 0) {
|
|
106
108
|
return opts;
|
|
@@ -137,11 +139,20 @@ export function createFormEditHooks(
|
|
|
137
139
|
formState,
|
|
138
140
|
useExpression
|
|
139
141
|
);
|
|
142
|
+
const scalarControl = formState.data.fields[field.field];
|
|
143
|
+
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
if (!visible) scalarControl.value = null;
|
|
146
|
+
else if (scalarControl.current.value == null) {
|
|
147
|
+
scalarControl.value = defaultValue;
|
|
148
|
+
}
|
|
149
|
+
}, [visible, defaultValue]);
|
|
140
150
|
return getDefaultScalarControlProperties(
|
|
141
151
|
definition,
|
|
142
152
|
field,
|
|
143
153
|
visible,
|
|
144
154
|
defaultValue,
|
|
155
|
+
scalarControl,
|
|
145
156
|
formState.readonly
|
|
146
157
|
);
|
|
147
158
|
},
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
+
import { SchemaField } from "./types";
|
|
2
|
+
|
|
1
3
|
export * from "./types";
|
|
2
4
|
export * from "./schemaBuilder";
|
|
3
5
|
export * from "./controlRender";
|
|
4
6
|
export * from "./hooks";
|
|
7
|
+
|
|
8
|
+
export function fieldHasTag(field: SchemaField, tag: string) {
|
|
9
|
+
return Boolean(field.tags?.includes(tag));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function fieldDisplayName(field: SchemaField) {
|
|
13
|
+
return field.displayName ?? field.field;
|
|
14
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
export interface SchemaField {
|
|
2
2
|
type: string;
|
|
3
3
|
field: string;
|
|
4
|
-
displayName?: string;
|
|
5
|
-
tags?: string[];
|
|
6
|
-
system?: boolean;
|
|
7
|
-
collection?: boolean;
|
|
8
|
-
onlyForTypes?: string[];
|
|
9
|
-
required?: boolean;
|
|
4
|
+
displayName?: string | null;
|
|
5
|
+
tags?: string[] | null;
|
|
6
|
+
system?: boolean | null;
|
|
7
|
+
collection?: boolean | null;
|
|
8
|
+
onlyForTypes?: string[] | null;
|
|
9
|
+
required?: boolean | null;
|
|
10
10
|
defaultValue?: any;
|
|
11
|
-
isTypeField?: boolean;
|
|
12
|
-
searchable?: boolean;
|
|
13
|
-
options?: FieldOption[];
|
|
11
|
+
isTypeField?: boolean | null;
|
|
12
|
+
searchable?: boolean | null;
|
|
13
|
+
options?: FieldOption[] | null;
|
|
14
14
|
/**
|
|
15
15
|
* @deprecated Use options directly
|
|
16
16
|
*/
|
|
17
|
-
restrictions?: SchemaRestrictions | undefined;
|
|
17
|
+
restrictions?: SchemaRestrictions | undefined | null;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export enum FieldType {
|
|
@@ -37,7 +37,7 @@ export interface EntityRefField extends SchemaField {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export interface SchemaRestrictions {
|
|
40
|
-
options
|
|
40
|
+
options?: FieldOption[] | null;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
export interface FieldOption {
|
|
@@ -59,9 +59,9 @@ export type AnyControlDefinition =
|
|
|
59
59
|
|
|
60
60
|
export interface ControlDefinition {
|
|
61
61
|
type: string;
|
|
62
|
-
title?: string;
|
|
63
|
-
dynamic?: DynamicProperty[];
|
|
64
|
-
adornments?: ControlAdornment[];
|
|
62
|
+
title?: string | null;
|
|
63
|
+
dynamic?: DynamicProperty[] | null;
|
|
64
|
+
adornments?: ControlAdornment[] | null;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
export enum ControlDefinitionType {
|
|
@@ -125,10 +125,10 @@ export interface AccordionAdornment extends ControlAdornment {
|
|
|
125
125
|
export interface DataControlDefinition extends ControlDefinition {
|
|
126
126
|
type: ControlDefinitionType.Data;
|
|
127
127
|
field: string;
|
|
128
|
-
required?: boolean;
|
|
129
|
-
renderOptions?: RenderOptions;
|
|
128
|
+
required?: boolean | null;
|
|
129
|
+
renderOptions?: RenderOptions | null;
|
|
130
130
|
defaultValue?: any;
|
|
131
|
-
readonly?: boolean;
|
|
131
|
+
readonly?: boolean | null;
|
|
132
132
|
}
|
|
133
133
|
|
|
134
134
|
export interface RenderOptions {
|
|
@@ -156,7 +156,7 @@ export interface HtmlEditorRenderOptions extends RenderOptions {
|
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
export interface DateTimeRenderOptions extends RenderOptions {
|
|
159
|
-
format?: string;
|
|
159
|
+
format?: string | null;
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
export interface IconListRenderOptions extends RenderOptions {
|
|
@@ -165,7 +165,7 @@ export interface IconListRenderOptions extends RenderOptions {
|
|
|
165
165
|
|
|
166
166
|
export interface IconMapping {
|
|
167
167
|
value: string;
|
|
168
|
-
materialIcon
|
|
168
|
+
materialIcon?: string | null;
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
export interface CheckListRenderOptions extends RenderOptions {}
|
|
@@ -191,13 +191,13 @@ export interface IconSelectionRenderOptions extends RenderOptions {}
|
|
|
191
191
|
export interface GroupedControlsDefinition extends ControlDefinition {
|
|
192
192
|
type: ControlDefinitionType.Group;
|
|
193
193
|
children: AnyControlDefinition[];
|
|
194
|
-
compoundField?: string;
|
|
194
|
+
compoundField?: string | null;
|
|
195
195
|
groupOptions: GroupRenderOptions;
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
export interface GroupRenderOptions {
|
|
199
199
|
type: GroupRenderType;
|
|
200
|
-
hideTitle?: boolean;
|
|
200
|
+
hideTitle?: boolean | null;
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
export enum GroupRenderType {
|
|
@@ -213,7 +213,7 @@ export interface GroupElementRenderer extends GroupRenderOptions {
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
export interface GridRenderer extends GroupRenderOptions {
|
|
216
|
-
columns
|
|
216
|
+
columns?: number | null;
|
|
217
217
|
}
|
|
218
218
|
|
|
219
219
|
export interface DisplayControlDefinition extends ControlDefinition {
|