@bolttech/form-engine-core 1.1.0 → 1.1.1
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.
- package/CHANGELOG.md +30 -10
- package/index.d.ts +349 -349
- package/index.esm.js +1 -1
- package/lite.esm.js +1 -1
- package/package.json +11 -2
- package/src/index.d.ts +3 -3
- package/src/managers/field.d.ts +3 -0
- package/src/managers/form.d.ts +1 -0
package/index.d.ts
CHANGED
|
@@ -2,383 +2,77 @@ import * as rxjs from 'rxjs';
|
|
|
2
2
|
import { Subject, BehaviorSubject, Subscription } from 'rxjs';
|
|
3
3
|
import { TCurrencyLocalCode, TCurrencyCode } from '@gaignoux/currency';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
type OneOf<T, K = keyof T> = K extends keyof T ? AllowOnly<T, K> : never;
|
|
9
|
-
type TValidationPayload = [
|
|
10
|
-
unknown,
|
|
11
|
-
TValidationMethods,
|
|
12
|
-
TFormValues<unknown>?
|
|
13
|
-
];
|
|
14
|
-
type TValidationHandler = Record<string, (...args: TValidationPayload) => boolean>;
|
|
5
|
+
declare const TEMPLATE_AVALIABLE_SCOPES: readonly ["fields", "iVars", "form"];
|
|
6
|
+
declare const ALLOWED_RESET_PROPS_MUTATIONS: (keyof Pick<IFormField, "api" | "apiSchema" | "props" | "validations" | "visibilityConditions" | "resetValues">)[];
|
|
15
7
|
|
|
16
|
-
|
|
8
|
+
/** @virtual */
|
|
9
|
+
|
|
10
|
+
type TAllowedResetPropsMutations = (typeof ALLOWED_RESET_PROPS_MUTATIONS)[number];
|
|
17
11
|
/**
|
|
18
|
-
* @type
|
|
19
|
-
* Represents the
|
|
20
|
-
*
|
|
21
|
-
* @property {
|
|
22
|
-
* @property {
|
|
23
|
-
* @property {
|
|
12
|
+
* @type TAllowedResetPropsMutationsEnum
|
|
13
|
+
* Represents the allowed properties to be changed on resetPropertyValues.
|
|
14
|
+
*
|
|
15
|
+
* @property {never} api - api property
|
|
16
|
+
* @property {never} apiSchema - apiSchema property
|
|
17
|
+
* @property {never} props - props property
|
|
18
|
+
* @property {never} validations - validations property
|
|
19
|
+
* @property {never} visibilityConditions - visibilityConditions property
|
|
20
|
+
* @property {never} resetValues - resetValues property
|
|
24
21
|
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* const componentProps: TComponentPropsMapping = {
|
|
28
|
-
* getValue: 'getValueFunction',
|
|
29
|
-
* setValue: 'setValueFunction',
|
|
30
|
-
* onBlur: 'handleBlur',
|
|
31
|
-
* onClick: 'handleClick',
|
|
32
|
-
* onFocus: 'handleFocus',
|
|
33
|
-
* onKeyUp: 'handleKeyUp',
|
|
34
|
-
* onKeyDown: 'handleKeyDown',
|
|
35
|
-
* setErrorMessage: 'setErrorMessageFunction',
|
|
36
|
-
* setErrorState: 'setErrorStateFunction'
|
|
37
|
-
* };
|
|
38
|
-
* ```
|
|
39
22
|
* @interface
|
|
40
23
|
*/
|
|
41
|
-
type
|
|
42
|
-
getValue?: string;
|
|
43
|
-
setValue?: string;
|
|
44
|
-
setErrorMessage?: string;
|
|
45
|
-
setErrorState?: string;
|
|
46
|
-
};
|
|
24
|
+
type TAllowedResetPropsMutationsEnum = Record<TAllowedResetPropsMutations, never>;
|
|
47
25
|
/**
|
|
48
|
-
* @type
|
|
49
|
-
* Represents the
|
|
50
|
-
* name, events, and an optional function to handle value changes.
|
|
26
|
+
* @type TLengthValidation
|
|
27
|
+
* Represents the validation rules based on the length of the input.
|
|
51
28
|
*
|
|
52
|
-
* @property {
|
|
53
|
-
*
|
|
54
|
-
* @property {string} componentName - The name of the component.
|
|
55
|
-
* @property {TComponentPropsMapping} [events] - Mapping event properties for the component.
|
|
56
|
-
* @property {TValueChangeEvent} [valueChangeEvent] - Optional function to handle value changes.
|
|
29
|
+
* @property {'equal' | 'notEqual' | 'less' | 'lessOrEqual' | 'greater' | 'greaterOrEqual'} rule - The rule to apply.
|
|
30
|
+
* @property {number} target - The target value to compare against.
|
|
57
31
|
*
|
|
58
32
|
* @example
|
|
59
33
|
* ```typescript
|
|
60
|
-
* const
|
|
61
|
-
* {
|
|
62
|
-
* component: InputElement,
|
|
63
|
-
* componentName: 'input',
|
|
64
|
-
* events: {
|
|
65
|
-
* getValue: 'onChange2',
|
|
66
|
-
* onBlur: 'onBlur2',
|
|
67
|
-
* onFocus: 'onFocus2',
|
|
68
|
-
* }
|
|
69
|
-
* },
|
|
70
|
-
* {
|
|
71
|
-
* component: Container,
|
|
72
|
-
* componentName: 'row',
|
|
73
|
-
* },
|
|
74
|
-
* {
|
|
75
|
-
* component: Dropdown,
|
|
76
|
-
* componentName: 'dropdown',
|
|
77
|
-
* valueChangeEvent: (event: {
|
|
78
|
-
* id: string;
|
|
79
|
-
* label: string;
|
|
80
|
-
* value: string;
|
|
81
|
-
* }) => ({ _value: event.value, _stateValue: event.id }),
|
|
82
|
-
* },
|
|
83
|
-
* {
|
|
84
|
-
* component: DatePicker,
|
|
85
|
-
* componentName: 'datepicker',
|
|
86
|
-
* valueChangeEvent: (event: string) => event,
|
|
87
|
-
* },
|
|
88
|
-
* ];
|
|
34
|
+
* const lengthValidation: TLengthValidation = { rule: 'greaterOrEqual', target: 8 };
|
|
89
35
|
* ```
|
|
90
|
-
* @interface
|
|
91
36
|
*/
|
|
92
|
-
type
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
} & OneOf<{
|
|
97
|
-
component: T;
|
|
98
|
-
asynccomponent: T;
|
|
99
|
-
}>;
|
|
100
|
-
|
|
37
|
+
type TLengthValidation = {
|
|
38
|
+
rule: 'equal' | 'notEqual' | 'less' | 'lessOrEqual' | 'greater' | 'greaterOrEqual';
|
|
39
|
+
target: number;
|
|
40
|
+
};
|
|
101
41
|
/**
|
|
102
|
-
* @
|
|
103
|
-
*
|
|
42
|
+
* @type TCallbackValidation
|
|
43
|
+
* A custom validation function.
|
|
104
44
|
*
|
|
105
|
-
* @
|
|
106
|
-
* @
|
|
107
|
-
* @property {string} name - The name of the component.
|
|
108
|
-
* @property {string} nameToSubmit - The name of the field when submit values (optional).
|
|
109
|
-
* @property {TValidations} [validations] - The validation methods for the component.
|
|
110
|
-
* @property {TVisibility[]} [visibilityConditions] - The visibility conditions for the component.
|
|
111
|
-
* @property {TResetValueMethods[]} [resetValues] - The reset value methods for the component.
|
|
112
|
-
* @property {TApiEvent} [api] - The API configuration for the component.
|
|
113
|
-
* @property {TFormatters} [formatters] - The formatters for the component.
|
|
114
|
-
* @property {TMasks} [masks] - The masks for the component.
|
|
115
|
-
* @property {IComponentSchema[]} [children] - The child components.
|
|
116
|
-
* @property {boolean} visibility - visibility status the component will mount (to avoid SSR blinking)
|
|
117
|
-
* @property {boolean} persistValue - check this if you want the last visible value to be restored after a visiblity schema rule applied
|
|
45
|
+
* @param {unknown} value - The value to validate.
|
|
46
|
+
* @returns {boolean} - The result of the validation.
|
|
118
47
|
*
|
|
119
48
|
* @example
|
|
120
49
|
* ```typescript
|
|
121
|
-
* const
|
|
122
|
-
* component: 'input',
|
|
123
|
-
* props: { type: 'text', placeholder: 'Enter your name' },
|
|
124
|
-
* name: 'name',
|
|
125
|
-
* nameToSubmit: 'applicant.firstName',
|
|
126
|
-
* validations: {
|
|
127
|
-
* methods: {
|
|
128
|
-
* required: true,
|
|
129
|
-
* regex: '^([0-9]+)*$',
|
|
130
|
-
* max: 5,
|
|
131
|
-
* },
|
|
132
|
-
* eventMessages: {
|
|
133
|
-
* ON_FIELD_MOUNT: ['required'],
|
|
134
|
-
* ON_FIELD_CHANGE: ['regex', 'required'],
|
|
135
|
-
* ON_FIELD_BLUR: ['max', 'required'],
|
|
136
|
-
* },
|
|
137
|
-
* messages: {
|
|
138
|
-
* default: 'This field is required',
|
|
139
|
-
* regex: 'Only numbers are available.',
|
|
140
|
-
* max: 'Max of 5',
|
|
141
|
-
* },
|
|
142
|
-
* },
|
|
143
|
-
* visibilityConditions: [{ conditions: { field: 'age', value: 18 } }],
|
|
144
|
-
* resetValues: [{ field: 'age', resetTo: '' }],
|
|
145
|
-
* api: {
|
|
146
|
-
* defaultConfig: {
|
|
147
|
-
* config: { method: 'POST', url: 'https://api.example.com/submit' },
|
|
148
|
-
* events: [{ eventName: 'ON_FIELD_BLUR' }]
|
|
149
|
-
* }
|
|
150
|
-
* },
|
|
151
|
-
* formatters: { capitalize: true },
|
|
152
|
-
* masks: { currency: { align: 'left', decimal: '.', precision: 2, prefix: '$', thousands: ',' } },
|
|
153
|
-
* children: [],
|
|
154
|
-
* visibility: true,
|
|
155
|
-
* persistValue: true,
|
|
156
|
-
* };
|
|
50
|
+
* const callbackValidation: TCallbackValidation = (value) => typeof value === 'string';
|
|
157
51
|
* ```
|
|
158
52
|
*/
|
|
159
|
-
|
|
160
|
-
/** The type of component (e.g., 'input', 'button'). */
|
|
161
|
-
component: string;
|
|
162
|
-
/** The properties of the component. */
|
|
163
|
-
props?: TProps;
|
|
164
|
-
/** The name of the component. */
|
|
165
|
-
name: string;
|
|
166
|
-
/** The name of the field when submit values. */
|
|
167
|
-
nameToSubmit?: string;
|
|
168
|
-
/** The validation methods for the component. */
|
|
169
|
-
validations?: TValidations;
|
|
170
|
-
/** The API configuration for the component. */
|
|
171
|
-
api?: TApiEvent;
|
|
172
|
-
/** The visibility conditions for the component. */
|
|
173
|
-
visibilityConditions?: TVisibility[];
|
|
174
|
-
/** The reset value methods for the component. */
|
|
175
|
-
resetValues?: TResetValueMethods[];
|
|
176
|
-
/** The reset property values for the component. */
|
|
177
|
-
resetPropertyValues?: TResetPathMethods[];
|
|
178
|
-
/** The formatters for the component. */
|
|
179
|
-
formatters?: TFormatters;
|
|
180
|
-
/** The masks for the component. */
|
|
181
|
-
masks?: TMasks;
|
|
182
|
-
/** The child components. */
|
|
183
|
-
children?: IComponentSchema[];
|
|
184
|
-
/** Visibility status the component will mount (to avoid SSR blinking) */
|
|
185
|
-
visibility?: boolean;
|
|
186
|
-
/** Check this if you want the last visible value to be restored after a visiblity schema rule applied */
|
|
187
|
-
persistValue?: boolean;
|
|
188
|
-
}
|
|
189
|
-
interface IComponentSchemaAsFormField<T> extends IComponentSchema {
|
|
190
|
-
mapper?: TMapper<T>;
|
|
191
|
-
children?: IComponentSchemaAsFormField<T>[];
|
|
192
|
-
}
|
|
53
|
+
type TCallbackValidation = (value: unknown, formValues: Pick<TFormValues<unknown>, 'values' | 'metadata'>) => boolean;
|
|
193
54
|
/**
|
|
194
|
-
* @
|
|
195
|
-
* Represents
|
|
55
|
+
* @type TBetweenValidation
|
|
56
|
+
* Represents validation rules that check if a value is between a range.
|
|
196
57
|
*
|
|
197
|
-
* @property {
|
|
198
|
-
* @property {
|
|
199
|
-
* @property {string} [method] - The HTTP method used to submit the form (e.g., 'POST', 'GET') (experimental).
|
|
200
|
-
* @property {Record<string, unknown>} [initialValues] - The initial values for the form fields.
|
|
201
|
-
* @property {Record<string, unknown>} [iVars] - Dynamic key value pairs that change from any external source
|
|
202
|
-
* @property {IComponentSchema[]} [components] - The list of components included in the form.
|
|
203
|
-
* @property {boolean} [stopEventsOnSubmit] - stop all the events declared as callback on useForm/Form once form is submitted
|
|
204
|
-
* @property {TSchemaFormConfig} [config] - form configurations to change event debouncers and debugging tools
|
|
58
|
+
* @property {number} start - The start of the range.
|
|
59
|
+
* @property {number} end - The end of the range.
|
|
205
60
|
*
|
|
206
61
|
* @example
|
|
207
62
|
* ```typescript
|
|
208
|
-
* const
|
|
209
|
-
* index: 'userForm',
|
|
210
|
-
* initialValues: { name: '', email: '' },
|
|
211
|
-
* iVars: iVarsState,
|
|
212
|
-
* config: {
|
|
213
|
-
* defaultLogVerbose: true
|
|
214
|
-
* },
|
|
215
|
-
* components: [
|
|
216
|
-
* { component: 'input', name: 'name', props: { placeholder: 'Enter your name' } },
|
|
217
|
-
* { component: 'input', name: 'email', props: { placeholder: 'Enter your email' } }
|
|
218
|
-
* ]
|
|
219
|
-
* };
|
|
63
|
+
* const betweenValidation: TBetweenValidation = { start: 1, end: 10 };
|
|
220
64
|
* ```
|
|
221
65
|
*/
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
config?: TSchemaFormConfig;
|
|
227
|
-
initialValues?: Record<string, unknown>;
|
|
228
|
-
iVars?: Record<string, unknown>;
|
|
229
|
-
components?: IComponentSchema[];
|
|
230
|
-
stopEventsOnSubmit?: boolean;
|
|
231
|
-
/** Pre-fetched API response data for SSR. Keys are field names, values contain the API responses to hydrate. */
|
|
232
|
-
prefetchedData?: Record<string, TPrefetchedFieldData>;
|
|
233
|
-
}
|
|
66
|
+
type TBetweenValidation = {
|
|
67
|
+
start: number;
|
|
68
|
+
end: number;
|
|
69
|
+
};
|
|
234
70
|
/**
|
|
235
|
-
*
|
|
236
|
-
*
|
|
71
|
+
* @type TCreditCardMatch
|
|
72
|
+
* Represents the options for credit card matching.
|
|
237
73
|
*
|
|
238
|
-
* @property {
|
|
239
|
-
* @property {
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* ```typescript
|
|
243
|
-
* const prefetchedData = {
|
|
244
|
-
* countryField: {
|
|
245
|
-
* defaultResponse: [{ id: 'BR', label: 'Brazil' }, { id: 'US', label: 'United States' }],
|
|
246
|
-
* },
|
|
247
|
-
* stateField: {
|
|
248
|
-
* namedResponses: { statesByCountry: [{ id: 'SP', label: 'São Paulo' }] },
|
|
249
|
-
* },
|
|
250
|
-
* };
|
|
251
|
-
* ```
|
|
252
|
-
*/
|
|
253
|
-
interface TPrefetchedFieldData {
|
|
254
|
-
/** The pre-fetched response for the field's default API config. */
|
|
255
|
-
defaultResponse?: unknown;
|
|
256
|
-
/** Pre-fetched responses keyed by named API config name. */
|
|
257
|
-
namedResponses?: Record<string, unknown>;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* @type TFormValues<T>
|
|
262
|
-
* Represents the values and state of a form. It has a generic type that allows the importer to determine which type values key will return.
|
|
263
|
-
*
|
|
264
|
-
* @property {Generic Type} values - The current values of the form fields.
|
|
265
|
-
* @property {string[]} erroredFields - A list of field names that have errors.
|
|
266
|
-
* @property {boolean} isValid - Indicates whether the form is valid.
|
|
267
|
-
*
|
|
268
|
-
* @example
|
|
269
|
-
* ```typescript
|
|
270
|
-
* const formValues: TFormValues = {
|
|
271
|
-
* values: { name: 'John', age: 30 },
|
|
272
|
-
* erroredFields: ['email'],
|
|
273
|
-
* isValid: false
|
|
274
|
-
* };
|
|
275
|
-
* ```
|
|
276
|
-
*/
|
|
277
|
-
type TFormValues<T> = {
|
|
278
|
-
values: T;
|
|
279
|
-
metadata: unknown;
|
|
280
|
-
erroredFields: string[];
|
|
281
|
-
isValid: boolean;
|
|
282
|
-
};
|
|
283
|
-
/**
|
|
284
|
-
* @type TFormEntry
|
|
285
|
-
* Represents the entry configuration for a form.
|
|
286
|
-
*
|
|
287
|
-
* @property {IFormSchema} [schema] - The schema defining the structure and behavior of the form.
|
|
288
|
-
* @property {Record<string, unknown>} [initialValues] - The initial values for the form fields.
|
|
289
|
-
* @property {(data: TFormValues) => void} [onSubmit] - Callback function to handle form submission.
|
|
290
|
-
* @property {Subject<TFormDataPayload>} [dataSubject$] - data subject passed from formGroup instance
|
|
291
|
-
* @property {Subject<TFormValidationPayload>} [formValidSubject$] - formValid subject passed from formGroup instance
|
|
292
|
-
* @property {Subject<TFormSubmitPayload<unknown>>} [submitSubject$] - submit subject passed from formGroup instance
|
|
293
|
-
*
|
|
294
|
-
* @example
|
|
295
|
-
* ```typescript
|
|
296
|
-
* const formEntry: TFormEntry = {
|
|
297
|
-
* schema: [{ component: 'input', props: {}, name: 'name' }],
|
|
298
|
-
* initialValues: { name: 'John' },
|
|
299
|
-
* onSubmit: (data) => { console.log(data); }
|
|
300
|
-
* };
|
|
301
|
-
* ```
|
|
302
|
-
*/
|
|
303
|
-
type TFormEntry = Omit<IFormSchema, 'components'> & {
|
|
304
|
-
schema?: IFormSchema;
|
|
305
|
-
mappers?: TMapper<unknown>[];
|
|
306
|
-
dataSubject$?: Subject<TFormDataPayload>;
|
|
307
|
-
formValidSubject$?: Subject<TFormValidationPayload>;
|
|
308
|
-
submitSubject$?: Subject<TFormSubmitPayload<unknown>>;
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
declare const TEMPLATE_AVALIABLE_SCOPES: readonly ["fields", "iVars", "form"];
|
|
312
|
-
declare const ALLOWED_RESET_PROPS_MUTATIONS: (keyof Pick<IFormField, "api" | "apiSchema" | "props" | "validations" | "visibilityConditions" | "resetValues">)[];
|
|
313
|
-
|
|
314
|
-
/** @virtual */
|
|
315
|
-
|
|
316
|
-
type TAllowedResetPropsMutations = (typeof ALLOWED_RESET_PROPS_MUTATIONS)[number];
|
|
317
|
-
/**
|
|
318
|
-
* @type TAllowedResetPropsMutationsEnum
|
|
319
|
-
* Represents the allowed properties to be changed on resetPropertyValues.
|
|
320
|
-
*
|
|
321
|
-
* @property {never} api - api property
|
|
322
|
-
* @property {never} apiSchema - apiSchema property
|
|
323
|
-
* @property {never} props - props property
|
|
324
|
-
* @property {never} validations - validations property
|
|
325
|
-
* @property {never} visibilityConditions - visibilityConditions property
|
|
326
|
-
* @property {never} resetValues - resetValues property
|
|
327
|
-
*
|
|
328
|
-
* @interface
|
|
329
|
-
*/
|
|
330
|
-
type TAllowedResetPropsMutationsEnum = Record<TAllowedResetPropsMutations, never>;
|
|
331
|
-
/**
|
|
332
|
-
* @type TLengthValidation
|
|
333
|
-
* Represents the validation rules based on the length of the input.
|
|
334
|
-
*
|
|
335
|
-
* @property {'equal' | 'notEqual' | 'less' | 'lessOrEqual' | 'greater' | 'greaterOrEqual'} rule - The rule to apply.
|
|
336
|
-
* @property {number} target - The target value to compare against.
|
|
337
|
-
*
|
|
338
|
-
* @example
|
|
339
|
-
* ```typescript
|
|
340
|
-
* const lengthValidation: TLengthValidation = { rule: 'greaterOrEqual', target: 8 };
|
|
341
|
-
* ```
|
|
342
|
-
*/
|
|
343
|
-
type TLengthValidation = {
|
|
344
|
-
rule: 'equal' | 'notEqual' | 'less' | 'lessOrEqual' | 'greater' | 'greaterOrEqual';
|
|
345
|
-
target: number;
|
|
346
|
-
};
|
|
347
|
-
/**
|
|
348
|
-
* @type TCallbackValidation
|
|
349
|
-
* A custom validation function.
|
|
350
|
-
*
|
|
351
|
-
* @param {unknown} value - The value to validate.
|
|
352
|
-
* @returns {boolean} - The result of the validation.
|
|
353
|
-
*
|
|
354
|
-
* @example
|
|
355
|
-
* ```typescript
|
|
356
|
-
* const callbackValidation: TCallbackValidation = (value) => typeof value === 'string';
|
|
357
|
-
* ```
|
|
358
|
-
*/
|
|
359
|
-
type TCallbackValidation = (value: unknown, formValues: Pick<TFormValues<unknown>, 'values' | 'metadata'>) => boolean;
|
|
360
|
-
/**
|
|
361
|
-
* @type TBetweenValidation
|
|
362
|
-
* Represents validation rules that check if a value is between a range.
|
|
363
|
-
*
|
|
364
|
-
* @property {number} start - The start of the range.
|
|
365
|
-
* @property {number} end - The end of the range.
|
|
366
|
-
*
|
|
367
|
-
* @example
|
|
368
|
-
* ```typescript
|
|
369
|
-
* const betweenValidation: TBetweenValidation = { start: 1, end: 10 };
|
|
370
|
-
* ```
|
|
371
|
-
*/
|
|
372
|
-
type TBetweenValidation = {
|
|
373
|
-
start: number;
|
|
374
|
-
end: number;
|
|
375
|
-
};
|
|
376
|
-
/**
|
|
377
|
-
* @type TCreditCardMatch
|
|
378
|
-
* Represents the options for credit card matching.
|
|
379
|
-
*
|
|
380
|
-
* @property {string} numberCard - The credit card number.
|
|
381
|
-
* @property {string[]} availableOptions - The available options for matching.
|
|
74
|
+
* @property {string} numberCard - The credit card number.
|
|
75
|
+
* @property {string[]} availableOptions - The available options for matching.
|
|
382
76
|
*
|
|
383
77
|
* @example
|
|
384
78
|
* ```typescript
|
|
@@ -2369,6 +2063,312 @@ type TFormGroupOnValidEventPayload = {
|
|
|
2369
2063
|
*/
|
|
2370
2064
|
type TFormGroupOnSubmitEventPayload<T> = Record<string, TFormValues<T> | undefined>;
|
|
2371
2065
|
|
|
2066
|
+
type TComponentPropsMappingSubset = keyof (Pick<TEventPropsEnum, 'onBlur' | 'onChange' | 'onFocus' | 'onKeyDown' | 'onKeyUp' | 'onClick'> & Pick<TEventDataPropsEnum, 'onSubmit'>);
|
|
2067
|
+
/**
|
|
2068
|
+
* @type TComponentPropsMapping
|
|
2069
|
+
* Represents the mapping of component properties for various events and actions.
|
|
2070
|
+
* @property {string} [getValue] - component function name to get the value.
|
|
2071
|
+
* @property {string} [setValue] - component function name to set the value.
|
|
2072
|
+
* @property {string} [setErrorMessage] - component function name to set the error message.
|
|
2073
|
+
* @property {string} [setErrorState] - component function name to set the error state.
|
|
2074
|
+
*
|
|
2075
|
+
* @example
|
|
2076
|
+
* ```typescript
|
|
2077
|
+
* const componentProps: TComponentPropsMapping = {
|
|
2078
|
+
* getValue: 'getValueFunction',
|
|
2079
|
+
* setValue: 'setValueFunction',
|
|
2080
|
+
* onBlur: 'handleBlur',
|
|
2081
|
+
* onClick: 'handleClick',
|
|
2082
|
+
* onFocus: 'handleFocus',
|
|
2083
|
+
* onKeyUp: 'handleKeyUp',
|
|
2084
|
+
* onKeyDown: 'handleKeyDown',
|
|
2085
|
+
* setErrorMessage: 'setErrorMessageFunction',
|
|
2086
|
+
* setErrorState: 'setErrorStateFunction'
|
|
2087
|
+
* };
|
|
2088
|
+
* ```
|
|
2089
|
+
* @interface
|
|
2090
|
+
*/
|
|
2091
|
+
type TComponentPropsMapping = Partial<Record<TComponentPropsMappingSubset, string>> & {
|
|
2092
|
+
getValue?: string;
|
|
2093
|
+
setValue?: string;
|
|
2094
|
+
setErrorMessage?: string;
|
|
2095
|
+
setErrorState?: string;
|
|
2096
|
+
};
|
|
2097
|
+
/**
|
|
2098
|
+
* @type TMapper
|
|
2099
|
+
* Represents the mapping of a component, including the component type,
|
|
2100
|
+
* name, events, and an optional function to handle value changes.
|
|
2101
|
+
*
|
|
2102
|
+
* @property {ElementType} component - The component that will represent the input.
|
|
2103
|
+
* * @property {ElementType} asynccomponent - The component that will represent the input but dynamically imported (suspense/lazy).
|
|
2104
|
+
* @property {string} componentName - The name of the component.
|
|
2105
|
+
* @property {TComponentPropsMapping} [events] - Mapping event properties for the component.
|
|
2106
|
+
* @property {TValueChangeEvent} [valueChangeEvent] - Optional function to handle value changes.
|
|
2107
|
+
*
|
|
2108
|
+
* @example
|
|
2109
|
+
* ```typescript
|
|
2110
|
+
* const mappers: TMapper<ElementType>[] = [
|
|
2111
|
+
* {
|
|
2112
|
+
* component: InputElement,
|
|
2113
|
+
* componentName: 'input',
|
|
2114
|
+
* events: {
|
|
2115
|
+
* getValue: 'onChange2',
|
|
2116
|
+
* onBlur: 'onBlur2',
|
|
2117
|
+
* onFocus: 'onFocus2',
|
|
2118
|
+
* }
|
|
2119
|
+
* },
|
|
2120
|
+
* {
|
|
2121
|
+
* component: Container,
|
|
2122
|
+
* componentName: 'row',
|
|
2123
|
+
* },
|
|
2124
|
+
* {
|
|
2125
|
+
* component: Dropdown,
|
|
2126
|
+
* componentName: 'dropdown',
|
|
2127
|
+
* valueChangeEvent: (event: {
|
|
2128
|
+
* id: string;
|
|
2129
|
+
* label: string;
|
|
2130
|
+
* value: string;
|
|
2131
|
+
* }) => ({ _value: event.value, _stateValue: event.id }),
|
|
2132
|
+
* },
|
|
2133
|
+
* {
|
|
2134
|
+
* component: DatePicker,
|
|
2135
|
+
* componentName: 'datepicker',
|
|
2136
|
+
* valueChangeEvent: (event: string) => event,
|
|
2137
|
+
* },
|
|
2138
|
+
* ];
|
|
2139
|
+
* ```
|
|
2140
|
+
* @interface
|
|
2141
|
+
*/
|
|
2142
|
+
type TMapper<T> = {
|
|
2143
|
+
componentName: string;
|
|
2144
|
+
events?: TComponentPropsMapping;
|
|
2145
|
+
valueChangeEvent?: TValueChangeEvent;
|
|
2146
|
+
} & OneOf<{
|
|
2147
|
+
component: T;
|
|
2148
|
+
asynccomponent: T;
|
|
2149
|
+
}>;
|
|
2150
|
+
|
|
2151
|
+
/**
|
|
2152
|
+
* @interface IComponentSchema
|
|
2153
|
+
* Represents the schema for a component within a form.
|
|
2154
|
+
*
|
|
2155
|
+
* @property {string} component - The type of component (e.g., 'input', 'button').
|
|
2156
|
+
* @property {TProps} props - The properties of the component.
|
|
2157
|
+
* @property {string} name - The name of the component.
|
|
2158
|
+
* @property {string} nameToSubmit - The name of the field when submit values (optional).
|
|
2159
|
+
* @property {TValidations} [validations] - The validation methods for the component.
|
|
2160
|
+
* @property {TVisibility[]} [visibilityConditions] - The visibility conditions for the component.
|
|
2161
|
+
* @property {TResetValueMethods[]} [resetValues] - The reset value methods for the component.
|
|
2162
|
+
* @property {TApiEvent} [api] - The API configuration for the component.
|
|
2163
|
+
* @property {TFormatters} [formatters] - The formatters for the component.
|
|
2164
|
+
* @property {TMasks} [masks] - The masks for the component.
|
|
2165
|
+
* @property {IComponentSchema[]} [children] - The child components.
|
|
2166
|
+
* @property {boolean} visibility - visibility status the component will mount (to avoid SSR blinking)
|
|
2167
|
+
* @property {boolean} persistValue - check this if you want the last visible value to be restored after a visiblity schema rule applied
|
|
2168
|
+
*
|
|
2169
|
+
* @example
|
|
2170
|
+
* ```typescript
|
|
2171
|
+
* const schema: IComponentSchema = {
|
|
2172
|
+
* component: 'input',
|
|
2173
|
+
* props: { type: 'text', placeholder: 'Enter your name' },
|
|
2174
|
+
* name: 'name',
|
|
2175
|
+
* nameToSubmit: 'applicant.firstName',
|
|
2176
|
+
* validations: {
|
|
2177
|
+
* methods: {
|
|
2178
|
+
* required: true,
|
|
2179
|
+
* regex: '^([0-9]+)*$',
|
|
2180
|
+
* max: 5,
|
|
2181
|
+
* },
|
|
2182
|
+
* eventMessages: {
|
|
2183
|
+
* ON_FIELD_MOUNT: ['required'],
|
|
2184
|
+
* ON_FIELD_CHANGE: ['regex', 'required'],
|
|
2185
|
+
* ON_FIELD_BLUR: ['max', 'required'],
|
|
2186
|
+
* },
|
|
2187
|
+
* messages: {
|
|
2188
|
+
* default: 'This field is required',
|
|
2189
|
+
* regex: 'Only numbers are available.',
|
|
2190
|
+
* max: 'Max of 5',
|
|
2191
|
+
* },
|
|
2192
|
+
* },
|
|
2193
|
+
* visibilityConditions: [{ conditions: { field: 'age', value: 18 } }],
|
|
2194
|
+
* resetValues: [{ field: 'age', resetTo: '' }],
|
|
2195
|
+
* api: {
|
|
2196
|
+
* defaultConfig: {
|
|
2197
|
+
* config: { method: 'POST', url: 'https://api.example.com/submit' },
|
|
2198
|
+
* events: [{ eventName: 'ON_FIELD_BLUR' }]
|
|
2199
|
+
* }
|
|
2200
|
+
* },
|
|
2201
|
+
* formatters: { capitalize: true },
|
|
2202
|
+
* masks: { currency: { align: 'left', decimal: '.', precision: 2, prefix: '$', thousands: ',' } },
|
|
2203
|
+
* children: [],
|
|
2204
|
+
* visibility: true,
|
|
2205
|
+
* persistValue: true,
|
|
2206
|
+
* };
|
|
2207
|
+
* ```
|
|
2208
|
+
*/
|
|
2209
|
+
interface IComponentSchema {
|
|
2210
|
+
/** The type of component (e.g., 'input', 'button'). */
|
|
2211
|
+
component: string;
|
|
2212
|
+
/** The properties of the component. */
|
|
2213
|
+
props?: TProps;
|
|
2214
|
+
/** The name of the component. */
|
|
2215
|
+
name: string;
|
|
2216
|
+
/** The name of the field when submit values. */
|
|
2217
|
+
nameToSubmit?: string;
|
|
2218
|
+
/** The validation methods for the component. */
|
|
2219
|
+
validations?: TValidations;
|
|
2220
|
+
/** The API configuration for the component. */
|
|
2221
|
+
api?: TApiEvent;
|
|
2222
|
+
/** The visibility conditions for the component. */
|
|
2223
|
+
visibilityConditions?: TVisibility[];
|
|
2224
|
+
/** The reset value methods for the component. */
|
|
2225
|
+
resetValues?: TResetValueMethods[];
|
|
2226
|
+
/** The reset property values for the component. */
|
|
2227
|
+
resetPropertyValues?: TResetPathMethods[];
|
|
2228
|
+
/** The formatters for the component. */
|
|
2229
|
+
formatters?: TFormatters;
|
|
2230
|
+
/** The masks for the component. */
|
|
2231
|
+
masks?: TMasks;
|
|
2232
|
+
/** The child components. */
|
|
2233
|
+
children?: IComponentSchema[];
|
|
2234
|
+
/** Visibility status the component will mount (to avoid SSR blinking) */
|
|
2235
|
+
visibility?: boolean;
|
|
2236
|
+
/** Check this if you want the last visible value to be restored after a visiblity schema rule applied */
|
|
2237
|
+
persistValue?: boolean;
|
|
2238
|
+
}
|
|
2239
|
+
interface IComponentSchemaAsFormField<T> extends IComponentSchema {
|
|
2240
|
+
mapper?: TMapper<T>;
|
|
2241
|
+
children?: IComponentSchemaAsFormField<T>[];
|
|
2242
|
+
}
|
|
2243
|
+
/**
|
|
2244
|
+
* @interface IFormSchema
|
|
2245
|
+
* Represents the schema for a form.
|
|
2246
|
+
*
|
|
2247
|
+
* @property {string} index - The unique index or identifier for the form.
|
|
2248
|
+
* @property {string} [action] - The URL to which the form data will be submitted. (experimental)
|
|
2249
|
+
* @property {string} [method] - The HTTP method used to submit the form (e.g., 'POST', 'GET') (experimental).
|
|
2250
|
+
* @property {Record<string, unknown>} [initialValues] - The initial values for the form fields.
|
|
2251
|
+
* @property {Record<string, unknown>} [iVars] - Dynamic key value pairs that change from any external source
|
|
2252
|
+
* @property {IComponentSchema[]} [components] - The list of components included in the form.
|
|
2253
|
+
* @property {boolean} [stopEventsOnSubmit] - stop all the events declared as callback on useForm/Form once form is submitted
|
|
2254
|
+
* @property {TSchemaFormConfig} [config] - form configurations to change event debouncers and debugging tools
|
|
2255
|
+
*
|
|
2256
|
+
* @example
|
|
2257
|
+
* ```typescript
|
|
2258
|
+
* const formSchema: IFormSchema = {
|
|
2259
|
+
* index: 'userForm',
|
|
2260
|
+
* initialValues: { name: '', email: '' },
|
|
2261
|
+
* iVars: iVarsState,
|
|
2262
|
+
* config: {
|
|
2263
|
+
* defaultLogVerbose: true
|
|
2264
|
+
* },
|
|
2265
|
+
* components: [
|
|
2266
|
+
* { component: 'input', name: 'name', props: { placeholder: 'Enter your name' } },
|
|
2267
|
+
* { component: 'input', name: 'email', props: { placeholder: 'Enter your email' } }
|
|
2268
|
+
* ]
|
|
2269
|
+
* };
|
|
2270
|
+
* ```
|
|
2271
|
+
*/
|
|
2272
|
+
interface IFormSchema {
|
|
2273
|
+
index: string;
|
|
2274
|
+
action?: string;
|
|
2275
|
+
method?: string;
|
|
2276
|
+
config?: TSchemaFormConfig;
|
|
2277
|
+
initialValues?: Record<string, unknown>;
|
|
2278
|
+
iVars?: Record<string, unknown>;
|
|
2279
|
+
components?: IComponentSchema[];
|
|
2280
|
+
stopEventsOnSubmit?: boolean;
|
|
2281
|
+
/** Pre-fetched API response data for SSR. Keys are field names, values contain the API responses to hydrate. */
|
|
2282
|
+
prefetchedData?: Record<string, TPrefetchedFieldData>;
|
|
2283
|
+
}
|
|
2284
|
+
/**
|
|
2285
|
+
* Represents pre-fetched API data for a single field.
|
|
2286
|
+
* Used to hydrate API response caches during SSR so templates referencing API data resolve correctly.
|
|
2287
|
+
*
|
|
2288
|
+
* @property {unknown} [defaultResponse] - The pre-fetched response for the field's default API config.
|
|
2289
|
+
* @property {Record<string, unknown>} [namedResponses] - Pre-fetched responses for named API configs.
|
|
2290
|
+
*
|
|
2291
|
+
* @example
|
|
2292
|
+
* ```typescript
|
|
2293
|
+
* const prefetchedData = {
|
|
2294
|
+
* countryField: {
|
|
2295
|
+
* defaultResponse: [{ id: 'BR', label: 'Brazil' }, { id: 'US', label: 'United States' }],
|
|
2296
|
+
* },
|
|
2297
|
+
* stateField: {
|
|
2298
|
+
* namedResponses: { statesByCountry: [{ id: 'SP', label: 'São Paulo' }] },
|
|
2299
|
+
* },
|
|
2300
|
+
* };
|
|
2301
|
+
* ```
|
|
2302
|
+
*/
|
|
2303
|
+
interface TPrefetchedFieldData {
|
|
2304
|
+
/** The pre-fetched response for the field's default API config. */
|
|
2305
|
+
defaultResponse?: unknown;
|
|
2306
|
+
/** Pre-fetched responses keyed by named API config name. */
|
|
2307
|
+
namedResponses?: Record<string, unknown>;
|
|
2308
|
+
}
|
|
2309
|
+
|
|
2310
|
+
/**
|
|
2311
|
+
* @type TFormValues<T>
|
|
2312
|
+
* Represents the values and state of a form. It has a generic type that allows the importer to determine which type values key will return.
|
|
2313
|
+
*
|
|
2314
|
+
* @property {Generic Type} values - The current values of the form fields.
|
|
2315
|
+
* @property {string[]} erroredFields - A list of field names that have errors.
|
|
2316
|
+
* @property {boolean} isValid - Indicates whether the form is valid.
|
|
2317
|
+
*
|
|
2318
|
+
* @example
|
|
2319
|
+
* ```typescript
|
|
2320
|
+
* const formValues: TFormValues = {
|
|
2321
|
+
* values: { name: 'John', age: 30 },
|
|
2322
|
+
* erroredFields: ['email'],
|
|
2323
|
+
* isValid: false
|
|
2324
|
+
* };
|
|
2325
|
+
* ```
|
|
2326
|
+
*/
|
|
2327
|
+
type TFormValues<T> = {
|
|
2328
|
+
values: T;
|
|
2329
|
+
metadata: unknown;
|
|
2330
|
+
erroredFields: string[];
|
|
2331
|
+
isValid: boolean;
|
|
2332
|
+
};
|
|
2333
|
+
/**
|
|
2334
|
+
* @type TFormEntry
|
|
2335
|
+
* Represents the entry configuration for a form.
|
|
2336
|
+
*
|
|
2337
|
+
* @property {IFormSchema} [schema] - The schema defining the structure and behavior of the form.
|
|
2338
|
+
* @property {Record<string, unknown>} [initialValues] - The initial values for the form fields.
|
|
2339
|
+
* @property {(data: TFormValues) => void} [onSubmit] - Callback function to handle form submission.
|
|
2340
|
+
* @property {Subject<TFormDataPayload>} [dataSubject$] - data subject passed from formGroup instance
|
|
2341
|
+
* @property {Subject<TFormValidationPayload>} [formValidSubject$] - formValid subject passed from formGroup instance
|
|
2342
|
+
* @property {Subject<TFormSubmitPayload<unknown>>} [submitSubject$] - submit subject passed from formGroup instance
|
|
2343
|
+
*
|
|
2344
|
+
* @example
|
|
2345
|
+
* ```typescript
|
|
2346
|
+
* const formEntry: TFormEntry = {
|
|
2347
|
+
* schema: [{ component: 'input', props: {}, name: 'name' }],
|
|
2348
|
+
* initialValues: { name: 'John' },
|
|
2349
|
+
* onSubmit: (data) => { console.log(data); }
|
|
2350
|
+
* };
|
|
2351
|
+
* ```
|
|
2352
|
+
*/
|
|
2353
|
+
type TFormEntry = Omit<IFormSchema, 'components'> & {
|
|
2354
|
+
schema?: IFormSchema;
|
|
2355
|
+
mappers?: TMapper<unknown>[];
|
|
2356
|
+
dataSubject$?: Subject<TFormDataPayload>;
|
|
2357
|
+
formValidSubject$?: Subject<TFormValidationPayload>;
|
|
2358
|
+
submitSubject$?: Subject<TFormSubmitPayload<unknown>>;
|
|
2359
|
+
};
|
|
2360
|
+
|
|
2361
|
+
type AllowOnly<T, K extends keyof T> = Pick<T, K> & {
|
|
2362
|
+
[P in keyof Omit<T, K>]?: never;
|
|
2363
|
+
};
|
|
2364
|
+
type OneOf<T, K = keyof T> = K extends keyof T ? AllowOnly<T, K> : never;
|
|
2365
|
+
type TValidationPayload = [
|
|
2366
|
+
unknown,
|
|
2367
|
+
TValidationMethods,
|
|
2368
|
+
TFormValues<unknown>?
|
|
2369
|
+
];
|
|
2370
|
+
type TValidationHandler = Record<string, (...args: TValidationPayload) => boolean>;
|
|
2371
|
+
|
|
2372
2372
|
/**
|
|
2373
2373
|
* Global mutable registries for validations, formatters, and masks.
|
|
2374
2374
|
* Plugins (sub-path exports) can extend these at import time.
|