@apia/validations 4.0.9 → 4.0.13

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/dist/index.js CHANGED
@@ -1,11 +1,13 @@
1
1
  import { jsx, jsxs } from '@apia/theme/jsx-runtime';
2
- import { customEvents, propsStore, usePropsSelector, useUpdateEffect, useUnmount, useMount, PropsSelectorUndefinedObject, formatMessage, toBoolean, EventEmitter, StatefulEmitter, useSubscription, getLabel } from '@apia/util';
2
+ import { customEvents, usePropsSelector, propsStore, useUnmount, useMount, PropsSelectorUndefinedObject, formatMessage, toBoolean, EventEmitter, StatefulEmitter, useSubscription, getLabel, getValueByPath } from '@apia/util';
3
3
  import uniqueId from 'lodash-es/uniqueId';
4
- import React, { useState } from 'react';
5
- import { shallowEqual } from 'react-redux';
4
+ import React, { useState, createContext, useContext } from 'react';
6
5
  import { Box, getVariant, injectStyles, Input as Input$1, Label, Radio as Radio$1, Select as Select$1, Switch, Textarea as Textarea$1, Button } from '@apia/theme';
7
6
  import { notify } from '@apia/notifications';
8
- import { RequiredMark, Captcha, Checkbox as Checkbox$1, IconButton, getFieldErrorStyles, DateInput, ApiaUtil, NumberInput, SimpleButton, useModalContext } from '@apia/components';
7
+ import { RequiredMark, Captcha, Checkbox as Checkbox$1, IconButton, getFieldErrorStyles, DateInput, ApiaUtil, NumberInput, SimpleButton, useModalContext, FieldLabel } from '@apia/components';
8
+ import { shallowEqual } from 'react-redux';
9
+ import { makeObservable, observable } from 'mobx';
10
+ import { observer } from 'mobx-react-lite';
9
11
 
10
12
  const FormContext = React.createContext(
11
13
  {}
@@ -232,20 +234,20 @@ function getValidations(outerValidation) {
232
234
  };
233
235
  }
234
236
 
