@apia/validations 2.0.8 → 2.0.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.
Files changed (68) hide show
  1. package/dist/index.d.ts +771 -25
  2. package/dist/index.js +111 -31
  3. package/dist/index.js.map +1 -1
  4. package/package.json +3 -3
  5. package/dist/collector/Collector.d.ts +0 -32
  6. package/dist/collector/Collector.d.ts.map +0 -1
  7. package/dist/collector/CollectorField.d.ts +0 -41
  8. package/dist/collector/CollectorField.d.ts.map +0 -1
  9. package/dist/collector/ValidatedCollectorField.d.ts +0 -44
  10. package/dist/collector/ValidatedCollectorField.d.ts.map +0 -1
  11. package/dist/collector/fields/Button.d.ts +0 -13
  12. package/dist/collector/fields/Button.d.ts.map +0 -1
  13. package/dist/collector/fields/Checkbox.d.ts +0 -13
  14. package/dist/collector/fields/Checkbox.d.ts.map +0 -1
  15. package/dist/collector/fields/Input.d.ts +0 -11
  16. package/dist/collector/fields/Input.d.ts.map +0 -1
  17. package/dist/collector/fields/Label.d.ts +0 -13
  18. package/dist/collector/fields/Label.d.ts.map +0 -1
  19. package/dist/collector/fields/Select.d.ts +0 -26
  20. package/dist/collector/fields/Select.d.ts.map +0 -1
  21. package/dist/collector/fields/Textarea.d.ts +0 -11
  22. package/dist/collector/fields/Textarea.d.ts.map +0 -1
  23. package/dist/components/Captcha.d.ts +0 -13
  24. package/dist/components/Captcha.d.ts.map +0 -1
  25. package/dist/components/Checkbox.d.ts +0 -12
  26. package/dist/components/Checkbox.d.ts.map +0 -1
  27. package/dist/components/DeletableInput.d.ts +0 -9
  28. package/dist/components/DeletableInput.d.ts.map +0 -1
  29. package/dist/components/FieldWrapper.d.ts +0 -20
  30. package/dist/components/FieldWrapper.d.ts.map +0 -1
  31. package/dist/components/FileInput.d.ts +0 -11
  32. package/dist/components/FileInput.d.ts.map +0 -1
  33. package/dist/components/Input.d.ts +0 -9
  34. package/dist/components/Input.d.ts.map +0 -1
  35. package/dist/components/NumberInput.d.ts +0 -14
  36. package/dist/components/NumberInput.d.ts.map +0 -1
  37. package/dist/components/Radio.d.ts +0 -14
  38. package/dist/components/Radio.d.ts.map +0 -1
  39. package/dist/components/Select.d.ts +0 -17
  40. package/dist/components/Select.d.ts.map +0 -1
  41. package/dist/components/Switchbox.d.ts +0 -11
  42. package/dist/components/Switchbox.d.ts.map +0 -1
  43. package/dist/components/Textarea.d.ts +0 -9
  44. package/dist/components/Textarea.d.ts.map +0 -1
  45. package/dist/components/ValidationError.d.ts +0 -17
  46. package/dist/components/ValidationError.d.ts.map +0 -1
  47. package/dist/components/types.d.ts +0 -11
  48. package/dist/components/types.d.ts.map +0 -1
  49. package/dist/form/Form.d.ts +0 -89
  50. package/dist/form/Form.d.ts.map +0 -1
  51. package/dist/form/FormContext.d.ts +0 -27
  52. package/dist/form/FormContext.d.ts.map +0 -1
  53. package/dist/hooks/useField.d.ts +0 -76
  54. package/dist/hooks/useField.d.ts.map +0 -1
  55. package/dist/hooks/useFieldStatesClassNames.d.ts +0 -4
  56. package/dist/hooks/useFieldStatesClassNames.d.ts.map +0 -1
  57. package/dist/hooks/useFieldValue.d.ts +0 -8
  58. package/dist/hooks/useFieldValue.d.ts.map +0 -1
  59. package/dist/hooks/useValidationClass.d.ts +0 -49
  60. package/dist/hooks/useValidationClass.d.ts.map +0 -1
  61. package/dist/lang/es.d.ts +0 -32
  62. package/dist/lang/es.d.ts.map +0 -1
  63. package/dist/types.d.ts +0 -130
  64. package/dist/types.d.ts.map +0 -1
  65. package/dist/util.d.ts +0 -36
  66. package/dist/util.d.ts.map +0 -1
  67. package/dist/validationStore.d.ts +0 -113
  68. package/dist/validationStore.d.ts.map +0 -1
