@apia/validations 2.0.9 → 2.0.11

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 (68) hide show
  1. package/dist/collector/Collector.d.ts +39 -0
  2. package/dist/collector/Collector.d.ts.map +1 -0
  3. package/dist/collector/CollectorField.d.ts +40 -0
  4. package/dist/collector/CollectorField.d.ts.map +1 -0
  5. package/dist/collector/ValidatedCollectorField.d.ts +44 -0
  6. package/dist/collector/ValidatedCollectorField.d.ts.map +1 -0
  7. package/dist/collector/fields/Button.d.ts +13 -0
  8. package/dist/collector/fields/Button.d.ts.map +1 -0
  9. package/dist/collector/fields/Checkbox.d.ts +13 -0
  10. package/dist/collector/fields/Checkbox.d.ts.map +1 -0
  11. package/dist/collector/fields/Input.d.ts +11 -0
  12. package/dist/collector/fields/Input.d.ts.map +1 -0
  13. package/dist/collector/fields/Label.d.ts +13 -0
  14. package/dist/collector/fields/Label.d.ts.map +1 -0
  15. package/dist/collector/fields/Select.d.ts +26 -0
  16. package/dist/collector/fields/Select.d.ts.map +1 -0
  17. package/dist/collector/fields/Textarea.d.ts +11 -0
  18. package/dist/collector/fields/Textarea.d.ts.map +1 -0
  19. package/dist/components/Captcha.d.ts +13 -0
  20. package/dist/components/Captcha.d.ts.map +1 -0
  21. package/dist/components/Checkbox.d.ts +12 -0
  22. package/dist/components/Checkbox.d.ts.map +1 -0
  23. package/dist/components/DeletableInput.d.ts +9 -0
  24. package/dist/components/DeletableInput.d.ts.map +1 -0
  25. package/dist/components/FieldWrapper.d.ts +20 -0
  26. package/dist/components/FieldWrapper.d.ts.map +1 -0
  27. package/dist/components/FileInput.d.ts +11 -0
  28. package/dist/components/FileInput.d.ts.map +1 -0
  29. package/dist/components/Input.d.ts +9 -0
  30. package/dist/components/Input.d.ts.map +1 -0
  31. package/dist/components/NumberInput.d.ts +14 -0
  32. package/dist/components/NumberInput.d.ts.map +1 -0
  33. package/dist/components/Radio.d.ts +14 -0
  34. package/dist/components/Radio.d.ts.map +1 -0
  35. package/dist/components/Select.d.ts +17 -0
  36. package/dist/components/Select.d.ts.map +1 -0
  37. package/dist/components/Switchbox.d.ts +11 -0
  38. package/dist/components/Switchbox.d.ts.map +1 -0
  39. package/dist/components/Textarea.d.ts +9 -0
  40. package/dist/components/Textarea.d.ts.map +1 -0
  41. package/dist/components/ValidationError.d.ts +17 -0
  42. package/dist/components/ValidationError.d.ts.map +1 -0
  43. package/dist/components/types.d.ts +11 -0
  44. package/dist/components/types.d.ts.map +1 -0
  45. package/dist/form/Form.d.ts +89 -0
  46. package/dist/form/Form.d.ts.map +1 -0
  47. package/dist/form/FormContext.d.ts +27 -0
  48. package/dist/form/FormContext.d.ts.map +1 -0
  49. package/dist/hooks/useField.d.ts +76 -0
  50. package/dist/hooks/useField.d.ts.map +1 -0
  51. package/dist/hooks/useFieldStatesClassNames.d.ts +4 -0
  52. package/dist/hooks/useFieldStatesClassNames.d.ts.map +1 -0
  53. package/dist/hooks/useFieldValue.d.ts +8 -0
  54. package/dist/hooks/useFieldValue.d.ts.map +1 -0
  55. package/dist/hooks/useValidationClass.d.ts +49 -0
  56. package/dist/hooks/useValidationClass.d.ts.map +1 -0
  57. package/dist/index.d.ts +25 -771
  58. package/dist/index.js +70 -131
  59. package/dist/index.js.map +1 -1
  60. package/dist/lang/es.d.ts +32 -0
  61. package/dist/lang/es.d.ts.map +1 -0
  62. package/dist/types.d.ts +152 -0
  63. package/dist/types.d.ts.map +1 -0
  64. package/dist/util.d.ts +36 -0
  65. package/dist/util.d.ts.map +1 -0
  66. package/dist/validationStore.d.ts +105 -0
  67. package/dist/validationStore.d.ts.map +1 -0
  68. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -273,23 +273,14 @@ const validationsStore = new class {
273
273
  * Inicializa el estado de un nuevo formulario
274
274
  */
275
275
  __publicField$9(this, "registerForm", (formName, props = { avoidFieldsOverride: false }) => {
276
- props.persistToSessionStorage && this.loadFromSessionStorage(formName);
277
276
  if (this.activeForms.includes(formName)) {
278
- this.updateFormStore(
279
- formName,
280
- {
281
- avoidFieldsOverride: props.avoidFieldsOverride
282
- },
283
- true
284
- );
277
+ this.updateFormStore(formName, {
278
+ avoidFieldsOverride: props.avoidFieldsOverride
279
+ });
285
280
  if (props.initialValues)
286
281
  Object.entries(props.initialValues).forEach(([fieldId, fieldProps]) => {
287
282
  if (fieldProps instanceof Object)
288
- this.updateField(
289
- formName,
290
- { name: fieldId, ...fieldProps },
291
- { avoidFieldsOverride: props.avoidFieldsOverride }
292
- );
283
+ this.updateField(formName, { name: fieldId, ...fieldProps });
293
284
  });
294
285
  return;
295
286
  }
@@ -300,18 +291,10 @@ const validationsStore = new class {
300
291
  isTouched: false,
301
292
  isValid: false,
302
293
  name: formName,
303
- validationResult: null,
304
- persistToSessionStorage: props.persistToSessionStorage
294
+ validationResult: null
305
295
  });
306
296
  Object.entries(props.initialValues ?? {}).forEach(([name, value]) => {
307
- this.updateField(
308
- formName,
309
- { name, value },
310
- {
311
- markFormAsTouched: false,
312
- avoidFieldsOverride: props.avoidFieldsOverride
313
- }
314
- );
297
+ this.updateField(formName, { name, value }, { markFormAsTouched: false });
315
298
  });
316
299
  });
