@apia/validations 2.0.9 → 2.0.10

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 +32 -0
  2. package/dist/collector/Collector.d.ts.map +1 -0
  3. package/dist/collector/CollectorField.d.ts +41 -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 +31 -111
  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 +130 -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 +113 -0
  67. package/dist/validationStore.d.ts.map +1 -0
  68. package/package.json +3 -3
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: ": " }),
@@ -2037,7 +1962,7 @@ const _CollectorLabel = class _CollectorLabel extends CollectorField {
2037
1962
  __publicField$2(this, "type", "label");
2038
1963
  __publicField$2(this, "Component", () => {
2039
1964
  if (this.kind === "label")
2040
- return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
1965
+ return /* @__PURE__ */ jsx(Box, { as: this.kind, children: /* @__PURE__ */ jsx("span", { children: this.label }) });
2041
1966
  return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
2042
1967
  });
2043
1968
  }
@@ -2107,18 +2032,14 @@ class Collector {
2107
2032
  });
2108
2033
  __publicField(this, "private__id", `Collector${maxId++}`);
2109
2034
  __privateAdd(this, _fields, {});
2110
- __publicField(this, "add", (...fields) => {
2111
- fields.forEach((field) => {
2112
- __privateGet(this, _fields)[field.name] = field;
2113
- field.collector = this;
2114
- });
2115
- return this;
2035
+ __publicField(this, "add", (field) => {
2036
+ __privateGet(this, _fields)[field.name] = field;
2037
+ field.collector = this;
2116
2038
  });
2117
2039
  __publicField(this, "forEach", (cb) => {
2118
2040
  Object.values(__privateGet(this, _fields)).forEach(cb);
2119
2041
  });
2120
2042
  __privateAdd(this, _Component, ({
2121
- hideConfirm,
2122
2043
  resolve
2123
2044
  }) => {
2124
2045
  const modal = useModalContext();
@@ -2139,7 +2060,7 @@ class Collector {
2139
2060
  hasValidated = true;
2140
2061
  return /* @__PURE__ */ jsx(current.SuperComponent, {}, current.name);
2141
2062
  }),
2142
- hasValidated && !hideConfirm && /* @__PURE__ */ jsx(Box, { className: "collector__buttons", children: /* @__PURE__ */ jsx(Button, { variant: "outline", children: getLabel("btnCon").text }) })
2063
+ hasValidated && /* @__PURE__ */ jsx(Box, { className: "collector__buttons", children: /* @__PURE__ */ jsx(Button, { variant: "outline", children: getLabel("btnCon").text }) })
2143
2064
  ]
2144
2065
  }
2145
2066
  ) });
@@ -2147,7 +2068,7 @@ class Collector {
2147
2068
  __publicField(this, "show", (modalProps) => {
2148
2069
  return new Promise((resolve) => {
2149
2070
  const C = __privateGet(this, _Component);
2150
- ApiaUtil.instance.modals.open({
2071
+ new ApiaUtil().modals.open({
2151
2072
  onExited() {
2152
2073
  {
2153
2074
  resolve(null);
@@ -2156,7 +2077,6 @@ class Collector {
2156
2077
  children: /* @__PURE__ */ jsx(
2157
2078
  C,
2158
2079
  {
2159
- hideConfirm: modalProps?.hideConfirm,
2160
2080
  resolve: (values) => {
2161
2081
  resolve(values);
2162
2082
  }