235
- var __defProp$9 = Object.defineProperty;
236
- var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
237
- var __publicField$9 = (obj, key, value) => {
238
- __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
237
+ var __defProp$a = Object.defineProperty;
238
+ var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
239
+ var __publicField$a = (obj, key, value) => {
240
+ __defNormalProp$a(obj, typeof key !== "symbol" ? key + "" : key, value);
239
241
  return value;
240
242
  };
241
243
  const validationsStore = new class {
242
244
  constructor() {
243
- __publicField$9(this, "activeForms", []);
244
- __publicField$9(this, "timeouts", {});
245
- __publicField$9(this, "existsField", (formName, fieldName) => {
245
+ __publicField$a(this, "activeForms", []);
246
+ __publicField$a(this, "timeouts", {});
247
+ __publicField$a(this, "existsField", (formName, fieldName) => {
246
248
  return !!this.getFormStore(formName)?.fields?.includes(fieldName);
247
249
  });
248
- __publicField$9(this, "registerField", (formName, registerData, options = { registerIfExists: true }) => {
250
+ __publicField$a(this, "registerField", (formName, registerData, options = { registerIfExists: true }) => {
249
251
  const formProps = this.getFormStore(formName);
250
252
  if (formProps === void 0)
251
253
  this.registerForm(formName);
@@ -275,7 +277,7 @@ const validationsStore = new class {
275
277
  /**
276
278
  * Inicializa el estado de un nuevo formulario
277
279
  */
278
- __publicField$9(this, "registerForm", (formName, props = { avoidFieldsOverride: false }) => {
280
+ __publicField$a(this, "registerForm", (formName, props = { avoidFieldsOverride: false }) => {
279
281
  props.persistToSessionStorage && this.loadFromSessionStorage(formName);
280
282
  if (this.activeForms.includes(formName)) {
281
283
  this.updateFormStore(
@@ -321,7 +323,7 @@ const validationsStore = new class {
321
323
  * Setea el valor del campo con el nombre provisto. Si el campo no existe
322
324
  * en el formulario lo crea.
323
325
  */
324
- __publicField$9(this, "setFieldValue", (formName, fieldName, newValue, options) => {
326
+ __publicField$a(this, "setFieldValue", (formName, fieldName, newValue, options) => {
325
327
  if (!this.getFieldStore(formName, fieldName))
326
328
  this.registerField(formName, { name: fieldName, value: newValue });
327
329
  else
@@ -337,7 +339,7 @@ const validationsStore = new class {
337
339
  if (this.getFormStore(formName).isValid)
338
340
  this.updateFormStore(formName, { isValid: false });
339
341
  });
340
- __publicField$9(this, "setFormIsTouched", (formName, isTouched = true) => {
342
+ __publicField$a(this, "setFormIsTouched", (formName, isTouched = true) => {
341
343
  this.updateFormStore(formName, { isTouched }, true);
342
344
  });
343
345
  /**
@@ -345,7 +347,7 @@ const validationsStore = new class {
345
347
  * forma cualquier validación que pudiera aplicarse sobre el mismo, ya no se
346
348
  * aplicará.
347
349
  */
348
- __publicField$9(this, "unregisterField", (formName, fieldName) => {
350
+ __publicField$a(this, "unregisterField", (formName, fieldName) => {
349
351
  if (this.getFormStore(formName))
350
352
  this.updateFormStore(formName, {
351
353
  fields: this.getFormStore(formName).fields.filter((current) => {
@@ -358,7 +360,7 @@ const validationsStore = new class {
358
360
  * Elimina un formulario del store de validación junto con todos los campos
359
361
  * asociados al mismo.
360
362
  */
361
- __publicField$9(this, "unregisterForm", (formName) => {
363
+ __publicField$a(this, "unregisterForm", (formName) => {
362
364
  this.getFormStore(formName)?.fields?.forEach((current) => {
363
365
  return propsStore.removeField(this.makeFieldName(formName, current));
364
366
  });
@@ -372,7 +374,7 @@ const validationsStore = new class {
372
374
  * Actualiza las propiedades de un campo, si no existe, lo registra en el
373
375
  * formulario.
374
376
  */
375
- __publicField$9(this, "updateField", (formName, field, options) => {
377
+ __publicField$a(this, "updateField", (formName, field, options) => {
376
378
  const previousValue = this.getFieldStore(formName, field.name)?.value;
377
379
  if (this.getFormStore(formName))
378
380
  this.updateFormStore(formName, { isValid: false }, true);
@@ -400,7 +402,7 @@ const validationsStore = new class {
400
402
  /**
401
403
  * Actualiza múltiples campos al mismo tiempo.
402
404
  */
403
- __publicField$9(this, "updateMultipleFields", (formName, fields, options) => {
405
+ __publicField$a(this, "updateMultipleFields", (formName, fields, options) => {
404
406
  fields.forEach((current) => {
405
407
  return this.updateField(formName, current, options);
406
408
  });
@@ -429,7 +431,7 @@ const validationsStore = new class {
429
431
  * console.error('Validation error', result);
430
432
  * }
431
433
  */
432
- __publicField$9(this, "validateForm", async (formName) => {
434
+ __publicField$a(this, "validateForm", async (formName) => {
433
435
  const state = this.getFormStore(formName);
434
436
  if (!state)
435
437
  return { errors: { form: "No form with name " + formName } };
@@ -627,21 +629,16 @@ const Form = ({
627
629
  // eslint-disable-next-line react-hooks/exhaustive-deps
628
630
  []
629
631
  );
630
- const validationResult = useFormSelector(
631
- actualName,
632
- (props) => props.validationResult,
633
- shallowEqual
634
- );
635
- useUpdateEffect(() => {
636
- if (validationResult && onValidate)
637
- onValidate(validationResult);
638
- }, [validationResult]);
639
632
  const handleSubmit = React.useCallback(
640
- (ev) => {
633
+ async (ev) => {
641
634
  ev.preventDefault();
642
- void validationsStore.validateForm(actualName);
635
+ const validationRes = await validationsStore.validateForm(
636
+ actualName
637
+ );
638
+ if (validationRes && onValidate)
639
+ onValidate(validationRes);
643
640
  },
644
- [actualName]
641
+ [actualName, onValidate]
645
642
  );
646
643
  const contextValue = React.useMemo(
647
644
  () => ({
@@ -1725,10 +1722,10 @@ const Textarea = React.forwardRef(
1725
1722
  );
1726
1723
  Textarea.displayName = "Textarea";
1727
1724
 
1728
- var __defProp$8 = Object.defineProperty;
1729
- var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1730
- var __publicField$8 = (obj, key, value) => {
1731
- __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
1725
+ var __defProp$9 = Object.defineProperty;
1726
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1727
+ var __publicField$9 = (obj, key, value) => {
1728
+ __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
1732
1729
  return value;
1733
1730
  };
1734
1731
  var __accessCheck$2 = (obj, member, msg) => {
@@ -1753,17 +1750,17 @@ class CollectorField extends EventEmitter {
1753
1750
  __privateAdd$2(this, _emitter, new StatefulEmitter({
1754
1751
  isHidden: false
1755
1752
  }));
1756
- __publicField$8(this, "collector", {});
1757
- __publicField$8(this, "isHidden", () => __privateGet$2(this, _emitter).getState("isHidden"));
1758
- __publicField$8(this, "setIsHidden", (isHidden) => {
1753
+ __publicField$9(this, "collector", {});
1754
+ __publicField$9(this, "isHidden", () => __privateGet$2(this, _emitter).getState("isHidden"));
1755
+ __publicField$9(this, "setIsHidden", (isHidden) => {
1759
1756
  __privateGet$2(this, _emitter).emit("isHidden", isHidden);
1760
1757
  });
1761
- __publicField$8(this, "properties", {});
1762
- __publicField$8(this, "setProperties", (newProps) => {
1758
+ __publicField$9(this, "properties", {});
1759
+ __publicField$9(this, "setProperties", (newProps) => {
1763
1760
  this.properties = newProps;
1764
1761
  this.emit("propsUpdate", { ...this.properties });
1765
1762
  });
1766
- __publicField$8(this, "useProperties", () => {
1763
+ __publicField$9(this, "useProperties", () => {
1767
1764
  return useSubscription({
1768
1765
  getInitialState: () => this.properties,
1769
1766
  makeSubscription: (setProps) => this.on("propsUpdate", setProps)
@@ -1773,7 +1770,7 @@ class CollectorField extends EventEmitter {
1773
1770
  * Este componente se utiliza para controlar propiedades comunes a todos los
1774
1771
  * campos.
1775
1772
  */
1776
- __publicField$8(this, "SuperComponent", () => {
1773
+ __publicField$9(this, "SuperComponent", () => {
1777
1774
  if (__privateGet$2(this, _emitter).useState("isHidden"))
1778
1775
  return null;
1779
1776
  return /* @__PURE__ */ jsx(Box, { className: `collector__field ${this.type}`, children: /* @__PURE__ */ jsx(this.Component, {}) });
@@ -1785,10 +1782,10 @@ class CollectorField extends EventEmitter {
1785
1782
  }
1786
1783
  _emitter = new WeakMap();
1787
1784
 
1788
- var __defProp$7 = Object.defineProperty;
1789
- var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1790
- var __publicField$7 = (obj, key, value) => {
1791
- __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
1785
+ var __defProp$8 = Object.defineProperty;
1786
+ var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1787
+ var __publicField$8 = (obj, key, value) => {
1788
+ __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
1792
1789
  return value;
1793
1790
  };
1794
1791
  class ValidatedCollectorField extends CollectorField {
@@ -1797,21 +1794,21 @@ class ValidatedCollectorField extends CollectorField {
1797
1794
  this.name = name;
1798
1795
  this.label = label;
1799
1796
  this.config = config;
1800
- __publicField$7(this, "setInitialValue", (newValue) => {
1797
+ __publicField$8(this, "setInitialValue", (newValue) => {
1801
1798
  this.config.initialValue = newValue;
1802
1799
  });
1803
- __publicField$7(this, "setValidationFunction", (newFunction) => {
1800
+ __publicField$8(this, "setValidationFunction", (newFunction) => {
1804
1801
  this.config.validationFunction = newFunction;
1805
1802
  this.emit("functionUpdate", this.config.validationFunction);
1806
1803
  });
1807
- __publicField$7(this, "setValidationRules", (newRules) => {
1804
+ __publicField$8(this, "setValidationRules", (newRules) => {
1808
1805
  this.config.validationRules = newRules;
1809
1806
  this.emit("rulesUpdate", { ...this.config.validationRules });
1810
1807
  });
1811
- __publicField$7(this, "setValue", (newValue) => {
1808
+ __publicField$8(this, "setValue", (newValue) => {
1812
1809
  validationsStore.setFieldValue(this.collector.id, this.name, newValue);
1813
1810
  });
1814
- __publicField$7(this, "useValidationFunction", () => {
1811
+ __publicField$8(this, "useValidationFunction", () => {
1815
1812
  return useSubscription({
1816
1813
  getInitialState: () => () => this.config.validationFunction,
1817
1814
  makeSubscription: (setValidationFunction) => {
@@ -1819,7 +1816,7 @@ class ValidatedCollectorField extends CollectorField {
1819
1816
  }
1820
1817
  });
1821
1818
  });
1822
- __publicField$7(this, "useValidationRules", () => {
1819
+ __publicField$8(this, "useValidationRules", () => {
1823
1820
  return useSubscription({
1824
1821
  getInitialState: () => this.config.validationRules,
1825
1822
  makeSubscription: (setValidationRules) => {
@@ -1836,17 +1833,17 @@ class ValidatedCollectorField extends CollectorField {
1836
1833
  }
1837
1834
  }
1838
1835
 
1839
- var __defProp$6 = Object.defineProperty;
1840
- var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1841
- var __publicField$6 = (obj, key, value) => {
1842
- __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
1836
+ var __defProp$7 = Object.defineProperty;
1837
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1838
+ var __publicField$7 = (obj, key, value) => {
1839
+ __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
1843
1840
  return value;
1844
1841
  };
1845
1842
  class CollectorCheckbox extends ValidatedCollectorField {
1846
1843
  constructor() {
1847
1844
  super(...arguments);
1848
- __publicField$6(this, "type", "checkbox");
1849
- __publicField$6(this, "Component", () => {
1845
+ __publicField$7(this, "type", "checkbox");
1846
+ __publicField$7(this, "Component", () => {
1850
1847
  const rules = this.useValidationRules();
1851
1848
  const validationFunction = this.useValidationFunction();
1852
1849
  const props = this.useProperties();
@@ -1866,17 +1863,17 @@ class CollectorCheckbox extends ValidatedCollectorField {
1866
1863
  }
1867
1864
  }
1868
1865
 
1869
- var __defProp$5 = Object.defineProperty;
1870
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1871
- var __publicField$5 = (obj, key, value) => {
1872
- __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
1866
+ var __defProp$6 = Object.defineProperty;
1867
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1868
+ var __publicField$6 = (obj, key, value) => {
1869
+ __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
1873
1870
  return value;
1874
1871
  };
1875
1872
  class CollectorInput extends ValidatedCollectorField {
1876
1873
  constructor() {
1877
1874
  super(...arguments);
1878
- __publicField$5(this, "type", "input");
1879
- __publicField$5(this, "Component", () => {
1875
+ __publicField$6(this, "type", "input");
1876
+ __publicField$6(this, "Component", () => {
1880
1877
  const rules = this.useValidationRules();
1881
1878
  const validationFunction = this.useValidationFunction();
1882
1879
  const props = this.useProperties();
@@ -1895,10 +1892,10 @@ class CollectorInput extends ValidatedCollectorField {
1895
1892
  }
1896
1893
  }
1897
1894
 
1898
- var __defProp$4 = Object.defineProperty;
1899
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1900
- var __publicField$4 = (obj, key, value) => {
1901
- __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
1895
+ var __defProp$5 = Object.defineProperty;
1896
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1897
+ var __publicField$5 = (obj, key, value) => {
1898
+ __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
1902
1899
  return value;
1903
1900
  };
1904
1901
  var __accessCheck$1 = (obj, member, msg) => {
@@ -1916,7 +1913,7 @@ var __privateAdd$1 = (obj, member, value) => {
1916
1913
  };
1917
1914
  var __privateSet = (obj, member, value, setter) => {
1918
1915
  __accessCheck$1(obj, member, "write to private field");
1919
- setter ? setter.call(obj, value) : member.set(obj, value);
1916
+ member.set(obj, value);
1920
1917
  return value;
1921
1918
  };
1922
1919
  var _options, _useOptions;
@@ -1925,13 +1922,13 @@ class CollectorSelect extends ValidatedCollectorField {
1925
1922
  super(_name, label, config);
1926
1923
  this._name = _name;
1927
1924
  this.label = label;
1928
- __publicField$4(this, "type", "select");
1925
+ __publicField$5(this, "type", "select");
1929
1926
  __privateAdd$1(this, _options, []);
1930
- __publicField$4(this, "addOption", (newOption) => {
1927
+ __publicField$5(this, "addOption", (newOption) => {
1931
1928
  __privateSet(this, _options, [...__privateGet$1(this, _options), newOption]);
1932
1929
  this.emit("optionsUpdate", __privateGet$1(this, _options));
1933
1930
  });
1934
- __publicField$4(this, "setOptions", (newOptions) => {
1931
+ __publicField$5(this, "setOptions", (newOptions) => {
1935
1932
  __privateSet(this, _options, [...newOptions]);
1936
1933
  this.emit("optionsUpdate", __privateGet$1(this, _options));
1937
1934
  });
@@ -1942,7 +1939,7 @@ class CollectorSelect extends ValidatedCollectorField {
1942
1939
  });
1943
1940
  return options;
1944
1941
  });
1945
- __publicField$4(this, "Component", () => {
1942
+ __publicField$5(this, "Component", () => {
1946
1943
  const rules = this.useValidationRules();
1947
1944
  const validationFunction = this.useValidationFunction();
1948
1945
  const options = __privateGet$1(this, _useOptions).call(this);
@@ -1968,17 +1965,17 @@ class CollectorSelect extends ValidatedCollectorField {
1968
1965
  _options = new WeakMap();
1969
1966
  _useOptions = new WeakMap();
1970
1967
 
1971
- var __defProp$3 = Object.defineProperty;
1972
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1973
- var __publicField$3 = (obj, key, value) => {
1974
- __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
1968
+ var __defProp$4 = Object.defineProperty;
1969
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1970
+ var __publicField$4 = (obj, key, value) => {
1971
+ __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
1975
1972
  return value;
1976
1973
  };
1977
1974
  class CollectorTextarea extends ValidatedCollectorField {
1978
1975
  constructor() {
1979
1976
  super(...arguments);
1980
- __publicField$3(this, "type", "textarea");
1981
- __publicField$3(this, "Component", () => {
1977
+ __publicField$4(this, "type", "textarea");
1978
+ __publicField$4(this, "Component", () => {
1982
1979
  const rules = this.useValidationRules();
1983
1980
  const validationFunction = this.useValidationFunction();
1984
1981
  const props = this.useProperties();
@@ -1997,31 +1994,31 @@ class CollectorTextarea extends ValidatedCollectorField {
1997
1994
  }
1998
1995
  }
1999
1996
 
2000
- var __defProp$2 = Object.defineProperty;
2001
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2002
- var __publicField$2 = (obj, key, value) => {
2003
- __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
1997
+ var __defProp$3 = Object.defineProperty;
1998
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1999
+ var __publicField$3 = (obj, key, value) => {
2000
+ __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
2004
2001
  return value;
2005
2002
  };
2006
2003
  const _CollectorLabel = class _CollectorLabel extends CollectorField {
2007
2004
  constructor(label, kind = "label") {
2008
2005
  super(`label${_CollectorLabel.maxId++}`, label);
2009
2006
  this.kind = kind;
2010
- __publicField$2(this, "type", "label");
2011
- __publicField$2(this, "Component", () => {
2007
+ __publicField$3(this, "type", "label");
2008
+ __publicField$3(this, "Component", () => {
2012
2009
  if (this.kind === "label")
2013
2010
  return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
2014
2011
  return /* @__PURE__ */ jsx(Box, { as: this.kind, children: this.label });
2015
2012
  });
2016
2013
  }
2017
2014
  };
2018
- __publicField$2(_CollectorLabel, "maxId", 0);
2015
+ __publicField$3(_CollectorLabel, "maxId", 0);
2019
2016
  let CollectorLabel = _CollectorLabel;
2020
2017
 
2021
- var __defProp$1 = Object.defineProperty;
2022
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2023
- var __publicField$1 = (obj, key, value) => {
2024
- __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
2018
+ var __defProp$2 = Object.defineProperty;
2019
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2020
+ var __publicField$2 = (obj, key, value) => {
2021
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
2025
2022
  return value;
2026
2023
  };
2027
2024
  let i = 0;
@@ -2029,18 +2026,18 @@ class CollectorButton extends CollectorField {
2029
2026
  constructor(label, properties) {
2030
2027
  super(`Button${i++}`, label, properties);
2031
2028
  this.label = label;
2032
- __publicField$1(this, "type", "button");
2033
- __publicField$1(this, "Component", () => {
2029
+ __publicField$2(this, "type", "button");
2030
+ __publicField$2(this, "Component", () => {
2034
2031
  const props = this.useProperties();
2035
2032
  return /* @__PURE__ */ jsx(SimpleButton, { ...props, children: this.label });
2036
2033
  });
2037
2034
  }
2038
2035
  }
2039
2036
 
2040
- var __defProp = Object.defineProperty;
2041
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2042
- var __publicField = (obj, key, value) => {
2043
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
2037
+ var __defProp$1 = Object.defineProperty;
2038
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2039
+ var __publicField$1 = (obj, key, value) => {
2040
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
2044
2041
  return value;
2045
2042
  };
2046
2043
  var __accessCheck = (obj, member, msg) => {
@@ -2066,7 +2063,7 @@ try {
2066
2063
  let maxId = 0;
2067
2064
  class Collector {
2068
2065
  constructor() {
2069
- __publicField(this, "fields", {
2066
+ __publicField$1(this, "fields", {
2070
2067
  button: CollectorButton,
2071
2068
  checkbox: CollectorCheckbox,
2072
2069
  input: CollectorInput,
@@ -2078,17 +2075,17 @@ class Collector {
2078
2075
  select: CollectorSelect,
2079
2076
  textarea: CollectorTextarea
2080
2077
  });
2081
- __publicField(this, "private__id", `Collector${maxId++}`);
2082
- __publicField(this, "modal", null);
2078
+ __publicField$1(this, "private__id", `Collector${maxId++}`);
2079
+ __publicField$1(this, "modal", null);
2083
2080
  __privateAdd(this, _fields, {});
2084
- __publicField(this, "add", (...fields) => {
2081
+ __publicField$1(this, "add", (...fields) => {
2085
2082
  fields.forEach((field) => {
2086
2083
  __privateGet(this, _fields)[field.name] = field;
2087
2084
  field.collector = this;
2088
2085
  });
2089
2086
  return this;
2090
2087
  });
2091
- __publicField(this, "forEach", (cb) => {
2088
+ __publicField$1(this, "forEach", (cb) => {
2092
2089
  Object.values(__privateGet(this, _fields)).forEach(cb);
2093
2090
  });
2094
2091
  __privateAdd(this, _Component, ({
@@ -2127,7 +2124,7 @@ class Collector {
2127
2124
  }
2128
2125
  );
2129
2126
  });
2130
- __publicField(this, "show", (modalProps) => {
2127
+ __publicField$1(this, "show", (modalProps) => {
2131
2128
  const { onConfirm, ...actualModalProps } = modalProps ?? {};
2132
2129
  return new Promise((resolve) => {
2133
2130
  const C = __privateGet(this, _Component);
@@ -2173,5 +2170,256 @@ class Collector {
2173
2170
  _fields = new WeakMap();
2174
2171
  _Component = new WeakMap();
2175
2172
 
2176
- export { InnerCaptcha as Captcha, Checkbox, Collector, CollectorField, DeletableInput, FieldWrapper, FileInput, Form, FormContext, Input, NumberInputf, Radio, Select, Switchbox, Textarea, ValidatedCollectorField, ValidationError, classToValidate, classToValidationFunction, hasSucceedFieldValidation, hasSucceedFormValidation, initValidations, lang, useField, useFieldSelector, useFieldStatesClassNames, useFieldValue, useFormContext, useFormSelector, useValidationClass, validationsStore };
2173
+ const ValidableContext = createContext(
2174
+ void 0
2175
+ );
2176
+ function useValidableContext() {
2177
+ const context = useContext(ValidableContext);
2178
+ return context;
2179
+ }
2180
+
2181
+ function setValueByPath(obj, path, value) {
2182
+ if (path !== "") {
2183
+ const steps = path.split(".");
2184
+ if (steps.length === 0) {
2185
+ console.warn(`An empty path was provoided ${path}`);
2186
+ return null;
2187
+ }
2188
+ if (steps.length === 1) {
2189
+ obj[steps[0]] = value;
2190
+ } else {
2191
+ const step = steps.shift();
2192
+ if (!obj[step])
2193
+ obj[step] = {};
2194
+ setValueByPath(obj[step], steps.join("."), value);
2195
+ }
2196
+ }
2197
+ }
2198
+
2199
+ const ValidableField = observer(
2200
+ ({
2201
+ children,
2202
+ context,
2203
+ label,
2204
+ path,
2205
+ ...props
2206
+ }) => {
2207
+ const reactContext = useValidableContext();
2208
+ const actualContext = context ?? reactContext;
2209
+ if (!actualContext)
2210
+ throw new Error("ValidableContext not found");
2211
+ const validations = actualContext.getValidations(path);
2212
+ const error = actualContext.getError(path);
2213
+ return /* @__PURE__ */ jsx(
2214
+ FieldLabel,
2215
+ {
2216
+ ...props,
2217
+ error,
2218
+ label: label ?? validations?.fieldLabel ?? `Label not provided: ${path}`,
2219
+ required: validations?.required,
2220
+ children
2221
+ }
2222
+ );
2223
+ }
2224
+ );
2225
+
2226
+ function makeValidableField(Component) {
2227
+ const ActualComponent = observer(Component);
2228
+ const NewValidableField = (props) => {
2229
+ const reactContext = useValidableContext();
2230
+ const actualContext = props.context ?? reactContext;
2231
+ if (!actualContext)
2232
+ throw new Error("ValidableContext not found");
2233
+ return /* @__PURE__ */ jsx(ActualComponent, { ...props, context: actualContext });
2234
+ };
2235
+ return NewValidableField;
2236
+ }
2237
+
2238
+ const ValidableInput = makeValidableField(
2239
+ ({ type, context, label, path, onChange, ...props }) => {
2240
+ const handleChange = (event) => {
2241
+ context.setValue(path, event.target.value);
2242
+ onChange?.(event);
2243
+ };
2244
+ return /* @__PURE__ */ jsx(
2245
+ ValidableField,
2246
+ {
2247
+ context,
2248
+ path,
2249
+ label,
2250
+ ...props,
2251
+ className: `input ${props.className}`,
2252
+ children: /* @__PURE__ */ jsx(
2253
+ Input$1,
2254
+ {
2255
+ name: path,
2256
+ type,
2257
+ value: context.getValue(path),
2258
+ onChange: handleChange
2259
+ }
2260
+ )
2261
+ }
2262
+ );
2263
+ }
2264
+ );
2265
+
2266
+ const ValidableSelect = makeValidableField(({ context, label, path, onChange, options, ...props }) => {
2267
+ const handleChange = (event) => {
2268
+ context.setValue(path, event.target.value);
2269
+ onChange?.(event);
2270
+ };
2271
+ return /* @__PURE__ */ jsx(
2272
+ ValidableField,
2273
+ {
2274
+ context,
2275
+ path,
2276
+ label,
2277
+ ...props,
2278
+ className: `select ${props.className}`,
2279
+ children: /* @__PURE__ */ jsx(
2280
+ Select$1,
2281
+ {
2282
+ name: path,
2283
+ value: context.getValue(path),
2284
+ onChange: handleChange,
2285
+ children: options.map((option) => /* @__PURE__ */ jsx(
2286
+ "option",
2287
+ {
2288
+ selected: option.selected,
2289
+ value: option.value,
2290
+ children: option.label
2291
+ },
2292
+ option.value
2293
+ ))
2294
+ }
2295
+ )
2296
+ }
2297
+ );
2298
+ });
2299
+
2300
+ const ValidableTextarea = makeValidableField(
2301
+ ({ context, label, path, onChange, ...props }) => {
2302
+ const handleChange = (event) => {
2303
+ context.setValue(path, event.target.value);
2304
+ onChange?.(event);
2305
+ };
2306
+ return /* @__PURE__ */ jsx(
2307
+ ValidableField,
2308
+ {
2309
+ context,
2310
+ path,
2311
+ label,
2312
+ ...props,
2313
+ className: `textarea ${props.className}`,
2314
+ children: /* @__PURE__ */ jsx(
2315
+ Textarea$1,
2316
+ {
2317
+ name: path,
2318
+ value: context.getValue(path),
2319
+ onChange: handleChange
2320
+ }
2321
+ )
2322
+ }
2323
+ );
2324
+ }
2325
+ );
2326
+
2327
+ const ValidableCheck = makeValidableField(
2328
+ ({ context, label, path, onChange, ...props }) => {
2329
+ const handleChange = (event) => {
2330
+ context.setValue(path, event.target.value);
2331
+ onChange?.(event);
2332
+ };
2333
+ return /* @__PURE__ */ jsx(
2334
+ ValidableField,
2335
+ {
2336
+ context,
2337
+ path,
2338
+ avoidSemicolon: true,
2339
+ label,
2340
+ ...props,
2341
+ className: `checkbox ${props.className}`,
2342
+ children: /* @__PURE__ */ jsx(
2343
+ Checkbox$1,
2344
+ {
2345
+ name: path,
2346
+ checked: context.getValue(path),
2347
+ onChange: handleChange
2348
+ }
2349
+ )
2350
+ }
2351
+ );
2352
+ }
2353
+ );
2354
+
2355
+ var __defProp = Object.defineProperty;
2356
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2357
+ var __publicField = (obj, key, value) => {
2358
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
2359
+ return value;
2360
+ };
2361
+ async function validate(target, validations) {
2362
+ const errors = {};
2363
+ let focus = void 0;
2364
+ for (const key in validations) {
2365
+ const validation = validations[key];
2366
+ const value = getValueByPath(target, key);
2367
+ if (validation.validationFunction) {
2368
+ const validationResult = await validation.validationFunction(value);
2369
+ if (validationResult !== true) {
2370
+ errors[key] = validationResult;
2371
+ }
2372
+ } else if (validation.required && !value) {
2373
+ errors[key] = validation.errorsLabels?.required ?? getLabel("msgReqField", { text: { TOK1: key } }).text;
2374
+ } else if (validation.minLength !== void 0 && value && String(value).length < validation.minLength) {
2375
+ errors[key] = validation.errorsLabels?.minLength ?? getLabel("msgMinLength", { text: { TOK1: key } }).text;
2376
+ } else if (validation.maxLength !== void 0 && value && String(value).length > validation.maxLength) {
2377
+ errors[key] = validation.errorsLabels?.maxLength ?? getLabel("msgMaxLength", { text: { TOK1: key } }).text;
2378
+ } else if (validation.min !== void 0 && value && Number(value) < validation.min) {
2379
+ errors[key] = validation.errorsLabels?.min ?? getLabel("msgMinValue", { text: { TOK1: key } }).text;
2380
+ } else if (validation.max !== void 0 && value && Number(value) > validation.max) {
2381
+ errors[key] = validation.errorsLabels?.max ?? getLabel("msgMaxValue", { text: { TOK1: key } }).text;
2382
+ } else if (validation.pattern && value && !new RegExp(validation.pattern).test(String(value))) {
2383
+ errors[key] = validation.errorsLabels?.pattern ?? getLabel("msgExpRegFal", { text: { TOK1: key } }).text;
2384
+ }
2385
+ if (errors[key] && !focus) {
2386
+ focus = validation.focus || (() => document.querySelector(`[name=${key.replace(/\./g, "\\.")}]`)?.focus());
2387
+ }
2388
+ }
2389
+ const hasErrors = Object.keys(errors).length > 0;
2390
+ hasErrors && focus?.();
2391
+ return hasErrors ? errors : true;
2392
+ }
2393
+ class Validable {
2394
+ constructor() {
2395
+ __publicField(this, "errors", {});
2396
+ __publicField(this, "Context", ({ children }) => {
2397
+ return /* @__PURE__ */ jsx(ValidableContext.Provider, { value: this, children });
2398
+ });
2399
+ makeObservable(this, {
2400
+ errors: observable
2401
+ });
2402
+ }
2403
+ getError(path) {
2404
+ return this.errors[path];
2405
+ }
2406
+ getValidations(path) {
2407
+ return getValueByPath(this.validations, path);
2408
+ }
2409
+ getValue(path) {
2410
+ return getValueByPath(this.state, path);
2411
+ }
2412
+ setValue(path, value) {
2413
+ setValueByPath(this.state, path, value);
2414
+ }
2415
+ async validate() {
2416
+ const result = await validate(this.state, this.validations);
2417
+ if (result !== true) {
2418
+ Object.assign(this.errors, result);
2419
+ }
2420
+ return result;
2421
+ }
2422
+ }
2423
+
2424
+ export { InnerCaptcha as Captcha, Checkbox, Collector, CollectorField, DeletableInput, FieldWrapper, FileInput, Form, FormContext, Input, NumberInputf, Radio, Select, Switchbox, Textarea, Validable, ValidableCheck, ValidableContext, ValidableField, ValidableInput, ValidableSelect, ValidableTextarea, ValidatedCollectorField, ValidationError, classToValidate, classToValidationFunction, hasSucceedFieldValidation, hasSucceedFormValidation, initValidations, lang, useField, useFieldSelector, useFieldStatesClassNames, useFieldValue, useFormContext, useFormSelector, useValidableContext, useValidationClass, validate, validationsStore };
2177
2425
  //# sourceMappingURL=index.js.map