@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.js CHANGED
@@ -164,10 +164,37 @@ var init_errors = __esm({
164
164
  }
165
165
  });
166
166
 
167
- // ../form-validator/dist/esm/lib/error-derivation/helpers.js
168
- function getDecimalPlacesCount(multipleOf) {
169
- return String(multipleOf).split(".")?.[1]?.length || 0;
167
+ // ../form-validator/dist/esm/lib/utils/to-decimal.js
168
+ function toDecimal(value) {
169
+ const [mantissa, exponent = "0"] = String(Math.abs(value)).split("e");
170
+ const [integerPart, fractionPart = ""] = mantissa.split(".");
171
+ return {
172
+ unscaledValue: BigInt(integerPart + fractionPart),
173
+ scale: fractionPart.length - Number(exponent)
174
+ };
170
175
  }
176
+ function toScaledInteger({ unscaledValue, scale }, targetScale) {
177
+ return unscaledValue * 10n ** BigInt(targetScale - scale);
178
+ }
179
+ var init_to_decimal = __esm({
180
+ "../form-validator/dist/esm/lib/utils/to-decimal.js"() {
181
+ }
182
+ });
183
+
184
+ // ../form-validator/dist/esm/lib/utils/get-decimal-scale.js
185
+ function getDecimalScale(value) {
186
+ if (!Number.isFinite(value)) {
187
+ return 0;
188
+ }
189
+ return Math.max(toDecimal(value).scale, 0);
190
+ }
191
+ var init_get_decimal_scale = __esm({
192
+ "../form-validator/dist/esm/lib/utils/get-decimal-scale.js"() {
193
+ init_to_decimal();
194
+ }
195
+ });
196
+
197
+ // ../form-validator/dist/esm/lib/error-derivation/helpers.js
171
198
  function deserializeDate(value) {
172
199
  if (!value) {
173
200
  return {};
@@ -331,7 +358,7 @@ function deriveNumericValueError(validation, error) {
331
358
  if (isMultipleOfValueError) {
332
359
  const multipleOf = validation?.number?.multipleOf;
333
360
  if (multipleOf) {
334
- const decimalPlaces = getDecimalPlacesCount(multipleOf);
361
+ const decimalPlaces = getDecimalScale(multipleOf);
335
362
  return {
336
363
  ...error,
337
364
  errorType: ErrorType.DECIMAL_POINT_ERROR,
@@ -430,6 +457,7 @@ function deriveDateTimeFieldError(validation, error, value) {
430
457
  var init_error_derivers = __esm({
431
458
  "../form-validator/dist/esm/lib/error-derivation/error-derivers.js"() {
432
459
  init_errors();
460
+ init_get_decimal_scale();
433
461
  init_helpers();
434
462
  init_types();
435
463
  }
@@ -12560,6 +12588,43 @@ var init_max = __esm({
12560
12588
  init_core();
12561
12589
  }
12562
12590
  });
12591
+
12592
+ // ../form-validator/dist/esm/lib/utils/compile-is-multiple-of.js
12593
+ function compileIsMultipleOf(multipleOf) {
12594
+ if (!Number.isFinite(multipleOf) || multipleOf <= 0) {
12595
+ return () => false;
12596
+ }
12597
+ const divisor = toDecimal(multipleOf);
12598
+ const maxTolerableNoise = multipleOf * MAX_NOISE_TO_STEP_RATIO;
12599
+ return (value) => {
12600
+ if (!Number.isFinite(value)) {
12601
+ return true;
12602
+ }
12603
+ const dividend = toDecimal(value);
12604
+ const scale = Math.max(dividend.scale, divisor.scale);
12605
+ const scaledDivisor = toScaledInteger(divisor, scale);
12606
+ const remainder = toScaledInteger(dividend, scale) % scaledDivisor;
12607
+ if (remainder === 0n) {
12608
+ return true;
12609
+ }
12610
+ const noise = FLOATING_POINT_NOISE_ULPS * Math.abs(value) * Number.EPSILON;
12611
+ const distanceToNearestMultiple = remainder < scaledDivisor - remainder ? remainder : scaledDivisor - remainder;
12612
+ return isWithinNoise(distanceToNearestMultiple, scale, Math.min(noise, maxTolerableNoise));
12613
+ };
12614
+ }
12615
+ function isWithinNoise(distance, distanceScale, noise) {
12616
+ const noiseDecimal = toDecimal(noise);
12617
+ const scale = Math.max(distanceScale, noiseDecimal.scale);
12618
+ return distance * 10n ** BigInt(scale - distanceScale) <= toScaledInteger(noiseDecimal, scale);
12619
+ }
12620
+ var FLOATING_POINT_NOISE_ULPS, MAX_NOISE_TO_STEP_RATIO;
12621
+ var init_compile_is_multiple_of = __esm({
12622
+ "../form-validator/dist/esm/lib/utils/compile-is-multiple-of.js"() {
12623
+ init_to_decimal();
12624
+ FLOATING_POINT_NOISE_ULPS = 4;
12625
+ MAX_NOISE_TO_STEP_RATIO = 1e-6;
12626
+ }
12627
+ });
12563
12628
  function parseLocalDate(value) {
12564
12629
  const match4 = LOCAL_DATE_REGEX.exec(value);
12565
12630
  if (!match4 || isNaN(new Date(value).getTime())) {
@@ -12630,6 +12695,16 @@ var init_date_availability = __esm({
12630
12695
 
12631
12696
  // ../form-validator/dist/esm/lib/submission-validation/ajv/ajv-custom-keywords.js
12632
12697
  function addCustomKeywords(ajv, phoneValidationEnabled, lenientPhoneValidation) {
12698
+ ajv.removeKeyword("multipleOf");
12699
+ ajv.addKeyword({
12700
+ keyword: "multipleOf",
12701
+ type: "number",
12702
+ schemaType: "number",
12703
+ errors: true,
12704
+ compile(multipleOf) {
12705
+ return compileMultipleOfValidation(multipleOf);
12706
+ }
12707
+ });
12633
12708
  ajv.addKeyword({
12634
12709
  keyword: "dateAvailability",
12635
12710
  type: "string",
@@ -12789,6 +12864,26 @@ function getCallingCode(phoneNumber) {
12789
12864
  function onlyCallingCode(callingCode, data) {
12790
12865
  return data.replace(/^\+/, "") === callingCode;
12791
12866
  }
12867
+ function compileMultipleOfValidation(multipleOf) {
12868
+ const isMultipleOf = compileIsMultipleOf(multipleOf);
12869
+ const validate2 = (data) => {
12870
+ if (typeof data !== "number" || isMultipleOf(data)) {
12871
+ return true;
12872
+ }
12873
+ const errors = [
12874
+ {
12875
+ keyword: "multipleOf",
12876
+ message: `must be multiple of ${multipleOf}`,
12877
+ params: {
12878
+ multipleOf
12879
+ }
12880
+ }
12881
+ ];
12882
+ validate2.errors = errors;
12883
+ return false;
12884
+ };
12885
+ return validate2;
12886
+ }
12792
12887
  function compileDateAvailabilityValidation(availability) {
12793
12888
  const validate2 = (data) => {
12794
12889
  if (typeof data !== "string") {
@@ -12815,6 +12910,7 @@ var init_ajv_custom_keywords = __esm({
12815
12910
  "../form-validator/dist/esm/lib/submission-validation/ajv/ajv-custom-keywords.js"() {
12816
12911
  init_max();
12817
12912
  init_metadata_max_json();
12913
+ init_compile_is_multiple_of();
12818
12914
  init_date_availability();
12819
12915
  }
12820
12916
  });
@@ -12823,8 +12919,7 @@ var init_ajv_custom_keywords = __esm({
12823
12919
  function createAjvSchemaValidatorFactory(formSchemaMapper, phoneValidationEnabled, lenientPhoneValidation) {
12824
12920
  return function createAjvSchemaValidator(formsSchemas) {
12825
12921
  const ajv = new import_ajv.default({
12826
- allErrors: true,
12827
- multipleOfPrecision: 8
12922
+ allErrors: true
12828
12923
  });
12829
12924
  (0, import_ajv_formats.default)(ajv);
12830
12925
  addCustomFormats(ajv, phoneValidationEnabled);
@@ -43757,6 +43852,7 @@ function useTranslation2() {
43757
43852
 
43758
43853
  // ../form-experiments/dist/esm/experiments.js
43759
43854
  var EXPERIMENTS = {
43855
+ ADDRESS_HOUSE_NUMBER_FALLBACK: "addressHouseNumberFallback",
43760
43856
  ENABLE_FORM_SCHEMA_V2_PRICING_PLANS: "enableFormSchemaV2PricingPlans",
43761
43857
  STREETLESS_LOCALITY_FALLBACK: "streetlessLocalityFallback",
43762
43858
  SUBMIT_SUCCESS_NO_FOCUS: "specs.forms.SubmitSuccessNoFocus",
@@ -58676,6 +58772,22 @@ var searchByPostalCode = async ({ postalCode, countryCode }) => {
58676
58772
  proximity
58677
58773
  })));
58678
58774
  };
58775
+ var searchByQuery = async ({ query, countryCode }) => {
58776
+ const { searchResults } = await es_exports8.search({
58777
+ query,
58778
+ ...countryCode ? {
58779
+ countryCodes: [
58780
+ countryCode
58781
+ ]
58782
+ } : {}
58783
+ });
58784
+ return (searchResults ?? []).flatMap(({ address }) => {
58785
+ const commonAddress = toCommonAddress(address);
58786
+ return commonAddress ? [
58787
+ commonAddress
58788
+ ] : [];
58789
+ });
58790
+ };
58679
58791
  var AddressFieldsContext = /* @__PURE__ */ createContext(void 0);
58680
58792
  var AddressFieldsProvider = ({ children, fields }) => {
58681
58793
  const value = useMemo(() => ({
@@ -58758,35 +58870,111 @@ AddressLine2.Label = Label;
58758
58870
  AddressLine2.Input = Input3;
58759
58871
  AddressLine2.Description = Description;
58760
58872
  AddressLine2.Error = Error2;
58873
+
58874
+ // ../form-fields/dist/esm/ui/address-input/resolve-place.js
58875
+ var STREET_PLACE_TYPE = "route";
58876
+ var isStreetPrediction = (prediction) => prediction.types?.includes(STREET_PLACE_TYPE) ?? false;
58877
+ var ADDRESS_TEXT_SEPARATORS = /[\s,]+/;
58878
+ var toAddressTokens = (text) => text ? text.toLowerCase().split(ADDRESS_TEXT_SEPARATORS) : [];
58879
+ var containsHouseNumber = (text, houseNumber, streetName) => {
58880
+ if (!text || !houseNumber) {
58881
+ return false;
58882
+ }
58883
+ const streetNameTokens = toAddressTokens(streetName);
58884
+ return toAddressTokens(text).filter((token) => !streetNameTokens.includes(token)).includes(houseNumber.toLowerCase());
58885
+ };
58886
+ var postalCodeBase = (postalCode) => postalCode.split("-")[0];
58887
+ var isSamePostalArea = (place, candidate) => !place.postalCode || !candidate.postalCode || postalCodeBase(place.postalCode) === postalCodeBase(candidate.postalCode);
58888
+ var isSameStreet = (place, candidate) => Boolean(place.streetAddress?.name) && place.streetAddress?.name?.toLowerCase() === candidate.streetAddress?.name?.toLowerCase();
58889
+ var isSameStreetLocation = (place, candidate) => candidate.country === place.country && candidate.subdivision === place.subdivision && isSamePostalArea(place, candidate) && isSameStreet(place, candidate);
58890
+ var searchHouseNumber = async (query, place, searchByQuery2) => {
58891
+ try {
58892
+ const candidates = await searchByQuery2({
58893
+ query,
58894
+ countryCode: place.country ?? void 0
58895
+ });
58896
+ return candidates?.find((candidate) => isSameStreetLocation(place, candidate) && containsHouseNumber(query, candidate.streetAddress?.number, place.streetAddress?.name))?.streetAddress?.number;
58897
+ } catch {
58898
+ return void 0;
58899
+ }
58900
+ };
58901
+ var withHouseNumber = (place, houseNumber, suggestionText) => {
58902
+ const streetAddress = {
58903
+ ...place.streetAddress,
58904
+ number: houseNumber
58905
+ };
58906
+ if (!containsHouseNumber(suggestionText, houseNumber)) {
58907
+ return {
58908
+ ...place,
58909
+ streetAddress
58910
+ };
58911
+ }
58912
+ return {
58913
+ ...place,
58914
+ ...place.addressLine ? {
58915
+ addressLine: suggestionText
58916
+ } : {},
58917
+ streetAddress: {
58918
+ ...streetAddress,
58919
+ formattedAddressLine: suggestionText
58920
+ }
58921
+ };
58922
+ };
58923
+ var resolvePlace = async ({ searchId, prediction, getPlace: getPlace6, searchByQuery: searchByQuery2 }) => {
58924
+ const place = await getPlace6({
58925
+ searchId
58926
+ });
58927
+ if (!place || place.streetAddress?.number || !prediction?.description || !isStreetPrediction(prediction) || !searchByQuery2) {
58928
+ return place;
58929
+ }
58930
+ const houseNumber = await searchHouseNumber(prediction.description, place, searchByQuery2);
58931
+ return houseNumber ? withHouseNumber(place, houseNumber, prediction.textStructure?.mainText) : place;
58932
+ };
58933
+
58934
+ // ../form-fields/dist/esm/ui/address-input/use-atlas.js
58761
58935
  var useAtlas = ({ enabled }) => {
58762
58936
  const { actions } = useFieldActions();
58937
+ const { isExperimentEnabled: isExperimentEnabled2 } = useExperiments();
58938
+ const addressHouseNumberFallbackEnabled = isExperimentEnabled2(EXPERIMENTS.ADDRESS_HOUSE_NUMBER_FALLBACK);
58763
58939
  const [sessionToken, setSessionToken] = useState(generateUUID());
58764
58940
  const refreshSessionToken = useCallback(() => setSessionToken(generateUUID()), []);
58941
+ const resolvePlaceRequestId = useRef(0);
58765
58942
  return useMemo(() => {
58766
- const { predict: predict6, getPlace: getPlace6, searchByPostalCode: searchByPostalCode2 } = actions ?? {};
58943
+ const { predict: predict6, getPlace: getPlace6, searchByPostalCode: searchByPostalCode2, searchByQuery: searchByQuery2 } = actions ?? {};
58767
58944
  if (!enabled || !predict6 || !getPlace6 || !searchByPostalCode2) {
58768
58945
  return void 0;
58769
58946
  }
58947
+ const getPlaceInSession = (params) => getPlace6({
58948
+ ...params,
58949
+ sessionToken,
58950
+ refreshSessionToken
58951
+ });
58770
58952
  return {
58771
58953
  predict: (params) => predict6({
58772
58954
  ...params,
58773
58955
  sessionToken
58774
58956
  }),
58775
- getPlace: (params) => getPlace6({
58776
- ...params,
58777
- sessionToken,
58778
- refreshSessionToken
58779
- }),
58957
+ getPlace: getPlaceInSession,
58780
58958
  searchByPostalCode: (params) => searchByPostalCode2({
58781
58959
  ...params,
58782
58960
  sessionToken
58783
- })
58961
+ }),
58962
+ resolvePlace: async (params) => {
58963
+ const requestId = ++resolvePlaceRequestId.current;
58964
+ const place = await resolvePlace({
58965
+ ...params,
58966
+ getPlace: getPlaceInSession,
58967
+ searchByQuery: addressHouseNumberFallbackEnabled ? searchByQuery2 : void 0
58968
+ });
58969
+ return requestId === resolvePlaceRequestId.current ? place : void 0;
58970
+ }
58784
58971
  };
58785
58972
  }, [
58786
58973
  enabled,
58787
58974
  actions,
58788
58975
  sessionToken,
58789
- refreshSessionToken
58976
+ refreshSessionToken,
58977
+ addressHouseNumberFallbackEnabled
58790
58978
  ]);
58791
58979
  };
58792
58980
 
@@ -58987,8 +59175,10 @@ var AddressLineAutocomplete = ({ children, value, onChange, disabled, readOnly,
58987
59175
  return;
58988
59176
  }
58989
59177
  clearPredictions();
58990
- const address = await atlas?.getPlace({
58991
- searchId: String(key)
59178
+ const searchId = String(key);
59179
+ const address = await atlas?.resolvePlace({
59180
+ searchId,
59181
+ prediction: predictions.find((prediction) => prediction.searchId === searchId)
58992
59182
  });
58993
59183
  if (address && onAddressSelect) {
58994
59184
  onAddressSelect(address);
@@ -58996,7 +59186,8 @@ var AddressLineAutocomplete = ({ children, value, onChange, disabled, readOnly,
58996
59186
  }, [
58997
59187
  onAddressSelect,
58998
59188
  clearPredictions,
58999
- atlas
59189
+ atlas,
59190
+ predictions
59000
59191
  ]);
59001
59192
  const handleBlur = useCallback(() => {
59002
59193
  clearPredictions();
@@ -59763,8 +59954,10 @@ var StreetNameAutocomplete = ({ children, value, onChange, disabled, readOnly, r
59763
59954
  if (!key) {
59764
59955
  return;
59765
59956
  }
59766
- const address = await atlas?.getPlace({
59767
- searchId: String(key)
59957
+ const searchId = String(key);
59958
+ const address = await atlas?.resolvePlace({
59959
+ searchId,
59960
+ prediction: predictions.find((prediction) => prediction.searchId === searchId)
59768
59961
  });
59769
59962
  if (address && onAddressSelect) {
59770
59963
  onAddressSelect(address);
@@ -59772,7 +59965,8 @@ var StreetNameAutocomplete = ({ children, value, onChange, disabled, readOnly, r
59772
59965
  }, [
59773
59966
  onAddressSelect,
59774
59967
  clearPredictions,
59775
- atlas
59968
+ atlas,
59969
+ predictions
59776
59970
  ]);
59777
59971
  const handleBlur = useCallback(() => {
59778
59972
  clearPredictions();
@@ -60077,15 +60271,21 @@ var MultilineAddress = ({ children, className }) => {
60077
60271
  const { validateFields: parentValidateFields } = useFieldValidation();
60078
60272
  const formSchema = useForm();
60079
60273
  const fieldActions = useFieldActions();
60080
- const headlessFieldActions = useMemo(() => ({
60081
- ...fieldActions,
60082
- actions: {
60083
- predict: predict5,
60084
- getPlace: getPlace5,
60085
- searchByPostalCode,
60086
- ...fieldActions.actions
60087
- }
60088
- }), [
60274
+ const headlessFieldActions = useMemo(() => {
60275
+ const hostResolvesPlaces = Boolean(fieldActions.actions?.getPlace);
60276
+ return {
60277
+ ...fieldActions,
60278
+ actions: {
60279
+ predict: predict5,
60280
+ getPlace: getPlace5,
60281
+ searchByPostalCode,
60282
+ ...hostResolvesPlaces ? {} : {
60283
+ searchByQuery
60284
+ },
60285
+ ...fieldActions.actions
60286
+ }
60287
+ };
60288
+ }, [
60089
60289
  fieldActions
60090
60290
  ]);
60091
60291
  const { form, isMultilineAddressField: isMultilineAddressField2 } = useMultilineAddressForm({