@react-typed-forms/schemas 3.0.0-dev.99 → 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,25 +1,15 @@
1
- import {
2
- CompoundField,
3
- FieldOption,
4
- FieldType,
5
- ScalarField,
6
- SchemaField,
7
- SchemaFieldType,
8
- } from "./types";
1
+ import { CompoundField, FieldOption, FieldType, SchemaField } from "./types";
9
2
 
10
3
  type AllowedSchema<T> = T extends string
11
- ? ScalarField & {
12
- schemaType: SchemaFieldType.Scalar;
4
+ ? SchemaField & {
13
5
  type: FieldType.String | FieldType.Date | FieldType.DateTime;
14
6
  }
15
7
  : T extends number
16
- ? ScalarField & {
17
- schemaType: SchemaFieldType.Scalar;
8
+ ? SchemaField & {
18
9
  type: FieldType.Int | FieldType.Double;
19
10
  }
20
11
  : T extends boolean
21
- ? ScalarField & {
22
- schemaType: SchemaFieldType.Scalar;
12
+ ? SchemaField & {
23
13
  type: FieldType.Bool;
24
14
  }
25
15
  : T extends Array<infer E>
@@ -28,25 +18,31 @@ type AllowedSchema<T> = T extends string
28
18
  }
29
19
  : T extends { [key: string]: any }
30
20
  ? CompoundField & {
31
- schemaType: SchemaFieldType.Compound;
32
21
  type: FieldType.Compound;
33
22
  }
34
- : never;
35
- type AllowedField<T> = (name: string) => AllowedSchema<T>;
23
+ : SchemaField & { type: FieldType.Any };
36
24
 
37
- export function buildSchema<T>(def: {
38
- [K in keyof T]-?: AllowedField<T[K]>;
25
+ type AllowedField<T, K> = (
26
+ name: string,
27
+ ) => (SchemaField & { type: K }) | AllowedSchema<T>;
28
+
29
+ export function buildSchema<T, Custom = "">(def: {
30
+ [K in keyof T]-?: AllowedField<T[K], Custom>;
39
31
  }): SchemaField[] {
40
32
  return Object.entries(def).map((x) =>
41
- (x[1] as (n: string) => SchemaField)(x[0])
33
+ (x[1] as (n: string) => SchemaField)(x[0]),
42
34
  );
43
35
  }
44
36
 
45
37
  export function stringField(
46
38
  displayName: string,
47
- options?: Partial<Omit<ScalarField, "schemaType" | "type">>
39
+ options?: Partial<Omit<SchemaField, "type">>,
48
40
  ) {
49
- return makeScalarField({ type: FieldType.String, displayName, ...options });
41
+ return makeScalarField({
42
+ type: FieldType.String as const,
43
+ displayName,
44
+ ...options,
45
+ });
50
46
  }
51
47
 
52
48
  export function stringOptionsField(
@@ -54,29 +50,28 @@ export function stringOptionsField(
54
50
  ...options: FieldOption[]
55
51
  ) {
56
52
  return makeScalarField({
57
- type: FieldType.String,
53
+ type: FieldType.String as const,
58
54
  displayName,
59
- restrictions: { options },
55
+ options,
60
56
  });
61
57
  }
62
58
 
63
- export function withScalarOptions<S extends ScalarField>(
64
- options: Partial<ScalarField>,
65
- v: (name: string) => S
59
+ export function withScalarOptions<S extends SchemaField>(
60
+ options: Partial<SchemaField>,
61
+ v: (name: string) => S,
66
62
  ): (name: string) => S {
67
63
  return (n) => ({ ...v(n), ...options });
68
64
  }
69
65
 
70
- export function makeScalarField<S extends Partial<ScalarField>>(
71
- options: S
72
- ): (name: string) => ScalarField & { schemaType: SchemaFieldType.Scalar } & S {
66
+ export function makeScalarField<S extends Partial<SchemaField>>(
67
+ options: S,
68
+ ): (name: string) => SchemaField & S {
73
69
  return (n) => ({ ...defaultScalarField(n, n), ...options });
74
70
  }
75
71
 
76
72
  export function makeCompoundField<S extends Partial<CompoundField>>(
77
- options: S
73
+ options: S,
78
74
  ): (name: string) => CompoundField & {
79
- schemaType: SchemaFieldType.Compound;
80
75
  type: FieldType.Compound;
81
76
  } & S {
82
77
  return (n) => ({ ...defaultCompoundField(n, n, false), ...options });
@@ -84,27 +79,33 @@ export function makeCompoundField<S extends Partial<CompoundField>>(
84
79
 
85
80
  export function intField(
86
81
  displayName: string,
87
- options?: Partial<Omit<ScalarField, "schemaType" | "type">>
82
+ options?: Partial<Omit<SchemaField, "type">>,
88
83
  ) {
89
- return makeScalarField({ type: FieldType.Int, displayName, ...options });
84
+ return makeScalarField({
85
+ type: FieldType.Int as const,
86
+ displayName,
87
+ ...options,
88
+ });
90
89
  }
91
90
 
92
91
  export function boolField(
93
92
  displayName: string,
94
- options?: Partial<Omit<ScalarField, "schemaType" | "type">>
93
+ options?: Partial<Omit<SchemaField, "type">>,
95
94
  ) {
96
- return makeScalarField({ type: FieldType.Bool, displayName, ...options });
95
+ return makeScalarField({
96
+ type: FieldType.Bool as const,
97
+ displayName,
98
+ ...options,
99
+ });
97
100
  }
98
101
 
99
102
  export function compoundField<
100
- Other extends Partial<Omit<CompoundField, "type" | "schemaType">>
103
+ Other extends Partial<Omit<CompoundField, "type" | "schemaType">>,
101
104
  >(
102
105
  displayName: string,
103
106
  fields: SchemaField[],
104
- other: Other
107
+ other: Other,
105
108
  ): (name: string) => CompoundField & {
106
- schemaType: SchemaFieldType.Compound;
107
- type: FieldType.Compound;
108
109
  collection: Other["collection"];
109
110
  } {
110
111
  return (field) =>
@@ -112,56 +113,34 @@ export function compoundField<
112
113
  ...defaultCompoundField(field, displayName, false),
113
114
  ...other,
114
115
  children: fields,
115
- } as any);
116
+ }) as any;
116
117
  }
117
118
 
118
119
  export function defaultScalarField(
119
120
  field: string,
120
- displayName: string
121
- ): ScalarField & {
122
- schemaType: SchemaFieldType.Scalar;
121
+ displayName: string,
122
+ ): Omit<SchemaField, "type"> & {
123
123
  type: FieldType.String;
124
124
  } {
125
125
  return {
126
- restrictions: {
127
- options: [],
128
- },
129
- tags: [],
130
126
  field,
131
127
  displayName,
132
128
  type: FieldType.String,
133
- collection: false,
134
- searchable: false,
135
- schemaType: SchemaFieldType.Scalar,
136
- system: false,
137
- entityRefType: "",
138
- parentField: "",
139
- required: false,
140
- defaultValue: undefined,
141
- onlyForTypes: [],
142
- isTypeField: false,
143
129
  };
144
130
  }
145
131
 
146
132
  export function defaultCompoundField(
147
133
  field: string,
148
134
  displayName: string,
149
- collection: boolean
135
+ collection: boolean,
150
136
  ): CompoundField & {
151
137
  type: FieldType.Compound;
152
- schemaType: SchemaFieldType.Compound;
153
138
  } {
154
139
  return {
155
- tags: [],
156
140
  field,
157
141
  displayName,
158
142
  type: FieldType.Compound,
159
143
  collection,
160
- schemaType: SchemaFieldType.Compound,
161
- system: false,
162
- treeChildren: false,
163
144
  children: [],
164
- onlyForTypes: [],
165
- required: true,
166
145
  };
167
146
  }
@@ -0,0 +1,25 @@
1
+ import React from "react";
2
+ import { DefaultRendererOptions } from "./renderers";
3
+
4
+ export const defaultTailwindTheme: DefaultRendererOptions = {
5
+ label: {
6
+ groupLabelClass: "font-bold",
7
+ requiredElement: <span className="text-red-500"> *</span>,
8
+ },
9
+ array: {
10
+ removableClass: "grid grid-cols-[1fr_auto] items-center gap-x-2",
11
+ childClass: "grow my-2",
12
+ addActionClass: "my-2",
13
+ },
14
+ group: {
15
+ standardClassName: "space-y-4",
16
+ gridClassName: "gap-x-2 gap-y-4",
17
+ },
18
+ action: {
19
+ className: "bg-primary rounded-lg p-3 text-white",
20
+ },
21
+ layout: {
22
+ className: "flex flex-col",
23
+ errorClass: "text-sm text-danger-500",
24
+ },
25
+ };
package/src/types.ts CHANGED
@@ -1,18 +1,18 @@
1
1
  export interface SchemaField {
2
- schemaType: SchemaFieldType;
2
+ type: string;
3
3
  field: string;
4
- displayName: string;
5
- type: FieldType;
6
- tags: string[];
7
- system: boolean;
8
- collection: boolean;
9
- onlyForTypes: string[];
10
- required: boolean;
11
- }
12
-
13
- export enum SchemaFieldType {
14
- Scalar = "Scalar",
15
- Compound = "Compound",
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
+ notNullable?: boolean | null;
11
+ defaultValue?: any;
12
+ isTypeField?: boolean | null;
13
+ searchable?: boolean | null;
14
+ options?: FieldOption[] | null;
15
+ validators?: SchemaValidator[] | null;
16
16
  }
17
17
 
18
18
  export enum FieldType {
@@ -26,19 +26,13 @@ export enum FieldType {
26
26
  Compound = "Compound",
27
27
  AutoId = "AutoId",
28
28
  Image = "Image",
29
+ Any = "Any",
29
30
  }
30
31
 
31
- export interface ScalarField extends SchemaField {
32
+ export interface EntityRefField extends SchemaField {
33
+ type: FieldType.EntityRef;
32
34
  entityRefType: string;
33
35
  parentField: string;
34
- searchable: boolean;
35
- defaultValue: any;
36
- isTypeField: boolean;
37
- restrictions: SchemaRestrictions | undefined;
38
- }
39
-
40
- export interface SchemaRestrictions {
41
- options: FieldOption[] | undefined;
42
36
  }
43
37
 
44
38
  export interface FieldOption {
@@ -47,8 +41,9 @@ export interface FieldOption {
47
41
  }
48
42
 
49
43
  export interface CompoundField extends SchemaField {
44
+ type: FieldType.Compound;
50
45
  children: SchemaField[];
51
- treeChildren: boolean;
46
+ treeChildren?: boolean;
52
47
  }
53
48
 
54
49
  export type AnyControlDefinition =
@@ -59,9 +54,10 @@ export type AnyControlDefinition =
59
54
 
60
55
  export interface ControlDefinition {
61
56
  type: string;
62
- title?: string;
63
- dynamic?: DynamicProperty[];
64
- adornments?: ControlAdornment[];
57
+ title?: string | null;
58
+ dynamic?: DynamicProperty[] | null;
59
+ adornments?: ControlAdornment[] | null;
60
+ children?: ControlDefinition[] | null;
65
61
  }
66
62
 
67
63
  export enum ControlDefinitionType {
@@ -72,7 +68,7 @@ export enum ControlDefinitionType {
72
68
  }
73
69
 
74
70
  export interface DynamicProperty {
75
- type: DynamicPropertyType;
71
+ type: string;
76
72
  expr: EntityExpression;
77
73
  }
78
74
 
@@ -82,7 +78,7 @@ export enum DynamicPropertyType {
82
78
  }
83
79
 
84
80
  export interface EntityExpression {
85
- type: ExpressionType;
81
+ type: string;
86
82
  }
87
83
 
88
84
  export enum ExpressionType {
@@ -92,47 +88,68 @@ export enum ExpressionType {
92
88
  }
93
89
 
94
90
  export interface JsonataExpression extends EntityExpression {
91
+ type: ExpressionType.Jsonata;
95
92
  expression: string;
96
93
  }
97
94
 
98
95
  export interface FieldValueExpression extends EntityExpression {
96
+ type: ExpressionType.FieldValue;
99
97
  field: string;
100
98
  value: any;
101
99
  }
102
100
 
103
101
  export interface UserMatchExpression extends EntityExpression {
102
+ type: ExpressionType.UserMatch;
104
103
  userMatch: string;
105
104
  }
106
105
 
107
106
  export interface ControlAdornment {
108
- type: ControlAdornmentType;
107
+ type: string;
108
+ }
109
+
110
+ export enum AdornmentPlacement {
111
+ ControlStart = "ControlStart",
112
+ ControlEnd = "ControlEnd",
113
+ LabelStart = "LabelStart",
114
+ LabelEnd = "LabelEnd",
109
115
  }
110
116
 
111
117
  export enum ControlAdornmentType {
112
118
  Tooltip = "Tooltip",
113
119
  Accordion = "Accordion",
120
+ HelpText = "HelpText",
114
121
  }
115
122
 
116
123
  export interface TooltipAdornment extends ControlAdornment {
124
+ type: ControlAdornmentType.Tooltip;
117
125
  tooltip: string;
118
126
  }
119
127
 
120
128
  export interface AccordionAdornment extends ControlAdornment {
129
+ type: ControlAdornmentType.Accordion;
121
130
  title: string;
122
131
  defaultExpanded: boolean;
123
132
  }
124
133
 
134
+ export interface HelpTextAdornment extends ControlAdornment {
135
+ type: ControlAdornmentType.HelpText;
136
+ helpText: string;
137
+ placement: AdornmentPlacement;
138
+ }
139
+
125
140
  export interface DataControlDefinition extends ControlDefinition {
126
141
  type: ControlDefinitionType.Data;
127
142
  field: string;
128
- required?: boolean;
129
- renderOptions: RenderOptions;
143
+ required?: boolean | null;
144
+ renderOptions?: RenderOptions | null;
130
145
  defaultValue?: any;
131
- readonly?: boolean;
146
+ readonly?: boolean | null;
147
+ validators?: SchemaValidator[] | null;
148
+ hideTitle?: boolean | null;
132
149
  }
133
150
 
134
151
  export interface RenderOptions {
135
- type: DataRenderType;
152
+ type: string;
136
153
  }
137
154
 
138
155
  export enum DataRenderType {
@@ -144,34 +161,45 @@ export enum DataRenderType {
144
161
  UserSelection = "UserSelection",
145
162
  Synchronised = "Synchronised",
146
163
  IconSelector = "IconSelector",
147
-
148
164
  DateTime = "DateTime",
165
+ Checkbox = "Checkbox",
166
+ Dropdown = "Dropdown",
149
167
  }
150
168
 
151
- export interface RadioButtonRenderOptions extends RenderOptions {}
169
+ export interface RadioButtonRenderOptions extends RenderOptions {
170
+ type: DataRenderType.Radio;
171
+ }
152
172
 
153
- export interface StandardRenderer extends RenderOptions {}
173
+ export interface StandardRenderer extends RenderOptions {
174
+ type: DataRenderType.Standard;
175
+ }
154
176
 
155
177
  export interface HtmlEditorRenderOptions extends RenderOptions {
178
+ type: DataRenderType.HtmlEditor;
156
179
  allowImages: boolean;
157
180
  }
158
181
 
159
182
  export interface DateTimeRenderOptions extends RenderOptions {
160
- format?: string;
183
+ type: DataRenderType.DateTime;
184
+ format?: string | null;
161
185
  }
162
186
 
163
187
  export interface IconListRenderOptions extends RenderOptions {
188
+ type: DataRenderType.IconList;
164
189
  iconMappings: IconMapping[];
165
190
  }
166
191
 
167
192
  export interface IconMapping {
168
193
  value: string;
169
- materialIcon: string | undefined;
194
+ materialIcon?: string | null;
170
195
  }
171
196
 
172
- export interface CheckListRenderOptions extends RenderOptions {}
197
+ export interface CheckListRenderOptions extends RenderOptions {
198
+ type: DataRenderType.CheckList;
199
+ }
173
200
 
174
201
  export interface SynchronisedRenderOptions extends RenderOptions {
202
+ type: DataRenderType.Synchronised;
175
203
  fieldToSync: string;
176
204
  syncType: SyncTextType;
177
205
  }
@@ -183,22 +211,24 @@ export enum SyncTextType {
183
211
  }
184
212
 
185
213
  export interface UserSelectionRenderOptions extends RenderOptions {
214
+ type: DataRenderType.UserSelection;
186
215
  noGroups: boolean;
187
216
  noUsers: boolean;
188
217
  }
189
218
 
190
- export interface IconSelectionRenderOptions extends RenderOptions {}
219
+ export interface IconSelectionRenderOptions extends RenderOptions {
220
+ type: DataRenderType.IconSelector;
221
+ }
191
222
 
192
223
  export interface GroupedControlsDefinition extends ControlDefinition {
193
224
  type: ControlDefinitionType.Group;
194
- children: AnyControlDefinition[];
195
- compoundField?: string;
196
- groupOptions: GroupRenderOptions;
225
+ compoundField?: string | null;
226
+ groupOptions?: GroupRenderOptions;
197
227
  }
198
228
 
199
229
  export interface GroupRenderOptions {
200
- type: GroupRenderType;
201
- hideTitle?: boolean;
230
+ type: string;
231
+ hideTitle?: boolean | null;
202
232
  }
203
233
 
204
234
  export enum GroupRenderType {
@@ -207,14 +237,18 @@ export enum GroupRenderType {
207
237
  GroupElement = "GroupElement",
208
238
  }
209
239
 
210
- export interface StandardGroupRenderer extends GroupRenderOptions {}
240
+ export interface StandardGroupRenderer extends GroupRenderOptions {
241
+ type: GroupRenderType.Standard;
242
+ }
211
243
 
212
244
  export interface GroupElementRenderer extends GroupRenderOptions {
245
+ type: GroupRenderType.GroupElement;
213
246
  value: any;
214
247
  }
215
248
 
216
249
  export interface GridRenderer extends GroupRenderOptions {
217
- columns: number | undefined;
250
+ type: GroupRenderType.Grid;
251
+ columns?: number | null;
218
252
  }
219
253
 
220
254
  export interface DisplayControlDefinition extends ControlDefinition {
@@ -223,7 +257,7 @@ export interface DisplayControlDefinition extends ControlDefinition {
223
257
  }
224
258
 
225
259
  export interface DisplayData {
226
- type: DisplayDataType;
260
+ type: string;
227
261
  }
228
262
 
229
263
  export enum DisplayDataType {
@@ -232,10 +266,12 @@ export enum DisplayDataType {
232
266
  }
233
267
 
234
268
  export interface TextDisplay extends DisplayData {
269
+ type: DisplayDataType.Text;
235
270
  text: string;
236
271
  }
237
272
 
238
273
  export interface HtmlDisplay extends DisplayData {
274
+ type: DisplayDataType.Html;
239
275
  html: string;
240
276
  }
241
277
 
@@ -243,3 +279,83 @@ export interface ActionControlDefinition extends ControlDefinition {
243
279
  type: ControlDefinitionType.Action;
244
280
  actionId: string;
245
281
  }
282
+
283
+ export enum ValidatorType {
284
+ Jsonata = "Jsonata",
285
+ Date = "Date",
286
+ }
287
+ export interface SchemaValidator {
288
+ type: string;
289
+ }
290
+
291
+ export interface JsonataValidator extends SchemaValidator {
292
+ type: ValidatorType.Jsonata;
293
+ expression: string;
294
+ }
295
+
296
+ export enum DateComparison {
297
+ NotBefore = "NotBefore",
298
+ NotAfter = "NotAfter",
299
+ }
300
+
301
+ export interface DateValidator extends SchemaValidator {
302
+ type: ValidatorType.Date;
303
+ comparison: DateComparison;
304
+ fixedDate?: string | null;
305
+ daysFromCurrent?: number | null;
306
+ }
307
+
308
+ export function isDataControlDefinition(
309
+ x: ControlDefinition,
310
+ ): x is DataControlDefinition {
311
+ return x.type === ControlDefinitionType.Data;
312
+ }
313
+
314
+ export function isGroupControlsDefinition(
315
+ x: ControlDefinition,
316
+ ): x is GroupedControlsDefinition {
317
+ return x.type === ControlDefinitionType.Group;
318
+ }
319
+
320
+ export function isDisplayControlsDefinition(
321
+ x: ControlDefinition,
322
+ ): x is DisplayControlDefinition {
323
+ return x.type === ControlDefinitionType.Display;
324
+ }
325
+
326
+ export function isActionControlsDefinition(
327
+ x: ControlDefinition,
328
+ ): x is ActionControlDefinition {
329
+ return x.type === ControlDefinitionType.Action;
330
+ }
331
+
332
+ export interface ControlVisitor<A> {
333
+ data(d: DataControlDefinition): A;
334
+ group(d: GroupedControlsDefinition): A;
335
+ display(d: DisplayControlDefinition): A;
336
+ action(d: ActionControlDefinition): A;
337
+ }
338
+
339
+ export function visitControlDefinition<A>(
340
+ x: ControlDefinition,
341
+ visitor: ControlVisitor<A>,
342
+ defaultValue: (c: ControlDefinition) => A,
343
+ ): A {
344
+ switch (x.type) {
345
+ case ControlDefinitionType.Action:
346
+ return visitor.action(x as ActionControlDefinition);
347
+ case ControlDefinitionType.Data:
348
+ return visitor.data(x as DataControlDefinition);
349
+ case ControlDefinitionType.Display:
350
+ return visitor.display(x as DisplayControlDefinition);
351
+ case ControlDefinitionType.Group:
352
+ return visitor.group(x as GroupedControlsDefinition);
353
+ default:
354
+ return defaultValue(x);
355
+ }
356
+ }
357
+ export function isGridRenderer(
358
+ options: GroupRenderOptions,
359
+ ): options is GridRenderer {
360
+ return options.type === GroupRenderType.Grid;
361
+ }