Skip to content

Commit 1139404

Browse files
committed
Fix updating selected flag when typing area codes
1 parent d3bde3d commit 1139404

20 files changed

+1053
-684
lines changed

build/js/data.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,7 @@ var factoryOutput = (() => {
13541354
dialCode: c[1],
13551355
priority: c[2] || 0,
13561356
areaCodes: c[3] || null,
1357+
partialAreaCodes: null,
13571358
nodeById: {}
13581359
};
13591360
}

build/js/data.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/js/intlTelInput.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare module "intl-tel-input/data" {
55
dialCode: string;
66
priority: number;
77
areaCodes: string[] | null;
8+
partialAreaCodes: string[] | null;
89
nodeById: object;
910
};
1011
const allCountries: Country[];
@@ -324,10 +325,12 @@ declare module "intl-tel-input" {
324325
numberType: object;
325326
};
326327
type NumberType = "FIXED_LINE_OR_MOBILE" | "FIXED_LINE" | "MOBILE" | "PAGER" | "PERSONAL_NUMBER" | "PREMIUM_RATE" | "SHARED_COST" | "TOLL_FREE" | "UAN" | "UNKNOWN" | "VOICEMAIL" | "VOIP";
327-
type SelectedCountryData = Country | {
328+
type SelectedCountryData = {
328329
name?: string;
329330
iso2?: string;
330331
dialCode?: string;
332+
areaCodes?: string[];
333+
partialAreaCodes?: string[];
331334
};
332335
interface AllOptions {
333336
allowDropdown: boolean;
@@ -438,6 +441,7 @@ declare module "intl-tel-input" {
438441
private _handleEnterKey;
439442
private _updateValFromNumber;
440443
private _updateCountryFromNumber;
444+
private _isAreaCodeMatch;
441445
private _getCountryFromNumber;
442446
private _highlightListItem;
443447
private _getCountryData;

build/js/intlTelInput.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,7 @@ var factoryOutput = (() => {
13571357
dialCode: c[1],
13581358
priority: c[2] || 0,
13591359
areaCodes: c[3] || null,
1360+
partialAreaCodes: null,
13601361
nodeById: {}
13611362
};
13621363
}
@@ -1881,7 +1882,7 @@ var factoryOutput = (() => {
18811882
}
18821883
}
18831884
}
1884-
//* Generate this.dialCodes and this.dialCodeToIso2Map.
1885+
//* Generate this.dialCodes and this.dialCodeToIso2Map and country.partialAreaCodes.
18851886
_processDialCodes() {
18861887
this.dialCodes = {};
18871888
this.dialCodeMaxLen = 0;
@@ -1900,9 +1901,16 @@ var factoryOutput = (() => {
19001901
for (let j = 0; j < c.areaCodes.length; j++) {
19011902
const areaCode = c.areaCodes[j];
19021903
for (let k = 1; k < areaCode.length; k++) {
1903-
const partialDialCode = c.dialCode + areaCode.substr(0, k);
1904+
const partialAreaCode = areaCode.substr(0, k);
1905+
const partialDialCode = c.dialCode + partialAreaCode;
19041906
this._addToDialCodeMap(rootIso2Code, partialDialCode);
19051907
this._addToDialCodeMap(c.iso2, partialDialCode);
1908+
if (!c.partialAreaCodes) {
1909+
c.partialAreaCodes = [];
1910+
}
1911+
if (!c.partialAreaCodes.includes(partialAreaCode)) {
1912+
c.partialAreaCodes.push(partialAreaCode);
1913+
}
19061914
}
19071915
this._addToDialCodeMap(c.iso2, c.dialCode + areaCode);
19081916
}
@@ -2560,6 +2568,19 @@ var factoryOutput = (() => {
25602568
}
25612569
return false;
25622570
}
2571+
//* Check if the given number matches an area code from the selected country.
2572+
_isAreaCodeMatch(numeric, dialCodeNumerics) {
2573+
const { areaCodes, partialAreaCodes, dialCode } = this.selectedCountryData;
2574+
const typedNumber = numeric.substring(dialCode.length);
2575+
const typedAreaCode = dialCodeNumerics.substring(dialCode.length);
2576+
if (areaCodes.includes(typedAreaCode)) {
2577+
return true;
2578+
}
2579+
if (partialAreaCodes.includes(typedNumber)) {
2580+
return true;
2581+
}
2582+
return false;
2583+
}
25632584
_getCountryFromNumber(fullNumber) {
25642585
const plusIndex = fullNumber.indexOf("+");
25652586
let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
@@ -2577,10 +2598,19 @@ var factoryOutput = (() => {
25772598
const dialCode = this._getDialCode(number, true);
25782599
const numeric = getNumeric(number);
25792600
if (dialCode) {
2580-
const iso2Codes = this.dialCodeToIso2Map[getNumeric(dialCode)];
2581-
const alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
2601+
const dialCodeNumerics = getNumeric(dialCode);
2602+
const iso2Codes = this.dialCodeToIso2Map[dialCodeNumerics];
2603+
const alreadySelected = iso2Codes.includes(this.selectedCountryData.iso2);
2604+
let areaCodeMatch = false;
2605+
if (alreadySelected) {
2606+
if (this.selectedCountryData.areaCodes && numeric.length > selectedDialCode.length) {
2607+
areaCodeMatch = this._isAreaCodeMatch(numeric, dialCodeNumerics);
2608+
} else {
2609+
areaCodeMatch = true;
2610+
}
2611+
}
25822612
const isRegionlessNanpNumber = selectedDialCode === "1" && isRegionlessNanp(numeric);
2583-
if (!isRegionlessNanpNumber && !alreadySelected) {
2613+
if (!isRegionlessNanpNumber && (!alreadySelected || !areaCodeMatch)) {
25842614
for (let j = 0; j < iso2Codes.length; j++) {
25852615
if (iso2Codes[j]) {
25862616
return iso2Codes[j];

build/js/intlTelInput.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/js/intlTelInputWithUtils.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ var factoryOutput = (() => {
13561356
dialCode: c[1],
13571357
priority: c[2] || 0,
13581358
areaCodes: c[3] || null,
1359+
partialAreaCodes: null,
13591360
nodeById: {}
13601361
};
13611362
}
@@ -1880,7 +1881,7 @@ var factoryOutput = (() => {
18801881
}
18811882
}
18821883
}
1883-
//* Generate this.dialCodes and this.dialCodeToIso2Map.
1884+
//* Generate this.dialCodes and this.dialCodeToIso2Map and country.partialAreaCodes.
18841885
_processDialCodes() {
18851886
this.dialCodes = {};
18861887
this.dialCodeMaxLen = 0;
@@ -1899,9 +1900,16 @@ var factoryOutput = (() => {
18991900
for (let j = 0; j < c.areaCodes.length; j++) {
19001901
const areaCode = c.areaCodes[j];
19011902
for (let k = 1; k < areaCode.length; k++) {
1902-
const partialDialCode = c.dialCode + areaCode.substr(0, k);
1903+
const partialAreaCode = areaCode.substr(0, k);
1904+
const partialDialCode = c.dialCode + partialAreaCode;
19031905
this._addToDialCodeMap(rootIso2Code, partialDialCode);
19041906
this._addToDialCodeMap(c.iso2, partialDialCode);
1907+
if (!c.partialAreaCodes) {
1908+
c.partialAreaCodes = [];
1909+
}
1910+
if (!c.partialAreaCodes.includes(partialAreaCode)) {
1911+
c.partialAreaCodes.push(partialAreaCode);
1912+
}
19051913
}
19061914
this._addToDialCodeMap(c.iso2, c.dialCode + areaCode);
19071915
}
@@ -2559,6 +2567,19 @@ var factoryOutput = (() => {
25592567
}
25602568
return false;
25612569
}
2570+
//* Check if the given number matches an area code from the selected country.
2571+
_isAreaCodeMatch(numeric, dialCodeNumerics) {
2572+
const { areaCodes, partialAreaCodes, dialCode } = this.selectedCountryData;
2573+
const typedNumber = numeric.substring(dialCode.length);
2574+
const typedAreaCode = dialCodeNumerics.substring(dialCode.length);
2575+
if (areaCodes.includes(typedAreaCode)) {
2576+
return true;
2577+
}
2578+
if (partialAreaCodes.includes(typedNumber)) {
2579+
return true;
2580+
}
2581+
return false;
2582+
}
25622583
_getCountryFromNumber(fullNumber) {
25632584
const plusIndex = fullNumber.indexOf("+");
25642585
let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
@@ -2576,10 +2597,19 @@ var factoryOutput = (() => {
25762597
const dialCode = this._getDialCode(number, true);
25772598
const numeric = getNumeric(number);
25782599
if (dialCode) {
2579-
const iso2Codes = this.dialCodeToIso2Map[getNumeric(dialCode)];
2580-
const alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
2600+
const dialCodeNumerics = getNumeric(dialCode);
2601+
const iso2Codes = this.dialCodeToIso2Map[dialCodeNumerics];
2602+
const alreadySelected = iso2Codes.includes(this.selectedCountryData.iso2);
2603+
let areaCodeMatch = false;
2604+
if (alreadySelected) {
2605+
if (this.selectedCountryData.areaCodes && numeric.length > selectedDialCode.length) {
2606+
areaCodeMatch = this._isAreaCodeMatch(numeric, dialCodeNumerics);
2607+
} else {
2608+
areaCodeMatch = true;
2609+
}
2610+
}
25812611
const isRegionlessNanpNumber = selectedDialCode === "1" && isRegionlessNanp(numeric);
2582-
if (!isRegionlessNanpNumber && !alreadySelected) {
2612+
if (!isRegionlessNanpNumber && (!alreadySelected || !areaCodeMatch)) {
25832613
for (let j = 0; j < iso2Codes.length; j++) {
25842614
if (iso2Codes[j]) {
25852615
return iso2Codes[j];

build/js/intlTelInputWithUtils.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)