package/dist/index.js CHANGED
@@ -273,14 +273,23 @@ 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);
276
277
  if (this.activeForms.includes(formName)) {
277
- this.updateFormStore(formName, {
278
- avoidFieldsOverride: props.avoidFieldsOverride
279
- });
278
+ this.updateFormStore(
279
+ formName,
280
+ {
281
+ avoidFieldsOverride: props.avoidFieldsOverride
282
+ },
283
+ true
284
+ );
280
285
  if (props.initialValues)
281
286
  Object.entries(props.initialValues).forEach(([fieldId, fieldProps]) => {
282
287
  if (fieldProps instanceof Object)
283
- this.updateField(formName, { name: fieldId, ...fieldProps });
288
+ this.updateField(
289
+ formName,
290
+ { name: fieldId, ...fieldProps },
291
+ { avoidFieldsOverride: props.avoidFieldsOverride }
292
+ );
284
293
  });
285
294
  return;
286
295
  }
@@ -291,10 +300,18 @@ const validationsStore = new class {
291
300
  isTouched: false,
292
301
  isValid: false,
293
302
  name: formName,
294
- validationResult: null
303
+ validationResult: null,
304
+ persistToSessionStorage: props.persistToSessionStorage
295
305
  });
296
306
  Object.entries(props.initialValues ?? {}).forEach(([name, value]) => {
297
- this.updateField(formName, { name, value }, { markFormAsTouched: false });
307
+ this.updateField(
308
+ formName,
309
+ { name, value },
310
+ {
311
+ markFormAsTouched: false,
312
+ avoidFieldsOverride: props.avoidFieldsOverride
313
+ }
314
+ );
298
315
  });
299
316
  });
