@apia/validations 2.0.11 → 3.0.2

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.
Files changed (69) hide show
  1. package/dist/index.d.ts +785 -25
  2. package/dist/index.js +131 -82
  3. package/dist/index.js.map +1 -1
  4. package/package.json +6 -6
  5. package/rollup.validations.mjs +3 -3
  6. package/dist/collector/Collector.d.ts +0 -39
  7. package/dist/collector/Collector.d.ts.map +0 -1
  8. package/dist/collector/CollectorField.d.ts +0 -40
  9. package/dist/collector/CollectorField.d.ts.map +0 -1
  10. package/dist/collector/ValidatedCollectorField.d.ts +0 -44
  11. package/dist/collector/ValidatedCollectorField.d.ts.map +0 -1
  12. package/dist/collector/fields/Button.d.ts +0 -13
  13. package/dist/collector/fields/Button.d.ts.map +0 -1
  14. package/dist/collector/fields/Checkbox.d.ts +0 -13
  15. package/dist/collector/fields/Checkbox.d.ts.map +0 -1
  16. package/dist/collector/fields/Input.d.ts +0 -11
  17. package/dist/collector/fields/Input.d.ts.map +0 -1
  18. package/dist/collector/fields/Label.d.ts +0 -13
  19. package/dist/collector/fields/Label.d.ts.map +0 -1
  20. package/dist/collector/fields/Select.d.ts +0 -26
  21. package/dist/collector/fields/Select.d.ts.map +0 -1
  22. package/dist/collector/fields/Textarea.d.ts +0 -11
  23. package/dist/collector/fields/Textarea.d.ts.map +0 -1
  24. package/dist/components/Captcha.d.ts +0 -13
  25. package/dist/components/Captcha.d.ts.map +0 -1
  26. package/dist/components/Checkbox.d.ts +0 -12
  27. package/dist/components/Checkbox.d.ts.map +0 -1
  28. package/dist/components/DeletableInput.d.ts +0 -9
  29. package/dist/components/DeletableInput.d.ts.map +0 -1
  30. package/dist/components/FieldWrapper.d.ts +0 -20
  31. package/dist/components/FieldWrapper.d.ts.map +0 -1
  32. package/dist/components/FileInput.d.ts +0 -11
  33. package/dist/components/FileInput.d.ts.map +0 -1
  34. package/dist/components/Input.d.ts +0 -9
  35. package/dist/components/Input.d.ts.map +0 -1
  36. package/dist/components/NumberInput.d.ts +0 -14
  37. package/dist/components/NumberInput.d.ts.map +0 -1
  38. package/dist/components/Radio.d.ts +0 -14
  39. package/dist/components/Radio.d.ts.map +0 -1
  40. package/dist/components/Select.d.ts +0 -17
  41. package/dist/components/Select.d.ts.map +0 -1
  42. package/dist/components/Switchbox.d.ts +0 -11
  43. package/dist/components/Switchbox.d.ts.map +0 -1
  44. package/dist/components/Textarea.d.ts +0 -9
  45. package/dist/components/Textarea.d.ts.map +0 -1
  46. package/dist/components/ValidationError.d.ts +0 -17
  47. package/dist/components/ValidationError.d.ts.map +0 -1
  48. package/dist/components/types.d.ts +0 -11
  49. package/dist/components/types.d.ts.map +0 -1
  50. package/dist/form/Form.d.ts +0 -89
  51. package/dist/form/Form.d.ts.map +0 -1
  52. package/dist/form/FormContext.d.ts +0 -27
  53. package/dist/form/FormContext.d.ts.map +0 -1
  54. package/dist/hooks/useField.d.ts +0 -76
  55. package/dist/hooks/useField.d.ts.map +0 -1
  56. package/dist/hooks/useFieldStatesClassNames.d.ts +0 -4
  57. package/dist/hooks/useFieldStatesClassNames.d.ts.map +0 -1
  58. package/dist/hooks/useFieldValue.d.ts +0 -8
  59. package/dist/hooks/useFieldValue.d.ts.map +0 -1
  60. package/dist/hooks/useValidationClass.d.ts +0 -49
  61. package/dist/hooks/useValidationClass.d.ts.map +0 -1
  62. package/dist/lang/es.d.ts +0 -32
  63. package/dist/lang/es.d.ts.map +0 -1
  64. package/dist/types.d.ts +0 -152
  65. package/dist/types.d.ts.map +0 -1
  66. package/dist/util.d.ts +0 -36
  67. package/dist/util.d.ts.map +0 -1
  68. package/dist/validationStore.d.ts +0 -105
  69. package/dist/validationStore.d.ts.map +0 -1
