@wix/form-public 0.46.0 → 0.48.0

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.d.cts CHANGED
@@ -4,6 +4,7 @@ export { FormValues } from './_tsup-dts-rollup.cjs';
4
4
  export { CallingCountryCode } from './_tsup-dts-rollup.cjs';
5
5
  export { useForm } from './_tsup-dts-rollup.cjs';
6
6
  export { FormProvider } from './_tsup-dts-rollup.cjs';
7
+ export { Currency } from './_tsup-dts-rollup.cjs';
7
8
  export { PhoneInputProps } from './_tsup-dts-rollup.cjs';
8
9
  export { MultilineAddressProps } from './_tsup-dts-rollup.cjs';
9
10
  export { TextAreaProps } from './_tsup-dts-rollup.cjs';
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { FormValues } from './_tsup-dts-rollup.js';
4
4
  export { CallingCountryCode } from './_tsup-dts-rollup.js';
5
5
  export { useForm } from './_tsup-dts-rollup.js';
6
6
  export { FormProvider } from './_tsup-dts-rollup.js';
7
+ export { Currency } from './_tsup-dts-rollup.js';
7
8
  export { PhoneInputProps } from './_tsup-dts-rollup.js';
8
9
  export { MultilineAddressProps } from './_tsup-dts-rollup.js';
9
10
  export { TextAreaProps } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -1719,6 +1719,267 @@ var init_errors = __esm({
1719
1719
  }
1720
1720
  });
1721
1721
 
