@react-typed-forms/schemas 1.0.0-dev.3 → 1.0.0-dev.5

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": "5ce6417fae765db5c61e400f851ecafe0ce756f0",
4
- "packages/schemas/package.json": "38baed2ed08306a8db8dbeb4649adb4f294fe615",
5
- "packages/schemas/src/controlRender.tsx": "340cfc1f2fedb77b3e456c2cf8bf50f40b808c36",
6
- "packages/schemas/src/hooks.ts": "982c0ee0c7749fc3c58f01ac8c44b8174f898861",
7
- "packages/schemas/src/index.ts": "b15fbb6de132cd912cf8141d5c4d2329e947546e",
4
+ "packages/schemas/package.json": "5202ff608c6285e7e95527d5d9fe442452e8a227",
5
+ "packages/schemas/src/controlRender.tsx": "3a1df1f3ade46594dbc15bca1228dc0d57e55da0",
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": "2ff2a346d753da0eca2fe4acbe1b709f72034e5c",
9
+ "packages/schemas/src/types.ts": "69013139f9905b1c995c88b0cd58bba71ccc85d6",
10
10
  "packages/schemas/tsconfig.json": "52e4f044fa580f4e0ad5830f3cfca574e2788372"
11
11
  },
12
12
  "arguments": "rimraf ./lib/ && tsc "
@@ -1,4 +1,4 @@
1
- import { ActionControlDefinition, AnyControlDefinition, CompoundField, ControlDefinition, DataControlDefinition, DisplayControlDefinition, FieldOption, GroupedControlsDefinition, SchemaField } from "./types";
1
+ import { ActionControlDefinition, CompoundField, ControlDefinition, DataControlDefinition, DisplayControlDefinition, FieldOption, GroupedControlsDefinition, SchemaField } from "./types";
2
2
  import React, { Key, ReactElement } from "react";
3
3
  import { Control } from "@react-typed-forms/core";
