@influenzanet/case-web-app-core 2.9.7-staging → 2.9.8-staging

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/build/index.es.js CHANGED
@@ -20990,6 +20990,67 @@ var COUNTRY_CODES = [
20990
20990
  }
20991
20991
  ];
20992
20992
 
20993
+ var DEFAULT_COUNTRY_CODES = COUNTRY_CODES.map(function (country) { return country.code; });
20994
+ // "+" and the "00" international dialling prefix are the only markers that make a number
20995
+ // unambiguously international. A bare country code is not one: several of them (31, 33, 34, 36,
20996
+ // 39, ...) are also valid starts of an Italian mobile number, so reading those as a prefix would
20997
+ // corrupt numbers entered in national form.
20998
+ var INTERNATIONAL_PREFIX = /^\s*(?:\+|00)/;
20999
+ /**
21000
+ * Splits a number written in international form into its country code and its national part, so a
21001
+ * pasted or autofilled number that already carries the prefix does not get prefixed a second time.
21002
+ * Returns null when the value is not in international form, or when no known country code matches
21003
+ * it yet, leaving the caller free to keep treating it as a national number.
21004
+ */
21005
+ var parseInternationalPhoneNumber = function (value, countryCodes) {
21006
+ if (countryCodes === void 0) { countryCodes = DEFAULT_COUNTRY_CODES; }
21007
+ var marker = INTERNATIONAL_PREFIX.exec(value);
21008
+ if (!marker) {
21009
+ return null;
21010
+ }
21011
+ var digits = value.slice(marker[0].length).replace(/\D/g, '');
21012
+ var matches = countryCodes.filter(function (code) { return digits.startsWith(code.slice(1)); });
21013
+ if (matches.length === 0) {
21014
+ return null;
21015
+ }
21016
+ // The longest match wins, so a country code starting with another one is not shadowed by it.
21017
+ var countryCode = matches.reduce(function (longest, code) {
21018
+ return code.length > longest.length ? code : longest;
21019
+ });
21020
+ return { countryCode: countryCode, localNumber: digits.slice(countryCode.length - 1) };
21021
+ };
21022
+ /**
21023
+ * Keeps the characters a national number may contain: digits, plus the spaces and hyphens people
21024
+ * use to group them. A leading "+" is kept as well, so a number being typed in international form
21025
+ * stays recognisable until enough digits are there to identify its country code.
21026
+ */
21027
+ var sanitizePhoneNumberInput = function (value) {
21028
+ // Both international markers are kept as a single "+", so a "00" number, or a "+" typed after a
21029
+ // space, is still recognised as international by composePhoneNumber and never prefixed again.
21030
+ var marker = INTERNATIONAL_PREFIX.exec(value);
21031
+ if (marker) {
21032
+ return '+' + value.slice(marker[0].length).replace(/[^\d\s-]/g, '');
21033
+ }
21034
+ return value.replace(/[^\d\s-]/g, '');
21035
+ };
21036
+ /**
21037
+ * Builds the value the form works with out of the selected prefix and the national number.
21038
+ */
21039
+ var composePhoneNumber = function (countryCode, localNumber) {
21040
+ // A prefix on its own is not a phone number: emitting one would make an empty field read as a
21041
+ // malformed number to the forms that treat the phone as optional. A value without any digit
21042
+ // (a stray "+" or spaces) counts as empty for the same reason.
21043
+ if (!/\d/.test(localNumber)) {
21044
+ return '';
21045
+ }
21046
+ // A number still in international form has no country code of its own yet, so it is reported as
21047
+ // it was typed rather than being prefixed again.
21048
+ if (localNumber.startsWith('+')) {
21049
+ return localNumber;
21050
+ }
21051
+ return countryCode + localNumber;
21052
+ };
21053
+
20993
21054
  var PhoneNumberInput = function (_a) {
20994
21055
  var value = _a.value, onChange = _a.onChange, label = _a.label, placeholder = _a.placeholder, className = _a.className, autoFocus = _a.autoFocus, disabled = _a.disabled, error = _a.error, onBlur = _a.onBlur;
20995
21056
  var t = useTranslation(['dialogs']).t;
@@ -21004,18 +21065,26 @@ var PhoneNumberInput = function (_a) {
21004
21065
  }), phoneNumber = _c[0], setPhoneNumber = _c[1];
21005
21066
  var handleCountryCodeChange = function (newCode) {
21006
21067
  setCountryCode(newCode);
21007
- onChange(newCode + phoneNumber);
21068
+ onChange(composePhoneNumber(newCode, phoneNumber));
21008
21069
  };
