@react-typed-forms/schemas 1.0.0-dev.10

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.
@@ -0,0 +1,148 @@
1
+ import { CompoundField, FieldOption, FieldType, SchemaField } from "./types";
2
+
3
+ type AllowedSchema<T> = T extends string
4
+ ? SchemaField & {
5
+ type: FieldType.String | FieldType.Date | FieldType.DateTime;
6
+ }
7
+ : T extends number
8
+ ? SchemaField & {
9
+ type: FieldType.Int | FieldType.Double;
10
+ }
11
+ : T extends boolean
12
+ ? SchemaField & {
13
+ type: FieldType.Bool;
14
+ }
15
+ : T extends Array<infer E>
16
+ ? AllowedSchema<E> & {
17
+ collection: true;
18
+ }
19
+ : T extends { [key: string]: any }
20
+ ? CompoundField & {
21
+ type: FieldType.Compound;
22
+ }
23
+ : SchemaField & { type: FieldType.Any };
24
+ type AllowedField<T> = (name: string) => AllowedSchema<T>;
25
+
26
+ export function buildSchema<T>(def: {
27
+ [K in keyof T]-?: AllowedField<T[K]>;
28
+ }): SchemaField[] {
29
+ return Object.entries(def).map((x) =>
30
+ (x[1] as (n: string) => SchemaField)(x[0])
31
+ );
32
+ }
33
+
34
+ export function stringField(
35
+ displayName: string,
36
+ options?: Partial<Omit<SchemaField, "type">>
37
+ ) {
38
+ return makeScalarField({
39
+ type: FieldType.String as const,
40
+ displayName,
41
+ ...options,
42
+ });
43
+ }
44
+
45
+ export function stringOptionsField(
46
+ displayName: string,
47
+ ...options: FieldOption[]
48
+ ) {
49
+ return makeScalarField({
50
+ type: FieldType.String as const,
51
+ displayName,
52
+ options,
53
+ });
54
+ }
55
+
56
+ export function withScalarOptions<S extends SchemaField>(
57
+ options: Partial<SchemaField>,
58
+ v: (name: string) => S
59
+ ): (name: string) => S {
60
+ return (n) => ({ ...v(n), ...options });
61
+ }
62
+
63
+ export function makeScalarField<S extends Partial<SchemaField>>(
64
+ options: S
65
+ ): (name: string) => SchemaField & S {
66
+ return (n) => ({ ...defaultScalarField(n, n), ...options });
67
+ }
68
+
69
+ export function makeCompoundField<S extends Partial<CompoundField>>(
70
+ options: S
71
+ ): (name: string) => CompoundField & {
72
+ type: FieldType.Compound;
73
+ } & S {
74
+ return (n) => ({ ...defaultCompoundField(n, n, false), ...options });
75
+ }
76
+
77
+ export function intField(
78
+ displayName: string,
79
+ options?: Partial<Omit<SchemaField, "type">>
80
+ ) {
81
+ return makeScalarField({
82
+ type: FieldType.Int as const,
83
+ displayName,
84
+ ...options,
85
+ });
86
+ }
87
+
88
+ export function boolField(
89
+ displayName: string,
90
+ options?: Partial<Omit<SchemaField, "type">>
91
+ ) {
92
+ return makeScalarField({
93
+ type: FieldType.Bool as const,
94
+ displayName,
95
+ ...options,
96
+ });
97
+ }
98
+
99
+ export function compoundField<
100
+ Other extends Partial<Omit<CompoundField, "type" | "schemaType">>
101
+ >(
102
+ displayName: string,
103
+ fields: SchemaField[],
104
+ other: Other
105
+ ): (name: string) => CompoundField & {
106
+ collection: Other["collection"];
107
+ } {
108
+ return (field) =>
109
+ ({
110
+ ...defaultCompoundField(field, displayName, false),
111
+ ...other,
112
+ children: fields,
113
+ } as any);
114
+ }
115
+
116
+ export function defaultScalarField(
117
+ field: string,
118
+ displayName: string
119
+ ): Omit<SchemaField, "type"> & {
120
+ type: FieldType.String;
121
+ } {
122
+ return {
123
+ field,
124
+ displayName,
125
+ type: FieldType.String,
126
+ };
127
+ }
128
+
129
+ export function defaultCompoundField(
130
+ field: string,
131
+ displayName: string,
132
+ collection: boolean
133
+ ): CompoundField & {
134
+ type: FieldType.Compound;
135
+ } {
136
+ return {
137
+ tags: [],
138
+ field,
139
+ displayName,
140
+ type: FieldType.Compound,
141
+ collection,
142
+ system: false,
143
+ treeChildren: false,
144
+ children: [],
145
+ onlyForTypes: [],
146
+ required: true,
147
+ };
148
+ }
package/src/types.ts ADDED
@@ -0,0 +1,321 @@
1
+ import { DataControlProperties } from "./controlRender";
2
+
3
+ export interface SchemaField {
4
+ type: string;
5
+ field: string;
6
+ displayName?: string | null;
7
+ tags?: string[] | null;
8
+ system?: boolean | null;
9
+ collection?: boolean | null;
10
+ onlyForTypes?: string[] | null;
11
+ required?: boolean | null;
12
+ defaultValue?: any;
13
+ isTypeField?: boolean | null;
14
+ searchable?: boolean | null;
15
+ options?: FieldOption[] | null;
16
+ /**
17
+ * @deprecated Use options directly
18
+ */
19
+ restrictions?: SchemaRestrictions | undefined | null;
20
+ }
21
+
22
+ export enum FieldType {
23
+ String = "String",
24
+ Bool = "Bool",
25
+ Int = "Int",
26
+ Date = "Date",
27
+ DateTime = "DateTime",
28
+ Double = "Double",
29
+ EntityRef = "EntityRef",
30
+ Compound = "Compound",
31
+ AutoId = "AutoId",
32
+ Image = "Image",
33
+ Any = "Any",
34
+ }
35
+
36
+ export interface EntityRefField extends SchemaField {
37
+ type: FieldType.EntityRef;
38
+ entityRefType: string;
39
+ parentField: string;
40
+ }
41
+
42
+ export interface SchemaRestrictions {
43
+ options?: FieldOption[] | null;
44
+ }
45
+
46
+ export interface FieldOption {
47
+ name: string;
48
+ value: any;
49
+ }
50
+
51
+ export interface CompoundField extends SchemaField {
52
+ type: FieldType.Compound;
53
+ children: SchemaField[];
54
+ treeChildren?: boolean;
55
+ }
56
+
57
+ export type AnyControlDefinition =
58
+ | DataControlDefinition
59
+ | GroupedControlsDefinition
60
+ | ActionControlDefinition
61
+ | DisplayControlDefinition;
62
+
63
+ export interface ControlDefinition {
64
+ type: string;
65
+ title?: string | null;
66
+ dynamic?: DynamicProperty[] | null;
67
+ adornments?: ControlAdornment[] | null;
68
+ }
69
+
70
+ export enum ControlDefinitionType {
71
+ Data = "Data",
72
+ Group = "Group",
73
+ Display = "Display",
74
+ Action = "Action",
75
+ }
76
+
77
+ export interface DynamicProperty {
78
+ type: string;
79
+ expr: EntityExpression;
80
+ }
81
+
82
+ export enum DynamicPropertyType {
83
+ Visible = "Visible",
84
+ DefaultValue = "DefaultValue",
85
+ }
86
+
87
+ export interface EntityExpression {
88
+ type: string;
89
+ }
90
+
91
+ export enum ExpressionType {
92
+ Jsonata = "Jsonata",
93
+ FieldValue = "FieldValue",
94
+ UserMatch = "UserMatch",
95
+ }
96
+
97
+ export interface JsonataExpression extends EntityExpression {
98
+ type: ExpressionType.Jsonata;
99
+ expression: string;
100
+ }
101
+
102
+ export interface FieldValueExpression extends EntityExpression {
103
+ type: ExpressionType.FieldValue;
104
+ field: string;
105
+ value: any;
106
+ }
107
+
108
+ export interface UserMatchExpression extends EntityExpression {
109
+ type: ExpressionType.UserMatch;
110
+ userMatch: string;
111
+ }
112
+
113
+ export interface ControlAdornment {
114
+ type: string;
115
+ }
116
+
117
+ export enum ControlAdornmentType {
118
+ Tooltip = "Tooltip",
119
+ Accordion = "Accordion",
120
+ }
121
+
122
+ export interface TooltipAdornment extends ControlAdornment {
123
+ type: ControlAdornmentType.Tooltip;
124
+ tooltip: string;
125
+ }
126
+
127
+ export interface AccordionAdornment extends ControlAdornment {
128
+ type: ControlAdornmentType.Accordion;
129
+ title: string;
130
+ defaultExpanded: boolean;
131
+ }
132
+
133
+ export interface DataControlDefinition extends ControlDefinition {
134
+ type: ControlDefinitionType.Data;
135
+ field: string;
136
+ required?: boolean | null;
137
+ renderOptions?: RenderOptions | null;
138
+ defaultValue?: any;
139
+ readonly?: boolean | null;
140
+ }
141
+
142
+ export interface RenderOptions {
143
+ type: string;
144
+ }
145
+
146
+ export enum DataRenderType {
147
+ Standard = "Standard",
148
+ Radio = "Radio",
149
+ HtmlEditor = "HtmlEditor",
150
+ IconList = "IconList",
151
+ CheckList = "CheckList",
152
+ UserSelection = "UserSelection",
153
+ Synchronised = "Synchronised",
154
+ IconSelector = "IconSelector",
155
+ DateTime = "DateTime",
156
+ }
157
+
158
+ export interface RadioButtonRenderOptions extends RenderOptions {
159
+ type: DataRenderType.Radio;
160
+ }
161
+
162
+ export interface StandardRenderer extends RenderOptions {
163
+ type: DataRenderType.Standard;
164
+ }
165
+
166
+ export interface HtmlEditorRenderOptions extends RenderOptions {
167
+ type: DataRenderType.HtmlEditor;
168
+ allowImages: boolean;
169
+ }
170
+
171
+ export interface DateTimeRenderOptions extends RenderOptions {
172
+ type: DataRenderType.DateTime;
173
+ format?: string | null;
174
+ }
175
+
176
+ export interface IconListRenderOptions extends RenderOptions {
177
+ type: DataRenderType.IconList;
178
+ iconMappings: IconMapping[];
179
+ }
180
+
181
+ export interface IconMapping {
182
+ value: string;
183
+ materialIcon?: string | null;
184
+ }
185
+
186
+ export interface CheckListRenderOptions extends RenderOptions {
187
+ type: DataRenderType.CheckList;
188
+ }
189
+
190
+ export interface SynchronisedRenderOptions extends RenderOptions {
191
+ type: DataRenderType.Synchronised;
192
+ fieldToSync: string;
193
+ syncType: SyncTextType;
194
+ }
195
+
196
+ export enum SyncTextType {
197
+ Camel = "Camel",
198
+ Snake = "Snake",
199
+ Pascal = "Pascal",
200
+ }
201
+
202
+ export interface UserSelectionRenderOptions extends RenderOptions {
203
+ type: DataRenderType.UserSelection;
204
+ noGroups: boolean;
205
+ noUsers: boolean;
206
+ }
207
+
208
+ export interface IconSelectionRenderOptions extends RenderOptions {
209
+ type: DataRenderType.IconSelector;
210
+ }
211
+
212
+ export interface GroupedControlsDefinition extends ControlDefinition {
213
+ type: ControlDefinitionType.Group;
214
+ children: ControlDefinition[];
215
+ compoundField?: string | null;
216
+ groupOptions: GroupRenderOptions;
217
+ }
218
+
219
+ export interface GroupRenderOptions {
220
+ type: string;
221
+ hideTitle?: boolean | null;
222
+ }
223
+
224
+ export enum GroupRenderType {
225
+ Standard = "Standard",
226
+ Grid = "Grid",
227
+ GroupElement = "GroupElement",
228
+ }
229
+
230
+ export interface StandardGroupRenderer extends GroupRenderOptions {
231
+ type: GroupRenderType.Standard;
232
+ }
233
+
234
+ export interface GroupElementRenderer extends GroupRenderOptions {
235
+ type: GroupRenderType.GroupElement;
236
+ value: any;
237
+ }
238
+
239
+ export interface GridRenderer extends GroupRenderOptions {
240
+ type: GroupRenderType.Grid;
241
+ columns?: number | null;
242
+ }
243
+
244
+ export interface DisplayControlDefinition extends ControlDefinition {
245
+ type: ControlDefinitionType.Display;
246
+ displayData: DisplayData;
247
+ }
248
+
249
+ export interface DisplayData {
250
+ type: string;
251
+ }
252
+
253
+ export enum DisplayDataType {
254
+ Text = "Text",
255
+ Html = "Html",
256
+ }
257
+
258
+ export interface TextDisplay extends DisplayData {
259
+ type: DisplayDataType.Text;
260
+ text: string;
261
+ }
262
+
263
+ export interface HtmlDisplay extends DisplayData {
264
+ type: DisplayDataType.Html;
265
+ html: string;
266
+ }
267
+
268
+ export interface ActionControlDefinition extends ControlDefinition {
269
+ type: ControlDefinitionType.Action;
270
+ actionId: string;
271
+ }
272
+
273
+ export function isDataControlDefinition(
274
+ x: ControlDefinition
275
+ ): x is DataControlDefinition {
276
+ return x.type === ControlDefinitionType.Data;
277
+ }
278
+
279
+ export function isGroupControlsDefinition(
280
+ x: ControlDefinition
281
+ ): x is GroupedControlsDefinition {
282
+ return x.type === ControlDefinitionType.Group;
283
+ }
284
+
285
+ export function isDisplayControlsDefinition(
286
+ x: ControlDefinition
287
+ ): x is DisplayControlDefinition {
288
+ return x.type === ControlDefinitionType.Display;
289
+ }
290
+
291
+ export function isActionControlsDefinition(
292
+ x: ControlDefinition
293
+ ): x is ActionControlDefinition {
294
+ return x.type === ControlDefinitionType.Action;
295
+ }
296
+
297
+ export interface ControlVisitor<A> {
298
+ data(d: DataControlDefinition): A;
299
+ group(d: GroupedControlsDefinition): A;
300
+ display(d: DisplayControlDefinition): A;
301
+ action(d: ActionControlDefinition): A;
302
+ }
303
+
304
+ export function visitControlDefinition<A>(
305
+ x: ControlDefinition,
306
+ visitor: ControlVisitor<A>,
307
+ defaultValue: (c: ControlDefinition) => A
308
+ ): A {
309
+ switch (x.type) {
310
+ case ControlDefinitionType.Action:
311
+ return visitor.action(x as ActionControlDefinition);
312
+ case ControlDefinitionType.Data:
313
+ return visitor.data(x as DataControlDefinition);
314
+ case ControlDefinitionType.Display:
315
+ return visitor.display(x as DisplayControlDefinition);
316
+ case ControlDefinitionType.Group:
317
+ return visitor.group(x as GroupedControlsDefinition);
318
+ default:
319
+ return defaultValue(x);
320
+ }
321
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2015",
4
+ "lib": ["dom", "dom.iterable", "esnext"],
5
+ "allowJs": true,
6
+ "skipLibCheck": true,
7
+ "strict": true,
8
+ "forceConsistentCasingInFileNames": true,
9
+ "esModuleInterop": true,
10
+ "declaration": true,
11
+ "module": "CommonJS",
12
+ "moduleResolution": "node",
13
+ "resolveJsonModule": true,
14
+ "isolatedModules": true,
15
+ "jsx": "react",
16
+ "outDir": "lib"
17
+ },
18
+ "include": ["src/**/*.ts", "src/**/*.tsx"],
19
+ "exclude": ["node_modules", "lib"]
20
+ }