@react-typed-forms/schemas 13.3.0 → 13.4.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,6 +1,8 @@
1
- import { AccordionAdornment, CheckListRenderOptions, ControlAdornmentType, ControlDefinition, DataControlDefinition, DataExpression, DataMatchExpression, DataRenderType, DateValidator, DisplayControlDefinition, DisplayOnlyRenderOptions, DynamicProperty, EntityExpression, GroupedControlsDefinition, JsonataExpression, JsonataRenderOptions, JsonataValidator, LengthValidator, RadioButtonRenderOptions, RenderOptions, SchemaField, SchemaMap, TextfieldRenderOptions, ValidatorType } from "./types";
1
+ import { AccordionAdornment, ActionControlDefinition, CheckListRenderOptions, ControlAdornmentType, ControlDefinition, DataControlDefinition, DataRenderType, DisplayControlDefinition, DisplayOnlyRenderOptions, DynamicProperty, GroupedControlsDefinition, JsonataRenderOptions, RadioButtonRenderOptions, RenderOptions, TextfieldRenderOptions } from "./controlDefinition";
2
2
  import { ActionRendererProps } from "./controlRender";
3
- import { ActionControlDefinition } from "./types";
3
+ import { DateValidator, JsonataValidator, LengthValidator, ValidatorType } from "./schemaValidator";
4
+ import { SchemaField, SchemaMap } from "./schemaField";
5
+ import { DataExpression, DataMatchExpression, EntityExpression, JsonataExpression } from "./entityExpression";
4
6
  export declare function dataControl(field: string, title?: string | null, options?: Partial<DataControlDefinition>): DataControlDefinition;
