intl-tel-input 23.3.2 → 23.4.0

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.0
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;
@@ -1675,6 +1678,7 @@ var factoryOutput = (() => {
1675
1678
  dropdownContainer,
1676
1679
  fixDropdownWidth,
1677
1680
  useFullscreenPopup,
1681
+ countrySearch,
1678
1682
  i18n
1679
1683
  } = this.options;
1680
1684
  let parentClass = "iti";
@@ -1751,26 +1755,28 @@ var factoryOutput = (() => {
1751
1755
  id: `iti-${this.id}__dropdown-content`,
1752
1756
  class: `iti__dropdown-content iti__hide ${extraClasses}`
1753
1757
  });
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
- );
1758
+ if (countrySearch) {
1759
+ this.searchInput = createEl(
1760
+ "input",
1761
+ {
1762
+ type: "text",
1763
+ class: "iti__search-input",
1764
+ placeholder: i18n.searchPlaceholder,
1765
+ role: "combobox",
1766
+ "aria-expanded": "true",
1767
+ "aria-label": i18n.searchPlaceholder,
1768
+ "aria-controls": `iti-${this.id}__country-listbox`,
1769
+ "aria-autocomplete": "list",
1770
+ "autocomplete": "off"
1771
+ },
1772
+ this.dropdownContent
1773
+ );
1774
+ this.searchResultsA11yText = createEl(
1775
+ "span",
1776
+ { class: "iti__a11y-text" },
1777
+ this.dropdownContent
1778
+ );
1779
+ }
1774
1780
  this.countryList = createEl(
1775
1781
  "ul",
1776
1782
  {
@@ -1781,8 +1787,10 @@ var factoryOutput = (() => {
1781
1787
  },
1782
1788
  this.dropdownContent
1783
1789
  );
1784
- this._appendListItems(this.countries, "iti__standard");
1785
- this._updateSearchResultsText();
1790
+ this._appendListItems();
1791
+ if (countrySearch) {
1792
+ this._updateSearchResultsText();
1793
+ }
1786
1794
  if (dropdownContainer) {
1787
1795
  let dropdownClasses = "iti iti--container";
1788
1796
  if (useFullscreenPopup) {
@@ -1817,15 +1825,16 @@ var factoryOutput = (() => {
1817
1825
  }
1818
1826
  }
1819
1827
  }
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];
1828
+ //* For each country: add a country list item <li> to the countryList <ul> container.
1829
+ _appendListItems() {
1830
+ for (let i = 0; i < this.countries.length; i++) {
1831
+ const c = this.countries[i];
1832
+ const extraClass = i === 0 ? "iti__highlight" : "";
1824
1833
  const listItem = createEl(
1825
1834
  "li",
1826
1835
  {
1827
1836
  id: `iti-${this.id}__item-${c.iso2}`,
1828
- class: `iti__country ${className}`,
1837
+ class: `iti__country ${extraClass}`,
1829
1838
  tabindex: "-1",
1830
1839
  role: "option",
1831
1840
  "data-dial-code": c.dialCode,
@@ -2066,24 +2075,26 @@ var factoryOutput = (() => {
2066
2075
  }
2067
2076
  //* Open the dropdown.
2068
2077
  _openDropdown() {
2069
- const { fixDropdownWidth } = this.options;
2078
+ const { fixDropdownWidth, countrySearch } = this.options;
2070
2079
  if (fixDropdownWidth) {
2071
2080
  this.dropdownContent.style.width = `${this.telInput.offsetWidth}px`;
2072
2081
  }
2073
2082
  this.dropdownContent.classList.remove("iti__hide");
2074
2083
  this.selectedCountry.setAttribute("aria-expanded", "true");
2075
2084
  this._setDropdownPosition();
2076
- const firstCountryItem = this.countryList.firstElementChild;
2077
- if (firstCountryItem) {
2078
- this._highlightListItem(firstCountryItem, false);
2079
- this.countryList.scrollTop = 0;
2085
+ if (countrySearch) {
2086
+ const firstCountryItem = this.countryList.firstElementChild;
2087
+ if (firstCountryItem) {
2088
+ this._highlightListItem(firstCountryItem, false);
2089
+ this.countryList.scrollTop = 0;
2090
+ }
2091
+ this.searchInput.focus();
2080
2092
  }
2081
- this.searchInput.focus();
2082
2093
  this._bindDropdownListeners();
2083
2094
  this.dropdownArrow.classList.add("iti__arrow--up");
2084
2095
  this._trigger("open:countrydropdown");
2085
2096
  }
2086
- //* Decide if should position dropdown above or below input (depends on position within viewport, and scroll).
2097
+ //* Set the dropdown position
2087
2098
  _setDropdownPosition() {
2088
2099
  if (this.options.dropdownContainer) {
2089
2100
  this.options.dropdownContainer.appendChild(this.dropdown);
@@ -2129,6 +2140,8 @@ var factoryOutput = (() => {
2129
2140
  "click",
2130
2141
  this._handleClickOffToClose
2131
2142
  );
2143
+ let query = "";
2144
+ let queryTimer = null;
2132
2145
  this._handleKeydownOnDropdown = (e) => {
2133
2146
  if (["ArrowUp", "ArrowDown", "Enter", "Escape"].includes(e.key)) {
2134
2147
  e.preventDefault();
@@ -2141,29 +2154,56 @@ var factoryOutput = (() => {
2141
2154
  this._closeDropdown();
2142
2155
  }
2143
2156
  }
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);
2157
+ if (!this.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
2158
+ e.stopPropagation();
2159
+ if (queryTimer) {
2160
+ clearTimeout(queryTimer);
2161
+ }
2162
+ query += e.key.toLowerCase();
2163
+ this._searchForCountry(query);
2164
+ queryTimer = setTimeout(() => {
2165
+ query = "";
2166
+ }, 1e3);
2152
2167
  }
2153
2168
  };
2154
- let keyupTimer = null;
2155
- this._handleSearchChange = () => {
2156
- if (keyupTimer) {
2157
- clearTimeout(keyupTimer);
2169
+ document.addEventListener("keydown", this._handleKeydownOnDropdown);
2170
+ if (this.options.countrySearch) {
2171
+ const doFilter = () => {
2172
+ const inputQuery = this.searchInput.value.trim();
2173
+ if (inputQuery) {
2174
+ this._filterCountries(inputQuery);
2175
+ } else {
2176
+ this._filterCountries("", true);
2177
+ }
2178
+ };
2179
+ let keyupTimer = null;
2180
+ this._handleSearchChange = () => {
2181
+ if (keyupTimer) {
2182
+ clearTimeout(keyupTimer);
2183
+ }
2184
+ keyupTimer = setTimeout(() => {
2185
+ doFilter();
2186
+ keyupTimer = null;
2187
+ }, 100);
2188
+ };
2189
+ this.searchInput.addEventListener("input", this._handleSearchChange);
2190
+ this.searchInput.addEventListener("click", (e) => e.stopPropagation());
2191
+ }
2192
+ }
2193
+ //* Hidden search (countrySearch disabled): Find the first list item whose name starts with the query string.
2194
+ _searchForCountry(query) {
2195
+ for (let i = 0; i < this.countries.length; i++) {
2196
+ const c = this.countries[i];
2197
+ const startsWith = c.name.substr(0, query.length).toLowerCase() === query;
2198
+ if (startsWith) {
2199
+ const listItem = c.nodeById[this.id];
2200
+ this._highlightListItem(listItem, false);
2201
+ this._scrollTo(listItem);
2202
+ break;
2158
2203
  }
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());
2204
+ }
2166
2205
  }
2206
+ //* Country search enabled: Filter the countries according to the search query.
2167
2207
  _filterCountries(query, isReset = false) {
2168
2208
  let noCountriesAddedYet = true;
2169
2209
  this.countryList.innerHTML = "";
@@ -2291,7 +2331,9 @@ var factoryOutput = (() => {
2291
2331
  this.highlightedItem.setAttribute("aria-selected", "true");
2292
2332
  const activeDescendant = this.highlightedItem.getAttribute("id") || "";
2293
2333
  this.selectedCountry.setAttribute("aria-activedescendant", activeDescendant);
2294
- this.searchInput.setAttribute("aria-activedescendant", activeDescendant);
2334
+ if (this.options.countrySearch) {
2335
+ this.searchInput.setAttribute("aria-activedescendant", activeDescendant);
2336
+ }
2295
2337
  }
2296
2338
  if (shouldFocus) {
2297
2339
  this.highlightedItem.focus();
@@ -2448,10 +2490,14 @@ var factoryOutput = (() => {
2448
2490
  if (this.highlightedItem) {
2449
2491
  this.highlightedItem.setAttribute("aria-selected", "false");
2450
2492
  }
2451
- this.searchInput.removeAttribute("aria-activedescendant");
2493
+ if (this.options.countrySearch) {
2494
+ this.searchInput.removeAttribute("aria-activedescendant");
2495
+ }
2452
2496
  this.dropdownArrow.classList.remove("iti__arrow--up");
2453
2497
  document.removeEventListener("keydown", this._handleKeydownOnDropdown);
2454
- this.searchInput.removeEventListener("input", this._handleSearchChange);
2498
+ if (this.options.countrySearch) {
2499
+ this.searchInput.removeEventListener("input", this._handleSearchChange);
2500
+ }
2455
2501
  document.documentElement.removeEventListener(
2456
2502
  "click",
2457
2503
  this._handleClickOffToClose
@@ -2762,7 +2808,7 @@ var factoryOutput = (() => {
2762
2808
  //* A map from instance ID to instance object.
2763
2809
  instances: {},
2764
2810
  loadUtils,
2765
- version: "23.3.2"
2811
+ version: "23.4.0"
2766
2812
  }
2767
2813
  );
2768
2814
  var intl_tel_input_default = intlTelInput;