@transferwise/components 0.0.0-experimental-4b360b4 → 0.0.0-experimental-0b0d7e6

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.
Files changed (110) hide show
  1. package/build/index.esm.js +115 -272
  2. package/build/index.esm.js.map +1 -1
  3. package/build/index.js +114 -271
  4. package/build/index.js.map +1 -1
  5. package/build/types/common/locale/index.d.ts +26 -43
  6. package/build/types/common/locale/index.d.ts.map +1 -1
  7. package/build/types/phoneNumberInput/PhoneNumberInput.d.ts +22 -27
  8. package/build/types/phoneNumberInput/PhoneNumberInput.d.ts.map +1 -1
  9. package/build/types/phoneNumberInput/data/countries.d.ts +5 -10
  10. package/build/types/phoneNumberInput/data/countries.d.ts.map +1 -1
  11. package/build/types/phoneNumberInput/index.d.ts +1 -1
  12. package/build/types/phoneNumberInput/index.d.ts.map +1 -1
  13. package/build/types/phoneNumberInput/utils/cleanNumber/cleanNumber.d.ts +1 -1
  14. package/build/types/phoneNumberInput/utils/cleanNumber/cleanNumber.d.ts.map +1 -1
  15. package/build/types/phoneNumberInput/utils/cleanNumber/index.d.ts +1 -1
  16. package/build/types/phoneNumberInput/utils/cleanNumber/index.d.ts.map +1 -1
  17. package/build/types/phoneNumberInput/utils/excludeCountries/excludeCountries.d.ts +8 -1
  18. package/build/types/phoneNumberInput/utils/excludeCountries/excludeCountries.d.ts.map +1 -1
  19. package/build/types/phoneNumberInput/utils/excludeCountries/index.d.ts +1 -1
  20. package/build/types/phoneNumberInput/utils/excludeCountries/index.d.ts.map +1 -1
  21. package/build/types/phoneNumberInput/utils/explodeNumberModel/index.d.ts +8 -4
  22. package/build/types/phoneNumberInput/utils/explodeNumberModel/index.d.ts.map +1 -1
  23. package/build/types/phoneNumberInput/utils/findCountryByCode/index.d.ts +1 -1
  24. package/build/types/phoneNumberInput/utils/findCountryByCode/index.d.ts.map +1 -1
  25. package/build/types/phoneNumberInput/utils/findCountryByPrefix/index.d.ts +1 -1
  26. package/build/types/phoneNumberInput/utils/findCountryByPrefix/index.d.ts.map +1 -1
  27. package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.d.ts +2 -1
  28. package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.d.ts.map +1 -1
  29. package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/index.d.ts +1 -1
  30. package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/index.d.ts.map +1 -1
  31. package/build/types/phoneNumberInput/utils/index.d.ts +11 -13
  32. package/build/types/phoneNumberInput/utils/index.d.ts.map +1 -1
  33. package/build/types/phoneNumberInput/utils/isStringNumeric/index.d.ts +1 -1
  34. package/build/types/phoneNumberInput/utils/isStringNumeric/index.d.ts.map +1 -1
  35. package/build/types/phoneNumberInput/utils/isStringNumeric/isStringNumeric.d.ts +1 -1
  36. package/build/types/phoneNumberInput/utils/isStringNumeric/isStringNumeric.d.ts.map +1 -1
  37. package/build/types/phoneNumberInput/utils/isValidPhoneNumber/index.d.ts +1 -1
  38. package/build/types/phoneNumberInput/utils/isValidPhoneNumber/index.d.ts.map +1 -1
  39. package/build/types/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.d.ts +6 -1
  40. package/build/types/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.d.ts.map +1 -1
  41. package/build/types/phoneNumberInput/utils/longestMatchingPrefix/index.d.ts +2 -1
  42. package/build/types/phoneNumberInput/utils/longestMatchingPrefix/index.d.ts.map +1 -1
  43. package/build/types/phoneNumberInput/utils/setDefaultPrefix/index.d.ts +7 -1
  44. package/build/types/phoneNumberInput/utils/setDefaultPrefix/index.d.ts.map +1 -1
  45. package/build/types/phoneNumberInput/utils/sortArrayByProperty/index.d.ts +1 -1
  46. package/build/types/phoneNumberInput/utils/sortArrayByProperty/index.d.ts.map +1 -1
  47. package/build/types/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.d.ts +1 -1
  48. package/build/types/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.d.ts.map +1 -1
  49. package/package.json +1 -1
  50. package/src/common/locale/{index.spec.js → index.spec.ts} +4 -4
  51. package/src/common/locale/index.ts +96 -0
  52. package/src/phoneNumberInput/PhoneNumberInput.spec.js +18 -22
  53. package/src/phoneNumberInput/PhoneNumberInput.tsx +193 -0
  54. package/src/phoneNumberInput/data/{countries.js → countries.ts} +9 -1
  55. package/src/phoneNumberInput/utils/cleanNumber/cleanNumber.ts +3 -0
  56. package/src/phoneNumberInput/utils/excludeCountries/{excludeCountries.spec.js → excludeCountries.spec.ts} +1 -1
  57. package/src/phoneNumberInput/utils/excludeCountries/{excludeCountries.js → excludeCountries.ts} +6 -5
  58. package/src/phoneNumberInput/utils/explodeNumberModel/{explodeNumberModel.spec.js → explodeNumberModel.spec.ts} +1 -1
  59. package/src/phoneNumberInput/utils/explodeNumberModel/index.ts +24 -0
  60. package/src/phoneNumberInput/utils/findCountryByCode/{findCountryByCode.spec.js → findCountryByCode.spec.ts} +0 -1
  61. package/src/phoneNumberInput/utils/findCountryByCode/index.ts +12 -0
  62. package/src/phoneNumberInput/utils/findCountryByPrefix/index.ts +12 -0
  63. package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.spec.ts +102 -0
  64. package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.ts +12 -0
  65. package/src/phoneNumberInput/utils/{index.js → index.ts} +0 -2
  66. package/src/phoneNumberInput/utils/isStringNumeric/{isStringNumeric.spec.js → isStringNumeric.spec.ts} +0 -1
  67. package/src/phoneNumberInput/utils/isStringNumeric/isStringNumeric.ts +1 -0
  68. package/src/phoneNumberInput/utils/isValidPhoneNumber/{isValidPhoneNumber.spec.js → isValidPhoneNumber.spec.ts} +1 -1
  69. package/src/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.ts +7 -0
  70. package/src/phoneNumberInput/utils/longestMatchingPrefix/index.ts +4 -0
  71. package/src/phoneNumberInput/utils/setDefaultPrefix/index.ts +20 -0
  72. package/src/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.ts +6 -0
  73. package/build/types/phoneNumberInput/utils/filterOptionsForQuery/index.d.ts +0 -2
  74. package/build/types/phoneNumberInput/utils/filterOptionsForQuery/index.d.ts.map +0 -1
  75. package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/index.d.ts +0 -2
  76. package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/index.d.ts.map +0 -1
  77. package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.d.ts +0 -3
  78. package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.d.ts.map +0 -1
  79. package/src/common/locale/index.js +0 -139
  80. package/src/phoneNumberInput/PhoneNumberInput.js +0 -210
  81. package/src/phoneNumberInput/data/countries.spec.js +0 -12
  82. package/src/phoneNumberInput/utils/cleanNumber/cleanNumber.js +0 -4
  83. package/src/phoneNumberInput/utils/explodeNumberModel/index.js +0 -27
  84. package/src/phoneNumberInput/utils/filterOptionsForQuery/filterOptionsForQuery.spec.js +0 -36
  85. package/src/phoneNumberInput/utils/filterOptionsForQuery/index.js +0 -11
  86. package/src/phoneNumberInput/utils/findCountryByCode/index.js +0 -10
  87. package/src/phoneNumberInput/utils/findCountryByPrefix/index.js +0 -11
  88. package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.js +0 -26
  89. package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.spec.js +0 -67
  90. package/src/phoneNumberInput/utils/isOptionAndFitsQuery/index.js +0 -1
  91. package/src/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.js +0 -25
  92. package/src/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.spec.js +0 -66
  93. package/src/phoneNumberInput/utils/isStringNumeric/isStringNumeric.js +0 -1
  94. package/src/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.js +0 -10
  95. package/src/phoneNumberInput/utils/longestMatchingPrefix/index.js +0 -2
  96. package/src/phoneNumberInput/utils/setDefaultPrefix/index.js +0 -25
  97. package/src/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.js +0 -3
  98. /package/src/phoneNumberInput/{PhoneNumberInput.story.js → PhoneNumberInput.story.tsx} +0 -0
  99. /package/src/phoneNumberInput/{index.js → index.ts} +0 -0
  100. /package/src/phoneNumberInput/utils/cleanNumber/{cleanNumber.spec.js → cleanNumber.spec.ts} +0 -0
  101. /package/src/phoneNumberInput/utils/cleanNumber/{index.js → index.ts} +0 -0
  102. /package/src/phoneNumberInput/utils/excludeCountries/{index.js → index.ts} +0 -0
  103. /package/src/phoneNumberInput/utils/findCountryByPrefix/{findCountryByPrefix.spec.js → findCountryByPrefix.spec.ts} +0 -0
  104. /package/src/phoneNumberInput/utils/groupCountriesByPrefix/{index.js → index.ts} +0 -0
  105. /package/src/phoneNumberInput/utils/isStringNumeric/{index.js → index.ts} +0 -0
  106. /package/src/phoneNumberInput/utils/isValidPhoneNumber/{index.js → index.ts} +0 -0
  107. /package/src/phoneNumberInput/utils/longestMatchingPrefix/{longestMatchingPrefix.spec.js → longestMatchingPrefix.spec.ts} +0 -0
  108. /package/src/phoneNumberInput/utils/setDefaultPrefix/{setDefaultPrefix.spec.js → setDefaultPrefix.spec.ts} +0 -0
  109. /package/src/phoneNumberInput/utils/sortArrayByProperty/{index.js → index.ts} +0 -0
  110. /package/src/phoneNumberInput/utils/sortArrayByProperty/{sortArrayByProperty.spec.js → sortArrayByProperty.spec.ts} +0 -0
