Skip to content

Commit 4fb9a74

Browse files
Fix the area code parsing issue (GH-60)
2 parents 37c6944 + 46fb877 commit 4fb9a74

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ range of opportunities for handling the data in your desired way.
5050
```javascript
5151
{
5252
countryCode: 1,
53-
areaCode: 702,
53+
areaCode: "702",
5454
phoneNumber: "1234567",
5555
isoCode: "us",
5656
valid: function valid(strict)

package.json

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

src/index.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const parsePhoneNumber = (formattedNumber: string, countriesList: typeof countri
6161

6262
/** Converts the parsed values of the country and area codes to integers if values present */
6363
const countryCode = countryCodeMatch.length > 0 ? parseInt(countryCodeMatch[0]) : null;
64-
const areaCode = areaCodeMatch.length > 1 ? parseInt(areaCodeMatch[1]) : null;
64+
const areaCode = areaCodeMatch.length > 1 ? areaCodeMatch[1] : null;
6565

6666
/** Parses the phone number by removing the country and area codes from the formatted value */
6767
const phoneNumberPattern = new RegExp(`^${countryCode}${(areaCode || "")}(\\d+)`);
@@ -146,9 +146,10 @@ const PhoneInput = ({
146146
}, [pattern])
147147

148148
const selectValue = useMemo(() => {
149-
const metadata = getMetadata(getRawValue(value), countriesList);
150-
return (metadata || countriesList[0])?.[0] + (metadata || countriesList[0])?.[2];
151-
}, [countriesList, value])
149+
let metadata = getMetadata(getRawValue(value), countriesList);
150+
metadata = metadata || countries.find(([iso]) => iso === countryCode);
151+
return ({...metadata})?.[0] + ({...metadata})?.[2];
152+
}, [countriesList, countryCode, value])
152153

153154
const setFieldValue = useCallback((value: PhoneNumber) => {
154155
if (formInstance) {
@@ -181,9 +182,9 @@ const PhoneInput = ({
181182

182183
const onChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
183184
const formattedNumber = displayFormat(clean(event.target.value).join(""));
184-
const phoneMetadata = parsePhoneNumber(formattedNumber, countriesList, countryCode);
185+
const phoneMetadata = parsePhoneNumber(formattedNumber, countriesList);
185186
handleChange({...phoneMetadata, valid: (strict: boolean) => checkValidity(phoneMetadata, strict)}, event);
186-
}, [clean, countriesList, countryCode, handleChange])
187+
}, [clean, countriesList, handleChange])
187188

188189
const onInput = useCallback((event: ChangeEvent<HTMLInputElement>) => {
189190
handleInput(event);

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {InputProps} from "antd/lib/input";
33

44
export interface PhoneNumber {
55
countryCode?: number | null;
6-
areaCode?: number | null;
6+
areaCode?: string | null;
77
phoneNumber?: string | null;
88
isoCode?: string;
99

tests/antd.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ describe("Checking the basic rendering and functionality", () => {
3737
render(<PhoneInput
3838
onMount={(value: any) => {
3939
assert(value.countryCode === 1);
40-
assert(value.areaCode === 702);
40+
assert(value.areaCode === "702");
4141
assert(value.phoneNumber === "1234567");
4242
assert(value.isoCode === "us");
4343
assert(value.valid());
4444
}}
45-
value={{countryCode: 1, areaCode: 702, phoneNumber: "1234567"}}
45+
value={{countryCode: 1, areaCode: "702", phoneNumber: "1234567"}}
4646
/>);
4747
assert(screen.getByDisplayValue("+1 (702) 123 4567"));
4848
})
@@ -71,7 +71,7 @@ describe("Checking the basic rendering and functionality", () => {
7171
it("Using the input with FormItem", async () => {
7272
render(<Form onFinish={({phone}: any) => {
7373
assert(phone.countryCode === 1);
74-
assert(phone.areaCode === 907);
74+
assert(phone.areaCode === "907");
7575
assert(phone.phoneNumber === "1234567");
7676
assert(phone.isoCode === "us");
7777
}}>
@@ -87,7 +87,7 @@ describe("Checking the basic rendering and functionality", () => {
8787
})
8888

8989
it("Checking input validation with FormItem", async () => {
90-
render(<Form initialValues={{phone: {countryCode: 1, areaCode: 702, phoneNumber: "1234567"}}}>
90+
render(<Form initialValues={{phone: {countryCode: 1, areaCode: "702", phoneNumber: "1234567"}}}>
9191
<FormItem name="phone" rules={[{
9292
validator: (_: any, {valid}: any) => {
9393
assert(valid());
@@ -102,7 +102,7 @@ describe("Checking the basic rendering and functionality", () => {
102102
})
103103

104104
it("Checking form with initial value", async () => {
105-
render(<Form initialValues={{phone: {countryCode: 1, areaCode: 702}}}>
105+
render(<Form initialValues={{phone: {countryCode: 1, areaCode: "702"}}}>
106106
<FormItem name="phone">
107107
<PhoneInput/>
108108
</FormItem>
@@ -114,7 +114,7 @@ describe("Checking the basic rendering and functionality", () => {
114114
})
115115

116116
it("Checking validation with casual form actions", async () => {
117-
render(<Form data-testid="form" initialValues={{phone: {countryCode: 1, areaCode: 702, phoneNumber: ""}}}>
117+
render(<Form data-testid="form" initialValues={{phone: {countryCode: 1, areaCode: "702", phoneNumber: ""}}}>
118118
<FormItem name="phone" rules={[{
119119
validator: (_: any, {valid}: any) => {
120120
if (valid()) return Promise.resolve();

0 commit comments

Comments
 (0)