21009
21070
  var handlePhoneNumberChange = function (newNumber) {
21010
- // Strip non-numeric characters (keep spaces and hyphens for readability)
21011
- var cleanNumber = newNumber.replace(/[^\d\s-]/g, '');
21071
+ // A number pasted or autofilled with its own prefix is split again, instead of being prefixed
21072
+ // a second time with the selected country code.
21073
+ var parsed = parseInternationalPhoneNumber(newNumber);
21074
+ if (parsed) {
21075
+ setCountryCode(parsed.countryCode);
21076
+ setPhoneNumber(parsed.localNumber);
21077
+ onChange(composePhoneNumber(parsed.countryCode, parsed.localNumber));
21078
+ return;
21079
+ }
21080
+ var cleanNumber = sanitizePhoneNumberInput(newNumber);
21012
21081
  setPhoneNumber(cleanNumber);
21013
- onChange(countryCode + cleanNumber);
21082
+ onChange(composePhoneNumber(countryCode, cleanNumber));
21014
21083
  };
21015
21084
  return (jsxs("div", __assign({ className: className }, { children: [label && (jsx("label", __assign({ className: "form-label mb-1" }, { children: label }), void 0)), jsxs("div", __assign({ className: "d-flex" }, { children: [jsx(SelectField, { className: "me-2", style: { width: '120px', flexShrink: 0 }, value: countryCode, onChange: function (event) { return handleCountryCodeChange(event.target.value); }, disabled: disabled, values: COUNTRY_CODES.map(function (country) { return ({
21016
21085
  code: country.code,
21017
21086
  label: "".concat(country.code, " ").concat(country.country)
21018
- }); }) }, void 0), jsx(TextField, { type: "text", placeholder: placeholder || t('addPhone.phoneInputPlaceholder'), value: phoneNumber, autoFocus: autoFocus, autoComplete: "tel", disabled: disabled, className: "flex-grow-1", onChange: function (event) { return handlePhoneNumberChange(event.target.value); }, onBlur: onBlur, style: { marginBottom: 0 } }, void 0)] }), void 0), jsxs("small", __assign({ className: "text-muted mt-1 d-block" }, { children: [t('addPhone.completeNumber'), ": ", countryCode + phoneNumber] }), void 0), error && (jsx("div", __assign({ className: "text-danger mt-1 small" }, { children: error }), void 0))] }), void 0));
21087
+ }); }) }, void 0), jsx(TextField, { type: "text", placeholder: placeholder || t('addPhone.phoneInputPlaceholder'), value: phoneNumber, autoFocus: autoFocus, autoComplete: "tel", disabled: disabled, className: "flex-grow-1", onChange: function (event) { return handlePhoneNumberChange(event.target.value); }, onBlur: onBlur, style: { marginBottom: 0 } }, void 0)] }), void 0), jsxs("small", __assign({ className: "text-muted mt-1 d-block" }, { children: [t('addPhone.completeNumber'), ": ", composePhoneNumber(countryCode, phoneNumber) || countryCode] }), void 0), error && (jsx("div", __assign({ className: "text-danger mt-1 small" }, { children: error }), void 0))] }), void 0));
21019
21088
  };
21020
21089
 
21021
21090
  var marginBottomClass = "mb-2";
@@ -22712,9 +22781,14 @@ var AddPhone = function () {
22712
22781
  });
22713
22782
  };
22714
22783
  var updateFullPhoneNumber = function (countryCode, phoneNumber) {
22715
- var cleanNumber = phoneNumber.replace(/[^\d\s-]/g, "");
22716
- var fullPhone = countryCode + cleanNumber;
22717
- setFormData(function (prev) { return (__assign(__assign({}, prev), { countryCode: countryCode, phoneNumber: cleanNumber, newPhone: fullPhone })); });
22784
+ // A number pasted or autofilled with its own prefix is split again, instead of being prefixed
22785
+ // a second time with the selected country code.
22786
+ var parsed = parseInternationalPhoneNumber(phoneNumber);
22787
+ var nextCountryCode = parsed ? parsed.countryCode : countryCode;
22788
+ var cleanNumber = parsed
22789
+ ? parsed.localNumber
22790
+ : sanitizePhoneNumberInput(phoneNumber);
22791
+ setFormData(function (prev) { return (__assign(__assign({}, prev), { countryCode: nextCountryCode, phoneNumber: cleanNumber, newPhone: composePhoneNumber(nextCountryCode, cleanNumber) })); });
22718
22792
  };
22719
22793
  var handleCountryCodeChange = function (event) {
22720
22794
  var newCountryCode = event.target.value;