300
317
  /**
@@ -318,7 +335,7 @@ const validationsStore = new class {
318
335
  this.updateFormStore(formName, { isValid: false });
319
336
  });
320
337
  __publicField$9(this, "setFormIsTouched", (formName, isTouched = true) => {
321
- this.updateFormStore(formName, { isTouched });
338
+ this.updateFormStore(formName, { isTouched }, true);
322
339
  });
323
340
  /**
324
341
  * Elimina un campo del registro de validación de un formulario. De esta
@@ -355,9 +372,11 @@ const validationsStore = new class {
355
372
  __publicField$9(this, "updateField", (formName, field, options) => {
356
373
  const previousValue = this.getFieldStore(formName, field.name)?.value;
357
374
  if (this.getFormStore(formName))
358
- this.updateFormStore(formName, { isValid: false });
375
+ this.updateFormStore(formName, { isValid: false }, true);
359
376
  else
360
377
  this.registerForm(formName);
378
+ if (previousValue !== void 0 && options?.avoidFieldsOverride)
379
+ return;
361
380
  const isTouched = field.value !== void 0 && field.value !== previousValue && options?.markFormAsTouched !== false;
362
381
  if (!this.existsField(formName, field.name))
363
382
  this.registerField(formName, field, {
@@ -449,19 +468,76 @@ const validationsStore = new class {
449
468
  });
450
469
  console.error("Campos que fallaron la validaci\xF3n", fieldsWithError);
451
470
  }
452
- this.updateFormStore(formName, {
453
- isValid: false,
454
- validationResult: validationErrors
455
- });
471
+ this.updateFormStore(
472
+ formName,
473
+ {
474
+ isValid: false,
475
+ validationResult: validationErrors
476
+ },
477
+ true
478
+ );
456
479
  return validationErrors;
457
480
  }
458
- this.updateFormStore(formName, {
459
- isValid: true,
460
- validationResult: validationValues
461
- });
481
+ this.updateFormStore(
482
+ formName,
483
+ {
484
+ isValid: true,
485
+ validationResult: validationValues
486
+ },
487
+ true
488
+ );
462
489
  return validationValues;
463
490
  });
464
491
  }
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
+ }
465
541
  getFieldStore(formName, fieldName) {
466
542
  return propsStore.getFieldProps(
467
543
  this.makeFieldName(formName, fieldName)
@@ -479,9 +555,12 @@ const validationsStore = new class {
479
555
  },
480
556
  { noEmit }
481
557
  );
558
+ this.persistFormToSessionStorage(formName);
482
559
  }
483
- updateFormStore(formName, newState) {
560
+ updateFormStore(formName, newState, avoidPersist = false) {
484
561
  propsStore.updateField(this.makeFormName(formName), newState);
562
+ if (!avoidPersist)
563
+ this.persistFormToSessionStorage(formName);
485
564
  }
486
565
  enableFormTouch(formName) {
487
566
  this.timeouts[formName] = true;
@@ -492,12 +571,6 @@ const validationsStore = new class {
492
571
  makeFormName(formName) {
493
572
  return `validation_${formName}`;
494
573
  }
495
- resetForm(formName) {
496
- const state = this.getFormStore(formName);
497
- state.fields.forEach((field) => {
498
- this.setFieldValue(formName, field, "");
499
- });
500
- }
501
574
  }();
502
575
  function useFormSelector(formName, selector, comparator) {
503
576
  return usePropsSelector(
@@ -541,6 +614,7 @@ const Form = ({
541
614
  initialValues,
542
615
  onUnmount,
543
616
  onValidate,
617
+ persistToSessionStorage,
544
618
  unregisterOnUnmount
545
619
  }) => {
546
620
  const actualName = React.useMemo(
@@ -581,7 +655,8 @@ const Form = ({
581
655
  useMount(() => {
582
656
  validationsStore.registerForm(actualName, {
583
657
  avoidFieldsOverride: avoidFieldsOverride ?? initialValues !== void 0,
584
- initialValues
658
+ initialValues,
659
+ persistToSessionStorage
585
660
  });
586
661
  setHasRegistered(true);
587
662
  });
@@ -841,7 +916,7 @@ const FieldWrapper = ({
841
916
  ] });
842
917
  }
843
918
  return /* @__PURE__ */ jsxs(Box, { className: `fieldWrapper ${className ?? ""}`, ...props, as, children: [
844
- /* @__PURE__ */ jsxs(Box, { as: "span", children: [
919
+ /* @__PURE__ */ jsxs(Box, { as: "span", className: "fieldWrapper__label", children: [
845
920
  window.SHOW_REQUIRED_POSITION !== "0" && label && /* @__PURE__ */ jsx(RequiredMark, { isRequired }),
846
921
  label,
847
922
  !avoidSemicolon && label && /* @__PURE__ */ jsx("abbr", { className: "semicolon", children: ": " }),
@@ -1962,7 +2037,7 @@ const _CollectorLabel = class _CollectorLabel extends CollectorField {
1962
2037
  __publicField$2(this, "type", "label");
1963
2038
  __publicField$2(this, "Component", () => {
1964
2039
  if (this.kind === "label")
1965
- return /* @__PURE__ */ jsx(Box, { as: this.kind, children: /* @__PURE__ */ jsx("span", { children: this.label }) });
2040
+ return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
1966
2041
  return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
1967
2042
  });
1968
2043
  }
@@ -2032,14 +2107,18 @@ class Collector {
2032
2107
  });
2033
2108
  __publicField(this, "private__id", `Collector${maxId++}`);
2034
2109
  __privateAdd(this, _fields, {});
2035
- __publicField(this, "add", (field) => {
2036
- __privateGet(this, _fields)[field.name] = field;
2037
- field.collector = this;
2110
+ __publicField(this, "add", (...fields) => {
2111
+ fields.forEach((field) => {
2112
+ __privateGet(this, _fields)[field.name] = field;
2113
+ field.collector = this;
2114
+ });
2115
+ return this;
2038
2116
  });
2039
2117
  __publicField(this, "forEach", (cb) => {
2040
2118
  Object.values(__privateGet(this, _fields)).forEach(cb);
2041
2119
  });
2042
2120
  __privateAdd(this, _Component, ({
2121
+ hideConfirm,
2043
2122
  resolve
2044
2123
  }) => {
2045
2124
  const modal = useModalContext();
@@ -2060,7 +2139,7 @@ class Collector {
2060
2139
  hasValidated = true;
2061
2140
  return /* @__PURE__ */ jsx(current.SuperComponent, {}, current.name);
2062
2141
  }),
2063
- hasValidated && /* @__PURE__ */ jsx(Box, { className: "collector__buttons", children: /* @__PURE__ */ jsx(Button, { variant: "outline", children: getLabel("btnCon").text }) })
2142
+ hasValidated && !hideConfirm && /* @__PURE__ */ jsx(Box, { className: "collector__buttons", children: /* @__PURE__ */ jsx(Button, { variant: "outline", children: getLabel("btnCon").text }) })
2064
2143
  ]
2065
2144
  }
2066
2145
  ) });
@@ -2068,7 +2147,7 @@ class Collector {
2068
2147
  __publicField(this, "show", (modalProps) => {
2069
2148
  return new Promise((resolve) => {
2070
2149
  const C = __privateGet(this, _Component);
2071
- new ApiaUtil().modals.open({
2150
+ ApiaUtil.instance.modals.open({
2072
2151
  onExited() {
2073
2152
  {
2074
2153
  resolve(null);
@@ -2077,6 +2156,7 @@ class Collector {
2077
2156
  children: /* @__PURE__ */ jsx(
2078
2157
  C,
2079
2158
  {
2159
+ hideConfirm: modalProps?.hideConfirm,
2080
2160
  resolve: (values) => {
2081
2161
  resolve(values);
2082
2162
  }