1722
+ // ../form-validator/dist/esm/lib/error-derivation/types.js
1723
+ var DateFieldErrorSuffix, DateTimeFieldErrorSuffix;
1724
+ var init_types = __esm({
1725
+ "../form-validator/dist/esm/lib/error-derivation/types.js"() {
1726
+ (function(DateFieldErrorSuffix2) {
1727
+ DateFieldErrorSuffix2["YEAR_MONTH"] = "year-month";
1728
+ DateFieldErrorSuffix2["YEAR_DAY"] = "year-day";
1729
+ DateFieldErrorSuffix2["MONTH_DAY"] = "month-day";
1730
+ DateFieldErrorSuffix2["YEAR"] = "year";
1731
+ DateFieldErrorSuffix2["MONTH"] = "month";
1732
+ DateFieldErrorSuffix2["DAY"] = "day";
1733
+ })(DateFieldErrorSuffix || (DateFieldErrorSuffix = {}));
1734
+ (function(DateTimeFieldErrorSuffix2) {
1735
+ DateTimeFieldErrorSuffix2["YEAR_MONTH_TIME"] = "year-month-time";
1736
+ DateTimeFieldErrorSuffix2["YEAR_DAY_TIME"] = "year-day-time";
1737
+ DateTimeFieldErrorSuffix2["MONTH_DAY_TIME"] = "month-day-time";
1738
+ DateTimeFieldErrorSuffix2["YEAR_TIME"] = "year-time";
1739
+ DateTimeFieldErrorSuffix2["MONTH_TIME"] = "month-time";
1740
+ DateTimeFieldErrorSuffix2["DAY_TIME"] = "day-time";
1741
+ DateTimeFieldErrorSuffix2["TIME"] = "time";
1742
+ })(DateTimeFieldErrorSuffix || (DateTimeFieldErrorSuffix = {}));
1743
+ }
1744
+ });
1745
+
1746
+ // ../form-validator/dist/esm/lib/error-derivation/helpers.js
1747
+ function getDecimalPlacesCount(multipleOf) {
1748
+ return String(multipleOf).split(".")?.[1]?.length || 0;
1749
+ }
1750
+ function deserializeDate(value) {
1751
+ if (!value) {
1752
+ return {};
1753
+ }
1754
+ const parts = value.split("-");
1755
+ if (parts.length === 3) {
1756
+ const [year, month, day] = parts;
1757
+ return {
1758
+ YEAR: year || void 0,
1759
+ MONTH: month || void 0,
1760
+ DAY: day || void 0
1761
+ };
1762
+ }
1763
+ return {};
1764
+ }
1765
+ function parseDateTimeValue(value) {
1766
+ if (!value) {
1767
+ return ["", ""];
1768
+ }
1769
+ const separator = value.indexOf("T") > -1 ? "T" : " ";
1770
+ const [date, time] = value.split(separator);
1771
+ return [date || "", time || ""];
1772
+ }
1773
+ function isTimeComplete(time) {
1774
+ if (!time) {
1775
+ return false;
1776
+ }
1777
+ const [hours, minutes] = time.split(":");
1778
+ return !!hours && !!minutes;
1779
+ }
1780
+ var init_helpers = __esm({
1781
+ "../form-validator/dist/esm/lib/error-derivation/helpers.js"() {
1782
+ }
1783
+ });
1784
+
1785
+ // ../form-validator/dist/esm/lib/error-derivation/error-derivers.js
1786
+ function deriveError(validation, error, options) {
1787
+ if (!error) {
1788
+ return error;
1789
+ }
1790
+ return derivePatternError(error) ?? deriveCharacterLengthError(validation, error) ?? deriveItemsCountError(validation, error) ?? deriveNumericValueError(validation, error) ?? deriveDateFieldError(validation, error, options?.value) ?? deriveDateTimeFieldError(validation, error, options?.value) ?? error;
1791
+ }
1792
+ function derivePatternError(error) {
1793
+ if (!error || error.errorType !== ErrorType.PATTERN_ERROR) {
1794
+ return void 0;
1795
+ }
1796
+ return {
1797
+ ...error,
1798
+ errorType: ErrorType.INVALID_VALUE_FOR_PATTERN_ERROR
1799
+ };
1800
+ }
1801
+ function deriveCharacterLengthError(validation, error) {
1802
+ if (!error) {
1803
+ return void 0;
1804
+ }
1805
+ const isMinMaxLengthError = error.errorType === ErrorType.MIN_LENGTH_ERROR || error.errorType === ErrorType.MAX_LENGTH_ERROR;
1806
+ if (!isMinMaxLengthError) {
1807
+ return void 0;
1808
+ }
1809
+ const minLength = validation?.string?.minLength;
1810
+ const maxLength = validation?.string?.maxLength;
1811
+ const hasMinMax = minLength && maxLength;
1812
+ if (hasMinMax && minLength === maxLength) {
1813
+ return {
1814
+ ...error,
1815
+ errorType: ErrorType.EXACT_CHARACTER_LENGTH_ERROR,
1816
+ errorMessage: `must have exactly ${minLength} characters`
1817
+ };
1818
+ }
1819
+ if (hasMinMax && minLength !== maxLength) {
1820
+ return {
1821
+ ...error,
1822
+ errorType: ErrorType.CHARACTER_LENGTH_RANGE_ERROR,
1823
+ errorMessage: `must have between ${minLength} and ${maxLength} characters`,
1824
+ params: {
1825
+ minLimit: minLength,
1826
+ maxLimit: maxLength
1827
+ }
1828
+ };
1829
+ }
1830
+ return void 0;
1831
+ }
1832
+ function deriveItemsCountError(validation, error) {
1833
+ if (!error) {
1834
+ return void 0;
1835
+ }
1836
+ const isMinMaxItemsError = error.errorType === ErrorType.MIN_ITEMS_ERROR || error.errorType === ErrorType.MAX_ITEMS_ERROR;
1837
+ if (!isMinMaxItemsError) {
1838
+ return void 0;
1839
+ }
1840
+ const minItems = validation?.array?.minItems;
1841
+ const maxItems = validation?.array?.maxItems;
1842
+ const hasMinMax = minItems && maxItems;
1843
+ if (hasMinMax && minItems === maxItems) {
1844
+ return {
1845
+ ...error,
1846
+ errorType: ErrorType.EXACT_ITEMS_NUMBER_ERROR,
1847
+ errorMessage: `must choose ${minItems} options`
1848
+ };
1849
+ }
1850
+ return void 0;
1851
+ }
1852
+ function deriveNumericValueError(validation, error) {
1853
+ if (!error) {
1854
+ return void 0;
1855
+ }
1856
+ const isMinMaxValueError = error.errorType === ErrorType.MIN_VALUE_ERROR || error.errorType === ErrorType.MAX_VALUE_ERROR;
1857
+ const isMultipleOfValueError = error.errorType === ErrorType.MULTIPLE_OF_VALUE_ERROR;
1858
+ if (isMinMaxValueError) {
1859
+ const { minPrice, maxPrice } = validation?.predefined?.paymentOptions?.products?.[0]?.dynamicPriceOptions ?? {};
1860
+ const minimum = validation?.number?.minimum ?? minPrice;
1861
+ const maximum = validation?.number?.maximum ?? maxPrice;
1862
+ const hasMinMax = minimum !== void 0 && maximum !== void 0;
1863
+ if (hasMinMax && minimum !== maximum) {
1864
+ return {
1865
+ ...error,
1866
+ errorType: ErrorType.VALUE_RANGE_ERROR,
1867
+ errorMessage: `must be from ${minimum} to ${maximum}`,
1868
+ params: {
1869
+ minLimit: minimum,
1870
+ maxLimit: maximum
1871
+ }
1872
+ };
1873
+ }
1874
+ }
1875
+ if (isMultipleOfValueError) {
1876
+ const multipleOf = validation?.number?.multipleOf;
1877
+ if (multipleOf) {
1878
+ const decimalPlaces = getDecimalPlacesCount(multipleOf);
1879
+ return {
1880
+ ...error,
1881
+ errorType: ErrorType.DECIMAL_POINT_ERROR,
1882
+ errorMessage: `must have ${decimalPlaces} number(s) after the decimal point`,
1883
+ params: {
1884
+ number: decimalPlaces
1885
+ }
1886
+ };
1887
+ }
1888
+ }
1889
+ return void 0;
1890
+ }
1891
+ function deriveDateFieldError(_validation, error, value) {
1892
+ const isFormatError = error?.errorType === ErrorType.FORMAT_ERROR;
1893
+ if (!error || !isFormatError) {
1894
+ return void 0;
1895
+ }
1896
+ const { YEAR: _YEAR, MONTH, DAY } = deserializeDate(value);
1897
+ const YEAR = _YEAR?.length === 4;
1898
+ if (!YEAR && !MONTH && !DAY) {
1899
+ return void 0;
1900
+ }
1901
+ const withErrorSuffix = (suffix) => ({
1902
+ ...error,
1903
+ errorType: ErrorType.INCOMPLETE_DATE_ERROR,
1904
+ params: {
1905
+ suffix
1906
+ }
1907
+ });
1908
+ if (!YEAR && !MONTH) {
1909
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR_MONTH);
1910
+ } else if (!YEAR && !DAY) {
1911
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR_DAY);
1912
+ } else if (!MONTH && !DAY) {
1913
+ return withErrorSuffix(DateFieldErrorSuffix.MONTH_DAY);
1914
+ } else if (!YEAR) {
1915
+ return withErrorSuffix(DateFieldErrorSuffix.YEAR);
1916
+ } else if (!MONTH) {
1917
+ return withErrorSuffix(DateFieldErrorSuffix.MONTH);
1918
+ } else if (!DAY) {
1919
+ return withErrorSuffix(DateFieldErrorSuffix.DAY);
1920
+ } else {
1921
+ return void 0;
1922
+ }
1923
+ }
1924
+ function deriveDateTimeFieldError(validation, error, value) {
1925
+ const isFormatError = error?.errorType === ErrorType.FORMAT_ERROR;
1926
+ if (!error || !isFormatError) {
1927
+ return void 0;
1928
+ }
1929
+ const [date, time] = parseDateTimeValue(value);
1930
+ const dateValidationError = deriveDateFieldError(validation, error, date);
1931
+ const { YEAR, MONTH, DAY } = deserializeDate(date);
1932
+ const timeComplete = isTimeComplete(time);
1933
+ if (!timeComplete && YEAR?.length !== 4 && !MONTH && !DAY) {
1934
+ return void 0;
1935
+ }
1936
+ if (!timeComplete) {
1937
+ const withErrorSuffix = (suffix) => ({
1938
+ ...error,
1939
+ errorType: ErrorType.INCOMPLETE_DATE_ERROR,
1940
+ params: {
1941
+ suffix
1942
+ }
1943
+ });
1944
+ const isDateFieldError = (validationError) => {
1945
+ return validationError?.errorType === ErrorType.INCOMPLETE_DATE_ERROR;
1946
+ };
1947
+ if (isDateFieldError(dateValidationError)) {
1948
+ switch (dateValidationError.params.suffix) {
1949
+ case DateFieldErrorSuffix.YEAR_MONTH:
1950
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_MONTH_TIME);
1951
+ case DateFieldErrorSuffix.YEAR_DAY:
1952
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_DAY_TIME);
1953
+ case DateFieldErrorSuffix.MONTH_DAY:
1954
+ return withErrorSuffix(DateTimeFieldErrorSuffix.MONTH_DAY_TIME);
1955
+ case DateFieldErrorSuffix.YEAR:
1956
+ return withErrorSuffix(DateTimeFieldErrorSuffix.YEAR_TIME);
1957
+ case DateFieldErrorSuffix.MONTH:
1958
+ return withErrorSuffix(DateTimeFieldErrorSuffix.MONTH_TIME);
1959
+ case DateFieldErrorSuffix.DAY:
1960
+ return withErrorSuffix(DateTimeFieldErrorSuffix.DAY_TIME);
1961
+ }
1962
+ }
1963
+ return withErrorSuffix(DateTimeFieldErrorSuffix.TIME);
1964
+ }
1965
+ return dateValidationError;
1966
+ }
1967
+ var init_error_derivers = __esm({
1968
+ "../form-validator/dist/esm/lib/error-derivation/error-derivers.js"() {
1969
+ init_errors();
1970
+ init_types();
1971
+ init_helpers();
1972
+ }
1973
+ });
1974
+
1975
+ // ../form-validator/dist/esm/lib/error-derivation/index.js
1976
+ var init_error_derivation = __esm({
1977
+ "../form-validator/dist/esm/lib/error-derivation/index.js"() {
1978
+ init_error_derivers();
1979
+ init_types();
1980
+ }
1981
+ });
1982
+
1722
1983
  // ../../node_modules/ajv/dist/compile/codegen/code.js
