@bolttech/form-engine-core 0.0.1-beta.14 → 0.0.1-beta.16
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/index.esm.js +34 -30
- package/package.json +1 -1
- package/src/interfaces/schema.d.ts +18 -4
- package/src/types/schema.d.ts +22 -6
package/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Subject, Subscription, combineLatest, startWith, groupBy, mergeMap, debounceTime, filter, map } from 'rxjs';
|
|
2
2
|
import creditCardType from 'credit-card-type';
|
|
3
|
-
import { isNumber as isNumber$1,
|
|
3
|
+
import { isNumber as isNumber$1, isEqual, get, isNil, set } from 'lodash';
|
|
4
4
|
import { getCurrencySymbol } from '@gaignoux/currency';
|
|
5
5
|
|
|
6
6
|
var TMutationEnum;
|
|
@@ -142,7 +142,7 @@ function traverseObject(obj, path) {
|
|
|
142
142
|
if (typeof item === 'object') {
|
|
143
143
|
result.push(...traverseObject(item, `${path ? `${path}.` : ``}${key}.${index}`));
|
|
144
144
|
} else if (typeof item === 'string') {
|
|
145
|
-
if (String(item).includes('$')) {
|
|
145
|
+
if (String(item).includes('${')) {
|
|
146
146
|
// const extractedPath = item.replace(/\$|{|}/g, '').split('.');
|
|
147
147
|
const extractedOriginPath = `${path ? `${path}.` : ``}${key}`.split('.');
|
|
148
148
|
result.push(Object.assign(Object.assign({
|
|
@@ -158,7 +158,7 @@ function traverseObject(obj, path) {
|
|
|
158
158
|
} else if (typeof value === 'object') {
|
|
159
159
|
result.push(...traverseObject(value, `${path ? `${path}.` : ``}${key}`));
|
|
160
160
|
} else if (typeof value === 'string') {
|
|
161
|
-
if (value.includes('$')) {
|
|
161
|
+
if (value.includes('${')) {
|
|
162
162
|
// const extractedPath = value.replace(/\$|{|}/g, '').split('.');
|
|
163
163
|
const destinationPath = `${path ? `${path}.` : ``}${key}`.split('.');
|
|
164
164
|
result.push(Object.assign(Object.assign({
|
|
@@ -2298,7 +2298,7 @@ class FormField {
|
|
|
2298
2298
|
dataSubject$,
|
|
2299
2299
|
mapper
|
|
2300
2300
|
}) {
|
|
2301
|
-
var _a, _b, _c, _d, _e, _f, _g
|
|
2301
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
2302
2302
|
this.fieldStateSubscription$ = new Subscription();
|
|
2303
2303
|
this.originalSchema = schemaComponent;
|
|
2304
2304
|
this.config = {
|
|
@@ -2326,10 +2326,6 @@ class FormField {
|
|
|
2326
2326
|
this.fieldEventSubject$ = fieldEventSubject$;
|
|
2327
2327
|
this.dataSubject$ = dataSubject$;
|
|
2328
2328
|
this._props = schemaComponent.props || {};
|
|
2329
|
-
this._value = '';
|
|
2330
|
-
this._stateValue = ((_c = this.mapper.events) === null || _c === void 0 ? void 0 : _c.setValue) ? {
|
|
2331
|
-
[this.mapper.events.setValue]: ''
|
|
2332
|
-
} : {};
|
|
2333
2329
|
this._metadata = '';
|
|
2334
2330
|
this.errorsString = '';
|
|
2335
2331
|
this.errorsList = [];
|
|
@@ -2337,9 +2333,9 @@ class FormField {
|
|
|
2337
2333
|
this._visibility = true;
|
|
2338
2334
|
this._api = {
|
|
2339
2335
|
default: {
|
|
2340
|
-
response: ((
|
|
2336
|
+
response: ((_e = (_d = (_c = this.apiSchema) === null || _c === void 0 ? void 0 : _c.defaultConfig) === null || _d === void 0 ? void 0 : _d.config) === null || _e === void 0 ? void 0 : _e.fallbackValue) || ''
|
|
2341
2337
|
},
|
|
2342
|
-
named: ((
|
|
2338
|
+
named: ((_f = this.apiSchema) === null || _f === void 0 ? void 0 : _f.configs) && Object.keys((_g = this.apiSchema) === null || _g === void 0 ? void 0 : _g.configs).reduce((acc, curr) => {
|
|
2343
2339
|
var _a, _b;
|
|
2344
2340
|
acc[curr] = {
|
|
2345
2341
|
response: ((_b = (_a = this.apiSchema) === null || _a === void 0 ? void 0 : _a.configs) === null || _b === void 0 ? void 0 : _b[curr].config.fallbackValue) || ''
|
|
@@ -2351,6 +2347,7 @@ class FormField {
|
|
|
2351
2347
|
this._valid = false;
|
|
2352
2348
|
this._mounted = true;
|
|
2353
2349
|
this.initializeObservers();
|
|
2350
|
+
this.value = this.initialValue || '';
|
|
2354
2351
|
}
|
|
2355
2352
|
/**
|
|
2356
2353
|
* method to initialize all recycled Subjects and initialize Observers on field instance creation or rerender
|
|
@@ -2390,12 +2387,6 @@ class FormField {
|
|
|
2390
2387
|
}) => event), mergeMap(group$ => group$.pipe(debounceTime(this.config.defaultAPIdebounceTimeMS))), filter(() => this.apiSubject$ && !this.apiSubject$.closed)).subscribe(payload => {
|
|
2391
2388
|
this.apiRequest(payload);
|
|
2392
2389
|
});
|
|
2393
|
-
if (!isNil(this.initialValue)) {
|
|
2394
|
-
this.value = this.initialValue;
|
|
2395
|
-
this.setFieldValidity({
|
|
2396
|
-
event: 'ON_FORM_SUBMIT'
|
|
2397
|
-
});
|
|
2398
|
-
}
|
|
2399
2390
|
}
|
|
2400
2391
|
/**
|
|
2401
2392
|
* Retrieves the properties associated with the form field.
|
|
@@ -2498,6 +2489,23 @@ class FormField {
|
|
|
2498
2489
|
set visibility(visible) {
|
|
2499
2490
|
if (typeof visible === 'undefined' || visible === this.visibility) return;
|
|
2500
2491
|
this._visibility = visible;
|
|
2492
|
+
/**
|
|
2493
|
+
* I was sure I would not require to gambiarra, but..
|
|
2494
|
+
* in order to ignore an hidden value on a form submit
|
|
2495
|
+
* or revalidate it when it comes back to visibility
|
|
2496
|
+
* I needed to...
|
|
2497
|
+
* I don't recommend setting private properties like this
|
|
2498
|
+
* this will force the field to be valid when it's hidden
|
|
2499
|
+
* and trigger the validation when it's visible
|
|
2500
|
+
*/
|
|
2501
|
+
if (!this.visibility) {
|
|
2502
|
+
this.value = '';
|
|
2503
|
+
this._valid = true;
|
|
2504
|
+
} else {
|
|
2505
|
+
this.setFieldValidity({
|
|
2506
|
+
event: 'ON_FIELD_MOUNT'
|
|
2507
|
+
});
|
|
2508
|
+
}
|
|
2501
2509
|
this.visibilitySubject$.next(this.visibility);
|
|
2502
2510
|
this.templateSubject$.next({
|
|
2503
2511
|
key: this.name,
|
|
@@ -2640,7 +2648,7 @@ class FormField {
|
|
|
2640
2648
|
setFieldValidity({
|
|
2641
2649
|
event
|
|
2642
2650
|
}) {
|
|
2643
|
-
var _a, _b;
|
|
2651
|
+
var _a, _b, _c, _d;
|
|
2644
2652
|
if (!this.validations || !this.visibility) {
|
|
2645
2653
|
this.errors = {};
|
|
2646
2654
|
this._valid = true;
|
|
@@ -2666,14 +2674,16 @@ class FormField {
|
|
|
2666
2674
|
}
|
|
2667
2675
|
});
|
|
2668
2676
|
this._valid = valid;
|
|
2669
|
-
if (this.validations.
|
|
2670
|
-
const
|
|
2671
|
-
(
|
|
2677
|
+
if ((_c = (_b = this.validations) === null || _b === void 0 ? void 0 : _b.eventMessages) === null || _c === void 0 ? void 0 : _c[event]) {
|
|
2678
|
+
const eventMessages = {};
|
|
2679
|
+
(_d = this.validations.eventMessages[event]) === null || _d === void 0 ? void 0 : _d.forEach(method => {
|
|
2672
2680
|
if (method in errors) {
|
|
2673
|
-
|
|
2681
|
+
eventMessages[method] = errors[method];
|
|
2674
2682
|
}
|
|
2675
2683
|
});
|
|
2676
|
-
this.errors =
|
|
2684
|
+
this.errors = eventMessages;
|
|
2685
|
+
} else if (event === 'ON_FORM_SUBMIT') {
|
|
2686
|
+
this.errors = errors;
|
|
2677
2687
|
}
|
|
2678
2688
|
}
|
|
2679
2689
|
/**
|
|
@@ -3209,16 +3219,10 @@ class FormCore {
|
|
|
3209
3219
|
const error = validations[validationKey](field.value, structElement.validations);
|
|
3210
3220
|
if (Array.isArray(structElement.fields)) {
|
|
3211
3221
|
structElement.fields.forEach(fieldKey => {
|
|
3212
|
-
if (!this.fields.has(fieldKey)) console.warn(`failed to update visibility onto field ${fieldKey}`);else
|
|
3213
|
-
this.fields.get(fieldKey).visibility = !error;
|
|
3214
|
-
if (error) this.fields.get(fieldKey).value = '';
|
|
3215
|
-
}
|
|
3222
|
+
if (!this.fields.has(fieldKey)) console.warn(`failed to update visibility onto field ${fieldKey}`);else this.fields.get(fieldKey).visibility = !error;
|
|
3216
3223
|
});
|
|
3217
3224
|
} else if (structElement.fields) {
|
|
3218
|
-
if (!this.fields.has(structElement.fields)) console.warn(`failed to update visibility onto field ${structElement.fields}`);else
|
|
3219
|
-
this.fields.get(structElement.fields).visibility = !error;
|
|
3220
|
-
if (error) this.fields.get(structElement.fields).value = '';
|
|
3221
|
-
}
|
|
3225
|
+
if (!this.fields.has(structElement.fields)) console.warn(`failed to update visibility onto field ${structElement.fields}`);else this.fields.get(structElement.fields).visibility = !error;
|
|
3222
3226
|
}
|
|
3223
3227
|
});
|
|
3224
3228
|
});
|
package/package.json
CHANGED
|
@@ -8,10 +8,9 @@ import { TApiEvent, TFormatters, TMasks, TProps, TResetValueMethods, TSchemaForm
|
|
|
8
8
|
* @property {TProps} props - The properties of the component.
|
|
9
9
|
* @property {string} name - The name of the component.
|
|
10
10
|
* @property {string} nameToSubmit - The name of the field when submit values (optional).
|
|
11
|
-
* @property {
|
|
11
|
+
* @property {TValidations} [validations] - The validation methods for the component.
|
|
12
12
|
* @property {TVisibility[]} [visibilityConditions] - The visibility conditions for the component.
|
|
13
13
|
* @property {TResetValueMethods[]} [resetValues] - The reset value methods for the component.
|
|
14
|
-
* @property {TErrorMessages} [errorMessages] - The error messages for the component.
|
|
15
14
|
* @property {TApiEvent} [api] - The API configuration for the component.
|
|
16
15
|
* @property {TFormatters} [formatters] - The formatters for the component.
|
|
17
16
|
* @property {TMasks} [masks] - The masks for the component.
|
|
@@ -24,10 +23,25 @@ import { TApiEvent, TFormatters, TMasks, TProps, TResetValueMethods, TSchemaForm
|
|
|
24
23
|
* props: { type: 'text', placeholder: 'Enter your name' },
|
|
25
24
|
* name: 'name',
|
|
26
25
|
* nameToSubmit: 'applicant.firstName',
|
|
27
|
-
* validations: {
|
|
26
|
+
* validations: {
|
|
27
|
+
* methods: {
|
|
28
|
+
* required: true,
|
|
29
|
+
* regex: '^([0-9]+)*$',
|
|
30
|
+
* max: 5,
|
|
31
|
+
* },
|
|
32
|
+
* eventMessages: {
|
|
33
|
+
* ON_FIELD_MOUNT: ['required'],
|
|
34
|
+
* ON_FIELD_CHANGE: ['regex', 'required'],
|
|
35
|
+
* ON_FIELD_BLUR: ['max', 'required'],
|
|
36
|
+
* },
|
|
37
|
+
* messages: {
|
|
38
|
+
* default: 'This field is required',
|
|
39
|
+
* regex: 'Only numbers are available.',
|
|
40
|
+
* max: 'Max of 5',
|
|
41
|
+
* },
|
|
42
|
+
* },
|
|
28
43
|
* visibilityConditions: [{ conditions: { field: 'age', value: 18 } }],
|
|
29
44
|
* resetValues: [{ field: 'age', resetTo: '' }],
|
|
30
|
-
* errorMessages: { required: 'This field is required.' },
|
|
31
45
|
* api: { defaultConfig: { config: { method: 'POST', url: 'https://api.example.com/submit' }, events: [{ eventName: 'ON_FORM_SUBMIT' }] } },
|
|
32
46
|
* formatters: { capitalize: true },
|
|
33
47
|
* masks: { currency: { align: 'left', decimal: '.', precision: 2, prefix: '$', thousands: ',' } },
|
package/src/types/schema.d.ts
CHANGED
|
@@ -657,19 +657,35 @@ type TApiConfig = {
|
|
|
657
657
|
type TProps = Record<string, unknown>;
|
|
658
658
|
/**
|
|
659
659
|
* @type TValidations
|
|
660
|
-
* Represents the validation
|
|
660
|
+
* Represents the validation configuration for form fields, including methods, event-specific messages, and error messages.
|
|
661
|
+
*
|
|
662
|
+
* @property {TValidationMethods} methods - The validation methods to be applied.
|
|
663
|
+
* @property {Partial<Record<TEvents, string[]>>} eventMessages - The messages to be displayed for specific validation events.
|
|
664
|
+
* @property {TErrorMessages} messages - The general error messages for validation methods.
|
|
661
665
|
*
|
|
662
666
|
* @example
|
|
663
|
-
* ```typescript
|
|
664
667
|
* const validations: TValidations = {
|
|
665
|
-
*
|
|
668
|
+
* methods: {
|
|
669
|
+
* max: 100,
|
|
670
|
+
* required: true,
|
|
671
|
+
* regex: '^[a-zA-Z0-9]+$',
|
|
672
|
+
* },
|
|
673
|
+
* eventMessages: {
|
|
674
|
+
* ON_FIELD_MOUNT: ['required'],
|
|
675
|
+
* ON_FIELD_CHANGE: ['regex', 'required'],
|
|
676
|
+
* ON_FIELD_BLUR: ['max', 'required'],
|
|
677
|
+
* },
|
|
678
|
+
* messages: {
|
|
679
|
+
* default: 'This field is required',
|
|
680
|
+
* max: 'Value exceeds the maximum limit',
|
|
681
|
+
* regex: 'Value does not match the pattern',
|
|
682
|
+
* },
|
|
666
683
|
* };
|
|
667
|
-
* ```
|
|
668
684
|
*/
|
|
669
685
|
type TValidations = {
|
|
670
686
|
methods: TValidationMethods;
|
|
671
|
-
|
|
672
|
-
messages
|
|
687
|
+
eventMessages?: Partial<Record<TEvents, string[]>>;
|
|
688
|
+
messages?: TErrorMessages;
|
|
673
689
|
};
|
|
674
690
|
/**
|
|
675
691
|
* @type TErrorMessages
|