@bolttech/form-engine-core 0.0.1-beta.8 → 0.0.1-beta.9

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 CHANGED
@@ -174,6 +174,53 @@ function traverseObject(obj, path) {
174
174
  }
175
175
  return result;
176
176
  }
177
+ /**
178
+ * Recursively creates a nested object structure based on the given parts array,
179
+ * setting the specified value at the appropriate nested level.
180
+ *
181
+ * @param {string[]} parts - Array of strings representing the keys for the nested object.
182
+ * @param {Record<string, unknown>} subObject - The sub-object to be updated or used for recursion.
183
+ * @param {unknown} value - The value to be set at the final nested level.
184
+ * @returns {Record<string, unknown>} - The updated object with the new nested structure and value.
185
+ */
186
+ const getNewPart = (parts, subObject, value) => {
187
+ const clonedParts = [...parts];
188
+ clonedParts.splice(0, 1);
189
+ const part = parts.length !== 1 ? getNewPart(clonedParts, subObject ? subObject[parts[0]] : {}, value) : value;
190
+ return Object.assign(Object.assign({}, subObject), {
191
+ [parts[0]]: part
192
+ });
193
+ };
194
+ /**
195
+ * Encapsulates in a given object, at a given path the provided value
196
+ *
197
+ * @example
198
+ * ORIGINAL object
199
+ *
200
+ * encapsulateIn({maintain: 'spread_me'}, 'a.b.c','test')
201
+ *
202
+ * RESULT
203
+ *
204
+ * {
205
+ * maintain: 'spread_me',
206
+ * c: 'test
207
+ * }
208
+ * }
209
+ *
210
+ * @param origin - The original object where the new value will be appended
211
+ * @param path - The path at which the new value will be placed
212
+ * @param value - The new value
213
+ * @returns One new object with the new value at the provided path merged with the given object
214
+ */
215
+ const encapsulateIn = (origin, path, value) => {
216
+ const parts = path.split('.');
217
+ if (parts.length === 1) {
218
+ return Object.assign(Object.assign({}, origin), {
219
+ [path]: value
220
+ });
221
+ }
222
+ return getNewPart(parts, origin, value);
223
+ };
177
224
 
178
225
  /**
179
226
  * Splits a string by inserting specified values at given positions.
@@ -681,16 +728,9 @@ const currency = (value, masks) => {
681
728
  if (integerPart === '') {
682
729
  integerPart = '0';
683
730
  }
684
- console.log('Before', {
685
- convertedValue
686
- });
687
731
  if (align === 'right' && String(value).endsWith(' ')) {
688
732
  convertedValue = convertedValue.slice(0, -1);
689
733
  }
690
- console.log('After', {
691
- value,
692
- convertedValue
693
- });
694
734
  let newRawValue = integerPart;
695
735
  let decimalPart = convertedValue.slice(convertedValue.length - precision);
696
736
  if (precision > 0) {
@@ -2297,6 +2337,7 @@ class FormField {
2297
2337
  defaultStateRefreshTimeMS: Number(config === null || config === void 0 ? void 0 : config.defaultStateRefreshTimeMS) ? Number(config === null || config === void 0 ? void 0 : config.defaultStateRefreshTimeMS) : DEFAULT_STATE_REFRESH_TIME
2298
2338
  };
2299
2339
  this.name = schemaComponent.name;
2340
+ this.nameToSubmit = schemaComponent.nameToSubmit;
2300
2341
  this.component = schemaComponent.component;
2301
2342
  this.path = path;
2302
2343
  this.children = children;
@@ -2640,14 +2681,19 @@ class FormField {
2640
2681
  const errors = Object.assign({}, this.errors);
2641
2682
  const schemaValidations = (_a = this.validations) === null || _a === void 0 ? void 0 : _a.config;
2642
2683
  schemaValidations && Object.keys(schemaValidations).forEach(validationKey => {
2643
- var _a, _b;
2684
+ var _a;
2644
2685
  const error = validations[validationKey](this.value, schemaValidations);
2645
2686
  // setting valid flag
2646
2687
  valid = !error && valid;
2647
2688
  // setting error messages
2648
2689
  if (((_a = this.validations) === null || _a === void 0 ? void 0 : _a.events.includes(event)) || event === 'ON_FORM_SUBMIT') {
2649
- if (error && ((_b = this.errorMessages) === null || _b === void 0 ? void 0 : _b[validationKey])) {
2650
- errors[validationKey] = this.errorMessages[validationKey];
2690
+ if (error && this.errorMessages) {
2691
+ if (validationKey in this.errorMessages) {
2692
+ const messages = this.errorMessages;
2693
+ errors[validationKey] = messages[validationKey];
2694
+ } else if ('default' in this.errorMessages) {
2695
+ errors[validationKey] = this.errorMessages.default;
2696
+ }
2651
2697
  } else {
2652
2698
  delete errors[validationKey];
2653
2699
  }
@@ -3389,11 +3435,11 @@ class FormCore {
3389
3435
  * @returns {TFormValues} The current form values.
3390
3436
  */