package/dist/index.js CHANGED
@@ -72,6 +72,9 @@ function hasSucceedFormValidation(result) {
72
72
  return !("errors" in result);
73
73
  }
74
74
  async function focusOnField(formName, field) {
75
+ if (field.focus) {
76
+ return await field.focus() || true;
77
+ }
75
78
  const formElement = document.getElementById(
76
79
  `ValidationForm-${formName}`
77
80
  );
@@ -273,14 +276,23 @@ const validationsStore = new class {
273
276
  * Inicializa el estado de un nuevo formulario
274
277
  */
275
278
  __publicField$9(this, "registerForm", (formName, props = { avoidFieldsOverride: false }) => {
279
+ props.persistToSessionStorage && this.loadFromSessionStorage(formName);
276
280
  if (this.activeForms.includes(formName)) {
277
- this.updateFormStore(formName, {
278
- avoidFieldsOverride: props.avoidFieldsOverride
279
- });
281
+ this.updateFormStore(
282
+ formName,
283
+ {
284
+ avoidFieldsOverride: props.avoidFieldsOverride
285
+ },
286
+ true
287
+ );
280
288
  if (props.initialValues)
281
289
  Object.entries(props.initialValues).forEach(([fieldId, fieldProps]) => {
282
290
  if (fieldProps instanceof Object)
283
- this.updateField(formName, { name: fieldId, ...fieldProps });
291
+ this.updateField(
292
+ formName,
293
+ { name: fieldId, ...fieldProps },
294
+ { avoidFieldsOverride: props.avoidFieldsOverride }
295
+ );
284
296
  });
285
297
  return;
286
298
  }
@@ -291,10 +303,18 @@ const validationsStore = new class {
291
303
  isTouched: false,
292
304
  isValid: false,
293
305
  name: formName,
294
- validationResult: null
306
+ validationResult: null,
307
+ persistToSessionStorage: props.persistToSessionStorage
295
308
  });
296
309
  Object.entries(props.initialValues ?? {}).forEach(([name, value]) => {
297
- this.updateField(formName, { name, value }, { markFormAsTouched: false });
310
+ this.updateField(
311
+ formName,
312
+ { name, value },
313
+ {
314
+ markFormAsTouched: false,
315
+ avoidFieldsOverride: props.avoidFieldsOverride
316
+ }
317
+ );
298
318
  });
299
319
  });
300
320
  /**
@@ -318,7 +338,7 @@ const validationsStore = new class {
318
338
  this.updateFormStore(formName, { isValid: false });
319
339
  });
320
340
  __publicField$9(this, "setFormIsTouched", (formName, isTouched = true) => {
321
- this.updateFormStore(formName, { isTouched });
341
+ this.updateFormStore(formName, { isTouched }, true);
322
342
  });
323
343
  /**
324
344
  * Elimina un campo del registro de validación de un formulario. De esta
@@ -355,9 +375,11 @@ const validationsStore = new class {
355
375
  __publicField$9(this, "updateField", (formName, field, options) => {
356
376
  const previousValue = this.getFieldStore(formName, field.name)?.value;
357
377
  if (this.getFormStore(formName))
358
- this.updateFormStore(formName, { isValid: false });
378
+ this.updateFormStore(formName, { isValid: false }, true);
359
379
  else
360
380
  this.registerForm(formName);
381
+ if (previousValue !== void 0 && options?.avoidFieldsOverride)
382
+ return;
361
383
  const isTouched = field.value !== void 0 && field.value !== previousValue && options?.markFormAsTouched !== false;
362
384
  if (!this.existsField(formName, field.name))
363
385
  this.registerField(formName, field, {
@@ -449,19 +471,76 @@ const validationsStore = new class {
449
471
  });
450
472
  console.error("Campos que fallaron la validaci\xF3n", fieldsWithError);
451
473
  }
452
- this.updateFormStore(formName, {
453
- isValid: false,
454
- validationResult: validationErrors
455
- });
474
+ this.updateFormStore(
475
+ formName,
476
+ {
477
+ isValid: false,
478
+ validationResult: validationErrors
479
+ },
480
+ true
481
+ );
456
482
  return validationErrors;
457
483
  }
458
- this.updateFormStore(formName, {
459
- isValid: true,
460
- validationResult: validationValues
461
- });
484
+ this.updateFormStore(
485
+ formName,
486
+ {
487
+ isValid: true,
488
+ validationResult: validationValues
489
+ },
490
+ true
491
+ );
462
492
  return validationValues;
463
493
  });
464
494
  }
495
+ loadFromSessionStorage(formName) {
496
+ const storageValue = sessionStorage.getItem(this.makeFormName(formName));
497
+ if (storageValue) {
498
+ try {
499
+ const parsedValue = JSON.parse(storageValue);
500
+ this.activeForms.push(formName);
501
+ this.updateFormStore(formName, parsedValue.formProps, true);
502
+ Object.entries(parsedValue.fields).forEach(([name, field]) => {
503
+ propsStore.updateField(
504
+ this.makeFieldName(formName, name),
505
+ {
506
+ ...this.getFieldStore(formName, name),
507
+ ...field
508
+ },
509
+ { noEmit: true }
510
+ );
511
+ });
512
+ return true;
513
+ } catch (e) {
514
+ }
515
+ }
516
+ return false;
517
+ }
518
+ persistFormToSessionStorage(formName) {
519
+ const { isTouched, isValid, validationResult, ...formProps } = this.getFormStore(formName);
520
+ const shouldPersist = formProps.persistToSessionStorage;
521
+ if (shouldPersist) {
522
+ const fields = formProps.fields.map((current) => {
523
+ const {
524
+ validationFunction,
525
+ isTouched: isTouched2,
526
+ error,
527
+ validationValueParser,
528
+ ...field
529
+ } = this.getFieldStore(formName, current);
530
+ return field;
531
+ }).reduce(
532
+ (o, c) => ({ ...o, [c.name]: c }),
533
+ {}
534
+ );
535
+ sessionStorage.setItem(
536
+ this.makeFormName(formName),
537
+ JSON.stringify({
538
+ formProps,
539
+ fields
540
+ })
541
+ );
542
+ }
543
+ }
465
544
  getFieldStore(formName, fieldName) {
466
545
  return propsStore.getFieldProps(
467
546
  this.makeFieldName(formName, fieldName)
@@ -479,9 +558,12 @@ const validationsStore = new class {
479
558
  },
480
559
  { noEmit }
481
560
  );
561
+ this.persistFormToSessionStorage(formName);
482
562
  }
483
- updateFormStore(formName, newState) {
563
+ updateFormStore(formName, newState, avoidPersist = false) {
484
564
  propsStore.updateField(this.makeFormName(formName), newState);
565
+ if (!avoidPersist)
566
+ this.persistFormToSessionStorage(formName);
485
567
  }
486
568
  enableFormTouch(formName) {
487
569
  this.timeouts[formName] = true;
@@ -492,12 +574,6 @@ const validationsStore = new class {
492
574
  makeFormName(formName) {
493
575
  return `validation_${formName}`;
494
576
  }
495
- resetForm(formName) {
496
- const state = this.getFormStore(formName);
497
- state.fields.forEach((field) => {
498
- this.setFieldValue(formName, field, "");
499
- });
500
- }
501
577
  }();
502
578
  function useFormSelector(formName, selector, comparator) {
503
579
  return usePropsSelector(
@@ -541,6 +617,7 @@ const Form = ({
541
617
  initialValues,
542
618
  onUnmount,
543
619
  onValidate,
620
+ persistToSessionStorage,
544
621
  unregisterOnUnmount
545
622
  }) => {
546
623
  const actualName = React.useMemo(
@@ -581,7 +658,8 @@ const Form = ({
581
658
  useMount(() => {
582
659
  validationsStore.registerForm(actualName, {
583
660
  avoidFieldsOverride: avoidFieldsOverride ?? initialValues !== void 0,
584
- initialValues
661
+ initialValues,
662
+ persistToSessionStorage
585
663
  });
586
664
  setHasRegistered(true);
587
665
  });
@@ -592,7 +670,7 @@ const Form = ({
592
670
  {
593
671
  as: "form",
594
672
  ...getVariant("layout.common.validations.form"),
595
- className,
673
+ className: `validationsForm ${className}`,
596
674
  id: `ValidationForm-${actualName}`,
597
675
  ...{ name: actualName },
598
676
  onSubmit: handleSubmit,
@@ -841,7 +919,7 @@ const FieldWrapper = ({
841
919
  ] });
842
920
  }
843
921
  return /* @__PURE__ */ jsxs(Box, { className: `fieldWrapper ${className ?? ""}`, ...props, as, children: [
844
- /* @__PURE__ */ jsxs(Box, { as: "span", children: [
922
+ /* @__PURE__ */ jsxs(Box, { as: "span", className: "fieldWrapper__label", children: [
845
923
  window.SHOW_REQUIRED_POSITION !== "0" && label && /* @__PURE__ */ jsx(RequiredMark, { isRequired }),
846
924
  label,
847
925
  !avoidSemicolon && label && /* @__PURE__ */ jsx("abbr", { className: "semicolon", children: ": " }),
@@ -1651,15 +1729,15 @@ var __publicField$8 = (obj, key, value) => {
1651
1729
  __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
1652
1730
  return value;
1653
1731
  };
1654
- var __accessCheck$3 = (obj, member, msg) => {
1732
+ var __accessCheck$2 = (obj, member, msg) => {
1655
1733
  if (!member.has(obj))
1656
1734
  throw TypeError("Cannot " + msg);
1657
1735
  };
1658
- var __privateGet$3 = (obj, member, getter) => {
1659
- __accessCheck$3(obj, member, "read from private field");
1736
+ var __privateGet$2 = (obj, member, getter) => {
1737
+ __accessCheck$2(obj, member, "read from private field");
1660
1738
  return getter ? getter.call(obj) : member.get(obj);
1661
1739
  };
1662
- var __privateAdd$3 = (obj, member, value) => {
1740
+ var __privateAdd$2 = (obj, member, value) => {
1663
1741
  if (member.has(obj))
1664
1742
  throw TypeError("Cannot add the same private member more than once");
1665
1743
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
@@ -1670,13 +1748,13 @@ class CollectorField extends EventEmitter {
1670
1748
  super();
1671
1749
  this.name = name;
1672
1750
  this.label = label;
1673
- __privateAdd$3(this, _emitter, new StatefulEmitter({
1751
+ __privateAdd$2(this, _emitter, new StatefulEmitter({
1674
1752
  isHidden: false
1675
1753
  }));
1676
1754
  __publicField$8(this, "collector", {});
1677
- __publicField$8(this, "isHidden", () => __privateGet$3(this, _emitter).getState("isHidden"));
1755
+ __publicField$8(this, "isHidden", () => __privateGet$2(this, _emitter).getState("isHidden"));
1678
1756
  __publicField$8(this, "setIsHidden", (isHidden) => {
1679
- __privateGet$3(this, _emitter).emit("isHidden", isHidden);
1757
+ __privateGet$2(this, _emitter).emit("isHidden", isHidden);
1680
1758
  });
1681
1759
  __publicField$8(this, "properties", {});
1682
1760
  __publicField$8(this, "setProperties", (newProps) => {
@@ -1694,7 +1772,7 @@ class CollectorField extends EventEmitter {
1694
1772
  * campos.
1695
1773
  */
1696
1774
  __publicField$8(this, "SuperComponent", () => {
1697
- if (__privateGet$3(this, _emitter).useState("isHidden"))
1775
+ if (__privateGet$2(this, _emitter).useState("isHidden"))
1698
1776
  return null;
1699
1777
  return /* @__PURE__ */ jsx(Box, { className: `collector__field ${this.type}`, children: /* @__PURE__ */ jsx(this.Component, {}) });
1700
1778
  });
@@ -1711,50 +1789,29 @@ var __publicField$7 = (obj, key, value) => {
1711
1789
  __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
1712
1790
  return value;
1713
1791
  };
1714
- var __accessCheck$2 = (obj, member, msg) => {
1715
- if (!member.has(obj))
1716
- throw TypeError("Cannot " + msg);
1717
- };
1718
- var __privateGet$2 = (obj, member, getter) => {
1719
- __accessCheck$2(obj, member, "read from private field");
1720
- return getter ? getter.call(obj) : member.get(obj);
1721
- };
1722
- var __privateAdd$2 = (obj, member, value) => {
1723
- if (member.has(obj))
1724
- throw TypeError("Cannot add the same private member more than once");
1725
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1726
- };
1727
- var __privateSet$1 = (obj, member, value, setter) => {
1728
- __accessCheck$2(obj, member, "write to private field");
1729
- setter ? setter.call(obj, value) : member.set(obj, value);
1730
- return value;
1731
- };
1732
- var _rules, _validationFunction;
1733
1792
  class ValidatedCollectorField extends CollectorField {
1734
- constructor(_name, label, config) {
1735
- super(_name, label, config?.properties);
1736
- this._name = _name;
1793
+ constructor(name, label, config = {}) {
1794
+ super(name, label, config?.properties);
1795
+ this.name = name;
1737
1796
  this.label = label;
1738
- __publicField$7(this, "initialValue");
1739
- __privateAdd$2(this, _rules, {});
1740
- __privateAdd$2(this, _validationFunction, () => true);
1797
+ this.config = config;
1741
1798
  __publicField$7(this, "setInitialValue", (newValue) => {
1742
- this.initialValue = newValue;
1799
+ this.config.initialValue = newValue;
1743
1800
  });
1744
1801
  __publicField$7(this, "setValidationFunction", (newFunction) => {
1745
- __privateSet$1(this, _validationFunction, newFunction);
1746
- this.emit("functionUpdate", __privateGet$2(this, _validationFunction));
1802
+ this.config.validationFunction = newFunction;
1803
+ this.emit("functionUpdate", this.config.validationFunction);
1747
1804
  });
1748
1805
  __publicField$7(this, "setValidationRules", (newRules) => {
1749
- __privateSet$1(this, _rules, newRules);
1750
- this.emit("rulesUpdate", { ...__privateGet$2(this, _rules) });
1806
+ this.config.validationRules = newRules;
1807
+ this.emit("rulesUpdate", { ...this.config.validationRules });
1751
1808
  });
1752
1809
  __publicField$7(this, "setValue", (newValue) => {
1753
1810
  validationsStore.setFieldValue(this.collector.id, this.name, newValue);
1754
1811
  });
1755
1812
  __publicField$7(this, "useValidationFunction", () => {
1756
1813
  return useSubscription({
1757
- getInitialState: () => () => __privateGet$2(this, _validationFunction),
1814
+ getInitialState: () => () => this.config.validationFunction,
1758
1815
  makeSubscription: (setValidationFunction) => {
1759
1816
  return this.on("functionUpdate", setValidationFunction);
1760
1817
  }
@@ -1762,25 +1819,17 @@ class ValidatedCollectorField extends CollectorField {
1762
1819
  });
1763
1820
  __publicField$7(this, "useValidationRules", () => {
1764
1821
  return useSubscription({
1765
- getInitialState: () => __privateGet$2(this, _rules),
1822
+ getInitialState: () => this.config.validationRules,
1766
1823
  makeSubscription: (setValidationRules) => {
1767
1824
  return this.on("rulesUpdate", setValidationRules);
1768
1825
  }
1769
1826
  });
1770
1827
  });
1771
- if (config?.initialValue) {
1772
- this.initialValue = config.initialValue;
1773
- }
1774
- if (config?.validationFunction) {
1775
- __privateSet$1(this, _validationFunction, config.validationFunction);
1776
- }
1777
- if (config?.validationRules) {
1778
- __privateSet$1(this, _rules, config.validationRules);
1779
- }
1828
+ }
1829
+ get initialValue() {
1830
+ return this.config.initialValue;
1780
1831
  }
1781
1832
  }
1782
- _rules = new WeakMap();
1783
- _validationFunction = new WeakMap();
1784
1833
 
1785
1834
  var __defProp$6 = Object.defineProperty;
1786
1835
  var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -1800,7 +1849,7 @@ class CollectorCheckbox extends ValidatedCollectorField {
1800
1849
  Checkbox,
1801
1850
  {
1802
1851
  ...props,
1803
- initialValue: this.initialValue,
1852
+ initialValue: this.config.initialValue,
1804
1853
  validationFunction,
1805
1854
  name: this.name,
1806
1855
  label: this.label,
@@ -1830,7 +1879,7 @@ class CollectorInput extends ValidatedCollectorField {
1830
1879
  Input,
1831
1880
  {
1832
1881
  ...props,
1833
- initialValue: this.initialValue,
1882
+ initialValue: this.config.initialValue,
1834
1883
  validationFunction,
1835
1884
  name: this.name,
1836
1885
  label: this.label,
@@ -1897,7 +1946,7 @@ class CollectorSelect extends ValidatedCollectorField {
1897
1946
  Select,
1898
1947
  {
1899
1948
  ...props,
1900
- initialValue: this.initialValue,
1949
+ initialValue: this.config.initialValue,
1901
1950
  validationFunction,
1902
1951
  name: this.name,
1903
1952
  label: this.label,
@@ -1932,7 +1981,7 @@ class CollectorTextarea extends ValidatedCollectorField {
1932
1981
  Textarea,
1933
1982
  {
1934
1983
  ...props,
1935
- initialValue: this.initialValue,
1984
+ initialValue: this.config.initialValue,
1936
1985
  name: this.name,
1937
1986
  validationFunction,
1938
1987
  validationRules: rules,
@@ -1956,7 +2005,7 @@ const _CollectorLabel = class _CollectorLabel extends CollectorField {
1956
2005
  __publicField$2(this, "type", "label");
1957
2006
  __publicField$2(this, "Component", () => {
1958
2007
  if (this.kind === "label")
1959
- return /* @__PURE__ */ jsx(Box, { as: this.kind, children: /* @__PURE__ */ jsx("span", { children: this.label }) });
2008
+ return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
1960
2009
  return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
1961
2010
  });
1962
2011
  }