1723
1984
  var require_code = __commonJS({
1724
1985
  "../../node_modules/ajv/dist/compile/codegen/code.js"(exports) {
@@ -13315,6 +13576,7 @@ var init_form_validator = __esm({
13315
13576
  "../form-validator/dist/esm/lib/submission-validation/form-validator.js"() {
13316
13577
  init_errors();
13317
13578
  init_types_impl();
13579
+ init_error_derivation();
13318
13580
  init_create_ajv_schema_validator();
13319
13581
  init_form_schema_mapper();
13320
13582
  init_property_schema_mapper();
@@ -13415,10 +13677,11 @@ var init_form_validator = __esm({
13415
13677
  throw new Error(`Missing precompiled schema for form ${formId}`);
13416
13678
  }
13417
13679
  validate2(fieldSubmission);
13418
- return makeValidationErrors(validate2.errors);
13680
+ const baseErrors = makeValidationErrors(validate2.errors);
13681
+ return this.deriveErrors(formId, baseErrors, fieldSubmission);
13419
13682
  }
13420
13683
  validateSubmissionWithFieldMask(ajv, formId, fieldSubmission, fieldIdMasks) {
13421
- return fieldIdMasks.reduce((acc, mask) => {
13684
+ const baseErrors = fieldIdMasks.reduce((acc, mask) => {
13422
13685
  const validate2 = ajv.getSchema(`${formId}#/properties/${mask}`);
13423
13686
  if (!validate2) {
13424
13687
  acc.push({
@@ -13434,6 +13697,82 @@ var init_form_validator = __esm({
13434
13697
  }
13435
13698
  return acc;
13436
13699
  }, []);
13700
+ return this.deriveErrors(formId, baseErrors, fieldSubmission);
13701
+ }
13702
+ deriveErrors(formId, errors, submission) {
13703
+ return errors.map((error) => {
13704
+ const validation = this.getFieldValidation(formId, error.errorPath);
13705
+ const value = this.getFieldValue(submission, error.errorPath);
13706
+ return deriveError(validation, error, { value });
13707
+ }).filter((error) => error !== void 0);
13708
+ }
13709
+ getFieldValidation(formId, fieldPath) {
13710
+ const form = this.formSchemas.find((f) => f.id === formId);
13711
+ if (!form) {
13712
+ return void 0;
13713
+ }
13714
+ const pathParts = fieldPath.split("/");
13715
+ const rootFieldId = pathParts[0];
13716
+ const platformisedField = [
13717
+ ...form.formFields ?? [],
13718
+ ...form.deletedFormFields ?? []
13719
+ ].find((f) => {
13720
+ if (f.id === rootFieldId) {
13721
+ return true;
13722
+ }
13723
+ const inputTarget = f.inputOptions?.target;
13724
+ if (inputTarget === rootFieldId) {
13725
+ return true;
13726
+ }
13727
+ return false;
13728
+ });
13729
+ const legacyField = [
13730
+ ...form.fields ?? [],
13731
+ ...form.deletedFields ?? []
13732
+ ].find((f) => {
13733
+ if (f.id === rootFieldId || f.target === rootFieldId) {
13734
+ return true;
13735
+ }
13736
+ return false;
13737
+ });
13738
+ if (platformisedField) {
13739
+ return this.getPlatformisedFieldValidation(platformisedField);
13740
+ } else if (legacyField) {
13741
+ return this.getLegacyFieldValidation(legacyField);
13742
+ } else {
13743
+ return void 0;
13744
+ }
13745
+ }
13746
+ getLegacyFieldValidation(field) {
13747
+ return field.validation;
13748
+ }
13749
+ getPlatformisedFieldValidation(field) {
13750
+ const inputOptions = field.inputOptions;
13751
+ if (!inputOptions) {
13752
+ return void 0;
13753
+ }
13754
+ const validation = {};
13755
+ if (inputOptions.numberOptions?.validation) {
13756
+ validation.number = inputOptions.numberOptions.validation;
13757
+ }
13758
+ if (inputOptions.stringOptions?.validation) {
13759
+ validation.string = inputOptions.stringOptions.validation;
13760
+ }
13761
+ if (inputOptions.arrayOptions?.validation) {
13762
+ validation.array = inputOptions.arrayOptions.validation;
13763
+ }
13764
+ if (Object.keys(validation).length > 0) {
13765
+ return validation;
13766
+ }
13767
+ return void 0;
13768
+ }
13769
+ getFieldValue(submission, fieldPath) {
13770
+ const pathParts = fieldPath.split("/");
13771
+ let value = submission;
13772
+ for (const part of pathParts) {
13773
+ value = value?.[part];
13774
+ }
13775
+ return typeof value === "string" ? value : void 0;
13437
13776
  }
13438
13777
  isFormDisabled(formSchema) {
13439
13778
  const isDeadlinePast = formSchema?.limitationRule?.dateTimeDeadline && new Date(formSchema.limitationRule.dateTimeDeadline).getTime() <= (/* @__PURE__ */ new Date()).getTime();
@@ -13473,7 +13812,7 @@ var init_form_validator = __esm({
13473
13812
  });
13474
13813
 
13475
13814
  // ../form-validator/dist/esm/lib/backward-compatibility/field-utils.js
13476
- function isInputField(field) {
13815
+ function isInputField2(field) {
13477
13816
  if (field.validation) {
13478
13817
  asRequired(field.target, field, "target");
13479
13818
  }
@@ -13495,7 +13834,7 @@ function compareSchemaBackwardCompatibility(newSchema, knownSchema) {
13495
13834
  };
13496
13835
  }
13497
13836
  function getNonCompatibleFields(newSchema, knownSchema) {
13498
- newSchema.fields?.forEach(isInputField);
13837
+ newSchema.fields?.forEach(isInputField2);
13499
13838
  const newFieldsWithTarget = (newSchema.fields ?? []).filter(fieldHasTarget);
13500
13839
  const knownInputFields = [
13501
13840
  ...knownSchema.fields ?? [],
@@ -13854,6 +14193,8 @@ var init_form_schema_validator = __esm({
13854
14193
  var esm_exports = {};
13855
14194
  __export(esm_exports, {
13856
14195
  CALLING_COUNTRY_CODES: () => CALLING_COUNTRY_CODES,
14196
+ DateFieldErrorSuffix: () => DateFieldErrorSuffix,
14197
+ DateTimeFieldErrorSuffix: () => DateTimeFieldErrorSuffix,
13857
14198
  ErrorTypes: () => ErrorType,
13858
14199
  FILE_UPLOAD_FIELD_TYPE: () => FILE_UPLOAD_FIELD_TYPE,
13859
14200
  FormSchemaValidator: () => FormSchemaValidator,
@@ -13871,6 +14212,7 @@ var init_esm = __esm({
13871
14212
  init_form_schema_validator();
13872
14213
  init_errors();
13873
14214
  init_calling_country_codes();
14215
+ init_error_derivation();
13874
14216
  }
13875
14217
  });
13876
14218
 
@@ -15821,7 +16163,7 @@ function useFormFields() {
15821
16163
  var import_extends5 = __toESM(require_extends());
15822
16164
 
15823
16165
  // ../form-fields/dist/esm/ui/form/types/form-view.js
15824
- function isInputField2(field) {
16166
+ function isInputField(field) {
15825
16167
  return Boolean(field.target);
15826
16168
  }
15827
16169
  function isNestedFormField(field) {
@@ -16144,7 +16486,7 @@ var FormField = (_ref) => {
16144
16486
  FieldLayout
16145
16487
  }));
16146
16488
  }
16147
- if (isInputField2(field)) {
16489
+ if (isInputField(field)) {
16148
16490
  return /* @__PURE__ */ React14.createElement(InputField, (0, import_extends5.default)({}, rest, {
16149
16491
  field,
16150
16492
  FieldLayout
@@ -16236,7 +16578,7 @@ var DefaultFieldLayout = (_ref) => {
16236
16578
  layout,
16237
16579
  fieldType
16238
16580
  } = fieldView;
16239
- const dataHook = `form-field-${isInputField2(fieldView) ? fieldView.target : fieldView.id}`;
16581
+ const dataHook = `form-field-${isInputField(fieldView) ? fieldView.target : fieldView.id}`;
16240
16582
  const dataAttributes = {
16241
16583
  "data-hook": dataHook,
16242
16584
  "data-field-type": fieldType
@@ -21433,7 +21775,7 @@ var useForm3 = ({
21433
21775
  fieldsLayout
21434
21776
  } : null;
21435
21777
  };
21436
- var FormProvider = ({ children }) => {
21778
+ var FormProvider = ({ children, currency }) => {
21437
21779
  return /* @__PURE__ */ jsx(
21438
21780
  FormViewerProvider,
21439
21781
  {
@@ -21442,6 +21784,7 @@ var FormProvider = ({ children }) => {
21442
21784
  form: {},
21443
21785
  locale: "en",
21444
21786
  regionalFormat: "en",
21787
+ currency,
21445
21788
  WixRicosViewer: () => null,
21446
21789
  children: /* @__PURE__ */ jsx(ExternalDataProvider, { externalData: {}, children: /* @__PURE__ */ jsx(FormStatusProvider, { children }) })
21447
21790
  }