@wix/form-public 0.567.0 → 0.569.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
@@ -174,10 +174,37 @@ var init_errors = __esm({
174
174
  }
175
175
  });
176
176
 
177
- // ../form-validator/dist/esm/lib/error-derivation/helpers.js
178
- function getDecimalPlacesCount(multipleOf) {
179
- return String(multipleOf).split(".")?.[1]?.length || 0;
177
+ // ../form-validator/dist/esm/lib/utils/to-decimal.js
178
+ function toDecimal(value) {
179
+ const [mantissa, exponent = "0"] = String(Math.abs(value)).split("e");
180
+ const [integerPart, fractionPart = ""] = mantissa.split(".");
181
+ return {
182
+ unscaledValue: BigInt(integerPart + fractionPart),
183
+ scale: fractionPart.length - Number(exponent)
184
+ };
180
185
  }
186
+ function toScaledInteger({ unscaledValue, scale }, targetScale) {
187
+ return unscaledValue * 10n ** BigInt(targetScale - scale);
188
+ }
189
+ var init_to_decimal = __esm({
190
+ "../form-validator/dist/esm/lib/utils/to-decimal.js"() {
191
+ }
192
+ });
193
+
194
+ // ../form-validator/dist/esm/lib/utils/get-decimal-scale.js
195
+ function getDecimalScale(value) {
196
+ if (!Number.isFinite(value)) {
197
+ return 0;
198
+ }
199
+ return Math.max(toDecimal(value).scale, 0);
200
+ }
201
+ var init_get_decimal_scale = __esm({
202
+ "../form-validator/dist/esm/lib/utils/get-decimal-scale.js"() {
203
+ init_to_decimal();
204
+ }
205
+ });
206
+
207
+ // ../form-validator/dist/esm/lib/error-derivation/helpers.js
181
208
  function deserializeDate(value) {
182
209
  if (!value) {
183
210
  return {};
@@ -341,7 +368,7 @@ function deriveNumericValueError(validation, error) {
341
368
  if (isMultipleOfValueError) {
342
369
  const multipleOf = validation?.number?.multipleOf;
343
370
  if (multipleOf) {
344
- const decimalPlaces = getDecimalPlacesCount(multipleOf);
371
+ const decimalPlaces = getDecimalScale(multipleOf);
345
372
  return {
346
373
  ...error,
347
374
  errorType: types.ErrorType.DECIMAL_POINT_ERROR,
@@ -440,6 +467,7 @@ function deriveDateTimeFieldError(validation, error, value) {
440
467
  var init_error_derivers = __esm({
441
468
  "../form-validator/dist/esm/lib/error-derivation/error-derivers.js"() {
442
469
  init_errors();
470
+ init_get_decimal_scale();
443
471
  init_helpers();
444
472
  init_types();
445
473
  }
@@ -12570,6 +12598,43 @@ var init_max = __esm({
12570
12598
  init_core();
12571
12599
  }
12572
12600
  });
12601
+
12602
+ // ../form-validator/dist/esm/lib/utils/compile-is-multiple-of.js
12603
+ function compileIsMultipleOf(multipleOf) {
12604
+ if (!Number.isFinite(multipleOf) || multipleOf <= 0) {
12605
+ return () => false;
12606
+ }
12607
+ const divisor = toDecimal(multipleOf);
12608
+ const maxTolerableNoise = multipleOf * MAX_NOISE_TO_STEP_RATIO;
12609
+ return (value) => {
12610
+ if (!Number.isFinite(value)) {
12611
+ return true;
12612
+ }
12613
+ const dividend = toDecimal(value);
12614
+ const scale = Math.max(dividend.scale, divisor.scale);
12615
+ const scaledDivisor = toScaledInteger(divisor, scale);
12616
+ const remainder = toScaledInteger(dividend, scale) % scaledDivisor;
12617
+ if (remainder === 0n) {
12618
+ return true;
12619
+ }
12620
+ const noise = FLOATING_POINT_NOISE_ULPS * Math.abs(value) * Number.EPSILON;
12621
+ const distanceToNearestMultiple = remainder < scaledDivisor - remainder ? remainder : scaledDivisor - remainder;
12622
+ return isWithinNoise(distanceToNearestMultiple, scale, Math.min(noise, maxTolerableNoise));
12623
+ };
12624
+ }
12625
+ function isWithinNoise(distance, distanceScale, noise) {
12626
+ const noiseDecimal = toDecimal(noise);
12627
+ const scale = Math.max(distanceScale, noiseDecimal.scale);
12628
+ return distance * 10n ** BigInt(scale - distanceScale) <= toScaledInteger(noiseDecimal, scale);
12629
+ }
12630
+ var FLOATING_POINT_NOISE_ULPS, MAX_NOISE_TO_STEP_RATIO;
12631
+ var init_compile_is_multiple_of = __esm({
12632
+ "../form-validator/dist/esm/lib/utils/compile-is-multiple-of.js"() {
12633
+ init_to_decimal();
12634
+ FLOATING_POINT_NOISE_ULPS = 4;
12635
+ MAX_NOISE_TO_STEP_RATIO = 1e-6;
12636
+ }
12637
+ });
12573
12638
  function parseLocalDate(value) {
12574
12639
  const match4 = LOCAL_DATE_REGEX.exec(value);
12575
12640
  if (!match4 || isNaN(new Date(value).getTime())) {
@@ -12640,6 +12705,16 @@ var init_date_availability = __esm({
12640
12705
 
12641
12706
  // ../form-validator/dist/esm/lib/submission-validation/ajv/ajv-custom-keywords.js
12642
12707
  function addCustomKeywords(ajv, phoneValidationEnabled, lenientPhoneValidation) {
12708
+ ajv.removeKeyword("multipleOf");
12709
+ ajv.addKeyword({
12710
+ keyword: "multipleOf",
12711
+ type: "number",
12712
+ schemaType: "number",
12713
+ errors: true,
12714
+ compile(multipleOf) {
12715
+ return compileMultipleOfValidation(multipleOf);
12716
+ }
12717
+ });
12643
12718
  ajv.addKeyword({
12644
12719
  keyword: "dateAvailability",
12645
12720
  type: "string",
@@ -12799,6 +12874,26 @@ function getCallingCode(phoneNumber) {
12799
12874
  function onlyCallingCode(callingCode, data) {
12800
12875
  return data.replace(/^\+/, "") === callingCode;
12801
12876
  }
12877
+ function compileMultipleOfValidation(multipleOf) {
12878
+ const isMultipleOf = compileIsMultipleOf(multipleOf);
12879
+ const validate2 = (data) => {
12880
+ if (typeof data !== "number" || isMultipleOf(data)) {
12881
+ return true;
12882
+ }
12883
+ const errors = [
12884
+ {
12885
+ keyword: "multipleOf",
12886
+ message: `must be multiple of ${multipleOf}`,
12887
+ params: {
12888
+ multipleOf
12889
+ }
12890
+ }
12891
+ ];
12892
+ validate2.errors = errors;
12893
+ return false;
12894
+ };
12895
+ return validate2;
12896
+ }
12802
12897
  function compileDateAvailabilityValidation(availability) {
12803
12898
  const validate2 = (data) => {
12804
12899
  if (typeof data !== "string") {
@@ -12825,6 +12920,7 @@ var init_ajv_custom_keywords = __esm({
12825
12920
  "../form-validator/dist/esm/lib/submission-validation/ajv/ajv-custom-keywords.js"() {
12826
12921
  init_max();
12827
12922
  init_metadata_max_json();
12923
+ init_compile_is_multiple_of();
12828
12924
  init_date_availability();
12829
12925
  }
12830
12926
  });
@@ -12833,8 +12929,7 @@ var init_ajv_custom_keywords = __esm({
12833
12929
  function createAjvSchemaValidatorFactory(formSchemaMapper, phoneValidationEnabled, lenientPhoneValidation) {
12834
12930
  return function createAjvSchemaValidator(formsSchemas) {
12835
12931
  const ajv = new import_ajv.default({
12836
- allErrors: true,
12837
- multipleOfPrecision: 8
12932
+ allErrors: true
12838
12933
  });
12839
12934
  (0, import_ajv_formats.default)(ajv);
12840
12935
  addCustomFormats(ajv, phoneValidationEnabled);
@@ -43767,6 +43862,7 @@ function useTranslation2() {
43767
43862
 
43768
43863
  // ../form-experiments/dist/esm/experiments.js
43769
43864
  var EXPERIMENTS = {
43865
+ ADDRESS_HOUSE_NUMBER_FALLBACK: "addressHouseNumberFallback",
43770
43866
  ENABLE_FORM_SCHEMA_V2_PRICING_PLANS: "enableFormSchemaV2PricingPlans",
43771
43867
  STREETLESS_LOCALITY_FALLBACK: "streetlessLocalityFallback",
43772
43868
  SUBMIT_SUCCESS_NO_FOCUS: "specs.forms.SubmitSuccessNoFocus",
@@ -58686,6 +58782,22 @@ var searchByPostalCode = async ({ postalCode, countryCode }) => {
58686
58782
  proximity
58687
58783
  })));
58688
58784
  };
58785
+ var searchByQuery = async ({ query, countryCode }) => {
58786
+ const { searchResults } = await es_exports8.search({
58787
+ query,
58788
+ ...countryCode ? {
58789
+ countryCodes: [
58790
+ countryCode
58791
+ ]
58792
+ } : {}
58793
+ });
58794
+ return (searchResults ?? []).flatMap(({ address }) => {
58795
+ const commonAddress = toCommonAddress(address);
58796
+ return commonAddress ? [
58797
+ commonAddress
58798
+ ] : [];
58799
+ });
58800
+ };
58689
58801
  var AddressFieldsContext = /* @__PURE__ */ React172.createContext(void 0);
58690
58802
  var AddressFieldsProvider = ({ children, fields }) => {
58691
58803
  const value = React172.useMemo(() => ({
@@ -58768,35 +58880,111 @@ AddressLine2.Label = Label;
58768
58880
  AddressLine2.Input = Input3;
58769
58881
  AddressLine2.Description = Description;
58770
58882
  AddressLine2.Error = Error2;
58883
+
58884
+ // ../form-fields/dist/esm/ui/address-input/resolve-place.js
58885
+ var STREET_PLACE_TYPE = "route";
58886
+ var isStreetPrediction = (prediction) => prediction.types?.includes(STREET_PLACE_TYPE) ?? false;
58887
+ var ADDRESS_TEXT_SEPARATORS = /[\s,]+/;
58888
+ var toAddressTokens = (text) => text ? text.toLowerCase().split(ADDRESS_TEXT_SEPARATORS) : [];
58889
+ var containsHouseNumber = (text, houseNumber, streetName) => {
58890
+ if (!text || !houseNumber) {
58891
+ return false;
58892
+ }
58893
+ const streetNameTokens = toAddressTokens(streetName);
58894
+ return toAddressTokens(text).filter((token) => !streetNameTokens.includes(token)).includes(houseNumber.toLowerCase());
58895
+ };
58896
+ var postalCodeBase = (postalCode) => postalCode.split("-")[0];
58897
+ var isSamePostalArea = (place, candidate) => !place.postalCode || !candidate.postalCode || postalCodeBase(place.postalCode) === postalCodeBase(candidate.postalCode);
58898
+ var isSameStreet = (place, candidate) => Boolean(place.streetAddress?.name) && place.streetAddress?.name?.toLowerCase() === candidate.streetAddress?.name?.toLowerCase();
58899
+ var isSameStreetLocation = (place, candidate) => candidate.country === place.country && candidate.subdivision === place.subdivision && isSamePostalArea(place, candidate) && isSameStreet(place, candidate);
58900
+ var searchHouseNumber = async (query, place, searchByQuery2) => {
58901
+ try {
58902
+ const candidates = await searchByQuery2({
58903
+ query,
58904
+ countryCode: place.country ?? void 0
58905
+ });
58906
+ return candidates?.find((candidate) => isSameStreetLocation(place, candidate) && containsHouseNumber(query, candidate.streetAddress?.number, place.streetAddress?.name))?.streetAddress?.number;
58907
+ } catch {
58908
+ return void 0;
58909
+ }
58910
+ };
58911
+ var withHouseNumber = (place, houseNumber, suggestionText) => {
58912
+ const streetAddress = {
58913
+ ...place.streetAddress,
58914
+ number: houseNumber
58915
+ };
58916
+ if (!containsHouseNumber(suggestionText, houseNumber)) {
58917
+ return {
58918
+ ...place,
58919
+ streetAddress
58920
+ };
58921
+ }
58922
+ return {
58923
+ ...place,
58924
+ ...place.addressLine ? {
58925
+ addressLine: suggestionText
58926
+ } : {},
58927
+ streetAddress: {
58928
+ ...streetAddress,
58929
+ formattedAddressLine: suggestionText
58930
+ }
58931
+ };
58932
+ };
58933
+ var resolvePlace = async ({ searchId, prediction, getPlace: getPlace6, searchByQuery: searchByQuery2 }) => {
58934
+ const place = await getPlace6({
58935
+ searchId
58936
+ });
58937
+ if (!place || place.streetAddress?.number || !prediction?.description || !isStreetPrediction(prediction) || !searchByQuery2) {
58938
+ return place;
58939
+ }
58940
+ const houseNumber = await searchHouseNumber(prediction.description, place, searchByQuery2);
58941
+ return houseNumber ? withHouseNumber(place, houseNumber, prediction.textStructure?.mainText) : place;
58942
+ };
58943
+
58944
+ // ../form-fields/dist/esm/ui/address-input/use-atlas.js
58771
58945
  var useAtlas = ({ enabled }) => {
58772
58946
  const { actions } = useFieldActions();
58947
+ const { isExperimentEnabled: isExperimentEnabled2 } = useExperiments();
58948
+ const addressHouseNumberFallbackEnabled = isExperimentEnabled2(EXPERIMENTS.ADDRESS_HOUSE_NUMBER_FALLBACK);
58773
58949
  const [sessionToken, setSessionToken] = React172.useState(generateUUID());
58774
58950
  const refreshSessionToken = React172.useCallback(() => setSessionToken(generateUUID()), []);
58951
+ const resolvePlaceRequestId = React172.useRef(0);
58775
58952
  return React172.useMemo(() => {
58776
- const { predict: predict6, getPlace: getPlace6, searchByPostalCode: searchByPostalCode2 } = actions ?? {};
58953
+ const { predict: predict6, getPlace: getPlace6, searchByPostalCode: searchByPostalCode2, searchByQuery: searchByQuery2 } = actions ?? {};
58777
58954
  if (!enabled || !predict6 || !getPlace6 || !searchByPostalCode2) {
58778
58955
  return void 0;
58779
58956
  }
58957
+ const getPlaceInSession = (params) => getPlace6({
58958
+ ...params,
58959
+ sessionToken,
58960
+ refreshSessionToken
58961
+ });
58780
58962
  return {
58781
58963
  predict: (params) => predict6({
58782
58964
  ...params,
58783
58965
  sessionToken
58784
58966
  }),
58785
- getPlace: (params) => getPlace6({
58786
- ...params,
58787
- sessionToken,
58788
- refreshSessionToken
58789
- }),
58967
+ getPlace: getPlaceInSession,
58790
58968
  searchByPostalCode: (params) => searchByPostalCode2({
58791
58969
  ...params,
58792
58970
  sessionToken
58793
- })
58971
+ }),
58972
+ resolvePlace: async (params) => {
58973
+ const requestId = ++resolvePlaceRequestId.current;
58974
+ const place = await resolvePlace({
58975
+ ...params,
58976
+ getPlace: getPlaceInSession,
58977
+ searchByQuery: addressHouseNumberFallbackEnabled ? searchByQuery2 : void 0
58978
+ });
58979
+ return requestId === resolvePlaceRequestId.current ? place : void 0;
58980
+ }
58794
58981
  };
58795
58982
  }, [
58796
58983
  enabled,
58797
58984
  actions,
58798
58985
  sessionToken,
58799
- refreshSessionToken
58986
+ refreshSessionToken,
58987
+ addressHouseNumberFallbackEnabled
58800
58988
  ]);
58801
58989
  };
58802
58990
 
@@ -58997,8 +59185,10 @@ var AddressLineAutocomplete = ({ children, value, onChange, disabled, readOnly,
58997
59185
  return;
58998
59186
  }
58999
59187
  clearPredictions();
59000
- const address = await atlas?.getPlace({
59001
- searchId: String(key)
59188
+ const searchId = String(key);
59189
+ const address = await atlas?.resolvePlace({
59190
+ searchId,
59191
+ prediction: predictions.find((prediction) => prediction.searchId === searchId)
59002
59192
  });
59003
59193
  if (address && onAddressSelect) {
59004
59194
  onAddressSelect(address);
@@ -59006,7 +59196,8 @@ var AddressLineAutocomplete = ({ children, value, onChange, disabled, readOnly,
59006
59196
  }, [
59007
59197
  onAddressSelect,
59008
59198
  clearPredictions,
59009
- atlas
59199
+ atlas,
59200
+ predictions
59010
59201
  ]);
59011
59202
  const handleBlur = React172.useCallback(() => {
59012
59203
  clearPredictions();
@@ -59773,8 +59964,10 @@ var StreetNameAutocomplete = ({ children, value, onChange, disabled, readOnly, r
59773
59964
  if (!key) {
59774
59965
  return;
59775
59966
  }
59776
- const address = await atlas?.getPlace({
59777
- searchId: String(key)
59967
+ const searchId = String(key);
59968
+ const address = await atlas?.resolvePlace({
59969
+ searchId,
59970
+ prediction: predictions.find((prediction) => prediction.searchId === searchId)
59778
59971
  });
59779
59972
  if (address && onAddressSelect) {
59780
59973
  onAddressSelect(address);
@@ -59782,7 +59975,8 @@ var StreetNameAutocomplete = ({ children, value, onChange, disabled, readOnly, r
59782
59975
  }, [
59783
59976
  onAddressSelect,
59784
59977
  clearPredictions,
59785
- atlas
59978
+ atlas,
59979
+ predictions
59786
59980
  ]);
59787
59981
  const handleBlur = React172.useCallback(() => {
59788
59982
  clearPredictions();
@@ -60087,15 +60281,21 @@ var MultilineAddress = ({ children, className }) => {
60087
60281
  const { validateFields: parentValidateFields } = useFieldValidation();
60088
60282
  const formSchema = useForm();
60089
60283
  const fieldActions = useFieldActions();
60090
- const headlessFieldActions = React172.useMemo(() => ({
60091
- ...fieldActions,
60092
- actions: {
60093
- predict: predict5,
60094
- getPlace: getPlace5,
60095
- searchByPostalCode,
60096
- ...fieldActions.actions
60097
- }
60098
- }), [
60284
+ const headlessFieldActions = React172.useMemo(() => {
60285
+ const hostResolvesPlaces = Boolean(fieldActions.actions?.getPlace);
60286
+ return {
60287
+ ...fieldActions,
60288
+ actions: {
60289
+ predict: predict5,
60290
+ getPlace: getPlace5,
60291
+ searchByPostalCode,
60292
+ ...hostResolvesPlaces ? {} : {
60293
+ searchByQuery
60294
+ },
60295
+ ...fieldActions.actions
60296
+ }
60297
+ };
60298
+ }, [
60099
60299
  fieldActions
60100
60300
  ]);
60101
60301
  const { form, isMultilineAddressField: isMultilineAddressField2 } = useMultilineAddressForm({