@descope/web-components-ui 2.2.34 → 2.2.35

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.
@@ -1,7 +1,7 @@
1
1
  import { createBaseInputClass } from '../../../../baseClasses/createBaseInputClass';
2
2
  import { forwardAttrs, getComponentName } from '../../../../helpers/componentHelpers';
3
3
  import CountryCodes from '../../CountryCodes';
4
- import { comboBoxItem, parsePhoneNumber } from '../helpers';
4
+ import { comboBoxItem } from '../helpers';
5
5
  import { AsYouType, parsePhoneNumberFromString } from 'libphonenumber-js/min';
6
6
 
7
7
  export const componentName = getComponentName('phone-field-internal');
@@ -112,18 +112,24 @@ class PhoneFieldInternal extends BaseInputClass {
112
112
  return '';
113
113
  }
114
114
 
115
- const [countryCode, phoneNumber] = parsePhoneNumber(
116
- `${this.comboBox.value}-${this.textField.value}`
117
- );
115
+ if (this.allowAlphanumericInput) {
116
+ return `${this.comboBox.value}-${this.textField.value}`;
117
+ }
118
118
 
119
- return `${countryCode || this.comboBox.value}-${phoneNumber || this.textField.value}`;
119
+ return `${this.comboBox.value}-${this.textField.value.replace(/\D+/g, '')}`;
120
120
  }
121
121
 
122
122
  set value(val) {
123
- const [countryCode, nationalNumber] = parsePhoneNumber(val);
123
+ const nationalNumber = val.replace(
124
+ new RegExp(`^\\+?${this.comboBox.value.replace('+', '')}-?`),
125
+ ''
126
+ );
124
127
 
125
- this.#setCountryCode(countryCode);
126
- this.#setNationalNumber(nationalNumber);
128
+ if (this.isFormatValue) {
129
+ this.textField.value = this.#formatNationalNumber(nationalNumber);
130
+ } else {
131
+ this.textField.value = nationalNumber;
132
+ }
127
133
  }
128
134
 
129
135
  init() {
@@ -212,41 +218,6 @@ class PhoneFieldInternal extends BaseInputClass {
212
218
 
213
219
  this.handleFocusEventsDispatching(this.inputs);
214
220
  this.handleInputEventDispatching();
215
-
216
- // verify country code item against phone number pattern and replace if needed and country is allowed
217
- // (e.g. +1 can be US or CA, depending on the pattern)
218
- this.addEventListener('input', this.#handleSameCountryCodes.bind(this));
219
- }
220
-
221
- #setCountryCode(val) {
222
- if (!val || val === this.selectedCountryCode) return;
223
-
224
- let countryCodeItem = undefined;
225
-
226
- if (this.value) {
227
- // try to parse the phone number, and set country code item according to actual dial code (e.g. `+1` can be `US` or `CA`)
228
- const code = this.#getCountryCodeByPhoneNumber(`${val}-${this.textField.value}`);
229
- countryCodeItem = this.#getCountryByCodeId(code);
230
- }
231
-
232
- // in case country code item does not exist (for example: Parsed code is CA for +1 - but Canada is not allowed)
233
- // then use the first option with same dial code (e.g. in that case - `US` for +1)
234
- if (!countryCodeItem) {
235
- countryCodeItem = this.#getCountryByDialCode(val);
236
- }
237
-
238
- // set country code item; in it doesn't exist in list - set `undefined`
239
- this.comboBox.selectedItem = countryCodeItem;
240
- }
241
-
242
- #setNationalNumber(val) {
243
- if (this.isFormatValue) {
244
- val = this.#formatNationalNumber(val);
245
- }
246
-
247
- if (this.textField.value !== val) {
248
- this.textField.value = val;
249
- }
250
221
  }
251
222
 
252
223
  #formatNationalNumber(nationalNumber = '') {
@@ -259,7 +230,6 @@ class PhoneFieldInternal extends BaseInputClass {
259
230
  this.#ayt.reset();
260
231
 
261
232
  const formattedVal = this.#ayt.input(nationalNumber);
262
-
263
233
  return formattedVal || nationalNumber;
264
234
  }
265
235
 
@@ -282,24 +252,11 @@ class PhoneFieldInternal extends BaseInputClass {
282
252
  return this.restrictCountries.includes(countryCode);
283
253
  }
284
254
 
285
- // return country item by dial code `data-id` (e.g. `+1`)
286
- #getCountryByDialCode(dialCode) {
287
- return this.comboBox.items?.find((c) => c.getAttribute('data-id') === dialCode) || undefined;
288
- }
289
-
290
255
  // return country item by country code `data-country-code` (e.g. `US`)
291
256
  #getCountryByCodeId(countryCode) {
292
257
  return this.comboBox.items?.find((c) => c.getAttribute('data-country-code') === countryCode);
293
258
  }
294
259
 
295
- #getCountryCodeByPhoneNumber(val) {
296
- if (!val) return undefined;
297
- const parsed = parsePhoneNumberFromString(val);
298
- if (!parsed?.country) return undefined;
299
- const foundCountryItem = this.#getCountryByCodeId(parsed.country);
300
- return foundCountryItem?.getAttribute('data-country-code');
301
- }
302
-
303
260
  #updateComboBoxItems(restrictCountries) {
304
261
  const items = restrictCountries.length
305
262
  ? CountryCodes.filter((c) => restrictCountries.includes(c.code))
@@ -324,23 +281,6 @@ class PhoneFieldInternal extends BaseInputClass {
324
281
  }
325
282
  }
326
283
 
327
- #handleSameCountryCodes() {
328
- if (!this.value) return;
329
-
330
- const country = this.#getCountryCodeByPhoneNumber(this.value);
331
-
332
- if (!country) return;
333
-
334
- // re-set country code if needed (same coutnry code for different countries, e.g. +1 for US or CA)
335
- if (this.selectedCountryCode !== country) {
336
- const foundCountryItem = this.#getCountryByCodeId(country);
337
- // if found country is defined in country list then set it, otherwise - clear phone number
338
- if (foundCountryItem) {
339
- this.comboBox.selectedItem = foundCountryItem;
340
- }
341
- }
342
- }
343
-
344
284
  #handleLabelTypeAttrs(attrName, newValue) {
345
285
  // set or remove label attributes from inner text/combo components on `label-type` change
346
286
  const attr = mapAttrs[attrName] || attrName;