3391
3437
  getFormValues() {
3392
- const values = {};
3438
+ let values = {};
3393
3439
  const erroredFields = [];
3394
3440
  this.fields.forEach((val, key) => {
3395
3441
  if (val.value) {
3396
- values[key] = val.value;
3442
+ values = Object.assign(Object.assign({}, values), encapsulateIn(values, val.nameToSubmit || key, val.value));
3397
3443
  }
3398
3444
  if (!val.valid) {
3399
3445
  erroredFields.push(key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine-core",
3
- "version": "0.0.1-beta.8",
3
+ "version": "0.0.1-beta.9",
4
4
  "module": "./index.esm.js",
5
5
  "type": "module",
6
6
  "main": "./index.esm.js",
@@ -61,4 +61,26 @@ declare function extractFieldKeys(expression: string): {
61
61
  * ```
62
62
  */
63
63
  declare function traverseObject(obj: any, path?: string): TSubscribedTemplates[];
64
- export { makeRequest, traverseObject, extractFieldKeys };
64
+ /**
65
+ * Encapsulates in a given object, at a given path the provided value
66
+ *
67
+ * @example
68
+ * ORIGINAL object
69
+ *
70
+ * encapsulateIn({maintain: 'spread_me'}, 'a.b.c','test')
71
+ *
72
+ * RESULT
73
+ *
74
+ * {
75
+ * maintain: 'spread_me',
76
+ * c: 'test
77
+ * }
78
+ * }
79
+ *
80
+ * @param origin - The original object where the new value will be appended
81
+ * @param path - The path at which the new value will be placed
82
+ * @param value - The new value
83
+ * @returns One new object with the new value at the provided path merged with the given object
84
+ */
85
+ declare const encapsulateIn: (origin: Record<string, unknown>, path: string, value: unknown) => Record<string, unknown>;
86
+ export { makeRequest, traverseObject, extractFieldKeys, encapsulateIn };
@@ -6,6 +6,7 @@ import { TApiEvent, TErrorMessages, TEvent, TFormatters, TMasks, TProps, TResetV
6
6
  * @property {string} component - The type of component (e.g., 'input', 'button').
7
7
  * @property {TProps} props - The properties of the component.
8
8
  * @property {string} name - The name of the component.
9
+ * @property {string} nameToSubmit - The name of the field when submit values (optional).
9
10
  * @property {TEvent<TValidationMethods>} [validations] - The validation methods for the component.
10
11
  * @property {TVisibility[]} [visibilityConditions] - The visibility conditions for the component.
11
12
  * @property {TResetValueMethods[]} [resetValues] - The reset value methods for the component.
@@ -21,6 +22,7 @@ import { TApiEvent, TErrorMessages, TEvent, TFormatters, TMasks, TProps, TResetV
21
22
  * component: 'input',
22
23
  * props: { type: 'text', placeholder: 'Enter your name' },
23
24
  * name: 'name',
25
+ * nameToSubmit: 'applicant.firstName',
24
26
  * validations: { config: { required: true }, events: [{ eventName: 'ON_FIELD_BLUR' }] },
25
27
  * visibilityConditions: [{ conditions: { field: 'age', value: 18 } }],
26
28
  * resetValues: [{ field: 'age', resetTo: '' }],
@@ -36,6 +38,7 @@ interface IComponentSchema {
36
38
  component: string;
37
39
  props?: TProps;
38
40
  name: string;
41
+ nameToSubmit?: string;
39
42
  validations?: TEvent<TValidationMethods>;
40
43
  api?: TApiEvent;
41
44
  visibilityConditions?: TVisibility[];
@@ -9,6 +9,7 @@ import { TMapper } from '../types/mapper';
9
9
  */
10
10
  declare class FormField {
11
11
  name: string;
12
+ nameToSubmit?: string;
12
13
  component: string;
13
14
  path?: string;
14
15
  children?: string[];
@@ -56,7 +56,7 @@ declare class FormGroup {
56
56
  * @param {string} options.formIndex
57
57
  * @param {string} options.fieldIndex
58
58
  */
59
- removeField({ formIndex, fieldIndex }: {
59
+ removeField({ formIndex, fieldIndex, }: {
60
60
  formIndex: string;
61
61
  fieldIndex: string;
62
62
  }): void;
@@ -79,7 +79,7 @@ declare class FormGroup {
79
79
  * @param {string[]} indexes form indexes to be submitted
80
80
  * @returns
81
81
  */
82
- submitMultipleFormsByIndex(indexes: string[]): TFormValues;
82
+ submitMultipleFormsByIndex<T>(indexes: string[]): TFormValues<T>;
83
83
  }
84
84
  type TFormGroup = FormGroup;
85
85
  export { TFormGroup, FormGroup };
@@ -700,10 +700,11 @@ type TResetValues = Partial<Record<TEvents, TResetValueMethods[]>>;
700
700
  * const errorMessages: TErrorMessages = {
701
701
  * required: 'This field is required.',
702
702
  * max: 'The value cannot exceed the maximum limit.'
703
+ * default: 'Default error message'
703
704
  * };
704
705
  * ```
705
706
  */
706
- type TErrorMessages = Partial<Record<keyof TValidationMethods, string>>;
707
+ type TErrorMessages = Partial<Record<keyof TValidationMethods & 'default', string>>;
707
708
  /**
708
709
  * Represents an event configuration with a specific type.
709
710
  *