intl-tel-input 25.4.5 → 25.4.8
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/README.md +5 -5
- package/angular/build/IntlTelInput.cjs +13 -10
- package/angular/build/IntlTelInput.d.ts +373 -1
- package/angular/build/IntlTelInput.js +13 -10
- package/angular/build/IntlTelInputWithUtils.cjs +13 -10
- package/angular/build/IntlTelInputWithUtils.js +13 -10
- package/angular/build/intl-tel-input/angular.js +205 -0
- package/angular/build/intl-tel-input/angularWithUtils.js +206 -0
- package/angular/build/intl-tel-input/data.js +1096 -0
- package/angular/build/intl-tel-input/i18n/ar/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/ar/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/ar/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/bg/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/bg/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/bg/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/bn/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/bn/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/bn/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/bs/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/bs/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/bs/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/ca/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/ca/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/ca/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/cs/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/cs/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/cs/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/da/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/da/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/da/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/de/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/de/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/de/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/ee/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/ee/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/ee/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/el/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/el/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/el/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/en/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/en/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/en/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/es/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/es/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/es/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/fa/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/fa/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/fa/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/fi/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/fi/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/fi/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/fr/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/fr/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/fr/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/hi/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/hi/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/hi/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/hr/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/hr/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/hr/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/hu/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/hu/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/hu/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/id/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/id/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/id/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/index.js +40 -0
- package/angular/build/intl-tel-input/i18n/it/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/it/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/it/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/ja/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/ja/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/ja/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/ko/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/ko/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/ko/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/mr/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/mr/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/mr/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/nl/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/nl/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/nl/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/no/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/no/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/no/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/pl/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/pl/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/pl/interface.js +24 -0
- package/angular/build/intl-tel-input/i18n/pt/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/pt/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/pt/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/ro/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/ro/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/ro/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/ru/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/ru/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/ru/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/sk/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/sk/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/sk/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/sv/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/sv/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/sv/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/te/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/te/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/te/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/th/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/th/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/th/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/tr/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/tr/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/tr/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/types.js +1 -0
- package/angular/build/intl-tel-input/i18n/uk/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/uk/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/uk/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/ur/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/ur/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/ur/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/uz/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/uz/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/uz/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/vi/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/vi/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/vi/interface.js +13 -0
- package/angular/build/intl-tel-input/i18n/zh/countries.js +245 -0
- package/angular/build/intl-tel-input/i18n/zh/index.js +5 -0
- package/angular/build/intl-tel-input/i18n/zh/interface.js +13 -0
- package/angular/build/intl-tel-input/intlTelInputWithUtils.js +4 -0
- package/angular/build/intl-tel-input/utils.js +1341 -0
- package/angular/build/intl-tel-input.js +1780 -0
- package/angular/build/types/intl-tel-input/angular.d.ts +40 -0
- package/angular/build/types/intl-tel-input/angularWithUtils.d.ts +40 -0
- package/angular/build/types/intl-tel-input/data.d.ts +11 -0
- package/angular/build/types/intl-tel-input/i18n/ar/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ar/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/ar/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/bg/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/bg/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/bg/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/bn/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/bn/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/bn/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/bs/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/bs/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/bs/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ca/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ca/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/ca/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/cs/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/cs/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/cs/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/da/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/da/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/da/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/de/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/de/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/de/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ee/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ee/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/ee/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/el/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/el/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/el/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/en/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/en/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/en/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/es/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/es/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/es/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/fa/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/fa/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/fa/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/fi/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/fi/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/fi/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/fr/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/fr/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/fr/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/hi/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/hi/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/hi/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/hr/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/hr/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/hr/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/hu/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/hu/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/hu/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/id/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/id/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/id/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/index.d.ts +39 -0
- package/angular/build/types/intl-tel-input/i18n/it/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/it/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/it/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ja/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ja/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/ja/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ko/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ko/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/ko/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/mr/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/mr/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/mr/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/nl/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/nl/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/nl/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/no/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/no/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/no/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/pl/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/pl/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/pl/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/pt/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/pt/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/pt/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ro/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ro/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/ro/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ru/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ru/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/ru/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/sk/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/sk/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/sk/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/sv/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/sv/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/sv/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/te/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/te/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/te/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/th/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/th/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/th/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/tr/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/tr/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/tr/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/types.d.ts +254 -0
- package/angular/build/types/intl-tel-input/i18n/uk/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/uk/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/uk/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ur/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/ur/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/ur/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/uz/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/uz/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/uz/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/vi/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/vi/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/vi/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/zh/countries.d.ts +3 -0
- package/angular/build/types/intl-tel-input/i18n/zh/index.d.ts +6 -0
- package/angular/build/types/intl-tel-input/i18n/zh/interface.d.ts +3 -0
- package/angular/build/types/intl-tel-input/intlTelInputWithUtils.d.ts +2 -0
- package/angular/build/types/intl-tel-input/utils.d.ts +2 -0
- package/angular/build/types/intl-tel-input.d.ts +199 -0
- package/build/js/data.js +1 -1
- package/build/js/data.min.js +1 -1
- package/build/js/intlTelInput.d.ts +1 -1
- package/build/js/intlTelInput.js +14 -11
- package/build/js/intlTelInput.min.js +2 -2
- package/build/js/intlTelInputWithUtils.js +14 -11
- package/build/js/intlTelInputWithUtils.min.js +2 -2
- package/package.json +7 -8
- package/react/README.md +1 -1
- package/react/build/IntlTelInput.cjs +13 -10
- package/react/build/IntlTelInput.d.ts +1 -1
- package/react/build/IntlTelInput.js +13 -10
- package/react/build/IntlTelInputWithUtils.cjs +13 -10
- package/react/build/IntlTelInputWithUtils.js +13 -10
- package/vue/README.md +1 -1
- package/vue/build/IntlTelInput.mjs +71 -73
- package/vue/build/IntlTelInputWithUtils.mjs +265 -267
|
@@ -0,0 +1,1780 @@
|
|
|
1
|
+
import allCountries from "./intl-tel-input/data";
|
|
2
|
+
import defaultEnglishStrings from "./intl-tel-input/i18n/en";
|
|
3
|
+
//* Populate the country names in the default language - useful if you want to use static getCountryData to populate another country dropdown etc.
|
|
4
|
+
for (let i = 0; i < allCountries.length; i++) {
|
|
5
|
+
allCountries[i].name = defaultEnglishStrings[allCountries[i].iso2];
|
|
6
|
+
}
|
|
7
|
+
//* These vars persist through all instances of the plugin.
|
|
8
|
+
let id = 0;
|
|
9
|
+
const defaults = {
|
|
10
|
+
//* Whether or not to allow the dropdown.
|
|
11
|
+
allowDropdown: true,
|
|
12
|
+
//* Add a placeholder in the input with an example number for the selected country.
|
|
13
|
+
autoPlaceholder: "polite",
|
|
14
|
+
//* Modify the parentClass.
|
|
15
|
+
containerClass: "",
|
|
16
|
+
//* The order of the countries in the dropdown. Defaults to alphabetical.
|
|
17
|
+
countryOrder: null,
|
|
18
|
+
//* Add a country search input at the top of the dropdown.
|
|
19
|
+
countrySearch: true,
|
|
20
|
+
//* Modify the auto placeholder.
|
|
21
|
+
customPlaceholder: null,
|
|
22
|
+
//* Append menu to specified element.
|
|
23
|
+
dropdownContainer: null,
|
|
24
|
+
//* Don't display these countries.
|
|
25
|
+
excludeCountries: [],
|
|
26
|
+
//* Fix the dropdown width to the input width (rather than being as wide as the longest country name).
|
|
27
|
+
fixDropdownWidth: true,
|
|
28
|
+
//* Format the number as the user types
|
|
29
|
+
formatAsYouType: true,
|
|
30
|
+
//* Format the input value during initialisation and on setNumber.
|
|
31
|
+
formatOnDisplay: true,
|
|
32
|
+
//* geoIp lookup function.
|
|
33
|
+
geoIpLookup: null,
|
|
34
|
+
//* Inject a hidden input with the name returned from this function, and on submit, populate it with the result of getNumber.
|
|
35
|
+
hiddenInput: null,
|
|
36
|
+
//* Internationalise the plugin text e.g. search input placeholder, country names.
|
|
37
|
+
i18n: {},
|
|
38
|
+
//* Initial country.
|
|
39
|
+
initialCountry: "",
|
|
40
|
+
//* A function to load the utils script.
|
|
41
|
+
loadUtils: null,
|
|
42
|
+
//* National vs international formatting for numbers e.g. placeholders and displaying existing numbers.
|
|
43
|
+
nationalMode: true,
|
|
44
|
+
//* Display only these countries.
|
|
45
|
+
onlyCountries: [],
|
|
46
|
+
//* Number type to use for placeholders.
|
|
47
|
+
placeholderNumberType: "MOBILE",
|
|
48
|
+
//* Show flags - for both the selected country, and in the country dropdown
|
|
49
|
+
showFlags: true,
|
|
50
|
+
//* Display the international dial code next to the selected flag.
|
|
51
|
+
separateDialCode: false,
|
|
52
|
+
//* Only allow certain chars e.g. a plus followed by numeric digits, and cap at max valid length.
|
|
53
|
+
strictMode: false,
|
|
54
|
+
//* Use full screen popup instead of dropdown for country list.
|
|
55
|
+
useFullscreenPopup: typeof navigator !== "undefined" && typeof window !== "undefined"
|
|
56
|
+
? //* We cannot just test screen size as some smartphones/website meta tags will report desktop resolutions.
|
|
57
|
+
//* Note: to target Android Mobiles (and not Tablets), we must find 'Android' and 'Mobile'
|
|
58
|
+
/Android.+Mobile|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || window.innerWidth <= 500
|
|
59
|
+
: false,
|
|
60
|
+
//* The number type to enforce during validation.
|
|
61
|
+
validationNumberTypes: ["MOBILE"],
|
|
62
|
+
};
|
|
63
|
+
//* https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Non-geographic_area_codes
|
|
64
|
+
const regionlessNanpNumbers = [
|
|
65
|
+
"800",
|
|
66
|
+
"822",
|
|
67
|
+
"833",
|
|
68
|
+
"844",
|
|
69
|
+
"855",
|
|
70
|
+
"866",
|
|
71
|
+
"877",
|
|
72
|
+
"880",
|
|
73
|
+
"881",
|
|
74
|
+
"882",
|
|
75
|
+
"883",
|
|
76
|
+
"884",
|
|
77
|
+
"885",
|
|
78
|
+
"886",
|
|
79
|
+
"887",
|
|
80
|
+
"888",
|
|
81
|
+
"889",
|
|
82
|
+
];
|
|
83
|
+
//* Extract the numeric digits from the given string.
|
|
84
|
+
const getNumeric = (s) => s.replace(/\D/g, "");
|
|
85
|
+
//* Normalise string: turns "Réunion" into "Reunion".
|
|
86
|
+
//* from https://stackoverflow.com/a/37511463
|
|
87
|
+
const normaliseString = (s = "") => s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
|
|
88
|
+
//* Check if the given number is a regionless NANP number (expects the number to contain an international dial code)
|
|
89
|
+
const isRegionlessNanp = (number) => {
|
|
90
|
+
const numeric = getNumeric(number);
|
|
91
|
+
if (numeric.charAt(0) === "1") {
|
|
92
|
+
const areaCode = numeric.substr(1, 3);
|
|
93
|
+
return regionlessNanpNumbers.includes(areaCode);
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
};
|
|
97
|
+
//* Iterate through the formattedValue until hit the right number of relevant chars.
|
|
98
|
+
const translateCursorPosition = (relevantChars, formattedValue, prevCaretPos, isDeleteForwards) => {
|
|
99
|
+
//* If the first char is a formatting char, and they backspace delete it:
|
|
100
|
+
//* Cursor should stay at the start (pos 0), rather than stick to the first digit (pos 1).
|
|
101
|
+
if (prevCaretPos === 0 && !isDeleteForwards) {
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
let count = 0;
|
|
105
|
+
for (let i = 0; i < formattedValue.length; i++) {
|
|
106
|
+
if (/[+0-9]/.test(formattedValue[i])) {
|
|
107
|
+
count++;
|
|
108
|
+
}
|
|
109
|
+
//* Normal case: stop when you hit the right number of relevant chars
|
|
110
|
+
//* (cursor will be just after the final relevant char).
|
|
111
|
+
if (count === relevantChars && !isDeleteForwards) {
|
|
112
|
+
return i + 1;
|
|
113
|
+
}
|
|
114
|
+
//* Spacial case: delete forwards (fn + delete on a mac):
|
|
115
|
+
//* Wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars).
|
|
116
|
+
if (isDeleteForwards && count === relevantChars + 1) {
|
|
117
|
+
return i;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return formattedValue.length;
|
|
121
|
+
};
|
|
122
|
+
//* Create a DOM element.
|
|
123
|
+
const createEl = (name, attrs, container) => {
|
|
124
|
+
const el = document.createElement(name);
|
|
125
|
+
if (attrs) {
|
|
126
|
+
Object.entries(attrs).forEach(([key, value]) => el.setAttribute(key, value));
|
|
127
|
+
}
|
|
128
|
+
if (container) {
|
|
129
|
+
container.appendChild(el);
|
|
130
|
+
}
|
|
131
|
+
return el;
|
|
132
|
+
};
|
|
133
|
+
//* Run a method on each instance of the plugin.
|
|
134
|
+
const forEachInstance = (method, ...args) => {
|
|
135
|
+
const { instances } = intlTelInput;
|
|
136
|
+
Object.values(instances).forEach((instance) => instance[method](...args));
|
|
137
|
+
};
|
|
138
|
+
//* This is our plugin class that we will create an instance of
|
|
139
|
+
// eslint-disable-next-line no-unused-vars
|
|
140
|
+
export class Iti {
|
|
141
|
+
constructor(input, customOptions = {}) {
|
|
142
|
+
this.id = id++;
|
|
143
|
+
this.telInput = input;
|
|
144
|
+
this.highlightedItem = null;
|
|
145
|
+
//* Process specified options / defaults.
|
|
146
|
+
this.options = Object.assign({}, defaults, customOptions);
|
|
147
|
+
this.hadInitialPlaceholder = Boolean(input.getAttribute("placeholder"));
|
|
148
|
+
}
|
|
149
|
+
//* Can't be private as it's called from intlTelInput convenience wrapper.
|
|
150
|
+
_init() {
|
|
151
|
+
//* If showing fullscreen popup, do not fix the width.
|
|
152
|
+
if (this.options.useFullscreenPopup) {
|
|
153
|
+
this.options.fixDropdownWidth = false;
|
|
154
|
+
}
|
|
155
|
+
//* If theres only one country, then use it!
|
|
156
|
+
if (this.options.onlyCountries.length === 1) {
|
|
157
|
+
this.options.initialCountry = this.options.onlyCountries[0];
|
|
158
|
+
}
|
|
159
|
+
//* When separateDialCode enabled, we force nationalMode to false (because the displayed dial code is supposed to be thought of as part of the typed number).
|
|
160
|
+
if (this.options.separateDialCode) {
|
|
161
|
+
this.options.nationalMode = false;
|
|
162
|
+
}
|
|
163
|
+
// if there is a country dropdown, but no flags and no separate dial code, then it suggests that there are multiple countries to choose from, but no way to see which one is currently selected, so we force nationalMode to false, as it doesn't make sense to show a national number placeholder if there's no way to see which country is selected
|
|
164
|
+
if (this.options.allowDropdown && !this.options.showFlags && !this.options.separateDialCode) {
|
|
165
|
+
this.options.nationalMode = false;
|
|
166
|
+
}
|
|
167
|
+
//* If we want a full screen dropdown, we must append it to the body.
|
|
168
|
+
if (this.options.useFullscreenPopup && !this.options.dropdownContainer) {
|
|
169
|
+
this.options.dropdownContainer = document.body;
|
|
170
|
+
}
|
|
171
|
+
this.isAndroid = typeof navigator !== "undefined" ? /Android/i.test(navigator.userAgent) : false;
|
|
172
|
+
//* Check if input has one parent with RTL.
|
|
173
|
+
this.isRTL = !!this.telInput.closest("[dir=rtl]");
|
|
174
|
+
const showOnDefaultSide = this.options.allowDropdown || this.options.separateDialCode;
|
|
175
|
+
this.showSelectedCountryOnLeft = this.isRTL ? !showOnDefaultSide : showOnDefaultSide;
|
|
176
|
+
//* Store original styling before we override it.
|
|
177
|
+
if (this.options.separateDialCode) {
|
|
178
|
+
if (this.isRTL) {
|
|
179
|
+
this.originalPaddingRight = this.telInput.style.paddingRight;
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
this.originalPaddingLeft = this.telInput.style.paddingLeft;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
//* Allow overriding the default interface strings.
|
|
186
|
+
this.options.i18n = Object.assign(Object.assign({}, defaultEnglishStrings), this.options.i18n);
|
|
187
|
+
//* these promises get resolved when their individual requests complete
|
|
188
|
+
//* this way the dev can do something like iti.promise.then(...) to know when all requests are complete.
|
|
189
|
+
const autoCountryPromise = new Promise((resolve, reject) => {
|
|
190
|
+
this.resolveAutoCountryPromise = resolve;
|
|
191
|
+
this.rejectAutoCountryPromise = reject;
|
|
192
|
+
});
|
|
193
|
+
const utilsScriptPromise = new Promise((resolve, reject) => {
|
|
194
|
+
this.resolveUtilsScriptPromise = resolve;
|
|
195
|
+
this.rejectUtilsScriptPromise = reject;
|
|
196
|
+
});
|
|
197
|
+
this.promise = Promise.all([autoCountryPromise, utilsScriptPromise]);
|
|
198
|
+
//* In various situations there could be no country selected initially, but we need to be able
|
|
199
|
+
//* to assume this variable exists.
|
|
200
|
+
this.selectedCountryData = {};
|
|
201
|
+
//* Process all the data: onlyCountries, excludeCountries, countryOrder etc.
|
|
202
|
+
this._processCountryData();
|
|
203
|
+
//* generate the markup.
|
|
204
|
+
this._generateMarkup();
|
|
205
|
+
//* Set the initial state of the input value and the selected country.
|
|
206
|
+
this._setInitialState();
|
|
207
|
+
//* Start all of the event listeners: input keydown, selectedCountry click.
|
|
208
|
+
this._initListeners();
|
|
209
|
+
//* Utils script, and auto country.
|
|
210
|
+
this._initRequests();
|
|
211
|
+
}
|
|
212
|
+
//********************
|
|
213
|
+
//* PRIVATE METHODS
|
|
214
|
+
//********************
|
|
215
|
+
//* Prepare all of the country data, including onlyCountries, excludeCountries, countryOrder options.
|
|
216
|
+
_processCountryData() {
|
|
217
|
+
//* Process onlyCountries or excludeCountries array if present.
|
|
218
|
+
this._processAllCountries();
|
|
219
|
+
//* Generate this.dialCodes and this.dialCodeToIso2Map.
|
|
220
|
+
this._processDialCodes();
|
|
221
|
+
//* Translate country names according to i18n option.
|
|
222
|
+
this._translateCountryNames();
|
|
223
|
+
//* Sort countries by countryOrder option (if present), then name.
|
|
224
|
+
this._sortCountries();
|
|
225
|
+
}
|
|
226
|
+
//* Sort countries by countryOrder option (if present), then name.
|
|
227
|
+
_sortCountries() {
|
|
228
|
+
if (this.options.countryOrder) {
|
|
229
|
+
this.options.countryOrder = this.options.countryOrder.map((country) => country.toLowerCase());
|
|
230
|
+
}
|
|
231
|
+
this.countries.sort((a, b) => {
|
|
232
|
+
//* Primary sort: countryOrder option.
|
|
233
|
+
const { countryOrder } = this.options;
|
|
234
|
+
if (countryOrder) {
|
|
235
|
+
const aIndex = countryOrder.indexOf(a.iso2);
|
|
236
|
+
const bIndex = countryOrder.indexOf(b.iso2);
|
|
237
|
+
const aIndexExists = aIndex > -1;
|
|
238
|
+
const bIndexExists = bIndex > -1;
|
|
239
|
+
if (aIndexExists || bIndexExists) {
|
|
240
|
+
if (aIndexExists && bIndexExists) {
|
|
241
|
+
return aIndex - bIndex;
|
|
242
|
+
}
|
|
243
|
+
return aIndexExists ? -1 : 1;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
//* Secondary sort: country name.
|
|
247
|
+
return a.name.localeCompare(b.name);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
//* Add a dial code to this.dialCodeToIso2Map.
|
|
251
|
+
_addToDialCodeMap(iso2, dialCode, priority) {
|
|
252
|
+
//* Update dialCodeMaxLen.
|
|
253
|
+
if (dialCode.length > this.dialCodeMaxLen) {
|
|
254
|
+
this.dialCodeMaxLen = dialCode.length;
|
|
255
|
+
}
|
|
256
|
+
//* If this entry doesn't already exist, then create it.
|
|
257
|
+
if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
|
|
258
|
+
this.dialCodeToIso2Map[dialCode] = [];
|
|
259
|
+
}
|
|
260
|
+
//* Bail if we already have this country for this dialCode.
|
|
261
|
+
for (let i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
|
|
262
|
+
if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
//* Check for undefined as 0 is falsy.
|
|
267
|
+
const index = priority !== undefined ? priority : this.dialCodeToIso2Map[dialCode].length;
|
|
268
|
+
this.dialCodeToIso2Map[dialCode][index] = iso2;
|
|
269
|
+
}
|
|
270
|
+
//* Process onlyCountries or excludeCountries array if present.
|
|
271
|
+
_processAllCountries() {
|
|
272
|
+
const { onlyCountries, excludeCountries } = this.options;
|
|
273
|
+
if (onlyCountries.length) {
|
|
274
|
+
const lowerCaseOnlyCountries = onlyCountries.map((country) => country.toLowerCase());
|
|
275
|
+
this.countries = allCountries.filter((country) => lowerCaseOnlyCountries.includes(country.iso2));
|
|
276
|
+
}
|
|
277
|
+
else if (excludeCountries.length) {
|
|
278
|
+
const lowerCaseExcludeCountries = excludeCountries.map((country) => country.toLowerCase());
|
|
279
|
+
this.countries = allCountries.filter((country) => !lowerCaseExcludeCountries.includes(country.iso2));
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
this.countries = allCountries;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
//* Translate Countries by object literal provided on config.
|
|
286
|
+
_translateCountryNames() {
|
|
287
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
288
|
+
const iso2 = this.countries[i].iso2.toLowerCase();
|
|
289
|
+
if (this.options.i18n.hasOwnProperty(iso2)) {
|
|
290
|
+
this.countries[i].name = this.options.i18n[iso2];
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
//* Generate this.dialCodes and this.dialCodeToIso2Map.
|
|
295
|
+
_processDialCodes() {
|
|
296
|
+
//* Here we store just dial codes, where the key is the dial code, and the value is true
|
|
297
|
+
//* e.g. { 1: true, 7: true, 20: true, ... }.
|
|
298
|
+
this.dialCodes = {};
|
|
299
|
+
this.dialCodeMaxLen = 0;
|
|
300
|
+
//* Here we map dialCodes (inc both dialCode and dialCode+areaCode) to iso2 codes e.g.
|
|
301
|
+
/*
|
|
302
|
+
* {
|
|
303
|
+
* 1: [ 'us', 'ca', ... ], # all NANP countries (with dial code "1")
|
|
304
|
+
* 12: [ 'us', 'ca', ... ], # subset of NANP countries (that have area codes starting with "2")
|
|
305
|
+
* 120: [ 'us', 'ca' ], # just US and Canada (that have area codes starting "20")
|
|
306
|
+
* 1204: [ 'ca' ], # only Canada (that has a "204" area code)
|
|
307
|
+
* ...
|
|
308
|
+
* }
|
|
309
|
+
*/
|
|
310
|
+
this.dialCodeToIso2Map = {};
|
|
311
|
+
//* First: add dial codes.
|
|
312
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
313
|
+
const c = this.countries[i];
|
|
314
|
+
if (!this.dialCodes[c.dialCode]) {
|
|
315
|
+
this.dialCodes[c.dialCode] = true;
|
|
316
|
+
}
|
|
317
|
+
this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
|
|
318
|
+
}
|
|
319
|
+
//* Next: add area codes.
|
|
320
|
+
//* This is a second loop over countries, to make sure we have all of the "root" countries
|
|
321
|
+
//* already in the map, so that we can access them, as each time we add an area code substring
|
|
322
|
+
//* to the map, we also need to include the "root" country's code, as that also matches.
|
|
323
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
324
|
+
const c = this.countries[i];
|
|
325
|
+
//* Area codes
|
|
326
|
+
if (c.areaCodes) {
|
|
327
|
+
const rootIso2Code = this.dialCodeToIso2Map[c.dialCode][0];
|
|
328
|
+
//* For each area code.
|
|
329
|
+
for (let j = 0; j < c.areaCodes.length; j++) {
|
|
330
|
+
const areaCode = c.areaCodes[j];
|
|
331
|
+
//* For each digit in the area code to add all partial matches as well.
|
|
332
|
+
for (let k = 1; k < areaCode.length; k++) {
|
|
333
|
+
const partialAreaCode = areaCode.substr(0, k);
|
|
334
|
+
const partialDialCode = c.dialCode + partialAreaCode;
|
|
335
|
+
//* Start with the root country, as that also matches this partial dial code.
|
|
336
|
+
this._addToDialCodeMap(rootIso2Code, partialDialCode);
|
|
337
|
+
this._addToDialCodeMap(c.iso2, partialDialCode);
|
|
338
|
+
}
|
|
339
|
+
//* Add the full area code.
|
|
340
|
+
this._addToDialCodeMap(c.iso2, c.dialCode + areaCode);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
//* Generate all of the markup for the plugin: the selected country overlay, and the dropdown.
|
|
346
|
+
_generateMarkup() {
|
|
347
|
+
var _a, _b, _c;
|
|
348
|
+
this.telInput.classList.add("iti__tel-input");
|
|
349
|
+
//* If autocomplete does not exist on the element and its form, then
|
|
350
|
+
//* prevent autocomplete as there's no safe, cross-browser event we can react to, so it can
|
|
351
|
+
//* easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
|
|
352
|
+
//* autocompleted number, which on submit could mean wrong number is saved.
|
|
353
|
+
if (!this.telInput.hasAttribute("autocomplete") &&
|
|
354
|
+
!(this.telInput.form && this.telInput.form.hasAttribute("autocomplete"))) {
|
|
355
|
+
this.telInput.setAttribute("autocomplete", "off");
|
|
356
|
+
}
|
|
357
|
+
const { allowDropdown, separateDialCode, showFlags, containerClass, hiddenInput, dropdownContainer, fixDropdownWidth, useFullscreenPopup, countrySearch, i18n, } = this.options;
|
|
358
|
+
//* Containers (mostly for positioning).
|
|
359
|
+
let parentClass = "iti";
|
|
360
|
+
if (allowDropdown) {
|
|
361
|
+
parentClass += " iti--allow-dropdown";
|
|
362
|
+
}
|
|
363
|
+
if (showFlags) {
|
|
364
|
+
parentClass += " iti--show-flags";
|
|
365
|
+
}
|
|
366
|
+
if (containerClass) {
|
|
367
|
+
parentClass += ` ${containerClass}`;
|
|
368
|
+
}
|
|
369
|
+
if (!useFullscreenPopup) {
|
|
370
|
+
parentClass += " iti--inline-dropdown";
|
|
371
|
+
}
|
|
372
|
+
const wrapper = createEl("div", { class: parentClass });
|
|
373
|
+
(_a = this.telInput.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(wrapper, this.telInput);
|
|
374
|
+
//* If we need a countryContainer
|
|
375
|
+
if (allowDropdown || showFlags || separateDialCode) {
|
|
376
|
+
this.countryContainer = createEl("div", { class: "iti__country-container" }, wrapper);
|
|
377
|
+
if (this.showSelectedCountryOnLeft) {
|
|
378
|
+
this.countryContainer.style.left = "0px";
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
this.countryContainer.style.right = "0px";
|
|
382
|
+
}
|
|
383
|
+
//* Selected country (displayed on left of input while allowDropdown is enabled, otherwise to right)
|
|
384
|
+
//* https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only
|
|
385
|
+
if (allowDropdown) {
|
|
386
|
+
this.selectedCountry = createEl("button", {
|
|
387
|
+
type: "button",
|
|
388
|
+
class: "iti__selected-country",
|
|
389
|
+
"aria-expanded": "false",
|
|
390
|
+
"aria-label": this.options.i18n.selectedCountryAriaLabel,
|
|
391
|
+
"aria-haspopup": "true",
|
|
392
|
+
"aria-controls": `iti-${this.id}__dropdown-content`,
|
|
393
|
+
"role": "combobox",
|
|
394
|
+
}, this.countryContainer);
|
|
395
|
+
if (this.telInput.disabled) {
|
|
396
|
+
this.selectedCountry.setAttribute("disabled", "true");
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
this.selectedCountry = createEl("div", { class: "iti__selected-country" }, this.countryContainer);
|
|
401
|
+
}
|
|
402
|
+
// The element that gets a grey background on hover (if allowDropdown enabled)
|
|
403
|
+
const selectedCountryPrimary = createEl("div", { class: "iti__selected-country-primary" }, this.selectedCountry);
|
|
404
|
+
//* This is where we will add the selected flag (or globe) class later
|
|
405
|
+
this.selectedCountryInner = createEl("div", { class: "iti__flag" }, selectedCountryPrimary);
|
|
406
|
+
this.selectedCountryA11yText = createEl("span", { class: "iti__a11y-text" }, this.selectedCountryInner);
|
|
407
|
+
if (allowDropdown) {
|
|
408
|
+
this.dropdownArrow = createEl("div", { class: "iti__arrow", "aria-hidden": "true" }, selectedCountryPrimary);
|
|
409
|
+
}
|
|
410
|
+
if (separateDialCode) {
|
|
411
|
+
this.selectedDialCode = createEl("div", { class: "iti__selected-dial-code" }, this.selectedCountry);
|
|
412
|
+
}
|
|
413
|
+
if (allowDropdown) {
|
|
414
|
+
const extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
|
|
415
|
+
this.dropdownContent = createEl("div", {
|
|
416
|
+
id: `iti-${this.id}__dropdown-content`,
|
|
417
|
+
class: `iti__dropdown-content iti__hide ${extraClasses}`,
|
|
418
|
+
});
|
|
419
|
+
if (countrySearch) {
|
|
420
|
+
this.searchInput = createEl("input", {
|
|
421
|
+
type: "text",
|
|
422
|
+
class: "iti__search-input",
|
|
423
|
+
placeholder: i18n.searchPlaceholder,
|
|
424
|
+
role: "combobox",
|
|
425
|
+
"aria-expanded": "true",
|
|
426
|
+
"aria-label": i18n.searchPlaceholder,
|
|
427
|
+
"aria-controls": `iti-${this.id}__country-listbox`,
|
|
428
|
+
"aria-autocomplete": "list",
|
|
429
|
+
"autocomplete": "off",
|
|
430
|
+
}, this.dropdownContent);
|
|
431
|
+
this.searchResultsA11yText = createEl("span", { class: "iti__a11y-text" }, this.dropdownContent);
|
|
432
|
+
}
|
|
433
|
+
this.countryList = createEl("ul", {
|
|
434
|
+
class: "iti__country-list",
|
|
435
|
+
id: `iti-${this.id}__country-listbox`,
|
|
436
|
+
role: "listbox",
|
|
437
|
+
"aria-label": i18n.countryListAriaLabel,
|
|
438
|
+
}, this.dropdownContent);
|
|
439
|
+
this._appendListItems();
|
|
440
|
+
if (countrySearch) {
|
|
441
|
+
this._updateSearchResultsText();
|
|
442
|
+
}
|
|
443
|
+
//* Create dropdownContainer markup.
|
|
444
|
+
if (dropdownContainer) {
|
|
445
|
+
let dropdownClasses = "iti iti--container";
|
|
446
|
+
if (containerClass) {
|
|
447
|
+
dropdownClasses += ` ${containerClass}`;
|
|
448
|
+
}
|
|
449
|
+
if (useFullscreenPopup) {
|
|
450
|
+
dropdownClasses += " iti--fullscreen-popup";
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
dropdownClasses += " iti--inline-dropdown";
|
|
454
|
+
}
|
|
455
|
+
this.dropdown = createEl("div", { class: dropdownClasses });
|
|
456
|
+
this.dropdown.appendChild(this.dropdownContent);
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
this.countryContainer.appendChild(this.dropdownContent);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
wrapper.appendChild(this.telInput);
|
|
464
|
+
this._updateInputPadding();
|
|
465
|
+
if (hiddenInput) {
|
|
466
|
+
const telInputName = this.telInput.getAttribute("name") || "";
|
|
467
|
+
const names = hiddenInput(telInputName);
|
|
468
|
+
if (names.phone) {
|
|
469
|
+
const existingInput = (_b = this.telInput.form) === null || _b === void 0 ? void 0 : _b.querySelector(`input[name="${names.phone}"]`);
|
|
470
|
+
if (existingInput) {
|
|
471
|
+
this.hiddenInput = existingInput;
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
//* Create hidden input for the full international number.
|
|
475
|
+
this.hiddenInput = createEl("input", {
|
|
476
|
+
type: "hidden",
|
|
477
|
+
name: names.phone,
|
|
478
|
+
});
|
|
479
|
+
wrapper.appendChild(this.hiddenInput);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (names.country) {
|
|
483
|
+
const existingInput = (_c = this.telInput.form) === null || _c === void 0 ? void 0 : _c.querySelector(`input[name="${names.country}"]`);
|
|
484
|
+
if (existingInput) {
|
|
485
|
+
this.hiddenInputCountry = existingInput;
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
//* Create hidden input for the selected country iso2 code.
|
|
489
|
+
this.hiddenInputCountry = createEl("input", {
|
|
490
|
+
type: "hidden",
|
|
491
|
+
name: names.country,
|
|
492
|
+
});
|
|
493
|
+
wrapper.appendChild(this.hiddenInputCountry);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
//* For each country: add a country list item <li> to the countryList <ul> container.
|
|
499
|
+
_appendListItems() {
|
|
500
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
501
|
+
const c = this.countries[i];
|
|
502
|
+
//* Start by highlighting the first item (useful when countrySearch disabled).
|
|
503
|
+
const extraClass = i === 0 ? "iti__highlight" : "";
|
|
504
|
+
const listItem = createEl("li", {
|
|
505
|
+
id: `iti-${this.id}__item-${c.iso2}`,
|
|
506
|
+
class: `iti__country ${extraClass}`,
|
|
507
|
+
tabindex: "-1",
|
|
508
|
+
role: "option",
|
|
509
|
+
"data-dial-code": c.dialCode,
|
|
510
|
+
"data-country-code": c.iso2,
|
|
511
|
+
"aria-selected": "false",
|
|
512
|
+
}, this.countryList);
|
|
513
|
+
//* Store this for later use e.g. country search filtering.
|
|
514
|
+
c.nodeById[this.id] = listItem;
|
|
515
|
+
let content = "";
|
|
516
|
+
//* Add the flag.
|
|
517
|
+
if (this.options.showFlags) {
|
|
518
|
+
content += `<div class='iti__flag iti__${c.iso2}'></div>`;
|
|
519
|
+
}
|
|
520
|
+
//* And the country name and dial code.
|
|
521
|
+
content += `<span class='iti__country-name'>${c.name}</span>`;
|
|
522
|
+
content += `<span class='iti__dial-code'>+${c.dialCode}</span>`;
|
|
523
|
+
listItem.insertAdjacentHTML("beforeend", content);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
//* Set the initial state of the input value and the selected country by:
|
|
527
|
+
//* 1. Extracting a dial code from the given number
|
|
528
|
+
//* 2. Using explicit initialCountry
|
|
529
|
+
_setInitialState(overrideAutoCountry = false) {
|
|
530
|
+
//* Fix firefox bug: when first load page (with input with value set to number with intl dial code)
|
|
531
|
+
//* and initialising plugin removes the dial code from the input, then refresh page,
|
|
532
|
+
//* and we try to init plugin again but this time on number without dial code so show globe icon.
|
|
533
|
+
const attributeValue = this.telInput.getAttribute("value");
|
|
534
|
+
const inputValue = this.telInput.value;
|
|
535
|
+
const useAttribute = attributeValue &&
|
|
536
|
+
attributeValue.charAt(0) === "+" &&
|
|
537
|
+
(!inputValue || inputValue.charAt(0) !== "+");
|
|
538
|
+
const val = useAttribute ? attributeValue : inputValue;
|
|
539
|
+
const dialCode = this._getDialCode(val);
|
|
540
|
+
const isRegionlessNanpNumber = isRegionlessNanp(val);
|
|
541
|
+
const { initialCountry, geoIpLookup } = this.options;
|
|
542
|
+
const isAutoCountry = initialCountry === "auto" && geoIpLookup;
|
|
543
|
+
//* If we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the
|
|
544
|
+
//* country, else fall back to the default country.
|
|
545
|
+
if (dialCode && !isRegionlessNanpNumber) {
|
|
546
|
+
this._updateCountryFromNumber(val);
|
|
547
|
+
}
|
|
548
|
+
else if (!isAutoCountry || overrideAutoCountry) {
|
|
549
|
+
const lowerInitialCountry = initialCountry ? initialCountry.toLowerCase() : "";
|
|
550
|
+
const isValidInitialCountry = lowerInitialCountry && this._getCountryData(lowerInitialCountry, true);
|
|
551
|
+
//* See if we should select a country.
|
|
552
|
+
if (isValidInitialCountry) {
|
|
553
|
+
this._setCountry(lowerInitialCountry);
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
if (dialCode && isRegionlessNanpNumber) {
|
|
557
|
+
//* Has intl dial code, is regionless nanp, and no initialCountry, so default to US.
|
|
558
|
+
this._setCountry("us");
|
|
559
|
+
}
|
|
560
|
+
else {
|
|
561
|
+
//* Display the empty state (globe icon).
|
|
562
|
+
this._setCountry();
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
//* NOTE: if initialCountry is set to auto, that will be handled separately.
|
|
567
|
+
//* Format - note this wont be run after _updateDialCode as that's only called if no val.
|
|
568
|
+
if (val) {
|
|
569
|
+
this._updateValFromNumber(val);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
//* Initialise the main event listeners: input keyup, and click selected country.
|
|
573
|
+
_initListeners() {
|
|
574
|
+
this._initTelInputListeners();
|
|
575
|
+
if (this.options.allowDropdown) {
|
|
576
|
+
this._initDropdownListeners();
|
|
577
|
+
}
|
|
578
|
+
if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
|
|
579
|
+
this._initHiddenInputListener();
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
//* Update hidden input on form submit.
|
|
583
|
+
_initHiddenInputListener() {
|
|
584
|
+
var _a;
|
|
585
|
+
this._handleHiddenInputSubmit = () => {
|
|
586
|
+
if (this.hiddenInput) {
|
|
587
|
+
this.hiddenInput.value = this.getNumber();
|
|
588
|
+
}
|
|
589
|
+
if (this.hiddenInputCountry) {
|
|
590
|
+
this.hiddenInputCountry.value = this.getSelectedCountryData().iso2 || "";
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
(_a = this.telInput.form) === null || _a === void 0 ? void 0 : _a.addEventListener("submit", this._handleHiddenInputSubmit);
|
|
594
|
+
}
|
|
595
|
+
//* initialise the dropdown listeners.
|
|
596
|
+
_initDropdownListeners() {
|
|
597
|
+
//* Hack for input nested inside label (which is valid markup): clicking the selected country to
|
|
598
|
+
//* open the dropdown would then automatically trigger a 2nd click on the input which would
|
|
599
|
+
//* close it again.
|
|
600
|
+
this._handleLabelClick = (e) => {
|
|
601
|
+
//* If the dropdown is closed, then focus the input, else ignore the click.
|
|
602
|
+
if (this.dropdownContent.classList.contains("iti__hide")) {
|
|
603
|
+
this.telInput.focus();
|
|
604
|
+
}
|
|
605
|
+
else {
|
|
606
|
+
e.preventDefault();
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
const label = this.telInput.closest("label");
|
|
610
|
+
if (label) {
|
|
611
|
+
label.addEventListener("click", this._handleLabelClick);
|
|
612
|
+
}
|
|
613
|
+
//* Toggle country dropdown on click.
|
|
614
|
+
this._handleClickSelectedCountry = () => {
|
|
615
|
+
//* Only intercept this event if we're opening the dropdown
|
|
616
|
+
//* else let it bubble up to the top ("click-off-to-close" listener)
|
|
617
|
+
//* we cannot just stopPropagation as it may be needed to close another instance.
|
|
618
|
+
if (this.dropdownContent.classList.contains("iti__hide") &&
|
|
619
|
+
!this.telInput.disabled &&
|
|
620
|
+
!this.telInput.readOnly) {
|
|
621
|
+
this._openDropdown();
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
this.selectedCountry.addEventListener("click", this._handleClickSelectedCountry);
|
|
625
|
+
//* Open dropdown if selected country is focused and they press up/down/space/enter.
|
|
626
|
+
this._handleCountryContainerKeydown = (e) => {
|
|
627
|
+
const isDropdownHidden = this.dropdownContent.classList.contains("iti__hide");
|
|
628
|
+
if (isDropdownHidden &&
|
|
629
|
+
["ArrowUp", "ArrowDown", " ", "Enter"].includes(e.key)) {
|
|
630
|
+
//* Prevent form from being submitted if "ENTER" was pressed.
|
|
631
|
+
e.preventDefault();
|
|
632
|
+
//* Prevent event from being handled again by document.
|
|
633
|
+
e.stopPropagation();
|
|
634
|
+
this._openDropdown();
|
|
635
|
+
}
|
|
636
|
+
//* Allow navigation from dropdown to input on TAB.
|
|
637
|
+
if (e.key === "Tab") {
|
|
638
|
+
this._closeDropdown();
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
this.countryContainer.addEventListener("keydown", this._handleCountryContainerKeydown);
|
|
642
|
+
}
|
|
643
|
+
//* Init many requests: utils script / geo ip lookup.
|
|
644
|
+
_initRequests() {
|
|
645
|
+
// eslint-disable-next-line prefer-const
|
|
646
|
+
let { loadUtils, initialCountry, geoIpLookup } = this.options;
|
|
647
|
+
//* If the user has specified the path to the utils script, fetch it on window.load, else resolve.
|
|
648
|
+
if (loadUtils && !intlTelInput.utils) {
|
|
649
|
+
this._handlePageLoad = () => {
|
|
650
|
+
var _a;
|
|
651
|
+
window.removeEventListener("load", this._handlePageLoad);
|
|
652
|
+
//* Catch and ignore any errors to prevent unhandled-promise failures.
|
|
653
|
+
//* The error from `attachUtils()` is also surfaced in each instance's
|
|
654
|
+
//* `promise` property, so it's not getting lost by being ignored here.
|
|
655
|
+
(_a = intlTelInput.attachUtils(loadUtils)) === null || _a === void 0 ? void 0 : _a.catch(() => { });
|
|
656
|
+
};
|
|
657
|
+
//* If the plugin is being initialised after the window.load event has already been fired.
|
|
658
|
+
if (intlTelInput.documentReady()) {
|
|
659
|
+
this._handlePageLoad();
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
//* Wait until the load event so we don't block any other requests e.g. the flags image.
|
|
663
|
+
window.addEventListener("load", this._handlePageLoad);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
this.resolveUtilsScriptPromise();
|
|
668
|
+
}
|
|
669
|
+
//* Don't bother with IP lookup if we already have a selected country.
|
|
670
|
+
const isAutoCountry = initialCountry === "auto" && geoIpLookup;
|
|
671
|
+
if (isAutoCountry && !this.selectedCountryData.iso2) {
|
|
672
|
+
this._loadAutoCountry();
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
this.resolveAutoCountryPromise();
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
//* Perform the geo ip lookup.
|
|
679
|
+
_loadAutoCountry() {
|
|
680
|
+
//* 3 options:
|
|
681
|
+
//* 1) Already loaded (we're done)
|
|
682
|
+
//* 2) Not already started loading (start)
|
|
683
|
+
//* 3) Already started loading (do nothing - just wait for loading callback to fire)
|
|
684
|
+
if (intlTelInput.autoCountry) {
|
|
685
|
+
this.handleAutoCountry();
|
|
686
|
+
}
|
|
687
|
+
else if (!intlTelInput.startedLoadingAutoCountry) {
|
|
688
|
+
//* Don't do this twice!
|
|
689
|
+
intlTelInput.startedLoadingAutoCountry = true;
|
|
690
|
+
if (typeof this.options.geoIpLookup === "function") {
|
|
691
|
+
this.options.geoIpLookup((iso2 = "") => {
|
|
692
|
+
const iso2Lower = iso2.toLowerCase();
|
|
693
|
+
const isValidIso2 = iso2Lower && this._getCountryData(iso2Lower, true);
|
|
694
|
+
if (isValidIso2) {
|
|
695
|
+
intlTelInput.autoCountry = iso2Lower;
|
|
696
|
+
//* Tell all instances the auto country is ready.
|
|
697
|
+
//TODO: this should just be the current instances
|
|
698
|
+
//* UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
|
|
699
|
+
//* away (e.g. if they have already done the geo ip lookup somewhere else). Using
|
|
700
|
+
//* setTimeout means that the current thread of execution will finish before executing
|
|
701
|
+
//* this, which allows the plugin to finish initialising.
|
|
702
|
+
setTimeout(() => forEachInstance("handleAutoCountry"));
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
this._setInitialState(true);
|
|
706
|
+
forEachInstance("rejectAutoCountryPromise");
|
|
707
|
+
}
|
|
708
|
+
}, () => {
|
|
709
|
+
this._setInitialState(true);
|
|
710
|
+
forEachInstance("rejectAutoCountryPromise");
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
_openDropdownWithPlus() {
|
|
716
|
+
this._openDropdown();
|
|
717
|
+
this.searchInput.value = "+";
|
|
718
|
+
this._filterCountries("", true);
|
|
719
|
+
}
|
|
720
|
+
//* Initialize the tel input listeners.
|
|
721
|
+
_initTelInputListeners() {
|
|
722
|
+
const { strictMode, formatAsYouType, separateDialCode, formatOnDisplay, allowDropdown, countrySearch } = this.options;
|
|
723
|
+
let userOverrideFormatting = false;
|
|
724
|
+
//* If the initial val contains any alpha chars (e.g. the extension separator "ext."), then set the override, as libphonenumber's AYT-formatter cannot handle alphas.
|
|
725
|
+
if (/\p{L}/u.test(this.telInput.value)) {
|
|
726
|
+
userOverrideFormatting = true;
|
|
727
|
+
}
|
|
728
|
+
//* On input event: (1) Update selected country, (2) Format-as-you-type.
|
|
729
|
+
//* Note that this fires AFTER the input is updated.
|
|
730
|
+
this._handleInputEvent = (e) => {
|
|
731
|
+
//* Android workaround for handling plus when separateDialCode enabled (as impossible to handle with keydown/keyup, for which e.key always returns "Unidentified", see https://stackoverflow.com/q/59584061/217866)
|
|
732
|
+
if (this.isAndroid && (e === null || e === void 0 ? void 0 : e.data) === "+" && separateDialCode && allowDropdown && countrySearch) {
|
|
733
|
+
const currentCaretPos = this.telInput.selectionStart || 0;
|
|
734
|
+
const valueBeforeCaret = this.telInput.value.substring(0, currentCaretPos - 1);
|
|
735
|
+
const valueAfterCaret = this.telInput.value.substring(currentCaretPos);
|
|
736
|
+
this.telInput.value = valueBeforeCaret + valueAfterCaret;
|
|
737
|
+
this._openDropdownWithPlus();
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
//* Update selected country.
|
|
741
|
+
if (this._updateCountryFromNumber(this.telInput.value)) {
|
|
742
|
+
this._triggerCountryChange();
|
|
743
|
+
}
|
|
744
|
+
//* If user types their own formatting char (not a plus or a numeric), or they paste something, then set the override.
|
|
745
|
+
const isFormattingChar = (e === null || e === void 0 ? void 0 : e.data) && /[^+0-9]/.test(e.data);
|
|
746
|
+
const isPaste = (e === null || e === void 0 ? void 0 : e.inputType) === "insertFromPaste" && this.telInput.value;
|
|
747
|
+
if (isFormattingChar || (isPaste && !strictMode)) {
|
|
748
|
+
userOverrideFormatting = true;
|
|
749
|
+
}
|
|
750
|
+
//* If user removes all formatting chars, then reset the override.
|
|
751
|
+
else if (!/[^+0-9]/.test(this.telInput.value)) {
|
|
752
|
+
userOverrideFormatting = false;
|
|
753
|
+
}
|
|
754
|
+
const disableFormatOnSetNumber = (e === null || e === void 0 ? void 0 : e.detail) && e.detail["isSetNumber"] && !formatOnDisplay;
|
|
755
|
+
//* Handle format-as-you-type, unless userOverrideFormatting, or disableFormatOnSetNumber.
|
|
756
|
+
if (formatAsYouType && !userOverrideFormatting && !disableFormatOnSetNumber) {
|
|
757
|
+
//* Maintain caret position after reformatting.
|
|
758
|
+
const currentCaretPos = this.telInput.selectionStart || 0;
|
|
759
|
+
const valueBeforeCaret = this.telInput.value.substring(0, currentCaretPos);
|
|
760
|
+
const relevantCharsBeforeCaret = valueBeforeCaret.replace(/[^+0-9]/g, "").length;
|
|
761
|
+
const isDeleteForwards = (e === null || e === void 0 ? void 0 : e.inputType) === "deleteContentForward";
|
|
762
|
+
const formattedValue = this._formatNumberAsYouType();
|
|
763
|
+
const newCaretPos = translateCursorPosition(relevantCharsBeforeCaret, formattedValue, currentCaretPos, isDeleteForwards);
|
|
764
|
+
this.telInput.value = formattedValue;
|
|
765
|
+
this.telInput.setSelectionRange(newCaretPos, newCaretPos);
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
//* This handles individual key presses as well as cut/paste events
|
|
769
|
+
//* the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
|
|
770
|
+
//* whereas "keyup" fires even for shift key, arrow key presses etc.
|
|
771
|
+
this.telInput.addEventListener("input", this._handleInputEvent);
|
|
772
|
+
if (strictMode || separateDialCode) {
|
|
773
|
+
//* On keydown event: (1) if strictMode then prevent invalid characters, (2) if separateDialCode then handle plus key
|
|
774
|
+
//* Note that this fires BEFORE the input is updated.
|
|
775
|
+
this._handleKeydownEvent = (e) => {
|
|
776
|
+
//* Only interested in actual character presses, rather than ctrl, alt, command, arrow keys, delete/backspace, cut/copy/paste etc.
|
|
777
|
+
if (e.key && e.key.length === 1 && !e.altKey && !e.ctrlKey && !e.metaKey) {
|
|
778
|
+
//* If separateDialCode, handle the plus key differently: open dropdown and put plus in the search input instead.
|
|
779
|
+
if (separateDialCode && allowDropdown && countrySearch && e.key === "+") {
|
|
780
|
+
e.preventDefault();
|
|
781
|
+
this._openDropdownWithPlus();
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
//* If strictMode, prevent invalid characters.
|
|
785
|
+
if (strictMode) {
|
|
786
|
+
const value = this.telInput.value;
|
|
787
|
+
const alreadyHasPlus = value.charAt(0) === "+";
|
|
788
|
+
const isInitialPlus = !alreadyHasPlus && this.telInput.selectionStart === 0 && e.key === "+";
|
|
789
|
+
const isNumeric = /^[0-9]$/.test(e.key);
|
|
790
|
+
const isAllowedChar = separateDialCode ? isNumeric : isInitialPlus || isNumeric;
|
|
791
|
+
// insert the new character in the right place
|
|
792
|
+
const newValue = value.slice(0, this.telInput.selectionStart) + e.key + value.slice(this.telInput.selectionEnd);
|
|
793
|
+
const newFullNumber = this._getFullNumber(newValue);
|
|
794
|
+
const coreNumber = intlTelInput.utils.getCoreNumber(newFullNumber, this.selectedCountryData.iso2);
|
|
795
|
+
const hasExceededMaxLength = this.maxCoreNumberLength && coreNumber.length > this.maxCoreNumberLength;
|
|
796
|
+
const newCountry = this._getNewCountryFromNumber(newFullNumber);
|
|
797
|
+
const isChangingDialCode = newCountry !== null;
|
|
798
|
+
// ignore the char if (1) it's not an allowed char, or (2) this new char will exceed the max length and this char will not change the selected country and it's not the initial plus (aka they're starting to type a dial code)
|
|
799
|
+
if (!isAllowedChar || (hasExceededMaxLength && !isChangingDialCode && !isInitialPlus)) {
|
|
800
|
+
e.preventDefault();
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
this.telInput.addEventListener("keydown", this._handleKeydownEvent);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
//* Adhere to the input's maxlength attr.
|
|
809
|
+
_cap(number) {
|
|
810
|
+
const max = parseInt(this.telInput.getAttribute("maxlength") || "", 10);
|
|
811
|
+
return max && number.length > max ? number.substr(0, max) : number;
|
|
812
|
+
}
|
|
813
|
+
//* Trigger a custom event on the input.
|
|
814
|
+
_trigger(name, detailProps = {}) {
|
|
815
|
+
const e = new CustomEvent(name, {
|
|
816
|
+
bubbles: true,
|
|
817
|
+
cancelable: true,
|
|
818
|
+
detail: detailProps,
|
|
819
|
+
});
|
|
820
|
+
this.telInput.dispatchEvent(e);
|
|
821
|
+
}
|
|
822
|
+
//* Open the dropdown.
|
|
823
|
+
_openDropdown() {
|
|
824
|
+
const { fixDropdownWidth, countrySearch } = this.options;
|
|
825
|
+
if (fixDropdownWidth) {
|
|
826
|
+
this.dropdownContent.style.width = `${this.telInput.offsetWidth}px`;
|
|
827
|
+
}
|
|
828
|
+
this.dropdownContent.classList.remove("iti__hide");
|
|
829
|
+
this.selectedCountry.setAttribute("aria-expanded", "true");
|
|
830
|
+
this._setDropdownPosition();
|
|
831
|
+
if (countrySearch) {
|
|
832
|
+
//* When countrySearch enabled, every time the dropdown is opened we reset by highlighting the first item and scrolling to top.
|
|
833
|
+
const firstCountryItem = this.countryList.firstElementChild;
|
|
834
|
+
if (firstCountryItem) {
|
|
835
|
+
this._highlightListItem(firstCountryItem, false);
|
|
836
|
+
this.countryList.scrollTop = 0;
|
|
837
|
+
}
|
|
838
|
+
this.searchInput.focus();
|
|
839
|
+
}
|
|
840
|
+
//* Bind all the dropdown-related listeners: mouseover, click, click-off, keydown.
|
|
841
|
+
this._bindDropdownListeners();
|
|
842
|
+
//* Update the arrow.
|
|
843
|
+
this.dropdownArrow.classList.add("iti__arrow--up");
|
|
844
|
+
this._trigger("open:countrydropdown");
|
|
845
|
+
}
|
|
846
|
+
//* Set the dropdown position
|
|
847
|
+
_setDropdownPosition() {
|
|
848
|
+
if (this.options.dropdownContainer) {
|
|
849
|
+
this.options.dropdownContainer.appendChild(this.dropdown);
|
|
850
|
+
}
|
|
851
|
+
if (!this.options.useFullscreenPopup) {
|
|
852
|
+
// getBoundingClientRect is relative to the viewport, so when you scroll down, pos.top goes down, hence needing to add on scrollTop below
|
|
853
|
+
const inputPosRelativeToVP = this.telInput.getBoundingClientRect();
|
|
854
|
+
const inputHeight = this.telInput.offsetHeight;
|
|
855
|
+
//* If dropdownContainer is enabled, calculate postion.
|
|
856
|
+
if (this.options.dropdownContainer) {
|
|
857
|
+
//* Calculate position.
|
|
858
|
+
this.dropdown.style.top = `${inputPosRelativeToVP.top + inputHeight}px`;
|
|
859
|
+
this.dropdown.style.left = `${inputPosRelativeToVP.left}px`;
|
|
860
|
+
//* Close menu on window scroll.
|
|
861
|
+
this._handleWindowScroll = () => this._closeDropdown();
|
|
862
|
+
window.addEventListener("scroll", this._handleWindowScroll);
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
//* We only bind dropdown listeners when the dropdown is open.
|
|
867
|
+
_bindDropdownListeners() {
|
|
868
|
+
//* When mouse over a list item, just highlight that one
|
|
869
|
+
//* we add the class "highlight", so if they hit "enter" we know which one to select.
|
|
870
|
+
this._handleMouseoverCountryList = (e) => {
|
|
871
|
+
var _a;
|
|
872
|
+
//* Handle event delegation, as we're listening for this event on the countryList.
|
|
873
|
+
const listItem = (_a = e.target) === null || _a === void 0 ? void 0 : _a.closest(".iti__country");
|
|
874
|
+
if (listItem) {
|
|
875
|
+
this._highlightListItem(listItem, false);
|
|
876
|
+
}
|
|
877
|
+
};
|
|
878
|
+
this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList);
|
|
879
|
+
//* Listen for country selection.
|
|
880
|
+
this._handleClickCountryList = (e) => {
|
|
881
|
+
var _a;
|
|
882
|
+
const listItem = (_a = e.target) === null || _a === void 0 ? void 0 : _a.closest(".iti__country");
|
|
883
|
+
if (listItem) {
|
|
884
|
+
this._selectListItem(listItem);
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
this.countryList.addEventListener("click", this._handleClickCountryList);
|
|
888
|
+
//* Click off to close (except when this initial opening click is bubbling up).
|
|
889
|
+
//* We cannot just stopPropagation as it may be needed to close another instance.
|
|
890
|
+
let isOpening = true;
|
|
891
|
+
this._handleClickOffToClose = () => {
|
|
892
|
+
if (!isOpening) {
|
|
893
|
+
this._closeDropdown();
|
|
894
|
+
}
|
|
895
|
+
isOpening = false;
|
|
896
|
+
};
|
|
897
|
+
document.documentElement.addEventListener("click", this._handleClickOffToClose);
|
|
898
|
+
//* Listen for up/down scrolling, enter to select, or escape to close.
|
|
899
|
+
//* Use keydown as keypress doesn't fire for non-char keys and we want to catch if they
|
|
900
|
+
//* just hit down and hold it to scroll down (no keyup event).
|
|
901
|
+
//* Listen on the document because that's where key events are triggered if no input has focus.
|
|
902
|
+
let query = "";
|
|
903
|
+
let queryTimer = null;
|
|
904
|
+
this._handleKeydownOnDropdown = (e) => {
|
|
905
|
+
//* prevent down key from scrolling the whole page, and enter key from submitting a form etc.
|
|
906
|
+
if (["ArrowUp", "ArrowDown", "Enter", "Escape"].includes(e.key)) {
|
|
907
|
+
e.preventDefault();
|
|
908
|
+
e.stopPropagation();
|
|
909
|
+
//* Up and down to navigate.
|
|
910
|
+
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
|
|
911
|
+
this._handleUpDownKey(e.key);
|
|
912
|
+
}
|
|
913
|
+
//* Enter to select.
|
|
914
|
+
else if (e.key === "Enter") {
|
|
915
|
+
this._handleEnterKey();
|
|
916
|
+
}
|
|
917
|
+
//* Esc to close
|
|
918
|
+
else if (e.key === "Escape") {
|
|
919
|
+
this._closeDropdown();
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
//* When countrySearch disabled: Listen for alpha chars to perform hidden search.
|
|
923
|
+
//* Regex allows one latin alpha char or space, based on https://stackoverflow.com/a/26900132/217866.
|
|
924
|
+
if (!this.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
|
|
925
|
+
e.stopPropagation();
|
|
926
|
+
//* Jump to countries that start with the query string.
|
|
927
|
+
if (queryTimer) {
|
|
928
|
+
clearTimeout(queryTimer);
|
|
929
|
+
}
|
|
930
|
+
query += e.key.toLowerCase();
|
|
931
|
+
this._searchForCountry(query);
|
|
932
|
+
//* If the timer hits 1 second, reset the query.
|
|
933
|
+
queryTimer = setTimeout(() => {
|
|
934
|
+
query = "";
|
|
935
|
+
}, 1000);
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
document.addEventListener("keydown", this._handleKeydownOnDropdown);
|
|
939
|
+
if (this.options.countrySearch) {
|
|
940
|
+
const doFilter = () => {
|
|
941
|
+
const inputQuery = this.searchInput.value.trim();
|
|
942
|
+
if (inputQuery) {
|
|
943
|
+
this._filterCountries(inputQuery);
|
|
944
|
+
}
|
|
945
|
+
else {
|
|
946
|
+
this._filterCountries("", true);
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
let keyupTimer = null;
|
|
950
|
+
this._handleSearchChange = () => {
|
|
951
|
+
//* Filtering country nodes is expensive (lots of DOM manipulation), so rate limit it.
|
|
952
|
+
if (keyupTimer) {
|
|
953
|
+
clearTimeout(keyupTimer);
|
|
954
|
+
}
|
|
955
|
+
keyupTimer = setTimeout(() => {
|
|
956
|
+
doFilter();
|
|
957
|
+
keyupTimer = null;
|
|
958
|
+
}, 100);
|
|
959
|
+
};
|
|
960
|
+
this.searchInput.addEventListener("input", this._handleSearchChange);
|
|
961
|
+
//* Stop propagation on search input click, so doesn't trigger click-off-to-close listener.
|
|
962
|
+
this.searchInput.addEventListener("click", (e) => e.stopPropagation());
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
//* Hidden search (countrySearch disabled): Find the first list item whose name starts with the query string.
|
|
966
|
+
_searchForCountry(query) {
|
|
967
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
968
|
+
const c = this.countries[i];
|
|
969
|
+
const startsWith = c.name.substr(0, query.length).toLowerCase() === query;
|
|
970
|
+
if (startsWith) {
|
|
971
|
+
const listItem = c.nodeById[this.id];
|
|
972
|
+
//* Update highlighting and scroll.
|
|
973
|
+
this._highlightListItem(listItem, false);
|
|
974
|
+
this._scrollTo(listItem);
|
|
975
|
+
break;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
//* Country search enabled: Filter the countries according to the search query.
|
|
980
|
+
_filterCountries(query, isReset = false) {
|
|
981
|
+
let noCountriesAddedYet = true;
|
|
982
|
+
this.countryList.innerHTML = "";
|
|
983
|
+
const normalisedQuery = normaliseString(query);
|
|
984
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
985
|
+
const c = this.countries[i];
|
|
986
|
+
const normalisedCountryName = normaliseString(c.name);
|
|
987
|
+
//* Initials: split on non-alpha chars (ignore ampersand, hyphen, dot etc) and take the first letter of each part.
|
|
988
|
+
const countryInitials = c.name.split(/[^a-zA-ZÀ-ÿа-яА-Я]/).map(word => word[0]).join("").toLowerCase();
|
|
989
|
+
const fullDialCode = `+${c.dialCode}`;
|
|
990
|
+
if (isReset ||
|
|
991
|
+
normalisedCountryName.includes(normalisedQuery) ||
|
|
992
|
+
fullDialCode.includes(normalisedQuery) ||
|
|
993
|
+
c.iso2.includes(normalisedQuery) ||
|
|
994
|
+
countryInitials.includes(normalisedQuery)) {
|
|
995
|
+
const listItem = c.nodeById[this.id];
|
|
996
|
+
if (listItem) {
|
|
997
|
+
this.countryList.appendChild(listItem);
|
|
998
|
+
}
|
|
999
|
+
//* Highlight the first item.
|
|
1000
|
+
if (noCountriesAddedYet) {
|
|
1001
|
+
this._highlightListItem(listItem, false);
|
|
1002
|
+
noCountriesAddedYet = false;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
//* If no countries are shown, unhighlight the previously highlighted item.
|
|
1007
|
+
if (noCountriesAddedYet) {
|
|
1008
|
+
this._highlightListItem(null, false);
|
|
1009
|
+
}
|
|
1010
|
+
//* Scroll to top (useful if user had previously scrolled down).
|
|
1011
|
+
this.countryList.scrollTop = 0;
|
|
1012
|
+
this._updateSearchResultsText();
|
|
1013
|
+
}
|
|
1014
|
+
//* Update search results text (for a11y).
|
|
1015
|
+
_updateSearchResultsText() {
|
|
1016
|
+
const { i18n } = this.options;
|
|
1017
|
+
const count = this.countryList.childElementCount;
|
|
1018
|
+
let searchText;
|
|
1019
|
+
if ("searchResultsText" in i18n) {
|
|
1020
|
+
searchText = i18n.searchResultsText(count);
|
|
1021
|
+
}
|
|
1022
|
+
else {
|
|
1023
|
+
if (count === 0) {
|
|
1024
|
+
searchText = i18n.zeroSearchResults;
|
|
1025
|
+
}
|
|
1026
|
+
else if (count === 1) {
|
|
1027
|
+
searchText = i18n.oneSearchResult;
|
|
1028
|
+
}
|
|
1029
|
+
else {
|
|
1030
|
+
// eslint-disable-next-line no-template-curly-in-string
|
|
1031
|
+
searchText = i18n.multipleSearchResults.replace("${count}", count.toString());
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
this.searchResultsA11yText.textContent = searchText;
|
|
1035
|
+
}
|
|
1036
|
+
//* Highlight the next/prev item in the list (and ensure it is visible).
|
|
1037
|
+
_handleUpDownKey(key) {
|
|
1038
|
+
var _a, _b;
|
|
1039
|
+
let next = key === "ArrowUp"
|
|
1040
|
+
? (_a = this.highlightedItem) === null || _a === void 0 ? void 0 : _a.previousElementSibling
|
|
1041
|
+
: (_b = this.highlightedItem) === null || _b === void 0 ? void 0 : _b.nextElementSibling;
|
|
1042
|
+
if (!next && this.countryList.childElementCount > 1) {
|
|
1043
|
+
//* Otherwise, we must be at the end, so loop round again.
|
|
1044
|
+
next =
|
|
1045
|
+
key === "ArrowUp"
|
|
1046
|
+
? this.countryList.lastElementChild
|
|
1047
|
+
: this.countryList.firstElementChild;
|
|
1048
|
+
}
|
|
1049
|
+
if (next) {
|
|
1050
|
+
//* Make sure the next item is visible
|
|
1051
|
+
//* (before calling focus(), which can cause the next item to scroll to the middle of the dropdown, which is jarring).
|
|
1052
|
+
this._scrollTo(next);
|
|
1053
|
+
//* If country search enabled, don't lose focus from the search input on up/down
|
|
1054
|
+
this._highlightListItem(next, false);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
//* Select the currently highlighted item.
|
|
1058
|
+
_handleEnterKey() {
|
|
1059
|
+
if (this.highlightedItem) {
|
|
1060
|
+
this._selectListItem(this.highlightedItem);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
//* Update the input's value to the given val (format first if possible)
|
|
1064
|
+
//* NOTE: this is called from _setInitialState, handleUtils and setNumber.
|
|
1065
|
+
_updateValFromNumber(fullNumber) {
|
|
1066
|
+
let number = fullNumber;
|
|
1067
|
+
if (this.options.formatOnDisplay &&
|
|
1068
|
+
intlTelInput.utils &&
|
|
1069
|
+
this.selectedCountryData) {
|
|
1070
|
+
const useNational = this.options.nationalMode ||
|
|
1071
|
+
(number.charAt(0) !== "+" && !this.options.separateDialCode);
|
|
1072
|
+
const { NATIONAL, INTERNATIONAL } = intlTelInput.utils.numberFormat;
|
|
1073
|
+
const format = useNational ? NATIONAL : INTERNATIONAL;
|
|
1074
|
+
number = intlTelInput.utils.formatNumber(number, this.selectedCountryData.iso2, format);
|
|
1075
|
+
}
|
|
1076
|
+
number = this._beforeSetNumber(number);
|
|
1077
|
+
this.telInput.value = number;
|
|
1078
|
+
}
|
|
1079
|
+
//* Check if need to select a new country based on the given number
|
|
1080
|
+
//* Note: called from _setInitialState, keyup handler, setNumber.
|
|
1081
|
+
_updateCountryFromNumber(fullNumber) {
|
|
1082
|
+
const iso2 = this._getNewCountryFromNumber(fullNumber);
|
|
1083
|
+
if (iso2 !== null) {
|
|
1084
|
+
return this._setCountry(iso2);
|
|
1085
|
+
}
|
|
1086
|
+
return false;
|
|
1087
|
+
}
|
|
1088
|
+
_ensureHasDialCode(number) {
|
|
1089
|
+
const { dialCode, nationalPrefix } = this.selectedCountryData;
|
|
1090
|
+
const alreadyHasPlus = number.charAt(0) === "+";
|
|
1091
|
+
if (alreadyHasPlus || !dialCode) {
|
|
1092
|
+
return number;
|
|
1093
|
+
}
|
|
1094
|
+
//* Don't remove "nationalPrefix" digit if separateDialCode is enabled, as it can be part of a valid area code e.g. in Russia then have area codes starting with 8, which is also the national prefix digit.
|
|
1095
|
+
const hasPrefix = nationalPrefix && number.charAt(0) === nationalPrefix && !this.options.separateDialCode;
|
|
1096
|
+
const cleanNumber = hasPrefix ? number.substring(1) : number;
|
|
1097
|
+
return `+${dialCode}${cleanNumber}`;
|
|
1098
|
+
}
|
|
1099
|
+
// Get the country ISO2 code from the given number
|
|
1100
|
+
// BUT ONLY IF ITS CHANGED FROM THE CURRENTLY SELECTED COUNTRY
|
|
1101
|
+
// NOTE: consider refactoring this to be more clear
|
|
1102
|
+
_getNewCountryFromNumber(fullNumber) {
|
|
1103
|
+
const plusIndex = fullNumber.indexOf("+");
|
|
1104
|
+
//* If it contains a plus, discard any chars before it e.g. accidental space char.
|
|
1105
|
+
//* This keeps the selected country auto-updating correctly, which we want as
|
|
1106
|
+
//* libphonenumber's validation/getNumber methods will ignore these chars anyway.
|
|
1107
|
+
let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
|
|
1108
|
+
const selectedIso2 = this.selectedCountryData.iso2;
|
|
1109
|
+
const selectedDialCode = this.selectedCountryData.dialCode;
|
|
1110
|
+
//* Ensure the number starts with the dial code, for getDialCode to work properly (e.g. if number is entered in national format, or with separateDialCode enabled)
|
|
1111
|
+
number = this._ensureHasDialCode(number);
|
|
1112
|
+
//* Try and extract valid dial code (plus area code digits) from input.
|
|
1113
|
+
const dialCodeMatch = this._getDialCode(number, true);
|
|
1114
|
+
const numeric = getNumeric(number);
|
|
1115
|
+
if (dialCodeMatch) {
|
|
1116
|
+
const dialCodeMatchNumeric = getNumeric(dialCodeMatch);
|
|
1117
|
+
const iso2Codes = this.dialCodeToIso2Map[dialCodeMatchNumeric];
|
|
1118
|
+
//* If they've just typed a dial code (from empty state), and it matches the last selected country, then stick to that country.
|
|
1119
|
+
//* e.g. if they select Aland Islands, then type it's dial code +358, we should stick to that country and not switch to Finland!
|
|
1120
|
+
if (!selectedIso2 && this.defaultCountry && iso2Codes.includes(this.defaultCountry)) {
|
|
1121
|
+
return this.defaultCountry;
|
|
1122
|
+
}
|
|
1123
|
+
//* Check if the right country is already selected (note: might be empty state - globe icon).
|
|
1124
|
+
// If the currently selected country has area codes, but none of them even partially matched the input number, then we need to switch to the default country for this dial code, so alreadySelected should be false
|
|
1125
|
+
const hasAreaCodesButNoneMatched = this.selectedCountryData.areaCodes && numeric.length > dialCodeMatchNumeric.length;
|
|
1126
|
+
const alreadySelected = selectedIso2 && iso2Codes.includes(selectedIso2) && !hasAreaCodesButNoneMatched;
|
|
1127
|
+
const isRegionlessNanpNumber = selectedDialCode === "1" && isRegionlessNanp(numeric);
|
|
1128
|
+
//* Only update the country if:
|
|
1129
|
+
//* A) NOT (we currently have a NANP country selected, and the number is a regionlessNanp)
|
|
1130
|
+
//* AND
|
|
1131
|
+
//* B) the right country is not already selected
|
|
1132
|
+
if (!isRegionlessNanpNumber && !alreadySelected) {
|
|
1133
|
+
//* If using onlyCountries option, iso2Codes[0] may be empty, so we must find the first non-empty index.
|
|
1134
|
+
for (let j = 0; j < iso2Codes.length; j++) {
|
|
1135
|
+
if (iso2Codes[j]) {
|
|
1136
|
+
return iso2Codes[j];
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
else if (number.charAt(0) === "+" && numeric.length) {
|
|
1142
|
+
//* Invalid dial code, so empty.
|
|
1143
|
+
//* Note: use getNumeric here because the number has not been formatted yet, so could contain bad chars.
|
|
1144
|
+
return "";
|
|
1145
|
+
}
|
|
1146
|
+
else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
|
|
1147
|
+
//* If no selected country, and user either clears the input, or just types a plus, then show default.
|
|
1148
|
+
return this.defaultCountry;
|
|
1149
|
+
}
|
|
1150
|
+
return null;
|
|
1151
|
+
}
|
|
1152
|
+
//* Remove highlighting from other list items and highlight the given item.
|
|
1153
|
+
_highlightListItem(listItem, shouldFocus) {
|
|
1154
|
+
const prevItem = this.highlightedItem;
|
|
1155
|
+
if (prevItem) {
|
|
1156
|
+
prevItem.classList.remove("iti__highlight");
|
|
1157
|
+
prevItem.setAttribute("aria-selected", "false");
|
|
1158
|
+
}
|
|
1159
|
+
//* Set this, even if it's null, as it will clear the highlight.
|
|
1160
|
+
this.highlightedItem = listItem;
|
|
1161
|
+
if (this.highlightedItem) {
|
|
1162
|
+
this.highlightedItem.classList.add("iti__highlight");
|
|
1163
|
+
this.highlightedItem.setAttribute("aria-selected", "true");
|
|
1164
|
+
const activeDescendant = this.highlightedItem.getAttribute("id") || "";
|
|
1165
|
+
this.selectedCountry.setAttribute("aria-activedescendant", activeDescendant);
|
|
1166
|
+
if (this.options.countrySearch) {
|
|
1167
|
+
this.searchInput.setAttribute("aria-activedescendant", activeDescendant);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
if (shouldFocus) {
|
|
1171
|
+
this.highlightedItem.focus();
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
//* Find the country data for the given iso2 code
|
|
1175
|
+
//* the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
|
|
1176
|
+
_getCountryData(iso2, allowFail) {
|
|
1177
|
+
for (let i = 0; i < this.countries.length; i++) {
|
|
1178
|
+
if (this.countries[i].iso2 === iso2) {
|
|
1179
|
+
return this.countries[i];
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
if (allowFail) {
|
|
1183
|
+
return null;
|
|
1184
|
+
}
|
|
1185
|
+
throw new Error(`No country data for '${iso2}'`);
|
|
1186
|
+
}
|
|
1187
|
+
//* Update the selected country, dial code (if separateDialCode), placeholder, title, and active list item.
|
|
1188
|
+
//* Note: called from _setInitialState, _updateCountryFromNumber, _selectListItem, setCountry.
|
|
1189
|
+
_setCountry(iso2) {
|
|
1190
|
+
const { separateDialCode, showFlags, i18n } = this.options;
|
|
1191
|
+
const prevCountry = this.selectedCountryData.iso2
|
|
1192
|
+
? this.selectedCountryData
|
|
1193
|
+
: {};
|
|
1194
|
+
//* Do this first as it will throw an error and stop if iso2 is invalid.
|
|
1195
|
+
this.selectedCountryData = iso2
|
|
1196
|
+
? this._getCountryData(iso2, false) || {}
|
|
1197
|
+
: {};
|
|
1198
|
+
//* Update the defaultCountry - we only need the iso2 from now on, so just store that.
|
|
1199
|
+
if (this.selectedCountryData.iso2) {
|
|
1200
|
+
this.defaultCountry = this.selectedCountryData.iso2;
|
|
1201
|
+
}
|
|
1202
|
+
//* Update the flag class and the a11y text.
|
|
1203
|
+
if (this.selectedCountryInner) {
|
|
1204
|
+
let flagClass = "";
|
|
1205
|
+
let a11yText = "";
|
|
1206
|
+
if (iso2 && showFlags) {
|
|
1207
|
+
flagClass = `iti__flag iti__${iso2}`;
|
|
1208
|
+
a11yText = `${this.selectedCountryData.name} +${this.selectedCountryData.dialCode}`;
|
|
1209
|
+
}
|
|
1210
|
+
else {
|
|
1211
|
+
flagClass = "iti__flag iti__globe";
|
|
1212
|
+
a11yText = i18n.noCountrySelected;
|
|
1213
|
+
}
|
|
1214
|
+
this.selectedCountryInner.className = flagClass;
|
|
1215
|
+
this.selectedCountryA11yText.textContent = a11yText;
|
|
1216
|
+
}
|
|
1217
|
+
this._setSelectedCountryTitleAttribute(iso2, separateDialCode);
|
|
1218
|
+
//* Update the selected dial code.
|
|
1219
|
+
if (separateDialCode) {
|
|
1220
|
+
const dialCode = this.selectedCountryData.dialCode
|
|
1221
|
+
? `+${this.selectedCountryData.dialCode}`
|
|
1222
|
+
: "";
|
|
1223
|
+
this.selectedDialCode.innerHTML = dialCode;
|
|
1224
|
+
this._updateInputPadding();
|
|
1225
|
+
}
|
|
1226
|
+
//* And the input's placeholder.
|
|
1227
|
+
this._updatePlaceholder();
|
|
1228
|
+
//* Update the maximum valid number length.
|
|
1229
|
+
this._updateMaxLength();
|
|
1230
|
+
//* Return if the country has changed or not.
|
|
1231
|
+
return prevCountry.iso2 !== iso2;
|
|
1232
|
+
}
|
|
1233
|
+
//* Update the input padding to make space for the selected country/dial code.
|
|
1234
|
+
_updateInputPadding() {
|
|
1235
|
+
if (this.selectedCountry) {
|
|
1236
|
+
//* offsetWidth is zero if input is in a hidden container during initialisation.
|
|
1237
|
+
const selectedCountryWidth = this.selectedCountry.offsetWidth || this._getHiddenSelectedCountryWidth();
|
|
1238
|
+
const inputPadding = selectedCountryWidth + 6;
|
|
1239
|
+
if (this.showSelectedCountryOnLeft) {
|
|
1240
|
+
this.telInput.style.paddingLeft = `${inputPadding}px`;
|
|
1241
|
+
}
|
|
1242
|
+
else {
|
|
1243
|
+
this.telInput.style.paddingRight = `${inputPadding}px`;
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
//* Update the maximum valid number length for the currently selected country.
|
|
1248
|
+
_updateMaxLength() {
|
|
1249
|
+
const { strictMode, placeholderNumberType, validationNumberTypes } = this.options;
|
|
1250
|
+
const { iso2 } = this.selectedCountryData;
|
|
1251
|
+
if (strictMode && intlTelInput.utils) {
|
|
1252
|
+
if (iso2) {
|
|
1253
|
+
const numberType = intlTelInput.utils.numberType[placeholderNumberType];
|
|
1254
|
+
let exampleNumber = intlTelInput.utils.getExampleNumber(iso2, false, numberType, true);
|
|
1255
|
+
//* See if adding more digits is still valid to get the true maximum valid length.
|
|
1256
|
+
let validNumber = exampleNumber;
|
|
1257
|
+
while (intlTelInput.utils.isPossibleNumber(exampleNumber, iso2, validationNumberTypes)) {
|
|
1258
|
+
validNumber = exampleNumber;
|
|
1259
|
+
exampleNumber += "0";
|
|
1260
|
+
}
|
|
1261
|
+
const coreNumber = intlTelInput.utils.getCoreNumber(validNumber, iso2);
|
|
1262
|
+
this.maxCoreNumberLength = coreNumber.length;
|
|
1263
|
+
// hack for Belarus, because for some reason, getCoreNumber("80294911911"), aka the placeholder number, returns "294911911" (9 digits), but getCoreNumber("8029491191"), aka you're typing the penultimate digit of the placeholder number, returns "8029491191" (10 digits) and so strictMode blocks it. so we increase the max length to 10 digits to allow this penultimate digit, and then when they type the final digit, getCoreNumber will return 9 digits again, so it will be fine.
|
|
1264
|
+
if (iso2 === "by") {
|
|
1265
|
+
this.maxCoreNumberLength = coreNumber.length + 1;
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
else {
|
|
1269
|
+
this.maxCoreNumberLength = null;
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
_setSelectedCountryTitleAttribute(iso2 = null, separateDialCode) {
|
|
1274
|
+
if (!this.selectedCountry) {
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
let title;
|
|
1278
|
+
if (iso2 && !separateDialCode) {
|
|
1279
|
+
title = `${this.selectedCountryData.name}: +${this.selectedCountryData.dialCode}`;
|
|
1280
|
+
}
|
|
1281
|
+
else if (iso2) {
|
|
1282
|
+
//* For screen reader output, we don't want to include the dial code in the reader output twice
|
|
1283
|
+
//* so just use the selected country name here:
|
|
1284
|
+
title = this.selectedCountryData.name;
|
|
1285
|
+
}
|
|
1286
|
+
else {
|
|
1287
|
+
title = "Unknown";
|
|
1288
|
+
}
|
|
1289
|
+
this.selectedCountry.setAttribute("title", title);
|
|
1290
|
+
}
|
|
1291
|
+
//* When the input is in a hidden container during initialisation, we must inject some markup
|
|
1292
|
+
//* into the end of the DOM to calculate the correct offsetWidth.
|
|
1293
|
+
//* NOTE: this is only used when separateDialCode is enabled, so countryContainer and selectedCountry
|
|
1294
|
+
//* will definitely exist.
|
|
1295
|
+
_getHiddenSelectedCountryWidth() {
|
|
1296
|
+
//* To get the right styling to apply, all we need is a shallow clone of the container,
|
|
1297
|
+
//* and then to inject a deep clone of the selectedCountry element.
|
|
1298
|
+
if (this.telInput.parentNode) {
|
|
1299
|
+
const containerClone = this.telInput.parentNode.cloneNode(false);
|
|
1300
|
+
containerClone.style.visibility = "hidden";
|
|
1301
|
+
document.body.appendChild(containerClone);
|
|
1302
|
+
const countryContainerClone = this.countryContainer.cloneNode();
|
|
1303
|
+
containerClone.appendChild(countryContainerClone);
|
|
1304
|
+
const selectedCountryClone = this.selectedCountry.cloneNode(true);
|
|
1305
|
+
countryContainerClone.appendChild(selectedCountryClone);
|
|
1306
|
+
const width = selectedCountryClone.offsetWidth;
|
|
1307
|
+
document.body.removeChild(containerClone);
|
|
1308
|
+
return width;
|
|
1309
|
+
}
|
|
1310
|
+
return 0;
|
|
1311
|
+
}
|
|
1312
|
+
//* Update the input placeholder to an example number from the currently selected country.
|
|
1313
|
+
_updatePlaceholder() {
|
|
1314
|
+
const { autoPlaceholder, placeholderNumberType, nationalMode, customPlaceholder, } = this.options;
|
|
1315
|
+
const shouldSetPlaceholder = autoPlaceholder === "aggressive" ||
|
|
1316
|
+
(!this.hadInitialPlaceholder && autoPlaceholder === "polite");
|
|
1317
|
+
if (intlTelInput.utils && shouldSetPlaceholder) {
|
|
1318
|
+
const numberType = intlTelInput.utils.numberType[placeholderNumberType];
|
|
1319
|
+
//* Note: Must set placeholder to empty string if no country selected (globe icon showing).
|
|
1320
|
+
let placeholder = this.selectedCountryData.iso2
|
|
1321
|
+
? intlTelInput.utils.getExampleNumber(this.selectedCountryData.iso2, nationalMode, numberType)
|
|
1322
|
+
: "";
|
|
1323
|
+
placeholder = this._beforeSetNumber(placeholder);
|
|
1324
|
+
if (typeof customPlaceholder === "function") {
|
|
1325
|
+
placeholder = customPlaceholder(placeholder, this.selectedCountryData);
|
|
1326
|
+
}
|
|
1327
|
+
this.telInput.setAttribute("placeholder", placeholder);
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
//* Called when the user selects a list item from the dropdown.
|
|
1331
|
+
_selectListItem(listItem) {
|
|
1332
|
+
//* Update selected country and active list item.
|
|
1333
|
+
const countryChanged = this._setCountry(listItem.getAttribute("data-country-code"));
|
|
1334
|
+
this._closeDropdown();
|
|
1335
|
+
this._updateDialCode(listItem.getAttribute("data-dial-code"));
|
|
1336
|
+
//* Focus the input.
|
|
1337
|
+
this.telInput.focus();
|
|
1338
|
+
if (countryChanged) {
|
|
1339
|
+
this._triggerCountryChange();
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
//* Close the dropdown and unbind any listeners.
|
|
1343
|
+
_closeDropdown() {
|
|
1344
|
+
this.dropdownContent.classList.add("iti__hide");
|
|
1345
|
+
this.selectedCountry.setAttribute("aria-expanded", "false");
|
|
1346
|
+
this.selectedCountry.removeAttribute("aria-activedescendant");
|
|
1347
|
+
if (this.highlightedItem) {
|
|
1348
|
+
this.highlightedItem.setAttribute("aria-selected", "false");
|
|
1349
|
+
}
|
|
1350
|
+
if (this.options.countrySearch) {
|
|
1351
|
+
this.searchInput.removeAttribute("aria-activedescendant");
|
|
1352
|
+
}
|
|
1353
|
+
//* Update the arrow.
|
|
1354
|
+
this.dropdownArrow.classList.remove("iti__arrow--up");
|
|
1355
|
+
//* Unbind key events.
|
|
1356
|
+
document.removeEventListener("keydown", this._handleKeydownOnDropdown);
|
|
1357
|
+
if (this.options.countrySearch) {
|
|
1358
|
+
this.searchInput.removeEventListener("input", this._handleSearchChange);
|
|
1359
|
+
}
|
|
1360
|
+
document.documentElement.removeEventListener("click", this._handleClickOffToClose);
|
|
1361
|
+
this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList);
|
|
1362
|
+
this.countryList.removeEventListener("click", this._handleClickCountryList);
|
|
1363
|
+
//* Remove menu from container.
|
|
1364
|
+
if (this.options.dropdownContainer) {
|
|
1365
|
+
if (!this.options.useFullscreenPopup) {
|
|
1366
|
+
window.removeEventListener("scroll", this._handleWindowScroll);
|
|
1367
|
+
}
|
|
1368
|
+
if (this.dropdown.parentNode) {
|
|
1369
|
+
this.dropdown.parentNode.removeChild(this.dropdown);
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
//* Unhook any deferred resource loads.
|
|
1373
|
+
if (this._handlePageLoad) {
|
|
1374
|
+
window.removeEventListener("load", this._handlePageLoad);
|
|
1375
|
+
}
|
|
1376
|
+
this._trigger("close:countrydropdown");
|
|
1377
|
+
}
|
|
1378
|
+
//* Check if an element is visible within it's container, else scroll until it is.
|
|
1379
|
+
_scrollTo(element) {
|
|
1380
|
+
const container = this.countryList;
|
|
1381
|
+
const scrollTop = document.documentElement.scrollTop;
|
|
1382
|
+
const containerHeight = container.offsetHeight;
|
|
1383
|
+
const containerTop = container.getBoundingClientRect().top + scrollTop;
|
|
1384
|
+
const containerBottom = containerTop + containerHeight;
|
|
1385
|
+
const elementHeight = element.offsetHeight;
|
|
1386
|
+
const elementTop = element.getBoundingClientRect().top + scrollTop;
|
|
1387
|
+
const elementBottom = elementTop + elementHeight;
|
|
1388
|
+
const newScrollTop = elementTop - containerTop + container.scrollTop;
|
|
1389
|
+
if (elementTop < containerTop) {
|
|
1390
|
+
//* Scroll up.
|
|
1391
|
+
container.scrollTop = newScrollTop;
|
|
1392
|
+
}
|
|
1393
|
+
else if (elementBottom > containerBottom) {
|
|
1394
|
+
//* Scroll down.
|
|
1395
|
+
const heightDifference = containerHeight - elementHeight;
|
|
1396
|
+
container.scrollTop = newScrollTop - heightDifference;
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
//* Replace any existing dial code with the new one
|
|
1400
|
+
//* Note: called from _selectListItem and setCountry
|
|
1401
|
+
_updateDialCode(newDialCodeBare) {
|
|
1402
|
+
const inputVal = this.telInput.value;
|
|
1403
|
+
//* Save having to pass this every time.
|
|
1404
|
+
const newDialCode = `+${newDialCodeBare}`;
|
|
1405
|
+
let newNumber;
|
|
1406
|
+
if (inputVal.charAt(0) === "+") {
|
|
1407
|
+
//* There's a plus so we're dealing with a replacement.
|
|
1408
|
+
const prevDialCode = this._getDialCode(inputVal);
|
|
1409
|
+
if (prevDialCode) {
|
|
1410
|
+
//* Current number contains a valid dial code, so replace it.
|
|
1411
|
+
newNumber = inputVal.replace(prevDialCode, newDialCode);
|
|
1412
|
+
}
|
|
1413
|
+
else {
|
|
1414
|
+
//* Current number contains an invalid dial code, so ditch it
|
|
1415
|
+
//* (no way to determine where the invalid dial code ends and the rest of the number begins)
|
|
1416
|
+
newNumber = newDialCode;
|
|
1417
|
+
}
|
|
1418
|
+
this.telInput.value = newNumber;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
//* Try and extract a valid international dial code from a full telephone number.
|
|
1422
|
+
//* Note: returns the raw string inc plus character and any whitespace/dots etc.
|
|
1423
|
+
_getDialCode(number, includeAreaCode) {
|
|
1424
|
+
let dialCode = "";
|
|
1425
|
+
//* Only interested in international numbers (starting with a plus)
|
|
1426
|
+
if (number.charAt(0) === "+") {
|
|
1427
|
+
let numericChars = "";
|
|
1428
|
+
//* Iterate over chars
|
|
1429
|
+
for (let i = 0; i < number.length; i++) {
|
|
1430
|
+
const c = number.charAt(i);
|
|
1431
|
+
//* If char is number.
|
|
1432
|
+
//* https://stackoverflow.com/a/8935649/217866
|
|
1433
|
+
if (!isNaN(parseInt(c, 10))) {
|
|
1434
|
+
numericChars += c;
|
|
1435
|
+
//* If current numericChars make a valid dial code.
|
|
1436
|
+
if (includeAreaCode) {
|
|
1437
|
+
if (this.dialCodeToIso2Map[numericChars]) {
|
|
1438
|
+
//* Store the actual raw string (useful for matching later).
|
|
1439
|
+
dialCode = number.substr(0, i + 1);
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
else {
|
|
1443
|
+
if (this.dialCodes[numericChars]) {
|
|
1444
|
+
dialCode = number.substr(0, i + 1);
|
|
1445
|
+
//* If we're just looking for a dial code, we can break as soon as we find one.
|
|
1446
|
+
break;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
//* Stop searching as soon as we can - in this case when we hit max len.
|
|
1450
|
+
if (numericChars.length === this.dialCodeMaxLen) {
|
|
1451
|
+
break;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
return dialCode;
|
|
1457
|
+
}
|
|
1458
|
+
//* Get the input val, adding the dial code if separateDialCode is enabled.
|
|
1459
|
+
_getFullNumber(overrideVal) {
|
|
1460
|
+
const val = overrideVal || this.telInput.value.trim();
|
|
1461
|
+
const { dialCode } = this.selectedCountryData;
|
|
1462
|
+
let prefix;
|
|
1463
|
+
const numericVal = getNumeric(val);
|
|
1464
|
+
if (this.options.separateDialCode &&
|
|
1465
|
+
val.charAt(0) !== "+" &&
|
|
1466
|
+
dialCode &&
|
|
1467
|
+
numericVal) {
|
|
1468
|
+
//* When using separateDialCode, it is visible so is effectively part of the typed number.
|
|
1469
|
+
prefix = `+${dialCode}`;
|
|
1470
|
+
}
|
|
1471
|
+
else {
|
|
1472
|
+
prefix = "";
|
|
1473
|
+
}
|
|
1474
|
+
return prefix + val;
|
|
1475
|
+
}
|
|
1476
|
+
//* Remove the dial code if separateDialCode is enabled also cap the length if the input has a maxlength attribute
|
|
1477
|
+
_beforeSetNumber(fullNumber) {
|
|
1478
|
+
let number = fullNumber;
|
|
1479
|
+
if (this.options.separateDialCode) {
|
|
1480
|
+
let dialCode = this._getDialCode(number);
|
|
1481
|
+
//* If there is a valid dial code.
|
|
1482
|
+
if (dialCode) {
|
|
1483
|
+
//* In case _getDialCode returned an area code as well.
|
|
1484
|
+
dialCode = `+${this.selectedCountryData.dialCode}`;
|
|
1485
|
+
//* a lot of numbers will have a space separating the dial code and the main number, and
|
|
1486
|
+
//* some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get rid of it.
|
|
1487
|
+
//* NOTE: Don't just trim all non-numerics as may want to preserve an open parenthesis etc.
|
|
1488
|
+
const start = number[dialCode.length] === " " || number[dialCode.length] === "-"
|
|
1489
|
+
? dialCode.length + 1
|
|
1490
|
+
: dialCode.length;
|
|
1491
|
+
number = number.substr(start);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
return this._cap(number);
|
|
1495
|
+
}
|
|
1496
|
+
//* Trigger the 'countrychange' event.
|
|
1497
|
+
_triggerCountryChange() {
|
|
1498
|
+
this._trigger("countrychange");
|
|
1499
|
+
}
|
|
1500
|
+
//* Format the number as the user types.
|
|
1501
|
+
_formatNumberAsYouType() {
|
|
1502
|
+
const val = this._getFullNumber();
|
|
1503
|
+
const result = intlTelInput.utils
|
|
1504
|
+
? intlTelInput.utils.formatNumberAsYouType(val, this.selectedCountryData.iso2)
|
|
1505
|
+
: val;
|
|
1506
|
+
//* If separateDialCode and they haven't (re)typed the dial code in the input as well,
|
|
1507
|
+
//* then remove the dial code.
|
|
1508
|
+
const { dialCode } = this.selectedCountryData;
|
|
1509
|
+
if (this.options.separateDialCode &&
|
|
1510
|
+
this.telInput.value.charAt(0) !== "+" &&
|
|
1511
|
+
result.includes(`+${dialCode}`)) {
|
|
1512
|
+
const afterDialCode = result.split(`+${dialCode}`)[1] || "";
|
|
1513
|
+
return afterDialCode.trim();
|
|
1514
|
+
}
|
|
1515
|
+
return result;
|
|
1516
|
+
}
|
|
1517
|
+
//**************************
|
|
1518
|
+
//* SECRET PUBLIC METHODS
|
|
1519
|
+
//**************************
|
|
1520
|
+
//* This is called when the geoip call returns.
|
|
1521
|
+
handleAutoCountry() {
|
|
1522
|
+
if (this.options.initialCountry === "auto" && intlTelInput.autoCountry) {
|
|
1523
|
+
//* We must set this even if there is an initial val in the input: in case the initial val is
|
|
1524
|
+
//* invalid and they delete it - they should see their auto country.
|
|
1525
|
+
this.defaultCountry = intlTelInput.autoCountry;
|
|
1526
|
+
const hasSelectedCountryOrGlobe = this.selectedCountryData.iso2 || this.selectedCountryInner.classList.contains("iti__globe");
|
|
1527
|
+
//* If no country/globe currently selected, then update the country.
|
|
1528
|
+
if (!hasSelectedCountryOrGlobe) {
|
|
1529
|
+
this.setCountry(this.defaultCountry);
|
|
1530
|
+
}
|
|
1531
|
+
this.resolveAutoCountryPromise();
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
//* This is called when the utils request completes.
|
|
1535
|
+
handleUtils() {
|
|
1536
|
+
//* If the request was successful
|
|
1537
|
+
if (intlTelInput.utils) {
|
|
1538
|
+
//* If there's an initial value in the input, then format it.
|
|
1539
|
+
if (this.telInput.value) {
|
|
1540
|
+
this._updateValFromNumber(this.telInput.value);
|
|
1541
|
+
}
|
|
1542
|
+
if (this.selectedCountryData.iso2) {
|
|
1543
|
+
this._updatePlaceholder();
|
|
1544
|
+
this._updateMaxLength();
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
this.resolveUtilsScriptPromise();
|
|
1548
|
+
}
|
|
1549
|
+
//********************
|
|
1550
|
+
//* PUBLIC METHODS
|
|
1551
|
+
//********************
|
|
1552
|
+
//* Remove plugin.
|
|
1553
|
+
destroy() {
|
|
1554
|
+
var _a, _b;
|
|
1555
|
+
this.telInput.iti = undefined;
|
|
1556
|
+
const { allowDropdown, separateDialCode } = this.options;
|
|
1557
|
+
if (allowDropdown) {
|
|
1558
|
+
//* Make sure the dropdown is closed (and unbind listeners).
|
|
1559
|
+
this._closeDropdown();
|
|
1560
|
+
this.selectedCountry.removeEventListener("click", this._handleClickSelectedCountry);
|
|
1561
|
+
this.countryContainer.removeEventListener("keydown", this._handleCountryContainerKeydown);
|
|
1562
|
+
//* Label click hack.
|
|
1563
|
+
const label = this.telInput.closest("label");
|
|
1564
|
+
if (label) {
|
|
1565
|
+
label.removeEventListener("click", this._handleLabelClick);
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
//* Unbind hiddenInput listeners.
|
|
1569
|
+
const { form } = this.telInput;
|
|
1570
|
+
if (this._handleHiddenInputSubmit && form) {
|
|
1571
|
+
form.removeEventListener("submit", this._handleHiddenInputSubmit);
|
|
1572
|
+
}
|
|
1573
|
+
//* Unbind key events, and cut/paste events.
|
|
1574
|
+
this.telInput.removeEventListener("input", this._handleInputEvent);
|
|
1575
|
+
if (this._handleKeydownEvent) {
|
|
1576
|
+
this.telInput.removeEventListener("keydown", this._handleKeydownEvent);
|
|
1577
|
+
}
|
|
1578
|
+
//* Remove attribute of id instance: data-intl-tel-input-id.
|
|
1579
|
+
this.telInput.removeAttribute("data-intl-tel-input-id");
|
|
1580
|
+
//* Restore original styling
|
|
1581
|
+
if (separateDialCode) {
|
|
1582
|
+
if (this.isRTL) {
|
|
1583
|
+
this.telInput.style.paddingRight = this.originalPaddingRight;
|
|
1584
|
+
}
|
|
1585
|
+
else {
|
|
1586
|
+
this.telInput.style.paddingLeft = this.originalPaddingLeft;
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
//* Remove markup (but leave the original input).
|
|
1590
|
+
const wrapper = this.telInput.parentNode;
|
|
1591
|
+
(_a = wrapper === null || wrapper === void 0 ? void 0 : wrapper.parentNode) === null || _a === void 0 ? void 0 : _a.insertBefore(this.telInput, wrapper);
|
|
1592
|
+
(_b = wrapper === null || wrapper === void 0 ? void 0 : wrapper.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(wrapper);
|
|
1593
|
+
delete intlTelInput.instances[this.id];
|
|
1594
|
+
}
|
|
1595
|
+
//* Get the extension from the current number.
|
|
1596
|
+
getExtension() {
|
|
1597
|
+
if (intlTelInput.utils) {
|
|
1598
|
+
return intlTelInput.utils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2);
|
|
1599
|
+
}
|
|
1600
|
+
return "";
|
|
1601
|
+
}
|
|
1602
|
+
//* Format the number to the given format.
|
|
1603
|
+
getNumber(format) {
|
|
1604
|
+
if (intlTelInput.utils) {
|
|
1605
|
+
const { iso2 } = this.selectedCountryData;
|
|
1606
|
+
return intlTelInput.utils.formatNumber(this._getFullNumber(), iso2, format);
|
|
1607
|
+
}
|
|
1608
|
+
return "";
|
|
1609
|
+
}
|
|
1610
|
+
//* Get the type of the entered number e.g. landline/mobile.
|
|
1611
|
+
getNumberType() {
|
|
1612
|
+
if (intlTelInput.utils) {
|
|
1613
|
+
return intlTelInput.utils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2);
|
|
1614
|
+
}
|
|
1615
|
+
return -99;
|
|
1616
|
+
}
|
|
1617
|
+
//* Get the country data for the currently selected country.
|
|
1618
|
+
getSelectedCountryData() {
|
|
1619
|
+
return this.selectedCountryData;
|
|
1620
|
+
}
|
|
1621
|
+
//* Get the validation error.
|
|
1622
|
+
getValidationError() {
|
|
1623
|
+
if (intlTelInput.utils) {
|
|
1624
|
+
const { iso2 } = this.selectedCountryData;
|
|
1625
|
+
return intlTelInput.utils.getValidationError(this._getFullNumber(), iso2);
|
|
1626
|
+
}
|
|
1627
|
+
return -99;
|
|
1628
|
+
}
|
|
1629
|
+
//* Validate the input val
|
|
1630
|
+
isValidNumber() {
|
|
1631
|
+
//* If there isn't a valid country selected, then it's not a valid number.
|
|
1632
|
+
if (!this.selectedCountryData.iso2) {
|
|
1633
|
+
return false;
|
|
1634
|
+
}
|
|
1635
|
+
const val = this._getFullNumber();
|
|
1636
|
+
const alphaCharPosition = val.search(/\p{L}/u);
|
|
1637
|
+
if (alphaCharPosition > -1) {
|
|
1638
|
+
const beforeAlphaChar = val.substring(0, alphaCharPosition);
|
|
1639
|
+
//* Workaround to allow some alpha chars e.g. "+1 (444) 444-4444 ext. 1234" while rejecting others e.g. "+1 (444) 444-FAST". The only legit use of alpha chars is as an extension separator, in which case, the number before it must be valid on its own.
|
|
1640
|
+
const beforeAlphaIsValid = this._utilsIsPossibleNumber(beforeAlphaChar);
|
|
1641
|
+
const isValid = this._utilsIsPossibleNumber(val);
|
|
1642
|
+
return beforeAlphaIsValid && isValid;
|
|
1643
|
+
}
|
|
1644
|
+
return this._utilsIsPossibleNumber(val);
|
|
1645
|
+
}
|
|
1646
|
+
_utilsIsPossibleNumber(val) {
|
|
1647
|
+
return intlTelInput.utils
|
|
1648
|
+
? intlTelInput.utils.isPossibleNumber(val, this.selectedCountryData.iso2, this.options.validationNumberTypes)
|
|
1649
|
+
: null;
|
|
1650
|
+
}
|
|
1651
|
+
//* Validate the input val (precise)
|
|
1652
|
+
isValidNumberPrecise() {
|
|
1653
|
+
//* If there isn't a valid country selected, then it's not a valid number.
|
|
1654
|
+
if (!this.selectedCountryData.iso2) {
|
|
1655
|
+
return false;
|
|
1656
|
+
}
|
|
1657
|
+
const val = this._getFullNumber();
|
|
1658
|
+
const alphaCharPosition = val.search(/\p{L}/u);
|
|
1659
|
+
if (alphaCharPosition > -1) {
|
|
1660
|
+
const beforeAlphaChar = val.substring(0, alphaCharPosition);
|
|
1661
|
+
//* Workaround to allow some alpha chars e.g. "+1 (444) 444-4444 ext. 1234" while rejecting others e.g. "+1 (444) 444-FAST". The only legit use of alpha chars is as an extension separator, in which case, the number before it must be valid on its own.
|
|
1662
|
+
const beforeAlphaIsValid = this._utilsIsValidNumber(beforeAlphaChar);
|
|
1663
|
+
const isValid = this._utilsIsValidNumber(val);
|
|
1664
|
+
return beforeAlphaIsValid && isValid;
|
|
1665
|
+
}
|
|
1666
|
+
return this._utilsIsValidNumber(val);
|
|
1667
|
+
}
|
|
1668
|
+
_utilsIsValidNumber(val) {
|
|
1669
|
+
return intlTelInput.utils
|
|
1670
|
+
? intlTelInput.utils.isValidNumber(val, this.selectedCountryData.iso2, this.options.validationNumberTypes)
|
|
1671
|
+
: null;
|
|
1672
|
+
}
|
|
1673
|
+
//* Update the selected country, and update the input val accordingly.
|
|
1674
|
+
setCountry(iso2) {
|
|
1675
|
+
const iso2Lower = iso2 === null || iso2 === void 0 ? void 0 : iso2.toLowerCase();
|
|
1676
|
+
const currentCountry = this.selectedCountryData.iso2;
|
|
1677
|
+
//* There is a country change IF: either there is a new country and it's different to the current one, OR there is no new country (i.e. globe state) and there is a current country
|
|
1678
|
+
const isCountryChange = (iso2 && (iso2Lower !== currentCountry) || (!iso2 && currentCountry));
|
|
1679
|
+
if (isCountryChange) {
|
|
1680
|
+
this._setCountry(iso2Lower);
|
|
1681
|
+
this._updateDialCode(this.selectedCountryData.dialCode);
|
|
1682
|
+
this._triggerCountryChange();
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
//* Set the input value and update the country.
|
|
1686
|
+
setNumber(number) {
|
|
1687
|
+
//* We must update the country first, which updates this.selectedCountryData, which is used for
|
|
1688
|
+
//* formatting the number before displaying it.
|
|
1689
|
+
const countryChanged = this._updateCountryFromNumber(number);
|
|
1690
|
+
this._updateValFromNumber(number);
|
|
1691
|
+
if (countryChanged) {
|
|
1692
|
+
this._triggerCountryChange();
|
|
1693
|
+
}
|
|
1694
|
+
//* This is required for the React cmp to update its state correctly.
|
|
1695
|
+
this._trigger("input", { isSetNumber: true });
|
|
1696
|
+
}
|
|
1697
|
+
//* Set the placeholder number typ
|
|
1698
|
+
setPlaceholderNumberType(type) {
|
|
1699
|
+
this.options.placeholderNumberType = type;
|
|
1700
|
+
this._updatePlaceholder();
|
|
1701
|
+
}
|
|
1702
|
+
setDisabled(disabled) {
|
|
1703
|
+
this.telInput.disabled = disabled;
|
|
1704
|
+
if (disabled) {
|
|
1705
|
+
this.selectedCountry.setAttribute("disabled", "true");
|
|
1706
|
+
}
|
|
1707
|
+
else {
|
|
1708
|
+
this.selectedCountry.removeAttribute("disabled");
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
/********************
|
|
1713
|
+
* STATIC METHODS
|
|
1714
|
+
********************/
|
|
1715
|
+
//* Load the utils script.
|
|
1716
|
+
const attachUtils = (source) => {
|
|
1717
|
+
//* 2 Options:
|
|
1718
|
+
//* 1) Not already started loading (start)
|
|
1719
|
+
//* 2) Already started loading (do nothing - just wait for the onload callback to fire, which will
|
|
1720
|
+
//* trigger handleUtils on all instances, invoking their resolveUtilsScriptPromise functions)
|
|
1721
|
+
if (!intlTelInput.utils &&
|
|
1722
|
+
!intlTelInput.startedLoadingUtilsScript) {
|
|
1723
|
+
let loadCall;
|
|
1724
|
+
if (typeof source === "function") {
|
|
1725
|
+
try {
|
|
1726
|
+
loadCall = Promise.resolve(source());
|
|
1727
|
+
}
|
|
1728
|
+
catch (error) {
|
|
1729
|
+
return Promise.reject(error);
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
else {
|
|
1733
|
+
return Promise.reject(new TypeError(`The argument passed to attachUtils must be a function that returns a promise for the utilities module, not ${typeof source}`));
|
|
1734
|
+
}
|
|
1735
|
+
//* Only do this once.
|
|
1736
|
+
intlTelInput.startedLoadingUtilsScript = true;
|
|
1737
|
+
return loadCall
|
|
1738
|
+
.then((module) => {
|
|
1739
|
+
const utils = module === null || module === void 0 ? void 0 : module.default;
|
|
1740
|
+
if (!utils || typeof utils !== "object") {
|
|
1741
|
+
throw new TypeError("The loader function passed to attachUtils did not resolve to a module object with utils as its default export.");
|
|
1742
|
+
}
|
|
1743
|
+
intlTelInput.utils = utils;
|
|
1744
|
+
forEachInstance("handleUtils");
|
|
1745
|
+
return true;
|
|
1746
|
+
})
|
|
1747
|
+
.catch((error) => {
|
|
1748
|
+
forEachInstance("rejectUtilsScriptPromise", error);
|
|
1749
|
+
throw error;
|
|
1750
|
+
});
|
|
1751
|
+
}
|
|
1752
|
+
return null;
|
|
1753
|
+
};
|
|
1754
|
+
//* Convenience wrapper.
|
|
1755
|
+
const intlTelInput = Object.assign((input, options) => {
|
|
1756
|
+
const iti = new Iti(input, options);
|
|
1757
|
+
iti._init();
|
|
1758
|
+
input.setAttribute("data-intl-tel-input-id", iti.id.toString());
|
|
1759
|
+
intlTelInput.instances[iti.id] = iti;
|
|
1760
|
+
input.iti = iti;
|
|
1761
|
+
return iti;
|
|
1762
|
+
}, {
|
|
1763
|
+
defaults,
|
|
1764
|
+
//* Using a static var like this allows us to mock it in the tests.
|
|
1765
|
+
documentReady: () => document.readyState === "complete",
|
|
1766
|
+
//* Get the country data object.
|
|
1767
|
+
getCountryData: () => allCountries,
|
|
1768
|
+
//* A getter for the plugin instance.
|
|
1769
|
+
getInstance: (input) => {
|
|
1770
|
+
const id = input.getAttribute("data-intl-tel-input-id");
|
|
1771
|
+
return id ? intlTelInput.instances[id] : null;
|
|
1772
|
+
},
|
|
1773
|
+
//* A map from instance ID to instance object.
|
|
1774
|
+
instances: {},
|
|
1775
|
+
attachUtils,
|
|
1776
|
+
startedLoadingUtilsScript: false,
|
|
1777
|
+
startedLoadingAutoCountry: false,
|
|
1778
|
+
version: process.env.VERSION,
|
|
1779
|
+
});
|
|
1780
|
+
export default intlTelInput;
|