@wix/form-public 0.549.0 → 0.551.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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ErrorType, ValidationMode } from '@wix/ambassador-forms-v1-validation-result/types';
2
2
  import { DayOfWeek, InputFieldInputType, ValidationFormat, RequiredIndicator as RequiredIndicator$1, RequiredIndicatorPlacement as RequiredIndicatorPlacement$1, PriceType, InputFieldPaymentComponentType, InputFieldNumberComponentType, InputFieldStringComponentType, FieldFieldType, Format as Format$1, SubmitSuccessAction as SubmitSuccessAction$1, ContactAutofill as ContactAutofill$1, UrlTargetEnumTarget, ArrayItemsItemType, OverrideEntityType as OverrideEntityType$1 } from '@wix/ambassador-forms-v4-form-http/types';
3
- import React172, { createContext, useRef, Component, createRef, Suspense, useMemo, useCallback, useState, useEffect, useContext, Children, isValidElement, cloneElement, forwardRef, useLayoutEffect, useImperativeHandle, Fragment, createElement, useId } from 'react';
3
+ import React172, { createContext, useRef, Component, createRef, Suspense, useMemo, useCallback, useState, useEffect, useContext, Children, isValidElement, cloneElement, forwardRef, useLayoutEffect, useImperativeHandle, Fragment, createElement, useId, useReducer } from 'react';
4
4
  import { initReactI18next } from 'react-i18next';
5
5
  import default2 from 'i18next';
6
6
  import ReactDOM from 'react-dom';
@@ -14763,12 +14763,25 @@ var init_form_validator = __esm({
14763
14763
  }))));
14764
14764
  const addressForms = this.getAddressForms(currentlyValidatableForm, submission);
14765
14765
  const objectArrayForms = this.getObjectArrayForms(currentlyValidatableForm);