package/build/index.js CHANGED
@@ -376,131 +376,76 @@ const Key = {
376
376
  COMMA: ','
377
377
  };
378
378
 
379
- /**
380
- * Default language
381
- *
382
- * @type {string}
383
- */
384
379
  const DEFAULT_LANG = 'en';
385
-
386
- /**
387
- * Default locale
388
- *
389
- * @type {string}
390
- */
391
380
  const DEFAULT_LOCALE = 'en-GB';
392
-
393
381
  /**
394
- * Array of languages that are written from the right to the left
395
- *
396
- * @type {string[]}
382
+ * Languages written right-to-left.
397
383
  */
398
384
  const RTL_LANGUAGES = ['ar', 'he'];
399
-
400
385
  /**
401
386
  * @deprecated The source of truth for supported languages lives in Crab.
402
- * @type {string[]}
403
387
  */
404
388
  const SUPPORTED_LANGUAGES = [DEFAULT_LANG, 'de', 'es', 'fr', 'hu', 'id', 'it', 'ja', 'pl', 'pt', 'ro', 'ru', 'th', 'tr', 'uk', 'zh'];
405
-
406
389
  /**
407
- * Verifies and adjusts locale (replace `_` with `-`)
408
- * Returns null if locale is unrecognized by {Intl.Locale}
390
+ * Verifies and adjusts locale, replacing `_` with `-`.
409
391
  *
410
- * @param {string} locale (`es`, `es_ES`, `en-GB`, `en`, `ja`, `ja-JP` etc)
411
- * @returns {string|null}
392
+ * @param locale `es`, `es_ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.
393
+ * @returns `null` if locale is unrecognized by `Intl.Locale`.
412
394
  */
