@react-typed-forms/schemas 3.0.0-dev.80 → 3.0.0-dev.82

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,12 +1,12 @@
1
1
  {
2
2
  "files": {
3
3
  "packages/schemas/.rush/temp/shrinkwrap-deps.json": "6ad43bbf71e5f191d9f571da269a25bb4d9e8909",
4
- "packages/schemas/package.json": "1ad0d4be24af79b4e7ee7b5ca9cd4142f8b4e8e0",
5
- "packages/schemas/src/controlRender.tsx": "8f985c464635df4ea471535da80c2d002eb0b4a3",
6
- "packages/schemas/src/hooks.ts": "89bcc068bd3a38a25f733cc1404649af82510e91",
4
+ "packages/schemas/package.json": "30089b42ee97d4ff79f147c02a8ddf0592ab48e3",
5
+ "packages/schemas/src/controlRender.tsx": "4bd6de1b15f9b75163358ef2b91d2e2fb9ca6f6b",
6
+ "packages/schemas/src/hooks.ts": "a34545b7c63dc78ea77974f6c2fc386daaee80ea",
7
7
  "packages/schemas/src/index.ts": "b15fbb6de132cd912cf8141d5c4d2329e947546e",
8
- "packages/schemas/src/schemaBuilder.ts": "3a36cbc35f70fca2b561f6d4ddc1bd1030eeb3f6",
9
- "packages/schemas/src/types.ts": "31659d00c1a1cfb2808af2c43c8a1c0ec271458d",
8
+ "packages/schemas/src/schemaBuilder.ts": "4662698fd46ac14459f429ce24d919ef91992de2",
9
+ "packages/schemas/src/types.ts": "0f55d35f7e4b052a01bc433799647d8288e237ab",
10
10
  "packages/schemas/tsconfig.json": "52e4f044fa580f4e0ad5830f3cfca574e2788372"
11
11
  },
12
12
  "arguments": "rimraf ./lib/ && tsc "
@@ -81,7 +81,8 @@ export declare function applyDefaultValues(v: {
81
81
  } | undefined, fields: SchemaField[]): any;
82
82
  export declare function applyDefaultForField(v: any, field: SchemaField, parent: SchemaField[], notElement?: boolean): any;
83
83
  export declare function defaultValueForFields(fields: SchemaField[]): any;
84
- export declare function defaultValueForField(sf: SchemaField, notElement?: boolean): any;
84
+ export declare function defaultValueForField(sf: SchemaField): any;
85
+ export declare function elementValueForField(sf: SchemaField): any;
85
86
  export declare function findScalarField(fields: SchemaField[], field: string): ScalarField | undefined;
86
87
  export declare function findCompoundField(fields: SchemaField[], field: string): CompoundField | undefined;
87
88
  export declare function findField(fields: SchemaField[], field: string): SchemaField | undefined;
@@ -23,7 +23,7 @@ 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.isSchemaControl = exports.fieldForControl = exports.controlForField = exports.renderControl = exports.controlTitle = exports.fieldDisplayName = exports.findField = exports.findCompoundField = exports.findScalarField = exports.defaultValueForField = exports.defaultValueForFields = exports.applyDefaultForField = exports.applyDefaultValues = exports.isCompoundField = exports.isScalarField = exports.useFormRendererComponents = exports.FormRendererComponentsContext = void 0;
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
27
  const types_1 = require("./types");
28
28
  const react_1 = __importStar(require("react"));
29
29
  const core_1 = require("@react-typed-forms/core");
@@ -55,7 +55,7 @@ function applyDefaultValues(v, fields) {
55
55
  out[x.field] =
56
56
  x.field in v
57
57
  ? applyDefaultForField(v[x.field], x, fields)
58
- : defaultValueForField(x, true);
58
+ : defaultValueForField(x);
59
59
  });
60
60
  return out;
61
61
  }
@@ -66,24 +66,37 @@ function applyDefaultForField(v, field, parent, notElement) {
66
66
  return ((_a = v) !== null && _a !== void 0 ? _a : []).map((x) => applyDefaultForField(x, field, parent, true));
67
67
  }
