@bolttech/form-engine-core 0.0.1-beta.25 → 0.0.1-beta.27

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
@@ -1933,7 +1933,7 @@ const conditions = (value, validations) => {
1933
1933
  * Validates if a date string matches a specific date format.
1934
1934
  * The function accepts strings with '/' or '-' separators and removes them before validating the format.
1935
1935
  *
1936
- * @param {string} string - The date string to be validated. It can contain '/' or '-' separators.
1936
+ * @param {string} value - The date string to be validated. It can contain '/' or '-' separators.
1937
1937
  * @param {string} format - The expected date format. It can be one of the following:
1938
1938
  * 'DDMMYYYY', 'YYYYMMDD', 'YYYYDDMM', 'MMDDYYYY',
1939
1939
  * 'DMYYYY', 'YYYYMD', 'YYYYDM', 'MDYYYY'.
@@ -2096,18 +2096,17 @@ const date = (value, validations) => {
2096
2096
  }
2097
2097
  const originValue = validations.date.origin.value || value;
2098
2098
  const mappedValue = dateRearrangeMapper[(_e = validations.date) === null || _e === void 0 ? void 0 : _e.origin.format](originValue).toString();
2099
- if (invalidStringDate(mappedValue, (_f = validations.date) === null || _f === void 0 ? void 0 : _f.origin.format)) return false;
2100
2099
  let originDate = new Date(mappedValue);
2101
2100
  let targetDate = new Date();
2102
2101
  let target = new Date();
2103
- if ((_h = (_g = validations.date) === null || _g === void 0 ? void 0 : _g.target) === null || _h === void 0 ? void 0 : _h.format) {
2104
- target = new Date(dateRearrangeMapper[(_k = (_j = validations.date) === null || _j === void 0 ? void 0 : _j.target) === null || _k === void 0 ? void 0 : _k.format](validations.date.target.value).toString());
2102
+ if ((_g = (_f = validations.date) === null || _f === void 0 ? void 0 : _f.target) === null || _g === void 0 ? void 0 : _g.format) {
2103
+ target = new Date(dateRearrangeMapper[(_j = (_h = validations.date) === null || _h === void 0 ? void 0 : _h.target) === null || _j === void 0 ? void 0 : _j.format](validations.date.target.value).toString());
2105
2104
  targetDate = target;
2106
2105
  }
2107
2106
  if (validations.date.origin.intervals) {
2108
2107
  targetDate = getIntervalsDate(originDate, validations.date.origin.intervals);
2109
2108
  const date = new Date();
2110
- if (((_l = validations.date.target) === null || _l === void 0 ? void 0 : _l.value) && target) {
2109
+ if (((_k = validations.date.target) === null || _k === void 0 ? void 0 : _k.value) && target) {
2111
2110
  date.setDate(target.getDate());
2112
2111
  date.setMonth(target.getMonth());
2113
2112
  }
@@ -2116,6 +2115,9 @@ const date = (value, validations) => {
2116
2115
  if (validations.date.onlyValidDate && (!(targetDate instanceof Date && isFinite(targetDate)) || !(targetDate instanceof Date && isFinite(originDate)) || originValue.length < 8)) {
2117
2116
  return true;
2118
2117
  }
2118
+ if (invalidStringDate(mappedValue, (_l = validations.date) === null || _l === void 0 ? void 0 : _l.origin.format)) {
2119
+ return false;
2120
+ }
2119
2121
  const originTimestamp = originDate.getTime();
2120
2122
  const targetTimestamp = targetDate.getTime();
2121
2123
  const operationsMapper = {
@@ -2126,7 +2128,7 @@ const date = (value, validations) => {
2126
2128
  '===': originTimestamp === targetTimestamp,
2127
2129
  '!==': originTimestamp !== targetTimestamp
2128
2130
  };
2129
- return !operationsMapper[(_m = validations.date) === null || _m === void 0 ? void 0 : _m.operator];
2131
+ return operationsMapper[(_m = validations.date) === null || _m === void 0 ? void 0 : _m.operator];
2130
2132
  };
2131
2133
  /**
2132
2134
  * @function validDate
@@ -2155,11 +2157,17 @@ const validDate = (value, validations) => {
2155
2157
  const month = parseInt(dateParts[0], 10) - 1; // Month is zero-based
2156
2158
  const day = parseInt(dateParts[1], 10);
2157
2159
  const date = new Date(year, month, day);
2158
- /* This line is here because we need to guarantee that the field will not be valid while typing,
2159
- * that is, imagine the scenario where the user wants to type 1995, when typing 19 or 199,
2160
- * the function returns valid, as there is a year 19 or 199.
2161
- */
2162
- if (date.getFullYear() < 1900) {
2160
+ /*
2161
+ * Motivation: due to the scenario in which a date field may be 'typeable',
2162
+ * we need to ensure that dates less than a thousand and such years are valid.
2163
+ * E.g.: user types 13-07-199 (without this condition, it becomes a valid date, as 199 is a valid year)
2164
+ *
2165
+ * Ps: 150 is a valid value for now, as we still cannot live more than that, from the
2166
+ * the moment we start to live longer, we can rethink this.
2167
+ */
2168
+ const today = new Date();
2169
+ today.setFullYear(today.getFullYear() - 150);
2170
+ if (date.getFullYear() < today.getFullYear()) {
2163
2171
  return true;
2164
2172
  }
2165
2173
  // Check if the date is valid
@@ -3321,6 +3329,21 @@ class FormCore {
3321
3329
  }
3322
3330
  });
3323
3331
  }
3332
+ /**
3333
+ * @internal
3334
+ * Update field visibility accordingly.
3335
+ *
3336
+ * @param {string} field - Field name to be updated.
3337
+ * @param {boolean} hasError - Condition to be used as visibility.
3338
+ * @param {boolean|undefined} showOnlyIfTrue - Flag to be considered when update field visibility. If it's true, then considered error, if it's false, always considered the opposite.
3339
+ */
3340
+ setFieldVisibility(field, hasError, showOnlyIfTrue) {
3341
+ if (!this.fields.has(field)) {
3342
+ console.warn(`failed to update visibility onto field ${field}`);
3343
+ } else {
3344
+ this.fields.get(field).visibility = showOnlyIfTrue ? hasError : !hasError;
3345
+ }
3346
+ }
3324
3347
  /**
3325
3348
  * Validates visibility conditions for a given event and updates field visibility accordingly.
3326
3349
  *
@@ -3334,17 +3357,19 @@ class FormCore {
3334
3357
  }) {
3335
3358
  const field = this.fields.get(key);
3336
3359
  const structVisibility = field === null || field === void 0 ? void 0 : field.visibilityConditions;
3337
- if (!structVisibility || !(structVisibility === null || structVisibility === void 0 ? void 0 : structVisibility.some(config => config.events.includes(event)))) return;
3360
+ if (!structVisibility || !(structVisibility === null || structVisibility === void 0 ? void 0 : structVisibility.some(config => config.events.includes(event)))) {
3361
+ return;
3362
+ }
3338
3363
  structVisibility.forEach(structElement => {
3339
3364
  if (!structElement.events.includes(event)) return;
3340
3365
  Object.keys(structElement.validations).forEach(validationKey => {
3341
3366
  const error = handleValidation(field.value, structElement.validations, validations, validationKey);
3342
3367
  if (Array.isArray(structElement.fields)) {
3343
3368
  structElement.fields.forEach(fieldKey => {
3344
- if (!this.fields.has(fieldKey)) console.warn(`failed to update visibility onto field ${fieldKey}`);else this.fields.get(fieldKey).visibility = structElement.showOnlyIfTrue ? error : !error;
3369
+ this.setFieldVisibility(fieldKey, error, !!(field.value && structElement.showOnlyIfTrue));
3345
3370
  });
3346
3371
  } else if (structElement.fields) {
3347
- if (!this.fields.has(structElement.fields)) console.warn(`failed to update visibility onto field ${structElement.fields}`);else this.fields.get(structElement.fields).visibility = structElement.showOnlyIfTrue ? error : !error;
3372
+ this.setFieldVisibility(structElement.fields, error, !!(field.value && structElement.showOnlyIfTrue));
3348
3373
  }
3349
3374
  });
3350
3375
  });
@@ -3533,7 +3558,7 @@ class FormCore {
3533
3558
  set(values, val.nameToSubmit || key, val.value);
3534
3559
  }
3535
3560
  });
3536
- console.log(values);
3561
+ console.table(values);
3537
3562
  }
3538
3563
  /**
3539
3564
  * Gets the current values of all form fields.
@@ -3741,7 +3766,7 @@ class FormGroup {
3741
3766
  * Prints the form group instance to the console.
3742
3767
  */
3743
3768
  printFormGroupInstance() {
3744
- console.log(this.forms);
3769
+ console.table(this.forms);
3745
3770
  }
3746
3771
  /**
3747
3772
  * Prototype submit function to multiple forms
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolttech/form-engine-core",
3
- "version": "0.0.1-beta.25",
3
+ "version": "0.0.1-beta.27",
4
4
  "module": "./index.esm.js",
5
5
  "type": "module",
6
6
  "main": "./index.esm.js",
@@ -139,6 +139,15 @@ declare class FormCore {
139
139
  * @private
140
140
  */
141
141
  private static checkIndexes;
142
+ /**
143
+ * @internal
144
+ * Update field visibility accordingly.
145
+ *
146
+ * @param {string} field - Field name to be updated.
147
+ * @param {boolean} hasError - Condition to be used as visibility.
148
+ * @param {boolean|undefined} showOnlyIfTrue - Flag to be considered when update field visibility. If it's true, then considered error, if it's false, always considered the opposite.
149
+ */
150
+ private setFieldVisibility;
142
151
  /**
143
152
  * Validates visibility conditions for a given event and updates field visibility accordingly.
144
153
  *