317
300
  /**
@@ -335,7 +318,7 @@ const validationsStore = new class {
335
318
  this.updateFormStore(formName, { isValid: false });
336
319
  });
337
320
  __publicField$9(this, "setFormIsTouched", (formName, isTouched = true) => {
338
- this.updateFormStore(formName, { isTouched }, true);
321
+ this.updateFormStore(formName, { isTouched });
339
322
  });
340
323
  /**
341
324
  * Elimina un campo del registro de validación de un formulario. De esta
@@ -372,11 +355,9 @@ const validationsStore = new class {
372
355
  __publicField$9(this, "updateField", (formName, field, options) => {
373
356
  const previousValue = this.getFieldStore(formName, field.name)?.value;
374
357
  if (this.getFormStore(formName))
375
- this.updateFormStore(formName, { isValid: false }, true);
358
+ this.updateFormStore(formName, { isValid: false });
376
359
  else
377
360
  this.registerForm(formName);
378
- if (previousValue !== void 0 && options?.avoidFieldsOverride)
379
- return;
380
361
  const isTouched = field.value !== void 0 && field.value !== previousValue && options?.markFormAsTouched !== false;
381
362
  if (!this.existsField(formName, field.name))
382
363
  this.registerField(formName, field, {
@@ -468,76 +449,19 @@ const validationsStore = new class {
468
449
  });
469
450
  console.error("Campos que fallaron la validaci\xF3n", fieldsWithError);
470
451
  }
471
- this.updateFormStore(
472
- formName,
473
- {
474
- isValid: false,
475
- validationResult: validationErrors
476
- },
477
- true
478
- );
452
+ this.updateFormStore(formName, {
453
+ isValid: false,
454
+ validationResult: validationErrors
455
+ });
479
456
  return validationErrors;
480
457
  }
481
- this.updateFormStore(
482
- formName,
483
- {
484
- isValid: true,
485
- validationResult: validationValues
486
- },
487
- true
488
- );
458
+ this.updateFormStore(formName, {
459
+ isValid: true,
460
+ validationResult: validationValues
461
+ });
489
462
  return validationValues;
490
463
  });
491
464
  }
492
- loadFromSessionStorage(formName) {
493
- const storageValue = sessionStorage.getItem(this.makeFormName(formName));
494
- if (storageValue) {
495
- try {
496
- const parsedValue = JSON.parse(storageValue);
497
- this.activeForms.push(formName);
498
- this.updateFormStore(formName, parsedValue.formProps, true);
499
- Object.entries(parsedValue.fields).forEach(([name, field]) => {
500
- propsStore.updateField(
501
- this.makeFieldName(formName, name),
502
- {
503
- ...this.getFieldStore(formName, name),
504
- ...field
505
- },
506
- { noEmit: true }
507
- );
508
- });
509
- return true;
510
- } catch (e) {
511
- }
512
- }
513
- return false;
514
- }
515
- persistFormToSessionStorage(formName) {
516
- const { isTouched, isValid, validationResult, ...formProps } = this.getFormStore(formName);
517
- const shouldPersist = formProps.persistToSessionStorage;
518
- if (shouldPersist) {
519
- const fields = formProps.fields.map((current) => {
520
- const {
521
- validationFunction,
522
- isTouched: isTouched2,
523
- error,
524
- validationValueParser,
525
- ...field
526
- } = this.getFieldStore(formName, current);
527
- return field;
528
- }).reduce(
529
- (o, c) => ({ ...o, [c.name]: c }),
530
- {}
531
- );
532
- sessionStorage.setItem(
533
- this.makeFormName(formName),
534
- JSON.stringify({
535
- formProps,
536
- fields
537
- })
538
- );
539
- }
540
- }
541
465
  getFieldStore(formName, fieldName) {
542
466
  return propsStore.getFieldProps(
543
467
  this.makeFieldName(formName, fieldName)
@@ -555,12 +479,9 @@ const validationsStore = new class {
555
479
  },
556
480
  { noEmit }
557
481
  );
558
- this.persistFormToSessionStorage(formName);
559
482
  }
560
- updateFormStore(formName, newState, avoidPersist = false) {
483
+ updateFormStore(formName, newState) {
561
484
  propsStore.updateField(this.makeFormName(formName), newState);
562
- if (!avoidPersist)
563
- this.persistFormToSessionStorage(formName);
564
485
  }
565
486
  enableFormTouch(formName) {
566
487
  this.timeouts[formName] = true;
@@ -571,6 +492,12 @@ const validationsStore = new class {
571
492
  makeFormName(formName) {
572
493
  return `validation_${formName}`;
573
494
  }
495
+ resetForm(formName) {
496
+ const state = this.getFormStore(formName);
497
+ state.fields.forEach((field) => {
498
+ this.setFieldValue(formName, field, "");
499
+ });
500
+ }
574
501
  }();
575
502
  function useFormSelector(formName, selector, comparator) {
576
503
  return usePropsSelector(
@@ -614,7 +541,6 @@ const Form = ({
614
541
  initialValues,
615
542
  onUnmount,
616
543
  onValidate,
617
- persistToSessionStorage,
618
544
  unregisterOnUnmount
619
545
  }) => {
620
546
  const actualName = React.useMemo(
@@ -655,8 +581,7 @@ const Form = ({
655
581
  useMount(() => {
656
582
  validationsStore.registerForm(actualName, {
657
583
  avoidFieldsOverride: avoidFieldsOverride ?? initialValues !== void 0,
658
- initialValues,
659
- persistToSessionStorage
584
+ initialValues
660
585
  });
661
586
  setHasRegistered(true);
662
587
  });
@@ -916,7 +841,7 @@ const FieldWrapper = ({
916
841
  ] });
917
842
  }
918
843
  return /* @__PURE__ */ jsxs(Box, { className: `fieldWrapper ${className ?? ""}`, ...props, as, children: [
919
- /* @__PURE__ */ jsxs(Box, { as: "span", className: "fieldWrapper__label", children: [
844
+ /* @__PURE__ */ jsxs(Box, { as: "span", children: [
920
845
  window.SHOW_REQUIRED_POSITION !== "0" && label && /* @__PURE__ */ jsx(RequiredMark, { isRequired }),
921
846
  label,
922
847
  !avoidSemicolon && label && /* @__PURE__ */ jsx("abbr", { className: "semicolon", children: ": " }),
@@ -1741,9 +1666,9 @@ var __privateAdd$3 = (obj, member, value) => {
1741
1666
  };
1742
1667
  var _emitter;
1743
1668
  class CollectorField extends EventEmitter {
1744
- constructor(_name, label, properties) {
1669
+ constructor(name, label, properties) {
1745
1670
  super();
1746
- this._name = _name;
1671
+ this.name = name;
1747
1672
  this.label = label;
1748
1673
  __privateAdd$3(this, _emitter, new StatefulEmitter({
1749
1674
  isHidden: false
@@ -1777,12 +1702,6 @@ class CollectorField extends EventEmitter {
1777
1702
  this.properties = properties;
1778
1703
  }
1779
1704
  }
1780
- get name() {
1781
- return this._name;
1782
- }
1783
- set name(name) {
1784
- this._name = name;
1785
- }
1786
1705
  }
1787
1706
  _emitter = new WeakMap();
1788
1707
 
@@ -1807,7 +1726,7 @@ var __privateAdd$2 = (obj, member, value) => {
1807
1726
  };
1808
1727
  var __privateSet$1 = (obj, member, value, setter) => {
1809
1728
  __accessCheck$2(obj, member, "write to private field");
1810
- member.set(obj, value);
1729
+ setter ? setter.call(obj, value) : member.set(obj, value);
1811
1730
  return value;
1812
1731
  };
1813
1732
  var _rules, _validationFunction;
@@ -1943,7 +1862,7 @@ var __privateAdd$1 = (obj, member, value) => {
1943
1862
  };
1944
1863
  var __privateSet = (obj, member, value, setter) => {
1945
1864
  __accessCheck$1(obj, member, "write to private field");
1946
- member.set(obj, value);
1865
+ setter ? setter.call(obj, value) : member.set(obj, value);
1947
1866
  return value;
1948
1867
  };
1949
1868
  var _options, _useOptions;
@@ -2037,7 +1956,7 @@ const _CollectorLabel = class _CollectorLabel extends CollectorField {
2037
1956
  __publicField$2(this, "type", "label");
2038
1957
  __publicField$2(this, "Component", () => {
2039
1958
  if (this.kind === "label")
2040
- return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
1959
+ return /* @__PURE__ */ jsx(Box, { as: this.kind, children: /* @__PURE__ */ jsx("span", { children: this.label }) });
2041
1960
  return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
2042
1961
  });
2043
1962
  }
@@ -2106,6 +2025,7 @@ class Collector {
2106
2025
  textarea: CollectorTextarea
2107
2026
  });
2108
2027
  __publicField(this, "private__id", `Collector${maxId++}`);
2028
+ __publicField(this, "modal", null);
2109
2029
  __privateAdd(this, _fields, {});
2110
2030
  __publicField(this, "add", (...fields) => {
2111
2031
  fields.forEach((field) => {
@@ -2123,33 +2043,44 @@ class Collector {
2123
2043
  }) => {
2124
2044
  const modal = useModalContext();
2125
2045
  let hasValidated = false;
2126
- return /* @__PURE__ */ jsx(Box, { ...getVariant("layout.util.collector.formContainer"), children: /* @__PURE__ */ jsxs(
2127
- Form,
2046
+ return /* @__PURE__ */ jsx(
2047
+ Box,
2128
2048
  {
2129
- name: this.private__id,
2130
- onValidate: (result) => {
2131
- if (hasSucceedFormValidation(result)) {
2132
- resolve(result.submitValues);
2133
- modal.close();
2049
+ className: "formContainer",
2050
+ ...getVariant("layout.util.collector.formContainer"),
2051
+ children: /* @__PURE__ */ jsxs(
2052
+ Form,
2053
+ {
2054
+ name: this.private__id,
2055
+ onValidate: (result) => {
2056
+ if (hasSucceedFormValidation(result)) {
2057
+ const res = resolve(result.submitValues);
2058
+ if (res) {
2059
+ modal.close();
2060
+ }
2061
+ }
2062
+ },
2063
+ children: [
2064
+ /* @__PURE__ */ jsx(Box, { className: "collector__fields", children: Object.values(__privateGet(this, _fields)).map((current) => {
2065
+ if (current instanceof ValidatedCollectorField)
2066
+ hasValidated = true;
2067
+ return /* @__PURE__ */ jsx(current.SuperComponent, {}, current.name);
2068
+ }) }),
2069
+ hasValidated && !hideConfirm && /* @__PURE__ */ jsx(Box, { className: "collector__buttons", children: /* @__PURE__ */ jsx(Button, { variant: "outline", children: getLabel("btnCon").text }) })
2070
+ ]
2134
2071
  }
2135
- },
2136
- children: [
2137
- Object.values(__privateGet(this, _fields)).map((current) => {
2138
- if (current instanceof ValidatedCollectorField)
2139
- hasValidated = true;
2140
- return /* @__PURE__ */ jsx(current.SuperComponent, {}, current.name);
2141
- }),
2142
- hasValidated && !hideConfirm && /* @__PURE__ */ jsx(Box, { className: "collector__buttons", children: /* @__PURE__ */ jsx(Button, { variant: "outline", children: getLabel("btnCon").text }) })
2143
- ]
2072
+ )
2144
2073
  }
2145
- ) });
2074
+ );
2146
2075
  });
2147
2076
  __publicField(this, "show", (modalProps) => {
2077
+ const { onConfirm, ...actualModalProps } = modalProps ?? {};
2148
2078
  return new Promise((resolve) => {
2149
2079
  const C = __privateGet(this, _Component);
2150
- ApiaUtil.instance.modals.open({
2151
- onExited() {
2080
+ const modal = ApiaUtil.instance.modals.open({
2081
+ onExited: () => {
2152
2082
  {
2083
+ this.modal = null;
2153
2084
  resolve(null);
2154
2085
  }
2155
2086
  },
@@ -2158,7 +2089,14 @@ class Collector {
2158
2089
  {
2159
2090
  hideConfirm: modalProps?.hideConfirm,
2160
2091
  resolve: (values) => {
2092
+ if (onConfirm) {
2093
+ const confirmResult = onConfirm(values);
2094
+ if (!confirmResult) {
2095
+ return false;
2096
+ }
2097
+ }
2161
2098
  resolve(values);
2099
+ return true;
2162
2100
  }
2163
2101
  }
2164
2102
  ),
@@ -2168,8 +2106,9 @@ class Collector {
2168
2106
  "input,textarea,select,button:not(.modal__closeButton)"
2169
2107
  );
2170
2108
  },
2171
- ...modalProps
2109
+ ...actualModalProps
2172
2110
  });
2111
+ this.modal = modal;
2173
2112
  });
2174
2113
  });
2175
2114
  }