14766
- return createAjvSchemaValidator([
14766
+ const ajv = createAjvSchemaValidator([
14767
14767
  ...this.formSchemas,
14768
14768
  ...nestedForms,
14769
14769
  ...addressForms,
14770
14770
  ...objectArrayForms
14771
14771
  ]);
14772
+ this.addPlaceholdersForMissingDeletedAddressForms(ajv, currentlyValidatableForm);
14773
+ return ajv;
14774
+ }
14775
+ // A deleted address field still references its address form. Without that
14776
+ // form the schema cannot compile, so accept any value for the field instead.
14777
+ addPlaceholdersForMissingDeletedAddressForms(ajv, form) {
14778
+ (form.deletedFields ?? []).filter((field) => field.target && field.validation?.predefined?.format === ValidationFormat.MULTILINE_ADDRESS).forEach((field) => {
14779
+ if (!ajv.getSchema(field.target)) {
14780
+ ajv.addSchema({
14781
+ $id: field.target
14782
+ });
14783
+ }
14784
+ });
14772
14785
  }
14773
14786
  // eslint-disable-next-line max-params
14774
14787
  constructor(formSchemas, validationMode, platformisedFieldBasedValidation = false, addressFormProvider = () => void 0, submissionValueProvider, lenientPhoneValidation = false) {
@@ -41046,15 +41059,15 @@ var getNormalizedColSpans = (cellsColSpans) => {
41046
41059
  var isLayoutFillObject = (value) => typeof value === "object" && value !== null;
41047
41060
  var isLayoutFillGradient = (value) => isLayoutFillObject(value);
41048
41061
  var isNormalizedLayoutFill = (value) => value?.type === "COLOR" ? typeof value.color === "string" && !!value.color : value?.type === "GRADIENT" ? isLayoutFillGradient(value.gradient) : false;
41049
- function resolveLayoutFill(fill, legacyColor) {
41050
- if (isNormalizedLayoutFill(fill)) {
41051
- return fill;
41062
+ function resolveLayoutFill(fill2, legacyColor) {
41063
+ if (isNormalizedLayoutFill(fill2)) {
41064
+ return fill2;
41052
41065
  }
41053
- const gradient = getLayoutFillGradient(fill);
41066
+ const gradient = getLayoutFillGradient(fill2);
41054
41067
  if (gradient) {
41055
41068
  return { type: "GRADIENT", gradient };
41056
41069
  }
41057
- const color = getLayoutFillColor(fill);
41070
+ const color = getLayoutFillColor(fill2);
41058
41071
  if (color !== void 0) {
41059
41072
  return { type: "COLOR", color };
41060
41073
  }
@@ -41063,10 +41076,10 @@ function resolveLayoutFill(fill, legacyColor) {
41063
41076
  }
41064
41077
  return void 0;
41065
41078
  }
41066
- var getLayoutFillColor = (fill) => fill && "color" in fill && typeof fill.color === "string" ? fill.color || void 0 : void 0;
41067
- var getLayoutFillGradient = (fill) => fill && "gradient" in fill && isLayoutFillGradient(fill.gradient) ? fill.gradient : void 0;
41068
- var getResolvedLayoutFillColor = (fill, legacyColor) => getLayoutFillColor(resolveLayoutFill(fill, legacyColor));
41069
- var getResolvedLayoutFillGradient = (fill, legacyColor) => getLayoutFillGradient(resolveLayoutFill(fill, legacyColor));
41079
+ var getLayoutFillColor = (fill2) => fill2 && "color" in fill2 && typeof fill2.color === "string" ? fill2.color || void 0 : void 0;
41080
+ var getLayoutFillGradient = (fill2) => fill2 && "gradient" in fill2 && isLayoutFillGradient(fill2.gradient) ? fill2.gradient : void 0;
41081
+ var getResolvedLayoutFillColor = (fill2, legacyColor) => getLayoutFillColor(resolveLayoutFill(fill2, legacyColor));
41082
+ var getResolvedLayoutFillGradient = (fill2, legacyColor) => getLayoutFillGradient(resolveLayoutFill(fill2, legacyColor));
41070
41083
 
41071
41084
  // ../../node_modules/@wix/ricos/dist/es/ricos-common-plugins/layout/section-dividers/divider-presets.js
41072
41085
  var DIVIDER_LAYERS_COUNT_MIN = 1;
@@ -48790,6 +48803,7 @@ function getSubmitFieldIdsV2(fields = []) {
48790
48803
  }
48791
48804
 
48792
48805
  // ../form-fields/dist/esm/services/form-view.js
48806
+ var FIELD_PATH_SEPARATOR = "/";
48793
48807
  function createFormView({ form, path = "", stepId, recursive = true, includeHiddenFields = false, isMobile: isMobile2 = false, breakpoint = DEFAULT_BREAKPOINT, useFieldsV2 = false, parentId = "" }) {
48794
48808
  const step = stepId ? form.steps?.find(({ id: id6 }) => id6 === stepId) : form.steps?.[0];
48795
48809
  const layout = step?.layout ?? {};
@@ -48872,7 +48886,7 @@ function createFieldView(field, layout, path, recursive, isMobile2, breakpoint =
48872
48886
  return;
48873
48887
  }
48874
48888
  const fieldTarget = field.target || "";
48875
- const fieldPath = path ? `${path}/${fieldTarget}` : fieldTarget;
48889
+ const fieldPath = path ? `${path}${FIELD_PATH_SEPARATOR}${fieldTarget}` : fieldTarget;
48876
48890
  const propertiesView = createPropertiesView(field, fieldPath, recursive, isMobile2);
48877
48891
  return {
48878
48892
  id: field.id,
@@ -48981,6 +48995,78 @@ function useAddressForms() {
48981
48995
  }
48982
48996
  return context;
48983
48997
  }
48998
+
48999
+ // ../form-fields/dist/esm/services/prehydration-values.js
49000
+ var isEmptyInputValue = (value) => value === void 0 || value === null || value === "";
49001
+ var isNested = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
49002
+ var fill = (values, [key, ...rest], text) => {
49003
+ const current = values[key];
49004
+ if (rest.length === 0) {
49005
+ return isEmptyInputValue(current) ? {
49006
+ ...values,
49007
+ [key]: text
49008
+ } : values;
49009
+ }
49010
+ if (!isEmptyInputValue(current) && !isNested(current)) {
49011
+ return values;
49012
+ }
49013
+ const nested = isNested(current) ? current : {};
49014
+ const filled = fill(nested, rest, text);
49015
+ return filled === nested ? values : {
49016
+ ...values,
49017
+ [key]: filled
49018
+ };
49019
+ };
49020
+ var read = (values, [key, ...rest]) => {
49021
+ const current = values[key];
49022
+ if (rest.length === 0) {
49023
+ return current;
49024
+ }
49025
+ return isNested(current) ? read(current, rest) : void 0;
49026
+ };
49027
+ var mergePrehydrationValues = (values, pending) => {
49028
+ let result2 = values;
49029
+ for (const [path, text] of pending) {
49030
+ result2 = fill(result2, path.split(FIELD_PATH_SEPARATOR), text);
49031
+ }
49032
+ return result2;
49033
+ };
49034
+ var prunePrehydrationValues = (values, pending) => {
49035
+ for (const path of pending.keys()) {
49036
+ if (!isEmptyInputValue(read(values, path.split(FIELD_PATH_SEPARATOR)))) {
49037
+ pending.delete(path);
49038
+ }
49039
+ }
49040
+ };
49041
+ var forgetPrehydrationValues = (pending, previousValues, nextValues) => {
49042
+ for (const path of pending.keys()) {
49043
+ const segments = path.split(FIELD_PATH_SEPARATOR);
49044
+ if (read(previousValues, segments) !== read(nextValues, segments)) {
49045
+ pending.delete(path);
49046
+ }
49047
+ }
49048
+ };
49049
+
49050
+ // ../form-fields/dist/esm/contexts/prehydration-values-context.js
49051
+ var PrehydrationValuesContext = /* @__PURE__ */ createContext(void 0);
49052
+ var useKeepPrehydrationValue = (ref) => {
49053
+ const registry = useContext(PrehydrationValuesContext);
49054
+ const { path, value } = useFieldPropsV2();
49055
+ const mountedFieldRef = useRef({
49056
+ path,
49057
+ value,
49058
+ registry
49059
+ });
49060
+ useEffect(() => {
49061
+ const field = mountedFieldRef.current;
49062
+ const domValue = ref.current?.value;
49063
+ if (field.registry && domValue && isEmptyInputValue(field.value)) {
49064
+ field.registry.current.set(field.path, domValue);
49065
+ }
49066
+ }, [
49067
+ ref
49068
+ ]);
49069
+ };
48984
49070
  var FieldInitialDataContext = createContext(void 0);
48985
49071
 
48986
49072
  // ../form-fields/dist/esm/fields/multiline-address/multiline-address/address.js
@@ -50709,7 +50795,7 @@ function autofillValues(form, values, userData) {
50709
50795
  }
50710
50796
 
50711
50797
  // ../form-viewer/dist/esm/hooks/use-form-values/use-form-values.js
50712
- var useFormValues = (form, values, onChange, userData, addressTemplates, enableAutofill) => {
50798
+ var useFormValues = (form, values, onChange, userData, addressTemplates, enableAutofill, prehydrationValues) => {
50713
50799
  const hasAutofilled = useRef(false);
50714
50800
  const normalizedValues = useMemo(() => {
50715
50801
  let filled = fillEmptyValues(form, values);
@@ -50725,17 +50811,35 @@ var useFormValues = (form, values, onChange, userData, addressTemplates, enableA
50725
50811
  addressTemplates,
50726
50812
  enableAutofill
50727
50813
  ]);
50814
+ const [, rerenderWithPrehydrationText] = useReducer((renders) => renders + 1, 0);
50815
+ const pendingCount = prehydrationValues?.current.size ?? 0;
50816
+ const renderedPendingCount = useRef(pendingCount);
50817
+ renderedPendingCount.current = pendingCount;
50818
+ const valuesWithPrehydrationText = useMemo(() => pendingCount > 0 && prehydrationValues ? mergePrehydrationValues(normalizedValues, prehydrationValues.current) : normalizedValues, [
50819
+ normalizedValues,
50820
+ pendingCount,
50821
+ prehydrationValues
50822
+ ]);
50728
50823
  useEffect(() => {
50729
- const hasAnyValueChanged = !deepEqual(normalizedValues, values);
50824
+ const pending = prehydrationValues?.current;
50825
+ if (pending?.size) {
50826
+ prunePrehydrationValues(values ?? {}, pending);
50827
+ }
50828
+ const merged = pending?.size ? mergePrehydrationValues(normalizedValues, pending) : normalizedValues;
50829
+ const hasAnyValueChanged = !deepEqual(merged, values);
50730
50830
  if (hasAnyValueChanged) {
50731
- onChange?.(normalizedValues);
50831
+ onChange?.(merged);
50832
+ }
50833
+ if ((pending?.size ?? 0) !== renderedPendingCount.current) {
50834
+ rerenderWithPrehydrationText();
50732
50835
  }
50733
50836
  }, [
50734
50837
  values,
50735
50838
  normalizedValues,
50736
- onChange
50839
+ onChange,
50840
+ prehydrationValues
50737
50841
  ]);
50738
- return normalizedValues;
50842
+ return valuesWithPrehydrationText;
50739
50843
  };
50740
50844
  var useFormView2 = (form, stepId, breakpoint) => {
50741
50845
  const { isMobile: isMobile2 } = useConfig();
@@ -51184,7 +51288,8 @@ var useForm2 = ({ form: paramForm, values: paramValues, stepId, onStepIdChange,
51184
51288
  ]);
51185
51289
  const formId = formWithOverrides.id;
51186
51290
  const { setFocusedFieldId } = useFocusedField();
51187
- const normalizedValues = useFormValues(formWithOverrides, paramValues, onChange, userData, addressTemplates, enableAutofill);
51291
+ const prehydrationValues = useRef(/* @__PURE__ */ new Map());
51292
+ const normalizedValues = useFormValues(formWithOverrides, paramValues, onChange, userData, addressTemplates, enableAutofill, prehydrationValues);
51188
51293
  const valuesRef = useRef(normalizedValues);
51189
51294
  valuesRef.current = normalizedValues;
51190
51295
  const errorsRef = useRef(errors);
@@ -51202,6 +51307,7 @@ var useForm2 = ({ form: paramForm, values: paramValues, stepId, onStepIdChange,
51202
51307
  [target]: newValue
51203
51308
  };
51204
51309
  const cleared = clearFieldsWhenHiddenByRule(paramForm, overrides, oldValues, newValues, oldErrors);
51310
+ forgetPrehydrationValues(prehydrationValues.current, oldValues, cleared.values);
51205
51311
  onChange(cleared.values);
51206
51312
  onValidate(cleared.errors);
51207
51313
  }
@@ -51354,7 +51460,8 @@ var useForm2 = ({ form: paramForm, values: paramValues, stepId, onStepIdChange,
51354
51460
  formWithOverrides,
51355
51461
  currentStep,
51356
51462
  fieldActions,
51357
- submitForm
51463
+ submitForm,
51464
+ prehydrationValues
51358
51465
  };
51359
51466
  };
51360
51467
  var dummyBi = {
@@ -56494,6 +56601,8 @@ var Guest = ({ className, ...internal }) => {
56494
56601
  return /* @__PURE__ */ React172.createElement("div", {
56495
56602
  className,
56496
56603
  "data-hook": `guest-${index2}`
56604
+ }, /* @__PURE__ */ React172.createElement(PrehydrationValuesContext.Provider, {
56605
+ value: void 0
56497
56606
  }, /* @__PURE__ */ React172.createElement(Form, {
56498
56607
  form: formView,
56499
56608
  formSchema,
@@ -56503,7 +56612,7 @@ var Guest = ({ className, ...internal }) => {
56503
56612
  onFieldChange,
56504
56613
  fields: formFields,
56505
56614
  fieldActions
56506
- }));
56615
+ })));
56507
56616
  };
56508
56617
  GuestCount.Label = EventsRepeaterLabel;
56509
56618
  GuestCount.Control = GuestCountControl;
@@ -58457,6 +58566,7 @@ var Input3 = ({ className }) => {
58457
58566
  preventScroll: true
58458
58567
  });
58459
58568
  }, id6);
58569
+ useKeepPrehydrationValue(inputRef);
58460
58570
  return /* @__PURE__ */ React172.createElement(Input, {
58461
58571
  ref: inputRef,
58462
58572
  id: inputId,
@@ -58757,6 +58867,7 @@ var Input4 = ({ className, ...internal }) => {
58757
58867
  preventScroll: true
58758
58868
  });
58759
58869
  }, id6);
58870
+ useKeepPrehydrationValue(inputRef);
58760
58871
  return /* @__PURE__ */ React172.createElement(Input, {
58761
58872
  ref: inputRef,
58762
58873
  id: inputId,
@@ -58831,6 +58942,7 @@ var Input5 = ({ className }) => {
58831
58942
  preventScroll: true
58832
58943
  });
58833
58944
  }, id6);
58945
+ useKeepPrehydrationValue(inputRef);
58834
58946
  return /* @__PURE__ */ React172.createElement(Input, {
58835
58947
  ref: inputRef,
58836
58948
  id: inputId,
@@ -58880,6 +58992,7 @@ var Input6 = ({ className }) => {
58880
58992
  preventScroll: true
58881
58993
  });
58882
58994
  }, id6);
58995
+ useKeepPrehydrationValue(inputRef);
58883
58996
  return /* @__PURE__ */ React172.createElement(Input, {
58884
58997
  ref: inputRef,
58885
58998
  id: inputId,
@@ -59342,6 +59455,7 @@ var Input8 = ({ className, ...internal }) => {
59342
59455
  preventScroll: true
59343
59456
  });
59344
59457
  }, id6);
59458
+ useKeepPrehydrationValue(inputRef);
59345
59459
  return /* @__PURE__ */ React172.createElement(Input, {
59346
59460
  ref: inputRef,
59347
59461
  id: inputId,
@@ -59527,6 +59641,7 @@ var Input9 = ({ className, ...internal }) => {
59527
59641
  preventScroll: true
59528
59642
  });
59529
59643
  }, id6);