5
7
  export declare function validatorOptions<A extends {
6
8
  type: string;
@@ -1,75 +1,29 @@
1
- import { Control } from "@react-typed-forms/core";
2
- import { SchemaDataNode, SchemaNode } from "./treeNodes";
3
- import { ControlDataContext } from "./util";
4
- export interface SchemaField {
5
- type: string;
6
- field: string;
7
- displayName?: string | null;
8
- tags?: string[] | null;
9
- system?: boolean | null;
10
- collection?: boolean | null;
11
- onlyForTypes?: string[] | null;
12
- required?: boolean | null;
13
- notNullable?: boolean | null;
14
- defaultValue?: any;
15
- isTypeField?: boolean | null;
16
- searchable?: boolean | null;
17
- options?: FieldOption[] | null;
18
- validators?: SchemaValidator[] | null;
19
- }
20
- export type SchemaMap = Record<string, SchemaField[]>;
21
- export declare enum FieldType {
22
- String = "String",
23
- Bool = "Bool",
24
- Int = "Int",
25
- Date = "Date",
26
- DateTime = "DateTime",
27
- Time = "Time",
28
- Double = "Double",
29
- EntityRef = "EntityRef",
30
- Compound = "Compound",
31
- AutoId = "AutoId",
32
- Image = "Image",
33
- Any = "Any"
34
- }
35
- export interface EntityRefField extends SchemaField {
36
- type: FieldType.EntityRef;
37
- entityRefType: string;
38
- parentField: string;
39
- }
40
- export interface FieldOption {
41
- name: string;
42
- value: any;
43
- description?: string | null;
44
- group?: string | null;
45
- disabled?: boolean | null;
46
- }
47
- export interface CompoundField extends SchemaField {
48
- type: FieldType.Compound;
49
- children: SchemaField[];
50
- treeChildren?: boolean;
51
- schemaRef?: string;
52
- }
1
+ import { SchemaValidator } from "./schemaValidator";
2
+ import { FieldOption, SchemaDataNode, SchemaField, SchemaInterface, SchemaNode } from "./schemaField";
3
+ import { EntityExpression } from "./entityExpression";
4
+ /**
5
+ * Interface representing the form context data.
6
+ */
7
+ export interface FormContextData {
8
+ option?: FieldOption;
9
+ optionSelected?: boolean;
10
+ }
11
+ /**
12
+ * Interface representing the control data context.
13
+ */
14
+ export interface ControlDataContext {
15
+ schemaInterface: SchemaInterface;
16
+ dataNode: SchemaDataNode | undefined;
17
+ parentNode: SchemaDataNode;
18
+ formData: FormContextData;
19
+ }
20
+ /**
21
+ * Represents any control definition.
22
+ */
53
23
  export type AnyControlDefinition = DataControlDefinition | GroupedControlsDefinition | ActionControlDefinition | DisplayControlDefinition;
54
- export declare enum ValidationMessageType {
55
- NotEmpty = "NotEmpty",
56
- MinLength = "MinLength",
57
- MaxLength = "MaxLength",
58
- NotAfterDate = "NotAfterDate",
59
- NotBeforeDate = "NotBeforeDate"
60
- }
61
- export interface SchemaInterface {
62
- isEmptyValue(field: SchemaField, value: any): boolean;
63
- textValue(field: SchemaField, value: any, element?: boolean): string | undefined;
64
- controlLength(field: SchemaField, control: Control<any>): number;
65
- valueLength(field: SchemaField, value: any): number;
66
- getOptions(field: SchemaField): FieldOption[] | undefined | null;
67
- getFilterOptions(array: SchemaDataNode, field: SchemaNode): FieldOption[] | undefined | null;
68
- parseToMillis(field: SchemaField, v: string): number;
69
- validationMessageText(field: SchemaField, messageType: ValidationMessageType, actual: any, expected: any): string;
70
- compareValue(field: SchemaField, v1: unknown, v2: unknown): number;
71
- searchText(field: SchemaField, value: any): string;
72
- }
24
+ /**
25
+ * Represents a control definition.
26
+ */
73
27
  export interface ControlDefinition {
74
28
  type: string;
75
29
  title?: string | null;
@@ -102,38 +56,6 @@ export declare enum DynamicPropertyType {
102
56
  Label = "Label",
103
57
  ActionData = "ActionData"
104
58
  }
105
- export interface EntityExpression {
106
- type: string;
107
- }
108
- export declare enum ExpressionType {
109
- Jsonata = "Jsonata",
110
- Data = "Data",
111
- DataMatch = "FieldValue",
112
- UserMatch = "UserMatch",
113
- NotEmpty = "NotEmpty",
114
- UUID = "UUID"
115
- }
116
- export interface JsonataExpression extends EntityExpression {
117
- type: ExpressionType.Jsonata;
118
- expression: string;
119
- }
120
- export interface DataExpression extends EntityExpression {
121
- type: ExpressionType.Data;
122
- field: string;
123
- }
124
- export interface DataMatchExpression extends EntityExpression {
125
- type: ExpressionType.DataMatch;
126
- field: string;
127
- value: any;
128
- }
129
- export interface NotEmptyExpression extends EntityExpression {
130
- type: ExpressionType.DataMatch;
131
- field: string;
132
- }
133
- export interface UserMatchExpression extends EntityExpression {
134
- type: ExpressionType.UserMatch;
135
- userMatch: string;
136
- }
137
59
  export interface ControlAdornment {
138
60
  type: string;
139
61
  }
@@ -370,33 +292,6 @@ export interface ActionControlDefinition extends ControlDefinition {
370
292
  actionId: string;
371
293
  actionData?: string | null;
372
294
  }
373
- export declare enum ValidatorType {
374
- Jsonata = "Jsonata",
375
- Date = "Date",
376
- Length = "Length"
377
- }
378
- export interface SchemaValidator {
379
- type: string;
380
- }
381
- export interface JsonataValidator extends SchemaValidator {
382
- type: ValidatorType.Jsonata;
383
- expression: string;
384
- }
385
- export interface LengthValidator extends SchemaValidator {
386
- type: ValidatorType.Length;
387
- min?: number | null;
388
- max?: number | null;
389
- }
390
- export declare enum DateComparison {
391
- NotBefore = "NotBefore",
392
- NotAfter = "NotAfter"
393
- }
394
- export interface DateValidator extends SchemaValidator {
395
- type: ValidatorType.Date;
396
- comparison: DateComparison;
397
- fixedDate?: string | null;
398
- daysFromCurrent?: number | null;
399
- }
400
295
  export declare function isDataControlDefinition(x: ControlDefinition): x is DataControlDefinition;
401
296
  export declare function isGroupControlsDefinition(x: ControlDefinition): x is GroupedControlsDefinition;
402
297
  export declare function isDisplayControlsDefinition(x: ControlDefinition): x is DisplayControlDefinition;
@@ -415,10 +310,30 @@ export declare function isDisplayOnlyRenderer(options: RenderOptions): options i
415
310
  export declare function isTextfieldRenderer(options: RenderOptions): options is TextfieldRenderOptions;
416
311
  export declare function isDataGroupRenderer(options?: RenderOptions | null): options is DataGroupRenderOptions;
417
312
  export declare function isArrayRenderer(options: RenderOptions): options is ArrayRenderOptions;
418
- export declare function findField(fields: SchemaField[], field: string): SchemaField | undefined;
419
- export declare function isScalarField(sf: SchemaField): sf is SchemaField;
420
- export declare function isCompoundField(sf: SchemaField): sf is CompoundField;
421
313
  export declare function isDataControl(c: ControlDefinition): c is DataControlDefinition;
422
314
  export declare function isGroupControl(c: ControlDefinition): c is GroupedControlsDefinition;
423
315
  export type ControlActionHandler = (actionId: string, actionData: any, ctx: ControlDataContext) => (() => void) | undefined;
424
316
  export declare function isCheckEntryClasses(options?: RenderOptions | null): options is CheckEntryClasses & RenderOptions;
317
+ export interface FormNode {
318
+ definition: ControlDefinition;
319
+ getChildNodes(): FormNode[];
320
+ parent?: FormNode;
321
+ }
322
+ export interface FormTreeLookup<A = string> {
323
+ getForm(formId: A): FormTreeNode | undefined;
324
+ }
325
+ export interface FormTreeNode extends FormTreeLookup {
326
+ rootNode: FormNode;
327
+ }
328
+ export declare function createFormLookup<A extends Record<string, ControlDefinition[]>>(formMap: A): FormTreeLookup<keyof A>;
329
+ export declare function fieldPathForDefinition(c: ControlDefinition): string[] | undefined;
330
+ export declare function lookupDataNode(c: ControlDefinition, parentNode: SchemaDataNode): SchemaDataNode | undefined;
331
+ export declare function traverseParents<A, B extends {
332
+ parent?: B | undefined;
333
+ }>(current: B | undefined, get: (b: B) => A, until?: (b: B) => boolean): A[];
334
+ export declare function getRootDataNode(dataNode: SchemaDataNode): SchemaDataNode;
335
+ export declare function getJsonPath(dataNode: SchemaDataNode): (string | number)[];
336
+ export declare function getSchemaPath(schemaNode: SchemaNode): SchemaField[];
337
+ export declare function getSchemaFieldList(schema: SchemaNode): SchemaField[];
338
+ export declare function visitControlDataArray<A>(controls: ControlDefinition[] | undefined | null, context: SchemaDataNode, cb: (definition: DataControlDefinition, node: SchemaDataNode) => A | undefined): A | undefined;
339
+ export declare function visitControlData<A>(definition: ControlDefinition, ctx: SchemaDataNode, cb: (definition: DataControlDefinition, field: SchemaDataNode) => A | undefined): A | undefined;
@@ -1,20 +1,76 @@
1
1
  import React, { FC, Key, ReactElement, ReactNode } from "react";
2
2
  import { Control } from "@react-typed-forms/core";
3
- import { AdornmentPlacement, ArrayActionOptions, ControlActionHandler, ControlAdornment, ControlDefinition, DataControlDefinition, DisplayData, FieldOption, GroupRenderOptions, RenderOptions, SchemaField, SchemaInterface, SchemaValidator } from "./types";
4
- import { ControlClasses, ControlDataContext, FormContextData, JsonPath } from "./util";
3
+ import { AdornmentPlacement, ArrayActionOptions, ControlActionHandler, ControlAdornment, ControlDefinition, DataControlDefinition, DisplayData, GroupRenderOptions, RenderOptions, ControlDataContext, FormContextData } from "./controlDefinition";
4
+ import { ControlClasses, JsonPath } from "./util";
5
5
  import { EvalExpressionHook, UseEvalExpressionHook } from "./hooks";
6
6
  import { ValidationContext } from "./validators";
7
- import { SchemaDataNode } from "./treeNodes";
7
+ import { SchemaValidator } from "./schemaValidator";
8
+ import { FieldOption, SchemaDataNode, SchemaField, SchemaInterface } from "./schemaField";
9
+ /**
10
+ * Interface for rendering different types of form controls.
11
+ */
8
12
  export interface FormRenderer {
13
+ /**
14
+ * Renders data control.
15
+ * @param props - Properties for data renderer.
16
+ * @returns A function that takes layout properties and returns layout properties.
17
+ */
9
18
  renderData: (props: DataRendererProps) => (layout: ControlLayoutProps) => ControlLayoutProps;
19
+ /**
20
+ * Renders group control.
21
+ * @param props - Properties for group renderer.
22
+ * @returns A function that takes layout properties and returns layout properties.
23
+ */
10
24
  renderGroup: (props: GroupRendererProps) => (layout: ControlLayoutProps) => ControlLayoutProps;
25
+ /**
26
+ * Renders display control.
27
+ * @param props - Properties for display renderer.
28
+ * @returns A React node.
29
+ */
11
30
  renderDisplay: (props: DisplayRendererProps) => ReactNode;
31
+ /**
32
+ * Renders action control.
33
+ * @param props - Properties for action renderer.
34
+ * @returns A React node.
35
+ */
12
36
  renderAction: (props: ActionRendererProps) => ReactNode;
37
+ /**
38
+ * Renders array control.
39
+ * @param props - Properties for array renderer.
40
+ * @returns A React node.
41
+ */
13
42
  renderArray: (props: ArrayRendererProps) => ReactNode;
43
+ /**
44
+ * Renders adornment.
45
+ * @param props - Properties for adornment renderer.
46
+ * @returns An adornment renderer.
47
+ */
14
48
  renderAdornment: (props: AdornmentProps) => AdornmentRenderer;
49
+ /**
50
+ * Renders label.
51
+ * @param props - Properties for label renderer.
52
+ * @param labelStart - React node to render at the start of the label.
53
+ * @param labelEnd - React node to render at the end of the label.
54
+ * @returns A React node.
55
+ */
15
56
  renderLabel: (props: LabelRendererProps, labelStart: ReactNode, labelEnd: ReactNode) => ReactNode;
57
+ /**
58
+ * Renders layout.
59
+ * @param props - Properties for control layout.
60
+ * @returns A rendered control.
61
+ */
16
62
  renderLayout: (props: ControlLayoutProps) => RenderedControl;
63
+ /**
64
+ * Renders visibility control.
65
+ * @param props - Properties for visibility renderer.
66
+ * @returns A React node.
67
+ */
17
68
  renderVisibility: (props: VisibilityRendererProps) => ReactNode;
69
+ /**
70
+ * Renders label text.
71
+ * @param props - React node for label text.
72
+ * @returns A React node.
73
+ */
18
74
  renderLabelText: (props: ReactNode) => ReactNode;
19
75
  }
20
76
  export interface AdornmentProps {
@@ -76,24 +132,76 @@ export interface ControlLayoutProps {
76
132
  className?: string | null;
77
133
  style?: React.CSSProperties;
78
134
  }
135
+ /**
136
+ * Enum representing the types of labels that can be rendered.
137
+ */
79
138
  export declare enum LabelType {
139
+ /**
140
+ * Label for a control.
141
+ */
80
142
  Control = 0,
143
+ /**
144
+ * Label for a group.
145
+ */
81
146
  Group = 1,
147
+ /**
148
+ * Label for text.
149
+ */
82
150
  Text = 2
83
151
  }
152
+ /**
153
+ * Properties for label renderers.
154
+ */
84
155
  export interface LabelRendererProps {
156
+ /**
157
+ * The type of the label.
158
+ */
85
159
  type: LabelType;
160
+ /**
161
+ * Whether to hide the label.
162
+ */
86
163
  hide?: boolean | null;
164
+ /**
165
+ * The content of the label.
166
+ */
87
167
  label: ReactNode;
168
+ /**
169
+ * Whether to show the label as being required.
170
+ * E.g. show an asterisk next to the label.
171
+ */
88
172
  required?: boolean | null;
173
+ /**
174
+ * The ID of the element the label is for.
175
+ */
89
176
  forId?: string;
177
+ /**
178
+ * The CSS class name for the label.
179
+ */
90
180
  className?: string;
91
181
  }
182
+ /**
183
+ * Properties for display renderers.
184
+ */
92
185
  export interface DisplayRendererProps {
186
+ /**
187
+ * The data to be displayed.
188
+ */
93
189
  data: DisplayData;
190
+ /**
191
+ * A control with dynamic value for display.
192
+ */
94
193
  display?: Control<string | undefined>;
194
+ /**
195
+ * The context for the control data.
196
+ */
95
197
  dataContext: ControlDataContext;
198
+ /**
199
+ * The CSS class name for the display renderer.
200
+ */
96
201
  className?: string;
202
+ /**
203
+ * The CSS styles for the display renderer.
204
+ */
97
205
  style?: React.CSSProperties;
98
206
  }
99
207
  export type ChildVisibilityFunc = (child: ControlDefinition, parentNode?: SchemaDataNode, dontOverride?: boolean) => EvalExpressionHook<boolean>;
@@ -1,6 +1,5 @@
1
- import { FieldOption, SchemaField, SchemaInterface, ValidationMessageType } from "./types";
2
1
  import { Control } from "@react-typed-forms/core";
3
- import { SchemaDataNode, SchemaNode } from "./treeNodes";
2
+ import { FieldOption, SchemaDataNode, SchemaField, SchemaInterface, SchemaNode, ValidationMessageType } from "./schemaField";
4
3
  export declare class DefaultSchemaInterface implements SchemaInterface {
5
4
  protected boolStrings: [string, string];
6
5
  protected parseDateTime: (s: string) => number;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Type representing a hook dependency, which can be a string, number, undefined, or null.
3
+ */
4
+ export type HookDep = string | number | undefined | null;
5
+ /**
6
+ * Interface representing a dynamic hook generator.
7
+ * @template A - The type of the hook result.
8
+ * @template P - The type of the hook context.
9
+ */
10
+ export interface DynamicHookGenerator<A, P> {
11
+ deps: HookDep;
12
+ state: any;
13
+ runHook(ctx: P, state: any): A;
14
+ }
15
+ /**
16
+ * Creates a dynamic hook generator.
17
+ * @template A - The type of the hook result.
18
+ * @template P - The type of the hook context.
19
+ * @template S - The type of the hook state.
20
+ * @param runHook - The function to run the hook.
21
+ * @param state - The initial state of the hook.
22
+ * @param deps - The dependencies of the hook.
23
+ * @returns The dynamic hook generator.
24
+ */
25
+ export declare function makeHook<A, P, S = undefined>(runHook: (ctx: P, state: S) => A, state: S, deps?: HookDep): DynamicHookGenerator<A, P>;
26
+ /**
27
+ * Type representing the value of a dynamic hook.
28
+ * @template A - The type of the dynamic hook generator.
29
+ */
30
+ export type DynamicHookValue<A> = A extends DynamicHookGenerator<infer V, any> ? V : never;
31
+ /**
32
+ * Converts an array of dependencies to a dependency string.
33
+ * @template A - The type of the dependencies.
34
+ * @param deps - The array of dependencies.
35
+ * @param asHookDep - The function to convert a dependency to a hook dependency.
36
+ * @returns The dependency string.
37
+ */
38
+ export declare function makeHookDepString<A>(deps: A[], asHookDep: (a: A) => HookDep): string;
39
+ /**
40
+ * Custom hook to use dynamic hooks.
41
+ * @template P - The type of the hook context.
42
+ * @template Hooks - The type of the hooks.
43
+ * @param hooks - The hooks to use.
44
+ * @returns A function that takes the hook context and returns the hook values.
45
+ */
46
+ export declare function useDynamicHooks<P, Hooks extends Record<string, DynamicHookGenerator<any, P>>>(hooks: Hooks): (p: P) => {
47
+ [K in keyof Hooks]: DynamicHookValue<Hooks[K]>;
48
+ };
49
+ /**
50
+ * Converts a value to a dependency string.
51
+ * @param x - The value to convert.
52
+ * @returns The dependency string.
53
+ */
54
+ export declare function toDepString(x: any): string;
@@ -0,0 +1,32 @@
1
+ export interface EntityExpression {
2
+ type: string;
3
+ }
4
+ export declare enum ExpressionType {
5
+ Jsonata = "Jsonata",
6
+ Data = "Data",
7
+ DataMatch = "FieldValue",
8
+ UserMatch = "UserMatch",
9
+ NotEmpty = "NotEmpty",
10
+ UUID = "UUID"
11
+ }
12
+ export interface JsonataExpression extends EntityExpression {
13
+ type: ExpressionType.Jsonata;
14
+ expression: string;
15
+ }
16
+ export interface DataExpression extends EntityExpression {
17
+ type: ExpressionType.Data;
18
+ field: string;
19
+ }
20
+ export interface DataMatchExpression extends EntityExpression {
21
+ type: ExpressionType.DataMatch;
22
+ field: string;
23
+ value: any;
24
+ }
25
+ export interface NotEmptyExpression extends EntityExpression {
26
+ type: ExpressionType.DataMatch;
27
+ field: string;
28
+ }
29
+ export interface UserMatchExpression extends EntityExpression {
30
+ type: ExpressionType.UserMatch;
31
+ userMatch: string;
32
+ }
package/lib/hooks.d.ts CHANGED
@@ -1,8 +1,10 @@
1
- import { ControlDefinition, DynamicPropertyType, EntityExpression, SchemaInterface } from "./types";
1
+ import { ControlDefinition, DynamicPropertyType, ControlDataContext } from "./controlDefinition";
2
2
  import React from "react";
3
3
  import { Control } from "@react-typed-forms/core";
4
- import { ControlDataContext, DynamicHookGenerator, JsonPath } from "./util";
5
- import { SchemaDataNode } from "./treeNodes";
4
+ import { JsonPath } from "./util";
5
+ import { DynamicHookGenerator } from "./dynamicHooks";
6
+ import { SchemaDataNode, SchemaInterface } from "./schemaField";
7
+ import { EntityExpression } from "./entityExpression";
6
8
  export type EvalExpressionHook<A = any> = DynamicHookGenerator<Control<A | undefined>, ControlDataContext>;
7
9
  export type UseEvalExpressionHook = (expr: EntityExpression | undefined | null, coerce: (v: any) => any) => DynamicHookGenerator<Control<any> | undefined, ControlDataContext>;
8
10
  export declare function useEvalVisibilityHook(useEvalExpressionHook: UseEvalExpressionHook, definition: ControlDefinition, overrideDataNode?: SchemaDataNode): EvalExpressionHook<boolean>;