68
68
  if (isCompoundField(field)) {
69
+ if (!v && !field.required)
70
+ return v;
69
71
  return applyDefaultValues(v, field.treeChildren ? parent : field.children);
70
72
  }
71
- return defaultValueForField(field, true);
73
+ return defaultValueForField(field);
72
74
  }
73
75
  exports.applyDefaultForField = applyDefaultForField;
74
76
  function defaultValueForFields(fields) {
75
- return Object.fromEntries(fields.map((x) => [x.field, defaultValueForField(x, true)]));
77
+ return Object.fromEntries(fields.map((x) => [x.field, defaultValueForField(x)]));
76
78
  }
77
79
  exports.defaultValueForFields = defaultValueForFields;
78
- function defaultValueForField(sf, notElement) {
79
- if (notElement && sf.collection)
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)
80
89
  return [];
90
+ return sf.defaultValue;
91
+ }
92
+ exports.defaultValueForField = defaultValueForField;
93
+ function elementValueForField(sf) {
81
94
  if (isCompoundField(sf)) {
82
95
  return defaultValueForFields(sf.children);
83
96
  }
84
97
  return sf.defaultValue;
85
98
  }
86
- exports.defaultValueForField = defaultValueForField;
99
+ exports.elementValueForField = elementValueForField;
87
100
  function findScalarField(fields, field) {
88
101
  return findField(fields, field);
89
102
  }
package/lib/hooks.js CHANGED
@@ -21,16 +21,22 @@ function useIsControlVisible(definition, formState, useExpression) {
21
21
  return Boolean(useExpression(visibleExpression.expr, formState));
22
22
  }
23
23
  const schemaFields = formState.fields;
24
- const typeControl = (0, react_1.useMemo)(() => {
24
+ const { typeControl, compoundField } = (0, react_1.useMemo)(() => {
25
25
  var _a, _b;
26
26
  const typeField = schemaFields.find((x) => (0, controlRender_1.isScalarField)(x) && x.isTypeField);
27
- return ((_b = (typeField && ((_a = formState.data.fields) === null || _a === void 0 ? void 0 : _a[typeField.field]))) !== null && _b !== void 0 ? _b : (0, core_1.newControl)(undefined));
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 };
28
33
  }, [schemaFields, formState.data]);
29
34
  const fieldName = (0, controlRender_1.fieldForControl)(definition);
30
35
  const onlyForTypes = (_b = (fieldName ? (0, controlRender_1.findField)(schemaFields, fieldName) : undefined)) === null || _b === void 0 ? void 0 : _b.onlyForTypes;
31
- return (0, core_1.useControlValue)(() => !onlyForTypes ||
32
- onlyForTypes.length === 0 ||
33
- Boolean(typeControl.value && onlyForTypes.includes(typeControl.value)));
36
+ return (0, core_1.useControlValue)(() => (!compoundField || compoundField.value != null) &&
37
+ (!onlyForTypes ||
38
+ onlyForTypes.length === 0 ||
39
+ Boolean(typeControl.value && onlyForTypes.includes(typeControl.value))));
34
40
  }
35
41
  exports.useIsControlVisible = useIsControlVisible;
