@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.cjs CHANGED
@@ -14773,12 +14773,25 @@ var init_form_validator = __esm({
14773
14773
  }))));
14774
14774
  const addressForms = this.getAddressForms(currentlyValidatableForm, submission);
14775
14775
  const objectArrayForms = this.getObjectArrayForms(currentlyValidatableForm);
14776
- return createAjvSchemaValidator([
14776
+ const ajv = createAjvSchemaValidator([
14777
14777
  ...this.formSchemas,
14778
14778
  ...nestedForms,
14779
14779
  ...addressForms,
14780
14780
  ...objectArrayForms
14781
14781
  ]);
14782
+ this.addPlaceholdersForMissingDeletedAddressForms(ajv, currentlyValidatableForm);
14783
+ return ajv;
14784
+ }
14785
+ // A deleted address field still references its address form. Without that
14786
+ // form the schema cannot compile, so accept any value for the field instead.
14787
+ addPlaceholdersForMissingDeletedAddressForms(ajv, form) {
14788
+ (form.deletedFields ?? []).filter((field) => field.target && field.validation?.predefined?.format === types$1.ValidationFormat.MULTILINE_ADDRESS).forEach((field) => {
14789
+ if (!ajv.getSchema(field.target)) {
14790
+ ajv.addSchema({
14791
+ $id: field.target
14792
+ });
14793
+ }
14794
+ });
14782
14795
  }
14783
14796
  // eslint-disable-next-line max-params
14784
14797
  constructor(formSchemas, validationMode, platformisedFieldBasedValidation = false, addressFormProvider = () => void 0, submissionValueProvider, lenientPhoneValidation = false) {
@@ -41056,15 +41069,15 @@ var getNormalizedColSpans = (cellsColSpans) => {
41056
41069
  var isLayoutFillObject = (value) => typeof value === "object" && value !== null;
41057
41070
  var isLayoutFillGradient = (value) => isLayoutFillObject(value);
41058
41071
  var isNormalizedLayoutFill = (value) => value?.type === "COLOR" ? typeof value.color === "string" && !!value.color : value?.type === "GRADIENT" ? isLayoutFillGradient(value.gradient) : false;
41059
- function resolveLayoutFill(fill, legacyColor) {
41060
- if (isNormalizedLayoutFill(fill)) {
41061
- return fill;
41072
+ function resolveLayoutFill(fill2, legacyColor) {
41073
+ if (isNormalizedLayoutFill(fill2)) {
41074
+ return fill2;
41062
41075
  }
41063
- const gradient = getLayoutFillGradient(fill);
41076
+ const gradient = getLayoutFillGradient(fill2);
41064
41077
  if (gradient) {
41065
41078
  return { type: "GRADIENT", gradient };
41066
41079
  }
41067
- const color = getLayoutFillColor(fill);
41080
+ const color = getLayoutFillColor(fill2);
41068
41081
  if (color !== void 0) {
41069
41082
  return { type: "COLOR", color };
41070
41083
  }
@@ -41073,10 +41086,10 @@ function resolveLayoutFill(fill, legacyColor) {
41073
41086
  }
41074
41087
  return void 0;
41075
41088
  }
41076
- var getLayoutFillColor = (fill) => fill && "color" in fill && typeof fill.color === "string" ? fill.color || void 0 : void 0;
41077
- var getLayoutFillGradient = (fill) => fill && "gradient" in fill && isLayoutFillGradient(fill.gradient) ? fill.gradient : void 0;
41078
- var getResolvedLayoutFillColor = (fill, legacyColor) => getLayoutFillColor(resolveLayoutFill(fill, legacyColor));
41079
- var getResolvedLayoutFillGradient = (fill, legacyColor) => getLayoutFillGradient(resolveLayoutFill(fill, legacyColor));
41089
+ var getLayoutFillColor = (fill2) => fill2 && "color" in fill2 && typeof fill2.color === "string" ? fill2.color || void 0 : void 0;
41090
+ var getLayoutFillGradient = (fill2) => fill2 && "gradient" in fill2 && isLayoutFillGradient(fill2.gradient) ? fill2.gradient : void 0;
41091
+ var getResolvedLayoutFillColor = (fill2, legacyColor) => getLayoutFillColor(resolveLayoutFill(fill2, legacyColor));
41092
+ var getResolvedLayoutFillGradient = (fill2, legacyColor) => getLayoutFillGradient(resolveLayoutFill(fill2, legacyColor));
41080
41093
 
41081
41094
  // ../../node_modules/@wix/ricos/dist/es/ricos-common-plugins/layout/section-dividers/divider-presets.js
41082
41095
  var DIVIDER_LAYERS_COUNT_MIN = 1;
@@ -48800,6 +48813,7 @@ function getSubmitFieldIdsV2(fields = []) {
48800
48813
  }
48801
48814
 
48802
48815
  // ../form-fields/dist/esm/services/form-view.js
48816
+ var FIELD_PATH_SEPARATOR = "/";
48803
48817
  function createFormView({ form, path = "", stepId, recursive = true, includeHiddenFields = false, isMobile: isMobile2 = false, breakpoint = DEFAULT_BREAKPOINT, useFieldsV2 = false, parentId = "" }) {
48804
48818
  const step = stepId ? form.steps?.find(({ id: id6 }) => id6 === stepId) : form.steps?.[0];
48805
48819
  const layout = step?.layout ?? {};
@@ -48882,7 +48896,7 @@ function createFieldView(field, layout, path, recursive, isMobile2, breakpoint =
48882
48896
  return;
48883
48897
  }
48884
48898
  const fieldTarget = field.target || "";
48885
- const fieldPath = path ? `${path}/${fieldTarget}` : fieldTarget;
48899
+ const fieldPath = path ? `${path}${FIELD_PATH_SEPARATOR}${fieldTarget}` : fieldTarget;
48886
48900
  const propertiesView = createPropertiesView(field, fieldPath, recursive, isMobile2);
48887
48901
  return {
48888
48902
  id: field.id,
@@ -48991,6 +49005,78 @@ function useAddressForms() {
48991
49005
  }
48992
49006
  return context;
48993
49007
  }
49008
+
49009
+ // ../form-fields/dist/esm/services/prehydration-values.js
49010
+ var isEmptyInputValue = (value) => value === void 0 || value === null || value === "";
49011
+ var isNested = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
49012
+ var fill = (values, [key, ...rest], text) => {
49013
+ const current = values[key];
49014
+ if (rest.length === 0) {
49015
+ return isEmptyInputValue(current) ? {
49016
+ ...values,
49017
+ [key]: text
49018
+ } : values;
49019
+ }
49020
+ if (!isEmptyInputValue(current) && !isNested(current)) {
49021
+ return values;
49022
+ }
49023
+ const nested = isNested(current) ? current : {};
49024
+ const filled = fill(nested, rest, text);
49025
+ return filled === nested ? values : {
49026
+ ...values,
49027
+ [key]: filled
49028
+ };
49029
+ };
49030
+ var read = (values, [key, ...rest]) => {
49031
+ const current = values[key];
49032
+ if (rest.length === 0) {
49033
+ return current;
49034
+ }
49035
+ return isNested(current) ? read(current, rest) : void 0;
49036
+ };
49037
+ var mergePrehydrationValues = (values, pending) => {
49038
+ let result2 = values;
49039
+ for (const [path, text] of pending) {
49040
+ result2 = fill(result2, path.split(FIELD_PATH_SEPARATOR), text);
49041
+ }
49042
+ return result2;
49043
+ };
49044
+ var prunePrehydrationValues = (values, pending) => {
49045
+ for (const path of pending.keys()) {
49046
+ if (!isEmptyInputValue(read(values, path.split(FIELD_PATH_SEPARATOR)))) {
49047
+ pending.delete(path);
49048
+ }
49049
+ }
49050
+ };
49051
+ var forgetPrehydrationValues = (pending, previousValues, nextValues) => {
49052
+ for (const path of pending.keys()) {
49053
+ const segments = path.split(FIELD_PATH_SEPARATOR);
49054
+ if (read(previousValues, segments) !== read(nextValues, segments)) {
49055
+ pending.delete(path);
49056
+ }
49057
+ }
49058
+ };
49059
+
49060
+ // ../form-fields/dist/esm/contexts/prehydration-values-context.js
49061
+ var PrehydrationValuesContext = /* @__PURE__ */ React172.createContext(void 0);
49062
+ var useKeepPrehydrationValue = (ref) => {
49063
+ const registry = React172.useContext(PrehydrationValuesContext);
49064
+ const { path, value } = useFieldPropsV2();
49065
+ const mountedFieldRef = React172.useRef({
49066
+ path,
49067
+ value,
49068
+ registry
49069
+ });
49070
+ React172.useEffect(() => {
49071
+ const field = mountedFieldRef.current;
49072
+ const domValue = ref.current?.value;
49073
+ if (field.registry && domValue && isEmptyInputValue(field.value)) {
49074
+ field.registry.current.set(field.path, domValue);
49075
+ }
49076
+ }, [
49077
+ ref
49078
+ ]);
49079
+ };
48994
49080
  var FieldInitialDataContext = React172.createContext(void 0);
48995
49081
 
48996
49082
  // ../form-fields/dist/esm/fields/multiline-address/multiline-address/address.js
@@ -50719,7 +50805,7 @@ function autofillValues(form, values, userData) {
50719
50805
  }
50720
50806
 
50721
50807
  // ../form-viewer/dist/esm/hooks/use-form-values/use-form-values.js
50722
- var useFormValues = (form, values, onChange, userData, addressTemplates, enableAutofill) => {
50808
+ var useFormValues = (form, values, onChange, userData, addressTemplates, enableAutofill, prehydrationValues) => {
50723
50809
  const hasAutofilled = React172.useRef(false);
50724
50810
  const normalizedValues = React172.useMemo(() => {
50725
50811
  let filled = fillEmptyValues(form, values);
@@ -50735,17 +50821,35 @@ var useFormValues = (form, values, onChange, userData, addressTemplates, enableA
50735
50821
  addressTemplates,
50736
50822
  enableAutofill
50737
50823
  ]);
50824
+ const [, rerenderWithPrehydrationText] = React172.useReducer((renders) => renders + 1, 0);
50825
+ const pendingCount = prehydrationValues?.current.size ?? 0;
50826
+ const renderedPendingCount = React172.useRef(pendingCount);
50827
+ renderedPendingCount.current = pendingCount;
50828
+ const valuesWithPrehydrationText = React172.useMemo(() => pendingCount > 0 && prehydrationValues ? mergePrehydrationValues(normalizedValues, prehydrationValues.current) : normalizedValues, [
50829
+ normalizedValues,
50830
+ pendingCount,
50831
+ prehydrationValues
50832
+ ]);
50738
50833
  React172.useEffect(() => {
50739
- const hasAnyValueChanged = !deepEqual(normalizedValues, values);
50834
+ const pending = prehydrationValues?.current;
50835
+ if (pending?.size) {
50836
+ prunePrehydrationValues(values ?? {}, pending);
50837
+ }
50838
+ const merged = pending?.size ? mergePrehydrationValues(normalizedValues, pending) : normalizedValues;
50839
+ const hasAnyValueChanged = !deepEqual(merged, values);
50740
50840
  if (hasAnyValueChanged) {
50741
- onChange?.(normalizedValues);
50841
+ onChange?.(merged);
50842
+ }
50843
+ if ((pending?.size ?? 0) !== renderedPendingCount.current) {
50844
+ rerenderWithPrehydrationText();
50742
50845
  }
50743
50846
  }, [
50744
50847
  values,
50745
50848
  normalizedValues,
50746
- onChange
50849
+ onChange,
50850
+ prehydrationValues
50747
50851
  ]);
50748
- return normalizedValues;
50852
+ return valuesWithPrehydrationText;
50749
50853
  };
50750
50854
  var useFormView2 = (form, stepId, breakpoint) => {
50751
50855
  const { isMobile: isMobile2 } = useConfig();
@@ -51194,7 +51298,8 @@ var useForm2 = ({ form: paramForm, values: paramValues, stepId, onStepIdChange,
51194
51298
  ]);
51195
51299
  const formId = formWithOverrides.id;
51196
51300
  const { setFocusedFieldId } = useFocusedField();
51197
- const normalizedValues = useFormValues(formWithOverrides, paramValues, onChange, userData, addressTemplates, enableAutofill);
51301
+ const prehydrationValues = React172.useRef(/* @__PURE__ */ new Map());
51302
+ const normalizedValues = useFormValues(formWithOverrides, paramValues, onChange, userData, addressTemplates, enableAutofill, prehydrationValues);
51198
51303
  const valuesRef = React172.useRef(normalizedValues);
51199
51304
  valuesRef.current = normalizedValues;
51200
51305
  const errorsRef = React172.useRef(errors);
@@ -51212,6 +51317,7 @@ var useForm2 = ({ form: paramForm, values: paramValues, stepId, onStepIdChange,
51212
51317
  [target]: newValue
51213
51318
  };
51214
51319
  const cleared = clearFieldsWhenHiddenByRule(paramForm, overrides, oldValues, newValues, oldErrors);
51320
+ forgetPrehydrationValues(prehydrationValues.current, oldValues, cleared.values);
51215
51321
  onChange(cleared.values);
51216
51322
  onValidate(cleared.errors);
51217
51323
  }
@@ -51364,7 +51470,8 @@ var useForm2 = ({ form: paramForm, values: paramValues, stepId, onStepIdChange,
51364
51470
  formWithOverrides,
51365
51471
  currentStep,
51366
51472
  fieldActions,
51367
- submitForm
51473
+ submitForm,
51474
+ prehydrationValues
51368
51475
  };
51369
51476
  };
51370
51477
  var dummyBi = {
@@ -56504,6 +56611,8 @@ var Guest = ({ className, ...internal }) => {
56504
56611
  return /* @__PURE__ */ React172__default.default.createElement("div", {
56505
56612
  className,
56506
56613
  "data-hook": `guest-${index2}`
56614
+ }, /* @__PURE__ */ React172__default.default.createElement(PrehydrationValuesContext.Provider, {
56615
+ value: void 0
56507
56616
  }, /* @__PURE__ */ React172__default.default.createElement(Form, {
56508
56617
  form: formView,
56509
56618
  formSchema,
@@ -56513,7 +56622,7 @@ var Guest = ({ className, ...internal }) => {
56513
56622
  onFieldChange,
56514
56623
  fields: formFields,
56515
56624
  fieldActions
56516
- }));
56625
+ })));
56517
56626
  };
56518
56627
  GuestCount.Label = EventsRepeaterLabel;
56519
56628
  GuestCount.Control = GuestCountControl;
@@ -58467,6 +58576,7 @@ var Input3 = ({ className }) => {
58467
58576
  preventScroll: true
58468
58577
  });
58469
58578
  }, id6);
58579
+ useKeepPrehydrationValue(inputRef);
58470
58580
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.Input, {
58471
58581
  ref: inputRef,
58472
58582
  id: inputId,
@@ -58767,6 +58877,7 @@ var Input4 = ({ className, ...internal }) => {
58767
58877
  preventScroll: true
58768
58878
  });
58769
58879
  }, id6);
58880
+ useKeepPrehydrationValue(inputRef);
58770
58881
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.Input, {
58771
58882
  ref: inputRef,
58772
58883
  id: inputId,
@@ -58841,6 +58952,7 @@ var Input5 = ({ className }) => {
58841
58952
  preventScroll: true
58842
58953
  });
58843
58954
  }, id6);
58955
+ useKeepPrehydrationValue(inputRef);
58844
58956
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.Input, {
58845
58957
  ref: inputRef,
58846
58958
  id: inputId,
@@ -58890,6 +59002,7 @@ var Input6 = ({ className }) => {
58890
59002
  preventScroll: true
58891
59003
  });
58892
59004
  }, id6);