59644
+ useKeepPrehydrationValue(inputRef);
59530
59645
  return /* @__PURE__ */ React172.createElement(Input, {
59531
59646
  ref: inputRef,
59532
59647
  id: inputId,
@@ -59600,6 +59715,7 @@ var Input10 = ({ className }) => {
59600
59715
  preventScroll: true
59601
59716
  });
59602
59717
  }, id6);
59718
+ useKeepPrehydrationValue(inputRef);
59603
59719
  return /* @__PURE__ */ React172.createElement(Input, {
59604
59720
  ref: inputRef,
59605
59721
  id: inputId,
@@ -63214,6 +63330,7 @@ var Input15 = ({ className }) => {
63214
63330
  preventScroll: true
63215
63331
  });
63216
63332
  }, id6);
63333
+ useKeepPrehydrationValue(ref);
63217
63334
  return /* @__PURE__ */ React172.createElement(TextArea$1, {
63218
63335
  ref,
63219
63336
  id: inputId,
@@ -63378,6 +63495,7 @@ var Input16 = ({ className }) => {
63378
63495
  preventScroll: true
63379
63496
  });
63380
63497
  }, id6);
63498
+ useKeepPrehydrationValue(inputRef);
63381
63499
  return /* @__PURE__ */ React172.createElement(Input, {
63382
63500
  ref: inputRef,
63383
63501
  id: inputId,