Skip to content

Commit c06748a

Browse files
Fix the mask selection issue (GH-37)
2 parents 3e4fa54 + 013753f commit c06748a

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.6",
2+
"version": "0.1.7",
33
"name": "antd-phone-input",
44
"description": "Advanced, highly customizable phone input component for Ant Design.",
55
"keywords": [

src/legacy/index.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ const parsePhoneNumber: ParsePhoneNumber = (value, data, formattedNumber) => {
2222
const isoCode = data?.countryCode;
2323
const countryCodePattern = /\+\d+/;
2424
const areaCodePattern = /\((\d+)\)/;
25+
const dialCodePattern = /^\+[\d\s]+\([\d\s]+\)/;
2526

2627
/** Parses the matching partials of the phone number by predefined regex patterns */
2728
const countryCodeMatch = formattedNumber ? (formattedNumber.match(countryCodePattern) || []) : [];
2829
const areaCodeMatch = formattedNumber ? (formattedNumber.match(areaCodePattern) || []) : [];
30+
const dialCodeMatch = formattedNumber ? (formattedNumber.match(dialCodePattern) || []) : [];
2931

3032
/** Converts the parsed values of the country and area codes to integers if values present */
3133
const countryCode = countryCodeMatch.length > 0 ? parseInt(countryCodeMatch[0]) : null;
3234
const areaCode = areaCodeMatch.length > 1 ? parseInt(areaCodeMatch[1]) : null;
3335

36+
/** Obtaining the dial code for comparing to the existing one - if the country mask contains an area code */
37+
const dialCode = dialCodeMatch.length > 0 ? dialCodeMatch[0].replaceAll(/[+\s()]/g, "") : null;
38+
const dialChanged = dialCode !== data?.dialCode;
39+
3440
/** Parses the phone number by removing the country and area codes from the formatted value */
3541
const phoneNumberPattern = new RegExp(`^${countryCode}${(areaCode || "")}(\\d+)`);
3642
const phoneNumberMatch = value ? (value.match(phoneNumberPattern) || []) : [];
@@ -43,7 +49,7 @@ const parsePhoneNumber: ParsePhoneNumber = (value, data, formattedNumber) => {
4349
rules.phoneNumber.includes((phoneNumber || "").toString().length),
4450
].every(Boolean);
4551

46-
return {countryCode, areaCode, phoneNumber, isoCode, valid};
52+
return {countryCode, areaCode, phoneNumber, isoCode, valid, dialChanged};
4753
}
4854

4955
const PhoneInput = ({
@@ -74,21 +80,22 @@ const PhoneInput = ({
7480
}, [inputClassProxy, size]);
7581

7682
const onChange: ReactPhoneOnChange = (value, data, event, formattedNumber) => {
77-
const metadata = parsePhoneNumber(value, data, formattedNumber);
83+
const {dialChanged, ...metadata} = parsePhoneNumber(value, data, formattedNumber);
7884
const code = metadata.isoCode as ISO2Code;
7985

8086
if (code !== currentCode) {
8187
/** Clears phone number when the country is selected manually */
82-
handleChange({...metadata, areaCode: null, phoneNumber: null}, event);
88+
metadata.areaCode = dialChanged ? null : metadata.areaCode;
89+
metadata.phoneNumber = null;
90+
metadata.valid = false;
8391
setCurrentCode(code);
84-
return;
8592
}
8693

8794
handleChange(metadata, event);
8895
}
8996

9097
const onMount: ReactPhoneOnMount = (rawValue, {countryCode, ...event}, formattedNumber) => {
91-
const metadata = parsePhoneNumber(rawValue, {countryCode}, formattedNumber);
98+
const {dialChanged, ...metadata} = parsePhoneNumber(rawValue, {countryCode}, formattedNumber);
9299
/** Initiates the current country code with the code of initial value */
93100
setCurrentCode(metadata.isoCode as ISO2Code);
94101
/** Initializes the existing value */

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {ChangeEvent, CSSProperties, FocusEvent, InputHTMLAttributes, KeyboardEve
22

33
export interface CountryData {
44
countryCode: string,
5+
dialCode?: string,
56
}
67

78
export interface PhoneNumber {
@@ -10,6 +11,7 @@ export interface PhoneNumber {
1011
phoneNumber?: string | null,
1112
isoCode?: string,
1213
valid?: boolean,
14+
dialChanged?: boolean,
1315
}
1416

1517
export interface AntInputProps {

0 commit comments

Comments
 (0)