36
42
  function getDefaultScalarControlProperties(control, field, visible, defaultValue, readonly) {
@@ -29,8 +29,8 @@ export declare function stringField(displayName: string, options?: Partial<Omit<
29
29
  system?: boolean | undefined;
30
30
  collection?: boolean | undefined;
31
31
  onlyForTypes?: string[] | undefined;
32
- entityRefType?: string | undefined;
33
32
  required?: boolean | undefined;
33
+ entityRefType?: string | undefined;
34
34
  parentField?: string | undefined;
35
35
  searchable?: boolean | undefined;
36
36
  defaultValue?: any;
@@ -64,8 +64,8 @@ export declare function intField(displayName: string, options?: Partial<Omit<Sca
64
64
  system?: boolean | undefined;
65
65
  collection?: boolean | undefined;
66
66
  onlyForTypes?: string[] | undefined;
67
- entityRefType?: string | undefined;
68
67
  required?: boolean | undefined;
68
+ entityRefType?: string | undefined;
69
69
  parentField?: string | undefined;
70
70
  searchable?: boolean | undefined;
71
71
  defaultValue?: any;
@@ -82,8 +82,8 @@ export declare function boolField(displayName: string, options?: Partial<Omit<Sc
82
82
  system?: boolean | undefined;
83
83
  collection?: boolean | undefined;
84
84
  onlyForTypes?: string[] | undefined;
85
- entityRefType?: string | undefined;
86
85
  required?: boolean | undefined;
86
+ entityRefType?: string | undefined;
87
87
  parentField?: string | undefined;
88
88
  searchable?: boolean | undefined;
89
89
  defaultValue?: any;
@@ -76,6 +76,7 @@ function defaultCompoundField(field, displayName, collection) {
76
76
  treeChildren: false,
77
77
  children: [],
78
78
  onlyForTypes: [],
79
+ required: true,
79
80
  };
80
81
  }
81
82
  exports.defaultCompoundField = defaultCompoundField;
package/lib/types.d.ts CHANGED
@@ -7,6 +7,7 @@ export interface SchemaField {
7
7
  system: boolean;
8
8
  collection: boolean;
9
9
  onlyForTypes: string[];
10
+ required: boolean;
10
11
  }
11
12
  export declare enum SchemaFieldType {
12
13
  Scalar = "Scalar",
@@ -26,7 +27,6 @@ export declare enum FieldType {
26
27
  }
27
28
  export interface ScalarField extends SchemaField {
28
29
  entityRefType: string;
29
- required: boolean;
30
30
  parentField: string;
31
31
  searchable: boolean;
32
32
  defaultValue: any;
@@ -44,7 +44,7 @@ export interface CompoundField extends SchemaField {
44
44
  children: SchemaField[];
45
45
  treeChildren: boolean;
46
46
  }
47
- export type AnyControlDefinition = ControlDefinition | DataControlDefinition | GroupedControlsDefinition | ActionControlDefinition | DisplayControlDefinition;
47
+ export type AnyControlDefinition = DataControlDefinition | GroupedControlsDefinition | ActionControlDefinition | DisplayControlDefinition;
48
48
  export interface ControlDefinition {
49
49
  type: string;
50
50
  title?: string;
@@ -116,7 +116,8 @@ export declare enum DataRenderType {
116
116
  CheckList = "CheckList",
117
117
  UserSelection = "UserSelection",
118
118
  Synchronised = "Synchronised",
119
- IconSelector = "IconSelector"
119
+ IconSelector = "IconSelector",
120
+ DateTime = "DateTime"
120
121
  }
121
122
  export interface RadioButtonRenderOptions extends RenderOptions {
122
123
  }
@@ -125,6 +126,9 @@ export interface StandardRenderer extends RenderOptions {
125
126
  export interface HtmlEditorRenderOptions extends RenderOptions {
126
127
  allowImages: boolean;
127
128
  }
129
+ export interface DateTimeRenderOptions extends RenderOptions {
130
+ format?: string;
131
+ }
128
132
  export interface IconListRenderOptions extends RenderOptions {
129
133
  iconMappings: IconMapping[];
130
134
  }
@@ -152,12 +156,12 @@ export interface IconSelectionRenderOptions extends RenderOptions {
152
156
  export interface GroupedControlsDefinition extends ControlDefinition {
153
157
  type: ControlDefinitionType.Group;
154
158
  children: AnyControlDefinition[];
155
- compoundField: string | undefined;
159
+ compoundField?: string;
156
160
  groupOptions: GroupRenderOptions;
157
161
  }
158
162
  export interface GroupRenderOptions {
159
163
  type: GroupRenderType;
160
- hideTitle: boolean;
164
+ hideTitle?: boolean;
161
165
  }
162
166
  export declare enum GroupRenderType {
163
167
  Standard = "Standard",
package/lib/types.js CHANGED
@@ -52,6 +52,7 @@ var DataRenderType;
52
52
  DataRenderType["UserSelection"] = "UserSelection";
53
53
  DataRenderType["Synchronised"] = "Synchronised";
54
54
  DataRenderType["IconSelector"] = "IconSelector";
55
+ DataRenderType["DateTime"] = "DateTime";
55
56
  })(DataRenderType = exports.DataRenderType || (exports.DataRenderType = {}));
56
57
  var SyncTextType;
57
58
  (function (SyncTextType) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-typed-forms/schemas",
3
- "version": "3.0.0-dev.80",
3
+ "version": "3.0.0-dev.82",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  "material-ui"
25
25
  ],
26
26
  "dependencies": {
27
- "@react-typed-forms/core": "3.0.0-dev.80"
27
+ "@react-typed-forms/core": "3.0.0-dev.82"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/react": "^18.0.22",
package/schemas.build.log CHANGED
@@ -1,2 +0,0 @@
1
- Invoking: rimraf ./lib/ && tsc
2
-
@@ -175,7 +175,7 @@ export function applyDefaultValues(
175
175
  out[x.field] =
176
176
  x.field in v
177
177
  ? applyDefaultForField(v[x.field], x, fields)
178
- : defaultValueForField(x, true);
178
+ : defaultValueForField(x);
179
179
  });
180
180
  return out;
181
181
  }
@@ -192,22 +192,31 @@ export function applyDefaultForField(
192
192
  );
193
193
  }
194
194
  if (isCompoundField(field)) {
195
+ if (!v && !field.required) return v;
195
196
  return applyDefaultValues(v, field.treeChildren ? parent : field.children);
196
197
  }
197
- return defaultValueForField(field, true);
198
+ return defaultValueForField(field);
198
199
  }
199
200
 
200
201
  export function defaultValueForFields(fields: SchemaField[]): any {
201
202
  return Object.fromEntries(
202
- fields.map((x) => [x.field, defaultValueForField(x, true)])
203
+ fields.map((x) => [x.field, defaultValueForField(x)])
203
204
  );
204
205
  }
205
206
 
206
- export function defaultValueForField(
207
- sf: SchemaField,
208
- notElement?: boolean
209
- ): any {
210
- if (notElement && sf.collection) return [];
207
+ export function defaultValueForField(sf: SchemaField): any {
208
+ if (isCompoundField(sf)) {
209
+ return sf.required
210
+ ? sf.collection
211
+ ? []
212
+ : defaultValueForFields(sf.children)
213
+ : undefined;
214
+ }
215
+ if (sf.collection) return [];
216
+ return (sf as ScalarField).defaultValue;
217
+ }
218
+
219
+ export function elementValueForField(sf: SchemaField): any {
211
220
  if (isCompoundField(sf)) {
212
221
  return defaultValueForFields(sf.children);
213
222
  }
@@ -297,7 +306,7 @@ export function renderControl(
297
306
  />
298
307
  );
299
308
  default:
300
- return <h1>Unknown control: {definition.type}</h1>;
309
+ return <h1>Unknown control: {(definition as any).type}</h1>;
301
310
  }
302
311
 
303
312
  function wrapElem(e: ReactElement): ReactElement {
@@ -406,7 +415,7 @@ function GroupRenderer({
406
415
  properties: groupProps,
407
416
  renderChild: (k, c, data, wrapChild) =>
408
417
  renderControl(
409
- c,
418
+ c as AnyControlDefinition,
410
419
  {
411
420
  ...formState,
412
421
  fields: compoundField!.children,
package/src/hooks.ts CHANGED
@@ -17,6 +17,7 @@ import {
17
17
  findField,
18
18
  FormEditHooks,
19
19
  FormEditState,
20
+ isGroupControl,
20
21
  isScalarField,
21
22
  } from "./controlRender";
22
23
  import { useMemo } from "react";
@@ -54,26 +55,34 @@ export function useIsControlVisible(
54
55
  }
55
56
  const schemaFields = formState.fields;
56
57
 
57
- const typeControl = useMemo(() => {
58
+ const { typeControl, compoundField } = useMemo(() => {
58
59
  const typeField = schemaFields.find(
59
60
  (x) => isScalarField(x) && x.isTypeField
60
61
  ) as ScalarField | undefined;
61
- return ((typeField && formState.data.fields?.[typeField.field]) ??
62
+
63
+ const typeControl = ((typeField &&
64
+ formState.data.fields?.[typeField.field]) ??
62
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 };
63
71
  }, [schemaFields, formState.data]);
64
72
 
65
73
  const fieldName = fieldForControl(definition);
66
74
  const onlyForTypes = (
67
75
  fieldName ? findField(schemaFields, fieldName) : undefined
68
76
  )?.onlyForTypes;
77
+
69
78
  return useControlValue(
70
79
  () =>
71
- !onlyForTypes ||
72
- onlyForTypes.length === 0 ||
73
- Boolean(typeControl.value && onlyForTypes.includes(typeControl.value))
80
+ (!compoundField || compoundField.value != null) &&
81
+ (!onlyForTypes ||
82
+ onlyForTypes.length === 0 ||
83
+ Boolean(typeControl.value && onlyForTypes.includes(typeControl.value)))
74
84
  );
75
85
  }
76
-
77
86
  export function getDefaultScalarControlProperties(
78
87
  control: DataControlDefinition,
79
88
  field: ScalarField,
@@ -162,5 +162,6 @@ export function defaultCompoundField(
162
162
  treeChildren: false,
163
163
  children: [],
164
164
  onlyForTypes: [],
165
+ required: true,
165
166
  };
166
167
  }
package/src/types.ts CHANGED
@@ -7,6 +7,7 @@ export interface SchemaField {
7
7
  system: boolean;
8
8
  collection: boolean;
9
9
  onlyForTypes: string[];
10
+ required: boolean;
10
11
  }
11
12
 
12
13
  export enum SchemaFieldType {
@@ -29,7 +30,6 @@ export enum FieldType {
29
30
 
30
31
  export interface ScalarField extends SchemaField {
31
32
  entityRefType: string;
32
- required: boolean;
33
33
  parentField: string;
34
34
  searchable: boolean;
35
35
  defaultValue: any;
@@ -52,7 +52,6 @@ export interface CompoundField extends SchemaField {
52
52
  }
53
53
 
54
54
  export type AnyControlDefinition =
55
- | ControlDefinition
56
55
  | DataControlDefinition
57
56
  | GroupedControlsDefinition
58
57
  | ActionControlDefinition
@@ -145,6 +144,8 @@ export enum DataRenderType {
145
144
  UserSelection = "UserSelection",
146
145
  Synchronised = "Synchronised",
147
146
  IconSelector = "IconSelector",
147
+
148
+ DateTime = "DateTime",
148
149
  }
149
150
 
150
151
  export interface RadioButtonRenderOptions extends RenderOptions {}
@@ -155,6 +156,10 @@ export interface HtmlEditorRenderOptions extends RenderOptions {
155
156
  allowImages: boolean;
156
157
  }
157
158
 
159
+ export interface DateTimeRenderOptions extends RenderOptions {
160
+ format?: string;
161
+ }
162
+
158
163
  export interface IconListRenderOptions extends RenderOptions {
159
164
  iconMappings: IconMapping[];
160
165
  }
@@ -187,13 +192,13 @@ export interface IconSelectionRenderOptions extends RenderOptions {}
187
192
  export interface GroupedControlsDefinition extends ControlDefinition {
188
193
  type: ControlDefinitionType.Group;
189
194
  children: AnyControlDefinition[];
190
- compoundField: string | undefined;
195
+ compoundField?: string;
191
196
  groupOptions: GroupRenderOptions;
192
197
  }
193
198
 
194
199
  export interface GroupRenderOptions {
195
200
  type: GroupRenderType;
196
- hideTitle: boolean;
201
+ hideTitle?: boolean;
197
202
  }
198
203
 
199
204
  export enum GroupRenderType {