@react-typed-forms/schemas 8.2.0 → 9.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,171 +0,0 @@
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
-
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>;
31
- }): SchemaField[] {
32
- return Object.entries(def).map((x) =>
33
- (x[1] as (n: string) => SchemaField)(x[0]),
34
- );
35
- }
36
-
37
- export function stringField(
38
- displayName: string,
39
- options?: Partial<Omit<SchemaField, "type">>,
40
- ) {
41
- return makeScalarField({
42
- type: FieldType.String as const,
43
- displayName,
44
- ...options,
45
- });
46
- }
47
-
48
- export function stringOptionsField(
49
- displayName: string,
50
- ...options: FieldOption[]
51
- ) {
52
- return makeScalarField({
53
- type: FieldType.String as const,
54
- displayName,
55
- options,
56
- });
57
- }
58
-
59
- export function withScalarOptions<
60
- S extends SchemaField,
61
- S2 extends Partial<SchemaField>,
62
- >(options: S2, v: (name: string) => S): (name: string) => S & S2 {
63
- return (n) => ({ ...v(n), ...options });
64
- }
65
-
66
- export function makeScalarField<S extends Partial<SchemaField>>(
67
- options: S,
68
- ): (name: string) => SchemaField & S {
69
- return (n) => ({ ...defaultScalarField(n, n), ...options });
70
- }
71
-
72
- export function makeCompoundField<S extends Partial<CompoundField>>(
73
- options: S,
74
- ): (name: string) => CompoundField & {
75
- type: FieldType.Compound;
76
- } & S {
77
- return (n) => ({ ...defaultCompoundField(n, n, false), ...options });
78
- }
79
-
80
- export function intField(
81
- displayName: string,
82
- options?: Partial<Omit<SchemaField, "type">>,
83
- ) {
84
- return makeScalarField({
85
- type: FieldType.Int as const,
86
- displayName,
87
- ...options,
88
- });
89
- }
90
-
91
- export function boolField(
92
- displayName: string,
93
- options?: Partial<Omit<SchemaField, "type">>,
94
- ) {
95
- return makeScalarField({
96
- type: FieldType.Bool as const,
97
- displayName,
98
- ...options,
99
- });
100
- }
101
-
102
- export function compoundField<
103
- Other extends Partial<Omit<CompoundField, "type" | "schemaType">>,
104
- >(
105
- displayName: string,
106
- fields: SchemaField[],
107
- other: Other,
108
- ): (name: string) => CompoundField & {
109
- collection: Other["collection"];
110
- } {
111
- return (field) =>
112
- ({
113
- ...defaultCompoundField(field, displayName, false),
114
- ...other,
115
- children: fields,
116
- }) as any;
117
- }
118
-
119
- export function defaultScalarField(
120
- field: string,
121
- displayName: string,
122
- ): Omit<SchemaField, "type"> & {
123
- type: FieldType.String;
124
- } {
125
- return {
126
- field,
127
- displayName,
128
- type: FieldType.String,
129
- };
130
- }
131
-
132
- export function defaultCompoundField(
133
- field: string,
134
- displayName: string,
135
- collection: boolean,
136
- ): CompoundField & {
137
- type: FieldType.Compound;
138
- } {
139
- return {
140
- field,
141
- displayName,
142
- type: FieldType.Compound,
143
- collection,
144
- children: [],
145
- };
146
- }
147
-
148
- export function mergeField(
149
- field: SchemaField,
150
- mergeInto: SchemaField[],
151
- ): SchemaField[] {
152
- const existing = mergeInto.find((x) => x.field === field.field);
153
- if (existing) {
154
- return mergeInto.map((x) =>
155
- x !== existing
156
- ? x
157
- : {
158
- ...x,
159
- onlyForTypes: mergeTypes(x.onlyForTypes, field.onlyForTypes),
160
- },
161
- );
162
- }
163
- return [...mergeInto, field];
164
-
165
- function mergeTypes(f?: string[] | null, s?: string[] | null) {
166
- if (!f) return s;
167
- if (!s) return f;
168
- const extras = s.filter((x) => !f.includes(x));
169
- return extras.length ? [...f, ...extras] : f;
170
- }
171
- }
@@ -1,31 +0,0 @@
1
- import { FieldType, SchemaField, SchemaInterface } from "./types";
2
-
3
- export const defaultSchemaInterface: SchemaInterface = {
4
- isEmptyValue: defaultIsEmpty,
5
- textValue: defaultTextValue,
6
- };
7
-
8
- export function defaultIsEmpty(f: SchemaField, value: any): boolean {
9
- if (f.collection)
10
- return Array.isArray(value) ? value.length === 0 : value == null;
11
- switch (f.type) {
12
- case FieldType.String:
13
- return !value;
14
- default:
15
- return value == null;
16
- }
17
- }
18
-
19
- export function defaultTextValue(
20
- f: SchemaField,
21
- value: any,
22
- ): string | undefined {
23
- switch (f.type) {
24
- case FieldType.DateTime:
25
- return new Date(value).toLocaleDateString();
26
- case FieldType.Date:
27
- return new Date(value).toLocaleDateString();
28
- default:
29
- return value != null ? value.toString() : undefined;
30
- }
31
- }
package/src/tailwind.tsx DELETED
@@ -1,29 +0,0 @@
1
- import React from "react";
2
- import { DefaultRendererOptions } from "./renderers";
3
-
4
- export const defaultTailwindTheme = {
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: "flex flex-col gap-4",
16
- gridClassName: "gap-x-2 gap-y-4",
17
- flexClassName: "gap-2",
18
- },
19
- action: {
20
- className: "bg-primary rounded-lg p-3 text-white",
21
- },
22
- layout: {
23
- className: "flex flex-col",
24
- errorClass: "text-sm text-danger-500",
25
- },
26
- data: {
27
- displayOnlyClass: "flex flex-row items-center gap-2",
28
- },
29
- } satisfies DefaultRendererOptions;
package/src/types.ts DELETED
@@ -1,423 +0,0 @@
1
- export interface SchemaField {
2
- type: string;
3
- field: string;
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
- }
17
-
18
- export enum FieldType {
19
- String = "String",
20
- Bool = "Bool",
21
- Int = "Int",
22
- Date = "Date",
23
- DateTime = "DateTime",
24
- Double = "Double",
25
- EntityRef = "EntityRef",
26
- Compound = "Compound",
27
- AutoId = "AutoId",
28
- Image = "Image",
29
- Any = "Any",
30
- }
31
-
32
- export interface EntityRefField extends SchemaField {
33
- type: FieldType.EntityRef;
34
- entityRefType: string;
35
- parentField: string;
36
- }
37
-
38
- export interface FieldOption {
39
- name: string;
40
- value: any;
41
- }
42
-
43
- export interface CompoundField extends SchemaField {
44
- type: FieldType.Compound;
45
- children: SchemaField[];
46
- treeChildren?: boolean;
47
- }
48
-
49
- export type AnyControlDefinition =
50
- | DataControlDefinition
51
- | GroupedControlsDefinition
52
- | ActionControlDefinition
53
- | DisplayControlDefinition;
54
-
55
- export interface SchemaInterface {
56
- isEmptyValue(field: SchemaField, value: any): boolean;
57
- textValue(
58
- field: SchemaField,
59
- value: any,
60
- element?: boolean,
61
- ): string | undefined;
62
- }
63
- export interface ControlDefinition {
64
- type: string;
65
- title?: string | null;
66
- styleClass?: string | null;
67
- layoutClass?: string | null;
68
- labelClass?: string | null;
69
- dynamic?: DynamicProperty[] | null;
70
- adornments?: ControlAdornment[] | null;
71
- children?: ControlDefinition[] | null;
72
- }
73
-
74
- export enum ControlDefinitionType {
75
- Data = "Data",
76
- Group = "Group",
77
- Display = "Display",
78
- Action = "Action",
79
- }
80
-
81
- export interface DynamicProperty {
82
- type: string;
83
- expr: EntityExpression;
84
- }
85
-
86
- export enum DynamicPropertyType {
87
- Visible = "Visible",
88
- DefaultValue = "DefaultValue",
89
- Readonly = "Readonly",
90
- Disabled = "Disabled",
91
- Display = "Display",
92
- Style = "Style",
93
- LayoutStyle = "LayoutStyle",
94
- AllowedOptions = "AllowedOptions",
95
- Label = "Label",
96
- }
97
-
98
- export interface EntityExpression {
99
- type: string;
100
- }
101
-
102
- export enum ExpressionType {
103
- Jsonata = "Jsonata",
104
- Data = "Data",
105
- DataMatch = "FieldValue",
106
- UserMatch = "UserMatch",
107
- }
108
-
109
- export interface JsonataExpression extends EntityExpression {
110
- type: ExpressionType.Jsonata;
111
- expression: string;
112
- }
113
-
114
- export interface DataExpression extends EntityExpression {
115
- type: ExpressionType.Data;
116
- field: string;
117
- }
118
-
119
- export interface DataMatchExpression extends EntityExpression {
120
- type: ExpressionType.DataMatch;
121
- field: string;
122
- value: any;
123
- }
124
-
125
- export interface UserMatchExpression extends EntityExpression {
126
- type: ExpressionType.UserMatch;
127
- userMatch: string;
128
- }
129
-
130
- export interface ControlAdornment {
131
- type: string;
132
- }
133
-
134
- export enum AdornmentPlacement {
135
- ControlStart = "ControlStart",
136
- ControlEnd = "ControlEnd",
137
- LabelStart = "LabelStart",
138
- LabelEnd = "LabelEnd",
139
- }
140
-
141
- export enum ControlAdornmentType {
142
- Tooltip = "Tooltip",
143
- Accordion = "Accordion",
144
- HelpText = "HelpText",
145
- Icon = "Icon",
146
- }
147
-
148
- export interface IconAdornment extends ControlAdornment {
149
- type: ControlAdornmentType.Icon;
150
- iconClass: string;
151
- placement?: AdornmentPlacement | null;
152
- }
153
-
154
- export interface TooltipAdornment extends ControlAdornment {
155
- type: ControlAdornmentType.Tooltip;
156
- tooltip: string;
157
- }
158
-
159
- export interface AccordionAdornment extends ControlAdornment {
160
- type: ControlAdornmentType.Accordion;
161
- title: string;
162
- defaultExpanded: boolean;
163
- }
164
-
165
- export interface HelpTextAdornment extends ControlAdornment {
166
- type: ControlAdornmentType.HelpText;
167
- helpText: string;
168
- placement?: AdornmentPlacement | null;
169
- }
170
-
171
- export interface DataControlDefinition extends ControlDefinition {
172
- type: ControlDefinitionType.Data;
173
- field: string;
174
- required?: boolean | null;
175
- renderOptions?: RenderOptions | null;
176
- defaultValue?: any;
177
- readonly?: boolean | null;
178
- validators?: SchemaValidator[] | null;
179
- hideTitle?: boolean | null;
180
- dontClearHidden?: boolean | null;
181
- }
182
-
183
- export interface RenderOptions {
184
- type: string;
185
- }
186
-
187
- export enum DataRenderType {
188
- Standard = "Standard",
189
- Radio = "Radio",
190
- HtmlEditor = "HtmlEditor",
191
- IconList = "IconList",
192
- CheckList = "CheckList",
193
- UserSelection = "UserSelection",
194
- Synchronised = "Synchronised",
195
- IconSelector = "IconSelector",
196
- DateTime = "DateTime",
197
- Checkbox = "Checkbox",
198
- Dropdown = "Dropdown",
199
- DisplayOnly = "DisplayOnly",
200
- }
201
-
202
- export interface RadioButtonRenderOptions extends RenderOptions {
203
- type: DataRenderType.Radio;
204
- }
205
-
206
- export interface StandardRenderer extends RenderOptions {
207
- type: DataRenderType.Standard;
208
- }
209
-
210
- export interface HtmlEditorRenderOptions extends RenderOptions {
211
- type: DataRenderType.HtmlEditor;
212
- allowImages: boolean;
213
- }
214
-
215
- export interface DateTimeRenderOptions extends RenderOptions {
216
- type: DataRenderType.DateTime;
217
- format?: string | null;
218
- }
219
-
220
- export interface IconListRenderOptions extends RenderOptions {
221
- type: DataRenderType.IconList;
222
- iconMappings: IconMapping[];
223
- }
224
-
225
- export interface DisplayOnlyRenderOptions extends RenderOptions {
226
- type: DataRenderType.DisplayOnly;
227
- emptyText?: string | null;
228
- sampleText?: string | null;
229
- }
230
- export interface IconMapping {
231
- value: string;
232
- materialIcon?: string | null;
233
- }
234
-
235
- export interface CheckListRenderOptions extends RenderOptions {
236
- type: DataRenderType.CheckList;
237
- }
238
-
239
- export interface SynchronisedRenderOptions extends RenderOptions {
240
- type: DataRenderType.Synchronised;
241
- fieldToSync: string;
242
- syncType: SyncTextType;
243
- }
244
-
245
- export enum SyncTextType {
246
- Camel = "Camel",
247
- Snake = "Snake",
248
- Pascal = "Pascal",
249
- }
250
-
251
- export interface UserSelectionRenderOptions extends RenderOptions {
252
- type: DataRenderType.UserSelection;
253
- noGroups: boolean;
254
- noUsers: boolean;
255
- }
256
-
257
- export interface IconSelectionRenderOptions extends RenderOptions {
258
- type: DataRenderType.IconSelector;
259
- }
260
-
261
- export interface GroupedControlsDefinition extends ControlDefinition {
262
- type: ControlDefinitionType.Group;
263
- compoundField?: string | null;
264
- groupOptions?: GroupRenderOptions;
265
- }
266
-
267
- export interface GroupRenderOptions {
268
- type: string;
269
- hideTitle?: boolean | null;
270
- }
271
-
272
- export enum GroupRenderType {
273
- Standard = "Standard",
274
- Grid = "Grid",
275
- Flex = "Flex",
276
- GroupElement = "GroupElement",
277
- }
278
-
279
- export interface StandardGroupRenderer extends GroupRenderOptions {
280
- type: GroupRenderType.Standard;
281
- }
282
-
283
- export interface FlexRenderer extends GroupRenderOptions {
284
- type: GroupRenderType.Flex;
285
- direction?: string | null;
286
- gap?: string | null;
287
- }
288
-
289
- export interface GroupElementRenderer extends GroupRenderOptions {
290
- type: GroupRenderType.GroupElement;
291
- value: any;
292
- }
293
-
294
- export interface GridRenderer extends GroupRenderOptions {
295
- type: GroupRenderType.Grid;
296
- columns?: number | null;
297
- }
298
-
299
- export interface DisplayControlDefinition extends ControlDefinition {
300
- type: ControlDefinitionType.Display;
301
- displayData: DisplayData;
302
- }
303
-
304
- export interface DisplayData {
305
- type: string;
306
- }
307
-
308
- export enum DisplayDataType {
309
- Text = "Text",
310
- Html = "Html",
311
- Icon = "Icon",
312
- }
313
- export interface TextDisplay extends DisplayData {
314
- type: DisplayDataType.Text;
315
- text: string;
316
- }
317
-
318
- export interface IconDisplay extends DisplayData {
319
- type: DisplayDataType.Icon;
320
- iconClass: string;
321
- }
322
-
323
- export interface HtmlDisplay extends DisplayData {
324
- type: DisplayDataType.Html;
325
- html: string;
326
- }
327
-
328
- export interface ActionControlDefinition extends ControlDefinition {
329
- type: ControlDefinitionType.Action;
330
- actionId: string;
331
- }
332
-
333
- export enum ValidatorType {
334
- Jsonata = "Jsonata",
335
- Date = "Date",
336
- }
337
- export interface SchemaValidator {
338
- type: string;
339
- }
340
-
341
- export interface JsonataValidator extends SchemaValidator {
342
- type: ValidatorType.Jsonata;
343
- expression: string;
344
- }
345
-
346
- export enum DateComparison {
347
- NotBefore = "NotBefore",
348
- NotAfter = "NotAfter",
349
- }
350
-
351
- export interface DateValidator extends SchemaValidator {
352
- type: ValidatorType.Date;
353
- comparison: DateComparison;
354
- fixedDate?: string | null;
355
- daysFromCurrent?: number | null;
356
- }
357
-
358
- export function isDataControlDefinition(
359
- x: ControlDefinition,
360
- ): x is DataControlDefinition {
361
- return x.type === ControlDefinitionType.Data;
362
- }
363
-
364
- export function isGroupControlsDefinition(
365
- x: ControlDefinition,
366
- ): x is GroupedControlsDefinition {
367
- return x.type === ControlDefinitionType.Group;
368
- }
369
-
370
- export function isDisplayControlsDefinition(
371
- x: ControlDefinition,
372
- ): x is DisplayControlDefinition {
373
- return x.type === ControlDefinitionType.Display;
374
- }
375
-
376
- export function isActionControlsDefinition(
377
- x: ControlDefinition,
378
- ): x is ActionControlDefinition {
379
- return x.type === ControlDefinitionType.Action;
380
- }
381
-
382
- export interface ControlVisitor<A> {
383
- data(d: DataControlDefinition): A;
384
- group(d: GroupedControlsDefinition): A;
385
- display(d: DisplayControlDefinition): A;
386
- action(d: ActionControlDefinition): A;
387
- }
388
-
389
- export function visitControlDefinition<A>(
390
- x: ControlDefinition,
391
- visitor: ControlVisitor<A>,
392
- defaultValue: (c: ControlDefinition) => A,
393
- ): A {
394
- switch (x.type) {
395
- case ControlDefinitionType.Action:
396
- return visitor.action(x as ActionControlDefinition);
397
- case ControlDefinitionType.Data:
398
- return visitor.data(x as DataControlDefinition);
399
- case ControlDefinitionType.Display:
400
- return visitor.display(x as DisplayControlDefinition);
401
- case ControlDefinitionType.Group:
402
- return visitor.group(x as GroupedControlsDefinition);
403
- default:
404
- return defaultValue(x);
405
- }
406
- }
407
- export function isGridRenderer(
408
- options: GroupRenderOptions,
409
- ): options is GridRenderer {
410
- return options.type === GroupRenderType.Grid;
411
- }
412
-
413
- export function isFlexRenderer(
414
- options: GroupRenderOptions,
415
- ): options is FlexRenderer {
416
- return options.type === GroupRenderType.Flex;
417
- }
418
-
419
- export function isDisplayOnlyRenderer(
420
- options: RenderOptions,
421
- ): options is DisplayOnlyRenderOptions {
422
- return options.type === DataRenderType.DisplayOnly;
423
- }