4
4
  export interface FormEditHooks {
@@ -13,7 +13,7 @@ export interface DataControlProperties {
13
13
  readonly: boolean;
14
14
  defaultValue: any;
15
15
  required: boolean;
16
- options: FieldOption[] | undefined;
16
+ options: FieldOption[] | undefined | null;
17
17
  customRender?: (props: DataRendererProps) => ReactElement;
18
18
  }
19
19
  export interface GroupControlProperties {
@@ -87,10 +87,9 @@ export declare function elementValueForField(sf: SchemaField): any;
87
87
  export declare function findScalarField(fields: SchemaField[], field: string): SchemaField | undefined;
88
88
  export declare function findCompoundField(fields: SchemaField[], field: string): CompoundField | undefined;
89
89
  export declare function findField(fields: SchemaField[], field: string): SchemaField | undefined;
90
- export declare function fieldDisplayName(sf: SchemaField): string;
91
- export declare function controlTitle(title: string | undefined, field: SchemaField): string;
92
- export declare function renderControl(definition: AnyControlDefinition, formState: FormEditState, hooks: FormEditHooks, key: Key, wrapChild?: (key: Key, db: ReactElement) => ReactElement): ReactElement;
90
+ export declare function controlTitle(title: string | undefined | null, field: SchemaField): string;
91
+ export declare function renderControl<S extends ControlDefinition>(definition: S, formState: FormEditState, hooks: FormEditHooks, key: Key, wrapChild?: (key: Key, db: ReactElement) => ReactElement): ReactElement;
93
92
  export declare function controlForField(field: string, formState: FormEditState): Control<any>;
94
- export declare function fieldForControl(c: ControlDefinition): string | undefined;
93
+ export declare function fieldForControl(c: ControlDefinition): string | null | undefined;
95
94
  export declare function isDataControl(c: ControlDefinition): c is DataControlDefinition;
96
95
  export declare function isGroupControl(c: ControlDefinition): c is GroupedControlsDefinition;
@@ -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.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;
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,36 +110,27 @@ 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) {
121
118
  const { fields } = formState;
122
- switch (definition.type) {
123
- case types_1.ControlDefinitionType.Data:
124
- const def = definition;
119
+ return (0, types_1.visitControlDefinition)(definition, {
120
+ data: (def) => {
125
121
  const fieldData = findScalarField(fields, def.field);
126
122
  if (!fieldData)
127
123
  return react_1.default.createElement("h1", null,
128
124
  "No schema field for: ",
129
125
  def.field);
130
126
  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
- }
127
+ },
128
+ group: (d) => (react_1.default.createElement(GroupRenderer, { key: key, hooks: hooks, groupDef: d, formState: formState, wrapElem: wrapElem })),
129
+ action: (d) => (react_1.default.createElement(ActionRenderer, { key: key, hooks: hooks, formState: formState, wrapElem: wrapElem, actionDef: d })),
130
+ display: (d) => (react_1.default.createElement(DisplayRenderer, { key: key, hooks: hooks, formState: formState, wrapElem: wrapElem, displayDef: d })),
131
+ }, () => react_1.default.createElement("h1", null,
132
+ "Unknown control: ",
133
+ definition.type));
142
134
  function wrapElem(e) {
143
135
  var _a;
144
136
  return (_a = wrapChild === null || wrapChild === void 0 ? void 0 : wrapChild(key, e)) !== null && _a !== void 0 ? _a : e;
package/lib/hooks.d.ts CHANGED
@@ -5,6 +5,6 @@ export type ExpressionHook = (expr: EntityExpression, formState: FormEditState)
5
5
  export declare function useDefaultValue(definition: DataControlDefinition, field: SchemaField, formState: FormEditState, useExpression: ExpressionHook): any;
6
6
  export declare function useIsControlVisible(definition: ControlDefinition, formState: FormEditState, useExpression: ExpressionHook): boolean;
7
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;
8
+ export declare function getOptionsForScalarField(field: SchemaField): FieldOption[] | undefined | null;
9
9
  export declare const defaultExpressionHook: ExpressionHook;
10
10
  export declare function createFormEditHooks(useExpression: ExpressionHook): FormEditHooks;
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;
@@ -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: FieldOption[] | undefined;
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: string | undefined;
138
+ materialIcon?: string | null;
139
139
  }
140
140
  export interface CheckListRenderOptions extends RenderOptions {
141
141
  }
@@ -156,13 +156,13 @@ export interface IconSelectionRenderOptions extends RenderOptions {
156
156
  }
157
157
  export interface GroupedControlsDefinition extends ControlDefinition {
158
158
  type: ControlDefinitionType.Group;
159
- children: AnyControlDefinition[];
160
- compoundField?: string;
159
+ children: ControlDefinition[];
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: number | undefined;
178
+ columns?: number | null;
179
179
  }
180
180
  export interface DisplayControlDefinition extends ControlDefinition {
181
181
  type: ControlDefinitionType.Display;
@@ -198,3 +198,14 @@ export interface ActionControlDefinition extends ControlDefinition {
198
198
  type: ControlDefinitionType.Action;
199
199
  actionId: string;
200
200
  }
201
+ export declare function isDataControlDefinition(x: ControlDefinition): x is DataControlDefinition;
202
+ export declare function isGroupControlsDefinition(x: ControlDefinition): x is GroupedControlsDefinition;
203
+ export declare function isDisplayControlsDefinition(x: ControlDefinition): x is DisplayControlDefinition;
204
+ export declare function isActionControlsDefinition(x: ControlDefinition): x is ActionControlDefinition;
205
+ export interface ControlVisitor<A> {
206
+ data(d: DataControlDefinition): A;
207
+ group(d: GroupedControlsDefinition): A;
208
+ display(d: DisplayControlDefinition): A;
209
+ action(d: ActionControlDefinition): A;
210
+ }
211
+ export declare function visitControlDefinition<A>(x: ControlDefinition, visitor: ControlVisitor<A>, defaultValue: (c: ControlDefinition) => A): A;
package/lib/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
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 = void 0;
3
+ exports.visitControlDefinition = exports.isActionControlsDefinition = exports.isDisplayControlsDefinition = exports.isGroupControlsDefinition = exports.isDataControlDefinition = exports.DisplayDataType = exports.GroupRenderType = exports.SyncTextType = exports.DataRenderType = exports.ControlAdornmentType = exports.ExpressionType = exports.DynamicPropertyType = exports.ControlDefinitionType = exports.FieldType = void 0;
4
4
  var FieldType;
5
5
  (function (FieldType) {
6
6
  FieldType["String"] = "String";
@@ -66,3 +66,34 @@ var DisplayDataType;
66
66
  DisplayDataType["Text"] = "Text";
67
67
  DisplayDataType["Html"] = "Html";
68
68
  })(DisplayDataType = exports.DisplayDataType || (exports.DisplayDataType = {}));
69
+ function isDataControlDefinition(x) {
70
+ return x.type === ControlDefinitionType.Data;
71
+ }
72
+ exports.isDataControlDefinition = isDataControlDefinition;
73
+ function isGroupControlsDefinition(x) {
74
+ return x.type === ControlDefinitionType.Group;
75
+ }
76
+ exports.isGroupControlsDefinition = isGroupControlsDefinition;
77
+ function isDisplayControlsDefinition(x) {
78
+ return x.type === ControlDefinitionType.Display;
79
+ }
80
+ exports.isDisplayControlsDefinition = isDisplayControlsDefinition;
81
+ function isActionControlsDefinition(x) {
82
+ return x.type === ControlDefinitionType.Action;
83
+ }
84
+ exports.isActionControlsDefinition = isActionControlsDefinition;
85
+ function visitControlDefinition(x, visitor, defaultValue) {
86
+ switch (x.type) {
87
+ case ControlDefinitionType.Action:
88
+ return visitor.action(x);
89
+ case ControlDefinitionType.Data:
90
+ return visitor.data(x);
91
+ case ControlDefinitionType.Display:
92
+ return visitor.display(x);
93
+ case ControlDefinitionType.Group:
94
+ return visitor.group(x);
95
+ default:
96
+ return defaultValue(x);
97
+ }
98
+ }
99
+ exports.visitControlDefinition = visitControlDefinition;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-typed-forms/schemas",
3
- "version": "1.0.0-dev.3",
3
+ "version": "1.0.0-dev.5",
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.101"
27
+ "@react-typed-forms/core": "3.0.0-dev.102"
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
-
@@ -10,9 +10,11 @@ import {
10
10
  FieldType,
11
11
  GroupedControlsDefinition,
12
12
  SchemaField,
13
+ visitControlDefinition,
13
14
  } from "./types";
14
15
  import React, { createContext, Key, ReactElement, useContext } from "react";
15
- import { Control, newControl, useControlEffect } from "@react-typed-forms/core";
16
+ import { Control, newControl } from "@react-typed-forms/core";
17
+ import { fieldDisplayName } from "./index";
16
18
 
17
19
  export interface FormEditHooks {
18
20
  useDataProperties(
@@ -41,7 +43,7 @@ export interface DataControlProperties {
41
43
  readonly: boolean;
42
44
  defaultValue: any;
43
45
  required: boolean;
44
- options: FieldOption[] | undefined;
46
+ options: FieldOption[] | undefined | null;
45
47
  customRender?: (props: DataRendererProps) => ReactElement;
46
48
  }
47
49
 
@@ -233,71 +235,68 @@ export function findField(
233
235
  ): SchemaField | undefined {
234
236
  return fields.find((x) => x.field === field);
235
237
  }
236
-
237
- export function fieldDisplayName(sf: SchemaField): string {
238
- return sf.displayName ? sf.displayName : sf.field;
239
- }
240
-
241
- export function controlTitle(title: string | undefined, field: SchemaField) {
238
+ export function controlTitle(
239
+ title: string | undefined | null,
240
+ field: SchemaField
241
+ ) {
242
242
  return title ? title : fieldDisplayName(field);
243
243
  }
244
244
 
245
- export function renderControl(
246
- definition: AnyControlDefinition,
245
+ export function renderControl<S extends ControlDefinition>(
246
+ definition: S,
247
247
  formState: FormEditState,
248
248
  hooks: FormEditHooks,
249
249
  key: Key,
250
250
  wrapChild?: (key: Key, db: ReactElement) => ReactElement
251
251
  ): ReactElement {
252
252
  const { fields } = formState;
253
- switch (definition.type) {
254
- case ControlDefinitionType.Data:
255
- const def = definition as DataControlDefinition;
256
- const fieldData = findScalarField(fields, def.field);
257
- if (!fieldData) return <h1>No schema field for: {def.field}</h1>;
258
- return (
259
- <DataRenderer
260
- key={key}
261
- wrapElem={wrapElem}
262
- formState={formState}
263
- hooks={hooks}
264
- controlDef={def}
265
- fieldData={fieldData}
266
- />
267
- );
268
- case ControlDefinitionType.Group:
269
- return (
253
+ return visitControlDefinition(
254
+ definition,
255
+ {
256
+ data: (def) => {
257
+ const fieldData = findScalarField(fields, def.field);
258
+ if (!fieldData) return <h1>No schema field for: {def.field}</h1>;
259
+ return (
260
+ <DataRenderer
261
+ key={key}
262
+ wrapElem={wrapElem}
263
+ formState={formState}
264
+ hooks={hooks}
265
+ controlDef={def}
266
+ fieldData={fieldData}
267
+ />
268
+ );
269
+ },
270
+ group: (d: GroupedControlsDefinition) => (
270
271
  <GroupRenderer
271
272
  key={key}
272
273
  hooks={hooks}
273
- groupDef={definition as GroupedControlsDefinition}
274
+ groupDef={d}
274
275
  formState={formState}
275
276
  wrapElem={wrapElem}
276
277
  />
277
- );
278
- case ControlDefinitionType.Display:
279
- return (
280
- <DisplayRenderer
278
+ ),
279
+ action: (d: ActionControlDefinition) => (
280
+ <ActionRenderer
281
281
  key={key}
282
282
  hooks={hooks}
283
283
  formState={formState}
284
284
  wrapElem={wrapElem}
285
- displayDef={definition as DisplayControlDefinition}
285
+ actionDef={d}
286
286
  />
287
- );
288
- case ControlDefinitionType.Action:
289
- return (
290
- <ActionRenderer
287
+ ),
288
+ display: (d: DisplayControlDefinition) => (
289
+ <DisplayRenderer
291
290
  key={key}
292
291
  hooks={hooks}
293
292
  formState={formState}
294
293
  wrapElem={wrapElem}
295
- actionDef={definition as ActionControlDefinition}
294
+ displayDef={d}
296
295
  />
297
- );
298
- default:
299
- return <h1>Unknown control: {(definition as any).type}</h1>;
300
- }
296
+ ),
297
+ },
298
+ () => <h1>Unknown control: {(definition as any).type}</h1>
299
+ );
301
300
 
302
301
  function wrapElem(e: ReactElement): ReactElement {
303
302
  return wrapChild?.(key, e) ?? e;
package/src/hooks.ts CHANGED
@@ -102,7 +102,7 @@ export function getDefaultScalarControlProperties(
102
102
 
103
103
  export function getOptionsForScalarField(
104
104
  field: SchemaField
105
- ): FieldOption[] | undefined {
105
+ ): FieldOption[] | undefined | null {
106
106
  const opts = field.options ?? field.restrictions?.options;
107
107
  if (opts?.length ?? 0 > 0) {
108
108
  return opts;
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,22 @@
1
+ import { DataControlProperties } from "./controlRender";
2
+
1
3
  export interface SchemaField {
2
4
  type: string;
3
5
  field: string;
4
- displayName?: string;
5
- tags?: string[];
6
- system?: boolean;
7
- collection?: boolean;
8
- onlyForTypes?: string[];
9
- required?: boolean;
6
+ displayName?: string | null;
7
+ tags?: string[] | null;
8
+ system?: boolean | null;
9
+ collection?: boolean | null;
10
+ onlyForTypes?: string[] | null;
11
+ required?: boolean | null;
10
12
  defaultValue?: any;
11
- isTypeField?: boolean;
12
- searchable?: boolean;
13
- options?: FieldOption[];
13
+ isTypeField?: boolean | null;
14
+ searchable?: boolean | null;
15
+ options?: FieldOption[] | null;
14
16
  /**
15
17
  * @deprecated Use options directly
16
18
  */
17
- restrictions?: SchemaRestrictions | undefined;
19
+ restrictions?: SchemaRestrictions | undefined | null;
18
20
  }
19
21
 
20
22
  export enum FieldType {
@@ -37,7 +39,7 @@ export interface EntityRefField extends SchemaField {
37
39
  }
38
40
 
39
41
  export interface SchemaRestrictions {
40
- options: FieldOption[] | undefined;
42
+ options?: FieldOption[] | null;
41
43
  }
42
44
 
43
45
  export interface FieldOption {
@@ -59,9 +61,9 @@ export type AnyControlDefinition =
59
61
 
60
62
  export interface ControlDefinition {
61
63
  type: string;
62
- title?: string;
63
- dynamic?: DynamicProperty[];
64
- adornments?: ControlAdornment[];
64
+ title?: string | null;
65
+ dynamic?: DynamicProperty[] | null;
66
+ adornments?: ControlAdornment[] | null;
65
67
  }
66
68
 
67
69
  export enum ControlDefinitionType {
@@ -125,10 +127,10 @@ export interface AccordionAdornment extends ControlAdornment {
125
127
  export interface DataControlDefinition extends ControlDefinition {
126
128
  type: ControlDefinitionType.Data;
127
129
  field: string;
128
- required?: boolean;
129
- renderOptions?: RenderOptions;
130
+ required?: boolean | null;
131
+ renderOptions?: RenderOptions | null;
130
132
  defaultValue?: any;
131
- readonly?: boolean;
133
+ readonly?: boolean | null;
132
134
  }
133
135
 
134
136
  export interface RenderOptions {
@@ -156,7 +158,7 @@ export interface HtmlEditorRenderOptions extends RenderOptions {
156
158
  }
157
159
 
158
160
  export interface DateTimeRenderOptions extends RenderOptions {
159
- format?: string;
161
+ format?: string | null;
160
162
  }
161
163
 
162
164
  export interface IconListRenderOptions extends RenderOptions {
@@ -165,7 +167,7 @@ export interface IconListRenderOptions extends RenderOptions {
165
167
 
166
168
  export interface IconMapping {
167
169
  value: string;
168
- materialIcon: string | undefined;
170
+ materialIcon?: string | null;
169
171
  }
170
172
 
171
173
  export interface CheckListRenderOptions extends RenderOptions {}
@@ -190,14 +192,14 @@ export interface IconSelectionRenderOptions extends RenderOptions {}
190
192
 
191
193
  export interface GroupedControlsDefinition extends ControlDefinition {
192
194
  type: ControlDefinitionType.Group;
193
- children: AnyControlDefinition[];
194
- compoundField?: string;
195
+ children: ControlDefinition[];
196
+ compoundField?: string | null;
195
197
  groupOptions: GroupRenderOptions;
196
198
  }
197
199
 
198
200
  export interface GroupRenderOptions {
199
201
  type: GroupRenderType;
200
- hideTitle?: boolean;
202
+ hideTitle?: boolean | null;
201
203
  }
202
204
 
203
205
  export enum GroupRenderType {
@@ -213,7 +215,7 @@ export interface GroupElementRenderer extends GroupRenderOptions {
213
215
  }
214
216
 
215
217
  export interface GridRenderer extends GroupRenderOptions {
216
- columns: number | undefined;
218
+ columns?: number | null;
217
219
  }
218
220
 
219
221
  export interface DisplayControlDefinition extends ControlDefinition {
@@ -242,3 +244,53 @@ export interface ActionControlDefinition extends ControlDefinition {
242
244
  type: ControlDefinitionType.Action;
243
245
  actionId: string;
244
246
  }
247
+
248
+ export function isDataControlDefinition(
249
+ x: ControlDefinition
250
+ ): x is DataControlDefinition {
251
+ return x.type === ControlDefinitionType.Data;
252
+ }
253
+
254
+ export function isGroupControlsDefinition(
255
+ x: ControlDefinition
256
+ ): x is GroupedControlsDefinition {
257
+ return x.type === ControlDefinitionType.Group;
258
+ }
259
+
260
+ export function isDisplayControlsDefinition(
261
+ x: ControlDefinition
262
+ ): x is DisplayControlDefinition {
263
+ return x.type === ControlDefinitionType.Display;
264
+ }
265
+
266
+ export function isActionControlsDefinition(
267
+ x: ControlDefinition
268
+ ): x is ActionControlDefinition {
269
+ return x.type === ControlDefinitionType.Action;
270
+ }
271
+
272
+ export interface ControlVisitor<A> {
273
+ data(d: DataControlDefinition): A;
274
+ group(d: GroupedControlsDefinition): A;
275
+ display(d: DisplayControlDefinition): A;
276
+ action(d: ActionControlDefinition): A;
277
+ }
278
+
279
+ export function visitControlDefinition<A>(
280
+ x: ControlDefinition,
281
+ visitor: ControlVisitor<A>,
282
+ defaultValue: (c: ControlDefinition) => A
283
+ ): A {
284
+ switch (x.type) {
285
+ case ControlDefinitionType.Action:
286
+ return visitor.action(x as ActionControlDefinition);
287
+ case ControlDefinitionType.Data:
288
+ return visitor.data(x as DataControlDefinition);
289
+ case ControlDefinitionType.Display:
290
+ return visitor.display(x as DisplayControlDefinition);
291
+ case ControlDefinitionType.Group:
292
+ return visitor.group(x as GroupedControlsDefinition);
293
+ default:
294
+ return defaultValue(x);
295
+ }
296
+ }