59005
+ useKeepPrehydrationValue(inputRef);
58893
59006
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.Input, {
58894
59007
  ref: inputRef,
58895
59008
  id: inputId,
@@ -59352,6 +59465,7 @@ var Input8 = ({ className, ...internal }) => {
59352
59465
  preventScroll: true
59353
59466
  });
59354
59467
  }, id6);
59468
+ useKeepPrehydrationValue(inputRef);
59355
59469
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.Input, {
59356
59470
  ref: inputRef,
59357
59471
  id: inputId,
@@ -59537,6 +59651,7 @@ var Input9 = ({ className, ...internal }) => {
59537
59651
  preventScroll: true
59538
59652
  });
59539
59653
  }, id6);
59654
+ useKeepPrehydrationValue(inputRef);
59540
59655
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.Input, {
59541
59656
  ref: inputRef,
59542
59657
  id: inputId,
@@ -59610,6 +59725,7 @@ var Input10 = ({ className }) => {
59610
59725
  preventScroll: true
59611
59726
  });
59612
59727
  }, id6);
59728
+ useKeepPrehydrationValue(inputRef);
59613
59729
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.Input, {
59614
59730
  ref: inputRef,
59615
59731
  id: inputId,
@@ -63224,6 +63340,7 @@ var Input15 = ({ className }) => {
63224
63340
  preventScroll: true
63225
63341
  });
63226
63342
  }, id6);
63343
+ useKeepPrehydrationValue(ref);
63227
63344
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.TextArea, {
63228
63345
  ref,
63229
63346
  id: inputId,
@@ -63388,6 +63505,7 @@ var Input16 = ({ className }) => {
63388
63505
  preventScroll: true
63389
63506
  });
63390
63507
  }, id6);
63508
+ useKeepPrehydrationValue(inputRef);
63391
63509
  return /* @__PURE__ */ React172__default.default.createElement(reactAriaComponents.Input, {
63392
63510
  ref: inputRef,
63393
63511
  id: inputId,