413
395
  function adjustLocale(locale) {
414
- if (!locale || locale.trim().length === 0) {
415
- return null;
416
- }
417
- try {
418
- const {
419
- baseName
420
- } = new Intl.Locale(locale.trim().replace('_', '-'));
421
- return baseName;
422
- } catch (error) {
423
- // eslint-disable-next-line no-console
424
- console.error(error);
425
- return null;
396
+ const localeTrimmed = locale?.trim();
397
+ if (localeTrimmed) {
398
+ try {
399
+ return new Intl.Locale(localeTrimmed.replace('_', '-')).baseName;
400
+ } catch (error) {
401
+ // eslint-disable-next-line no-console
402
+ console.error(error);
403
+ }
426
404
  }
405
+ return null;
427
406
  }
428
-
429
407
  /**
430
- * Provides corresponding lang (iso2) for provided locale
431
- * if locale is invalid or language is unsupported returns null
408
+ * Provides corresponding lang (iso2) for provided locale.
432
409
  *
433
410
  * @deprecated The use of this function almost always breaks language variants
434
411
  * e.g. Simplified and Traditional Chinese.
435
412
  * There should be no use case for this function.
436
413
  * To select the correct translations from a translations object, pass the
437
414
  * locale directly into Crab's getLocalisedMessages.
438
- * @param {string} locale (`es`, `es-ES`, `en-GB`, `en`, `ja`, `ja-JP` etc)
439
- * @returns {string|null} two-letter ISO639-1 language
415
+ * @param locale `es`, `es-ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.
416
+ * @returns Two-letter ISO 639-1 language code, falling back to `null` if locale is invalid or language is unsupported.
440
417
  */
441
418
  function getLangFromLocale(locale) {
442
419
  const adjustedLocale = adjustLocale(locale);
443
- if (adjustedLocale === null) {
444
- return null;
445
- }
446
- try {
420
+ if (adjustedLocale != null) {
447
421
  const {
448
422
  language
449
423
  } = new Intl.Locale(adjustedLocale);
450
424
  if (SUPPORTED_LANGUAGES.includes(language)) {
451
425
  return language;
452
426
  }
453
- return null;
454
- } catch (error) {
455
- // eslint-disable-next-line no-console
456
- console.error(error);
457
- return null;
458
427
  }
428
+ return null;
459
429
  }
460
-
461
430
  /**
462
- * Provides corresponding country code (iso2) for locales code with explicit region value (`es-ES`, `en-GB`, `ja-JP` etc.)
463
- * if the value is invalid or missing region it returns null
431
+ * Provides corresponding country code (iso2) for locales code with explicit region value.
464
432
  *
465
- * @param {string} locale
466
- * @returns {string|null}
433
+ * @param locale `es-ES`, `en-GB`, `ja-JP`, etc.
434
+ * @returns `null` if the locale is invalid or the region can‘t be identified.
467
435
  */
468
436
  function getCountryFromLocale(locale) {
469
437
  const adjustedLocale = adjustLocale(locale);
470
- if (adjustedLocale === null) {
471
- return null;
472
- }
473
- try {
474
- const {
475
- region
476
- } = new Intl.Locale(adjustedLocale);
477
- return region ?? null;
478
- } catch (error) {
479
- // eslint-disable-next-line no-console
480
- console.error(error);
481
- return null;
482
- }
438
+ return adjustedLocale != null ? new Intl.Locale(adjustedLocale).region ?? null : null;
483
439
  }
484
-
485
440
  /**
486
441
  * Provides the layout direction for a given locale.
487
- * If locale is invalid or language is unsupported returns Direction.LTR
488
442
  *
489
- * @param {string} locale (`es`, `es-ES`, `en-GB`, `en`, `ja`, `ja-JP` etc)
490
- * @returns {Direction} The layout direction based on the locale
443
+ * @param locale `es`, `es-ES`, `en-GB`, `en`, `ja`, `ja-JP`, etc.
444
+ * @returns The layout direction based on the locale, falling back to `Direction.LTR` if the locale is invalid or unsupported.
491
445
  */
492
446
  function getDirectionFromLocale(locale) {
493
- try {
494
- const adjustedLocale = adjustLocale(locale);
495
- const {
496
- language
497
- } = new Intl.Locale(adjustedLocale);
498
- return RTL_LANGUAGES.includes(language) ? exports.Direction.RTL : exports.Direction.LTR;
499
- } catch (error) {
500
- // eslint-disable-next-line no-console
501
- console.error(error);
502
- return exports.Direction.LTR;
503
- }
447
+ const adjustedLocale = adjustLocale(locale);
448
+ return adjustedLocale != null && RTL_LANGUAGES.includes(new Intl.Locale(adjustedLocale).language) ? exports.Direction.RTL : exports.Direction.LTR;
504
449
  }
505
450
 
506
451
  /**
@@ -9601,111 +9546,79 @@ const countries = [{
9601
9546
  iso3: 'ZWE',
9602
9547
  phone: '+263'
9603
9548
  }];
9604
- var countries$1 = countries;
9605
9549
 
9606
9550
  const longestMatchingPrefix = matchingCodes => matchingCodes.reduce((a, b) => a.phone.length > b.phone.length ? a : b);
9607
9551
 
9608
9552
  const findCountryByCode = code => {
9609
- let matchingCodes;
9610
- if (code && code.length === 2) {
9611
- matchingCodes = countries$1.filter(country => code.toUpperCase() === country.iso2);
9553
+ if (code.length === 2) {
9554
+ const matchingCodes = countries.filter(country => code.toUpperCase() === country.iso2);
9555
+ if (matchingCodes.length > 0) {
9556
+ return longestMatchingPrefix(matchingCodes);
9557
+ }
9612
9558
  }
9613
- return matchingCodes && matchingCodes.length > 0 ? longestMatchingPrefix(matchingCodes) : null;
9559
+ return null;
9614
9560
  };
9615
9561
 
9616
9562
  /**
9617
9563
  * Default phone code, the UK one `+44`
9618
9564
  */
9619
9565
  const DEFAULT_PHONE_CODE = '+44';
9620
-
9621
9566
  /**
9622
9567
  * Given a valid locale it returns the correspondent prefix if found or +44 otherwise.
9623
9568
  *
9624
- * @param {string} locale - a string that represent the locale ex:'es-ES'
9625
- * @param countryCode
9626
- * @returns {string}
9569
+ * @param locale BCP 47 language tag of locale, e.g. `"es-ES"`.
9570
+ * @param countryCode Two-letter country code (ISO 3166-1 alpha-2).
9627
9571
  */
9628
9572
  const setDefaultPrefix = (locale, countryCode) => {
9629
- const country = findCountryByCode(countryCode) ||
9630
- // when `locale` code has explicit region: `en-GB`, `en-US`, `ar-AE`
9631
- findCountryByCode(getCountryFromLocale(locale)) ||
9632
- // when `locale` code is only two chars value: `fr`, `es`
9633
- findCountryByCode(locale);
9634
- return country?.phone || DEFAULT_PHONE_CODE;
9573
+ const country = (countryCode != null ? findCountryByCode(countryCode) : null) ?? findCountryByCode(getCountryFromLocale(locale) ?? locale);
9574
+ return country?.phone ?? DEFAULT_PHONE_CODE;
9635
9575
  };
9636
9576
 
9637
9577
  /**
9638
9578
  *
9639
9579
  * @param phoneNumber
9640
- * @returns {boolean} - returns true for number that starts with '+' and contains a mix of digits and spaces with
9641
- * at least 4 digits.
9580
+ * @returns True if number that starts with "+" and contains a mix of digits and spaces with at least 4 digits.
9642
9581
  */
9643
- const isValidPhoneNumber = phoneNumber => /^\+[\d-\s]+$/.test(phoneNumber) && phoneNumber.match(/\d+/g) && phoneNumber.match(/\d+/g).join('').length >= 4;
9582
+ const isValidPhoneNumber = phoneNumber => /^\+[\d-\s]+$/.test(phoneNumber) && (phoneNumber.match(/\d+/g)?.join('').length ?? 0) >= 4;
9644
9583
 
9645
9584
  const findCountryByPrefix = number => {
9646
- let matchingCodes = null;
9647
- if (number && number.length > 1) {
9648
- matchingCodes = countries$1.filter(country => number.indexOf(country.phone) === 0);
9585
+ if (number.length > 1) {
9586
+ const matchingCodes = countries.filter(country => number.startsWith(country.phone));
9587
+ if (matchingCodes.length > 0) {
9588
+ return longestMatchingPrefix(matchingCodes);
9589
+ }
9649
9590
  }
9650
- return matchingCodes && matchingCodes.length > 0 ? longestMatchingPrefix(matchingCodes) : null;
9591
+ return null;
9651
9592
  };
9652
9593
 
9653
9594
  /**
9654
- * Given a sting in a valid format ex:'+447573135343' it returns an object of shape
9655
- * {prefix=+44 ,suffix=7573135343}
9656
- *
9657
- * @param {string} number - a string that defines a phone number.
9658
- * @returns {{prefix: (string|*), suffix: string, format: string}}
9595
+ * @param number Phone number in a format like "+447573135343"
9659
9596
  */
9660
9597
  const explodeNumberModel = number => {
9661
- let prefix = '';
9662
- let suffix = '';
9663
- let format = '';
9664
9598
  const country = findCountryByPrefix(number);
9665
- if (country) {
9666
- prefix = country.phone;
9667
- suffix = number.slice(country.phone.length);
9668
- format = country.phoneFormat || '';
9669
- } else {
9670
- prefix = '';
9671
- suffix = number.slice(1);
9672
- format = '';
9673
- }
9674
- return {
9675
- prefix,
9676
- suffix,
9677
- format
9599
+ return country ? {
9600
+ prefix: country.phone,
9601
+ suffix: number.slice(country.phone.length),
9602
+ format: country.phoneFormat
9603
+ } : {
9604
+ prefix: null,
9605
+ suffix: number.slice(1)
9678
9606
  };
9679
9607
  };
9680
9608
 
9681
9609
  const DIGITS_MATCH = /^$|^(\+)|([\d]+)/g;
9682
- const cleanNumber = number => number.match(DIGITS_MATCH) && number.match(DIGITS_MATCH).join('') || '';
9610
+ const cleanNumber = number => number.match(DIGITS_MATCH)?.join('') ?? '';
9683
9611
 
9684
- // Reference fro localeCompare : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
9685
- const sortArrayByProperty = (arrayToSort, property) => [...arrayToSort].sort((a, b) => a[property].localeCompare(b[property]));
9612
+ function sortArrayByProperty(arrayToSort, property) {
9613
+ return [...arrayToSort].sort((a, b) => a[property].localeCompare(b[property]));
9614
+ }
9686
9615
 
9687
9616
  const groupCountriesByPrefix = countries => {
9688
- const groupedArray = countries.reduce((accumulator, country) => {
9689
- const {
9690
- name,
9691
- iso2,
9692
- iso3,
9693
- phone
9694
- } = country;
9695
- if (accumulator[phone]) {
9696
- const previousValue = accumulator[phone];
9697
- accumulator[phone] = {
9698
- ...previousValue,
9699
- name: neptuneValidation.isArray(previousValue.name) ? [...previousValue.name, name] : [previousValue.name, name],
9700
- iso2: neptuneValidation.isArray(previousValue.iso2) ? [...previousValue.iso2, iso2] : [previousValue.iso2, iso2],
9701
- iso3: neptuneValidation.isArray(previousValue.iso3) ? [...previousValue.iso3, iso3] : [previousValue.iso3, iso3]
9702
- };
9703
- } else {
9704
- accumulator[phone] = country;
9705
- }
9706
- return accumulator;
9707
- }, {});
9708
- return Object.values(groupedArray);
9617
+ const countriesByPrefix = new Map();
9618
+ countries.forEach(country => {
9619
+ countriesByPrefix.set(country.phone, [...(countriesByPrefix.get(country.phone) ?? []), country]);
9620
+ });
9621
+ return countriesByPrefix;
9709
9622
  };
9710
9623
 
9711
9624
  // Reference fro localeCompare : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
@@ -9713,41 +9626,38 @@ const filterCountriesByIso3 = (countries, iso3Codes) => {
9713
9626
  const iso3CodesSet = new Set(iso3Codes);
9714
9627
  return countries.filter(country => !iso3CodesSet.has(country.iso3));
9715
9628
  };
9716
-
9717
9629
  /**
9718
9630
  * Removes the countries sepecified in the second param
9719
9631
  *
9720
- * @param {Array} countries: list of country metadata objects
9721
- * @param {Array} disabledCountries: list of iso3 country codes to remove from the list
9722
- * @returns
9632
+ * @param countries list of country metadata objects
9633
+ * @param disabledCountries list of iso3 country codes to remove from the list
9723
9634
  */
9724
9635
  const excludeCountries = (countries, disabledCountries) => {
9725
9636
  return disabledCountries.length > 0 ? filterCountriesByIso3(countries, disabledCountries) : countries;
9726
9637
  };
9727
9638
 
9728
9639
  const ALLOWED_PHONE_CHARS = /^$|^[\d-\s]+$/;
9729
- const PhoneNumberInput = props => {
9730
- const {
9731
- id,
9732
- onChange,
9733
- searchPlaceholder,
9734
- disabled,
9735
- required,
9736
- size,
9737
- placeholder,
9738
- onFocus,
9739
- onBlur,
9740
- countryCode,
9741
- selectProps,
9742
- disabledCountries
9743
- } = props;
9640
+ const defaultSelectProps = {};
9641
+ const defaultDisabledCountries = [];
9642
+ const PhoneNumberInput = ({
9643
+ id,
9644
+ required,
9645
+ disabled,
9646
+ initialValue,
9647
+ onChange,
9648
+ onFocus,
9649
+ onBlur,
9650
+ countryCode,
9651
+ searchPlaceholder = 'Prefix',
9652
+ size = exports.Size.MEDIUM,
9653
+ placeholder,
9654
+ selectProps = defaultSelectProps,
9655
+ disabledCountries = defaultDisabledCountries
9656
+ }) => {
9744
9657
  const {
9745
9658
  locale
9746
9659
  } = reactIntl.useIntl();
9747
- const getInitialValue = () => {
9748
- const {
9749
- initialValue
9750
- } = props;
9660
+ const [internalValue, setInternalValue] = React.useState(() => {
9751
9661
  const cleanValue = initialValue ? cleanNumber(initialValue) : null;
9752
9662
  if (!cleanValue || !isValidPhoneNumber(cleanValue)) {
9753
9663
  return {
@@ -9756,54 +9666,16 @@ const PhoneNumberInput = props => {
9756
9666
  };
9757
9667
  }
9758
9668
  return explodeNumberModel(cleanValue);
9759
- };
9760
- const [internalValue, setInternalValue] = React.useState(getInitialValue());
9669
+ });
9761
9670
  const [broadcastedValue, setBroadcastedValue] = React.useState(null);
9762
- const getSelectOptions = () => {
9763
- const countriesList = excludeCountries(countries$1, disabledCountries);
9764
- const listSortedByISO3 = groupCountriesByPrefix(sortArrayByProperty(countriesList, 'iso3'));
9765
- return listSortedByISO3.map(option => {
9766
- const {
9767
- phone,
9768
- iso3,
9769
- iso2,
9770
- name
9771
- } = option;
9772
- let note = '';
9773
- if (iso3) {
9774
- note = neptuneValidation.isArray(iso3) ? iso3.join(', ') : iso3;
9775
- } else if (iso2) {
9776
- note = neptuneValidation.isArray(iso2) ? iso2.join(', ') : iso2;
9777
- }
9778
- return {
9779
- type: 'option',
9780
- value: {
9781
- value: phone,
9782
- label: phone,
9783
- note: note
9784
- },
9785
- filterMatchers: [phone, note, name]
9786
- };
9787
- });
9788
- };
9789
- const options = getSelectOptions();
9790
- const onPrefixChange = ({
9791
- value
9792
- }) => {
9793
- setInternalValue({
9794
- prefix: value,
9795
- suffix: internalValue.suffix
9796
- });
9797
- };
9671
+ const countriesByPrefix = React.useMemo(() => groupCountriesByPrefix(sortArrayByProperty(excludeCountries(countries, disabledCountries), 'iso3')), [disabledCountries]);
9798
9672
  const onSuffixChange = event => {
9799
- const {
9800
- value = ''
9801
- } = event.target;
9802
- if (ALLOWED_PHONE_CHARS.test(value)) {
9803
- setInternalValue({
9804
- prefix: internalValue.prefix,
9805
- suffix: value
9806
- });
9673
+ const suffix = event.target.value;
9674
+ if (ALLOWED_PHONE_CHARS.test(suffix)) {
9675
+ setInternalValue(prev => ({
9676
+ ...prev,
9677
+ suffix
9678
+ }));
9807
9679
  }
9808
9680
  };
9809
9681
  const onPaste = event => {
@@ -9811,31 +9683,22 @@ const PhoneNumberInput = props => {
9811
9683
  return;
9812
9684
  }
9813
9685
  const pastedValue = (event.nativeEvent.clipboardData.getData('text/plain') || '').replace(/(\s|-)+/g, '');
9814
- const {
9815
- prefix: pastedPrefix,
9816
- suffix: pastedSuffix
9817
- } = explodeNumberModel(pastedValue);
9818
- const selectedPrefix = options.find(({
9819
- value
9820
- }) => value.value === pastedPrefix);
9821
- if (selectedPrefix && ALLOWED_PHONE_CHARS.test(pastedSuffix)) {
9822
- setInternalValue({
9823
- prefix: pastedPrefix,
9824
- suffix: pastedSuffix
9825
- });
9686
+ const pastedNumber = explodeNumberModel(pastedValue);
9687
+ if (pastedNumber.prefix != null && countriesByPrefix.has(pastedNumber.prefix) && ALLOWED_PHONE_CHARS.test(pastedNumber.suffix)) {
9688
+ setInternalValue(pastedNumber);
9826
9689
  }
9827
9690
  };
9828
9691
  React.useEffect(() => {
9829
9692
  if (broadcastedValue === null) {
9830
9693
  return setBroadcastedValue(internalValue);
9831
9694
  }
9832
- const internalPhoneNumber = internalValue.prefix + internalValue.suffix;
9833
- const broadcastedPhoneNumber = broadcastedValue.prefix + broadcastedValue.suffix;
9695
+ const internalPhoneNumber = `${internalValue.prefix ?? ''}${internalValue.suffix}`;
9696
+ const broadcastedPhoneNumber = `${broadcastedValue.prefix ?? ''}${broadcastedValue.suffix}`;
9834
9697
  if (internalPhoneNumber === broadcastedPhoneNumber) {
9835
9698
  return;
9836
9699
  }
9837
9700
  const newValue = isValidPhoneNumber(internalPhoneNumber) ? cleanNumber(internalPhoneNumber) : null;
9838
- onChange(newValue, internalValue.prefix);
9701
+ onChange(newValue, internalValue.prefix ?? '');
9839
9702
  setBroadcastedValue(internalValue);
9840
9703
  }, [onChange, broadcastedValue, internalValue]);
9841
9704
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
@@ -9843,18 +9706,29 @@ const PhoneNumberInput = props => {
9843
9706
  children: [/*#__PURE__*/jsxRuntime.jsx("div", {
9844
9707
  className: "tw-telephone__country-select",
9845
9708
  children: /*#__PURE__*/jsxRuntime.jsx(SelectInput, {
9846
- placeholder: "Select an option...",
9847
- items: options,
9848
- value: options.find(item => item.value.value === internalValue.prefix)?.value,
9849
- renderValue: (option, withinTrigger) => /*#__PURE__*/jsxRuntime.jsx(SelectInputOptionContent, {
9850
- title: option.label,
9851
- note: withinTrigger ? undefined : option.note
9709
+ placeholder: "Select an option\u2026",
9710
+ items: [...countriesByPrefix].map(([prefix, countries]) => ({
9711
+ type: 'option',
9712
+ value: prefix,
9713
+ filterMatchers: [prefix, ...countries.map(country => country.name), ...countries.map(country => country.iso3)]
9714
+ })),
9715
+ value: internalValue.prefix,
9716
+ renderValue: (prefix, withinTrigger) => /*#__PURE__*/jsxRuntime.jsx(SelectInputOptionContent, {
9717
+ title: prefix,
9718
+ note: withinTrigger ? undefined : countriesByPrefix.get(prefix)?.map(country => country.iso3).join(', ')
9852
9719
  }),
9853
9720
  filterable: true,
9854
9721
  filterPlaceholder: searchPlaceholder,
9855
9722
  disabled: disabled,
9856
9723
  size: size,
9857
- onChange: onPrefixChange,
9724
+ onChange: prefix => {
9725
+ const country = prefix != null ? findCountryByPrefix(prefix) : null;
9726
+ setInternalValue(prev => ({
9727
+ ...prev,
9728
+ prefix,
9729
+ format: country?.phoneFormat
9730
+ }));
9731
+ },
9858
9732
  ...selectProps
9859
9733
  })
9860
9734
  }), /*#__PURE__*/jsxRuntime.jsx("div", {
@@ -9880,37 +9754,6 @@ const PhoneNumberInput = props => {
9880
9754
  })]
9881
9755
  });
9882
9756
  };
9883
- PhoneNumberInput.propTypes = {
9884
- id: PropTypes__default.default.string,
9885
- required: PropTypes__default.default.bool,
9886
- disabled: PropTypes__default.default.bool,
9887
- initialValue: PropTypes__default.default.string,
9888
- onChange: PropTypes__default.default.func.isRequired,
9889
- onFocus: PropTypes__default.default.func,
9890
- onBlur: PropTypes__default.default.func,
9891
- countryCode: PropTypes__default.default.string,
9892
- searchPlaceholder: PropTypes__default.default.string,
9893
- size: PropTypes__default.default.oneOf(['sm', 'md', 'lg']),
9894
- placeholder: PropTypes__default.default.string,
9895
- selectProps: PropTypes__default.default.object,
9896
- /** List of iso3 codes of countries to remove from the list */
9897
- disabledCountries: PropTypes__default.default.arrayOf(PropTypes__default.default.string)
9898
- };
9899
- PhoneNumberInput.defaultProps = {
9900
- id: null,
9901
- required: false,
9902
- disabled: false,
9903
- initialValue: null,
9904
- onFocus() {},
9905
- onBlur() {},
9906
- countryCode: null,
9907
- searchPlaceholder: 'Prefix',
9908
- size: exports.Size.MEDIUM,
9909
- placeholder: '',
9910
- selectProps: {},
9911
- disabledCountries: []
9912
- };
9913
- var PhoneNumberInput$1 = PhoneNumberInput;
9914
9757
 
9915
9758
  const Progress = ({
9916
9759
  className,
@@ -15687,7 +15530,7 @@ exports.NavigationOptionsList = NavigationOptionList$1;
15687
15530
  exports.Nudge = Nudge;
15688
15531
  exports.Option = Option$2;
15689
15532
  exports.OverlayHeader = OverlayHeader$1;
15690
- exports.PhoneNumberInput = PhoneNumberInput$1;
15533
+ exports.PhoneNumberInput = PhoneNumberInput;
15691
15534
  exports.Popover = Popover$2;
15692
15535
  exports.ProcessIndicator = ProcessIndicator$1;
15693
15536
  exports.Progress = Progress;