intl-tel-input 23.3.2 → 23.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * International Telephone Input v23.3.2
2
+ * International Telephone Input v23.4.1
3
3
  * https://github.com/jackocnr/intl-tel-input.git
4
4
  * Licensed under the MIT license
5
5
  */
@@ -1401,6 +1401,8 @@ var factoryOutput = (() => {
1401
1401
  containerClass: "",
1402
1402
  //* The order of the countries in the dropdown. Defaults to alphabetical.
1403
1403
  countryOrder: null,
1404
+ //* Add a country search input at the top of the dropdown.
1405
+ countrySearch: true,
1404
1406
  //* Modify the auto placeholder.
1405
1407
  customPlaceholder: null,
1406
1408
  //* Append menu to specified element.
@@ -1523,6 +1525,7 @@ var factoryOutput = (() => {
1523
1525
  if (this.options.separateDialCode) {
1524
1526
  this.options.allowDropdown = true;
1525
1527
  this.options.nationalMode = false;
1528
+ this.options.countrySearch = true;
1526
1529
  }
1527
1530
  if (!this.options.showFlags && !this.options.separateDialCode) {
1528
1531
  this.options.nationalMode = false;
@@ -1532,6 +1535,13 @@ var factoryOutput = (() => {
1532
1535
  }
1533
1536
  this.isAndroid = typeof navigator !== "undefined" ? /Android/i.test(navigator.userAgent) : false;
1534
1537
  this.isRTL = !!this.telInput.closest("[dir=rtl]");
1538
+ if (this.options.separateDialCode) {
1539
+ if (this.isRTL) {
1540
+ this.originalPaddingRight = this.telInput.style.paddingRight;
1541
+ } else {
1542
+ this.originalPaddingLeft = this.telInput.style.paddingLeft;
1543
+ }
1544
+ }
1535
1545
  this.options.i18n = { ...en_default, ...this.options.i18n };
1536
1546
  const autoCountryPromise = new Promise((resolve, reject) => {
1537
1547
  this.resolveAutoCountryPromise = resolve;
@@ -1675,6 +1685,7 @@ var factoryOutput = (() => {
1675
1685
  dropdownContainer,
1676
1686
  fixDropdownWidth,
1677
1687
  useFullscreenPopup,
1688
+ countrySearch,
1678
1689
  i18n
1679
1690
  } = this.options;
1680
1691
  let parentClass = "iti";
@@ -1751,26 +1762,28 @@ var factoryOutput = (() => {
1751
1762
  id: `iti-${this.id}__dropdown-content`,
1752
1763
  class: `iti__dropdown-content iti__hide ${extraClasses}`
1753
1764
  });
1754
- this.searchInput = createEl(
1755
- "input",
1756
- {
1757
- type: "text",
1758
- class: "iti__search-input",
1759
- placeholder: i18n.searchPlaceholder,
1760
- role: "combobox",
1761
- "aria-expanded": "true",
1762
- "aria-label": i18n.searchPlaceholder,
1763
- "aria-controls": `iti-${this.id}__country-listbox`,
1764
- "aria-autocomplete": "list",
1765
- "autocomplete": "off"
1766
- },
1767
- this.dropdownContent
1768
- );
1769
- this.searchResultsA11yText = createEl(
1770
- "span",
1771
- { class: "iti__a11y-text" },
1772
- this.dropdownContent
1773
- );
1765
+ if (countrySearch) {
1766
+ this.searchInput = createEl(
1767
+ "input",
1768
+ {
1769
+ type: "text",
1770
+ class: "iti__search-input",
1771
+ placeholder: i18n.searchPlaceholder,
1772
+ role: "combobox",
1773
+ "aria-expanded": "true",
1774
+ "aria-label": i18n.searchPlaceholder,
1775
+ "aria-controls": `iti-${this.id}__country-listbox`,
1776
+ "aria-autocomplete": "list",
1777
+ "autocomplete": "off"
1778
+ },
1779
+ this.dropdownContent
1780
+ );
1781
+ this.searchResultsA11yText = createEl(
1782
+ "span",
1783
+ { class: "iti__a11y-text" },
1784
+ this.dropdownContent
1785
+ );
1786
+ }
1774
1787
  this.countryList = createEl(
1775
1788
  "ul",
1776
1789
  {
@@ -1781,8 +1794,10 @@ var factoryOutput = (() => {
1781
1794
  },
1782
1795
  this.dropdownContent
1783
1796
  );
1784
- this._appendListItems(this.countries, "iti__standard");
1785
- this._updateSearchResultsText();
1797
+ this._appendListItems();
1798
+ if (countrySearch) {
1799
+ this._updateSearchResultsText();
1800
+ }
1786
1801
  if (dropdownContainer) {
1787
1802
  let dropdownClasses = "iti iti--container";
1788
1803
  if (useFullscreenPopup) {
@@ -1817,15 +1832,16 @@ var factoryOutput = (() => {
1817
1832
  }
1818
1833
  }
1819
1834
  }
1820
- //* For each of the passed countries: add a country <li> to the countryList <ul> container.
1821
- _appendListItems(countries, className) {
1822
- for (let i = 0; i < countries.length; i++) {
1823
- const c = countries[i];
1835
+ //* For each country: add a country list item <li> to the countryList <ul> container.
1836
+ _appendListItems() {
1837
+ for (let i = 0; i < this.countries.length; i++) {
1838
+ const c = this.countries[i];
1839
+ const extraClass = i === 0 ? "iti__highlight" : "";
1824
1840
  const listItem = createEl(
1825
1841
  "li",
1826
1842
  {
1827
1843
  id: `iti-${this.id}__item-${c.iso2}`,
1828
- class: `iti__country ${className}`,
1844
+ class: `iti__country ${extraClass}`,
1829
1845
  tabindex: "-1",
1830
1846
  role: "option",
1831
1847
  "data-dial-code": c.dialCode,
@@ -2066,24 +2082,26 @@ var factoryOutput = (() => {
2066
2082
  }
2067
2083
  //* Open the dropdown.
2068
2084
  _openDropdown() {
2069
- const { fixDropdownWidth } = this.options;
2085
+ const { fixDropdownWidth, countrySearch } = this.options;
2070
2086
  if (fixDropdownWidth) {
2071
2087
  this.dropdownContent.style.width = `${this.telInput.offsetWidth}px`;
2072
2088
  }
2073
2089
  this.dropdownContent.classList.remove("iti__hide");
2074
2090
  this.selectedCountry.setAttribute("aria-expanded", "true");
2075
2091
  this._setDropdownPosition();
2076
- const firstCountryItem = this.countryList.firstElementChild;
2077
- if (firstCountryItem) {
2078
- this._highlightListItem(firstCountryItem, false);
2079
- this.countryList.scrollTop = 0;
2092
+ if (countrySearch) {
2093
+ const firstCountryItem = this.countryList.firstElementChild;
2094
+ if (firstCountryItem) {
2095
+ this._highlightListItem(firstCountryItem, false);
2096
+ this.countryList.scrollTop = 0;
2097
+ }
2098
+ this.searchInput.focus();
2080
2099
  }
2081
- this.searchInput.focus();
2082
2100
  this._bindDropdownListeners();
2083
2101
  this.dropdownArrow.classList.add("iti__arrow--up");
2084
2102
  this._trigger("open:countrydropdown");
2085
2103
  }
2086
- //* Decide if should position dropdown above or below input (depends on position within viewport, and scroll).
2104
+ //* Set the dropdown position
2087
2105
  _setDropdownPosition() {
2088
2106
  if (this.options.dropdownContainer) {
2089
2107
  this.options.dropdownContainer.appendChild(this.dropdown);
@@ -2129,6 +2147,8 @@ var factoryOutput = (() => {
2129
2147
  "click",
2130
2148
  this._handleClickOffToClose
2131
2149
  );
2150
+ let query = "";
2151
+ let queryTimer = null;
2132
2152
  this._handleKeydownOnDropdown = (e) => {
2133
2153
  if (["ArrowUp", "ArrowDown", "Enter", "Escape"].includes(e.key)) {
2134
2154
  e.preventDefault();
@@ -2141,29 +2161,56 @@ var factoryOutput = (() => {
2141
2161
  this._closeDropdown();
2142
2162
  }
2143
2163
  }
2144
- };
2145
- document.addEventListener("keydown", this._handleKeydownOnDropdown);
2146
- const doFilter = () => {
2147
- const inputQuery = this.searchInput.value.trim();
2148
- if (inputQuery) {
2149
- this._filterCountries(inputQuery);
2150
- } else {
2151
- this._filterCountries("", true);
2164
+ if (!this.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
2165
+ e.stopPropagation();
2166
+ if (queryTimer) {
2167
+ clearTimeout(queryTimer);
2168
+ }
2169
+ query += e.key.toLowerCase();
2170
+ this._searchForCountry(query);
2171
+ queryTimer = setTimeout(() => {
2172
+ query = "";
2173
+ }, 1e3);
2152
2174
  }
2153
2175
  };
2154
- let keyupTimer = null;
2155
- this._handleSearchChange = () => {
2156
- if (keyupTimer) {
2157
- clearTimeout(keyupTimer);
2176
+ document.addEventListener("keydown", this._handleKeydownOnDropdown);
2177
+ if (this.options.countrySearch) {
2178
+ const doFilter = () => {
2179
+ const inputQuery = this.searchInput.value.trim();
2180
+ if (inputQuery) {
2181
+ this._filterCountries(inputQuery);
2182
+ } else {
2183
+ this._filterCountries("", true);
2184
+ }
2185
+ };
2186
+ let keyupTimer = null;
2187
+ this._handleSearchChange = () => {
2188
+ if (keyupTimer) {
2189
+ clearTimeout(keyupTimer);
2190
+ }
2191
+ keyupTimer = setTimeout(() => {
2192
+ doFilter();
2193
+ keyupTimer = null;
2194
+ }, 100);
2195
+ };
2196
+ this.searchInput.addEventListener("input", this._handleSearchChange);
2197
+ this.searchInput.addEventListener("click", (e) => e.stopPropagation());
2198
+ }
2199
+ }
2200
+ //* Hidden search (countrySearch disabled): Find the first list item whose name starts with the query string.
2201
+ _searchForCountry(query) {
2202
+ for (let i = 0; i < this.countries.length; i++) {
2203
+ const c = this.countries[i];
2204
+ const startsWith = c.name.substr(0, query.length).toLowerCase() === query;
2205
+ if (startsWith) {
2206
+ const listItem = c.nodeById[this.id];
2207
+ this._highlightListItem(listItem, false);
2208
+ this._scrollTo(listItem);
2209
+ break;
2158
2210
  }
2159
- keyupTimer = setTimeout(() => {
2160
- doFilter();
2161
- keyupTimer = null;
2162
- }, 100);
2163
- };
2164
- this.searchInput.addEventListener("input", this._handleSearchChange);
2165
- this.searchInput.addEventListener("click", (e) => e.stopPropagation());
2211
+ }
2166
2212
  }
2213
+ //* Country search enabled: Filter the countries according to the search query.
2167
2214
  _filterCountries(query, isReset = false) {
2168
2215
  let noCountriesAddedYet = true;
2169
2216
  this.countryList.innerHTML = "";
@@ -2291,7 +2338,9 @@ var factoryOutput = (() => {
2291
2338
  this.highlightedItem.setAttribute("aria-selected", "true");
2292
2339
  const activeDescendant = this.highlightedItem.getAttribute("id") || "";
2293
2340
  this.selectedCountry.setAttribute("aria-activedescendant", activeDescendant);
2294
- this.searchInput.setAttribute("aria-activedescendant", activeDescendant);
2341
+ if (this.options.countrySearch) {
2342
+ this.searchInput.setAttribute("aria-activedescendant", activeDescendant);
2343
+ }
2295
2344
  }
2296
2345
  if (shouldFocus) {
2297
2346
  this.highlightedItem.focus();
@@ -2448,10 +2497,14 @@ var factoryOutput = (() => {
2448
2497
  if (this.highlightedItem) {
2449
2498
  this.highlightedItem.setAttribute("aria-selected", "false");
2450
2499
  }
2451
- this.searchInput.removeAttribute("aria-activedescendant");
2500
+ if (this.options.countrySearch) {
2501
+ this.searchInput.removeAttribute("aria-activedescendant");
2502
+ }
2452
2503
  this.dropdownArrow.classList.remove("iti__arrow--up");
2453
2504
  document.removeEventListener("keydown", this._handleKeydownOnDropdown);
2454
- this.searchInput.removeEventListener("input", this._handleSearchChange);
2505
+ if (this.options.countrySearch) {
2506
+ this.searchInput.removeEventListener("input", this._handleSearchChange);
2507
+ }
2455
2508
  document.documentElement.removeEventListener(
2456
2509
  "click",
2457
2510
  this._handleClickOffToClose
@@ -2606,7 +2659,8 @@ var factoryOutput = (() => {
2606
2659
  //********************
2607
2660
  //* Remove plugin.
2608
2661
  destroy() {
2609
- if (this.options.allowDropdown) {
2662
+ const { allowDropdown, separateDialCode } = this.options;
2663
+ if (allowDropdown) {
2610
2664
  this._closeDropdown();
2611
2665
  this.selectedCountry.removeEventListener(
2612
2666
  "click",
@@ -2630,6 +2684,13 @@ var factoryOutput = (() => {
2630
2684
  this.telInput.removeEventListener("keydown", this._handleKeydownEvent);
2631
2685
  }
2632
2686
  this.telInput.removeAttribute("data-intl-tel-input-id");
2687
+ if (separateDialCode) {
2688
+ if (this.isRTL) {
2689
+ this.telInput.style.paddingRight = this.originalPaddingRight;
2690
+ } else {
2691
+ this.telInput.style.paddingLeft = this.originalPaddingLeft;
2692
+ }
2693
+ }
2633
2694
  const wrapper = this.telInput.parentNode;
2634
2695
  wrapper?.parentNode?.insertBefore(this.telInput, wrapper);
2635
2696
  wrapper?.parentNode?.removeChild(wrapper);
@@ -2762,7 +2823,7 @@ var factoryOutput = (() => {
2762
2823
  //* A map from instance ID to instance object.
2763
2824
  instances: {},
2764
2825
  loadUtils,
2765
- version: "23.3.2"
2826
+ version: "23.4.1"
2766
2827
  }
2767
2828
  );
2768
2829
  var intl_tel_input_default = intlTelInput;