Skip to content

Commit 47a6b96

Browse files
Fix the erasing issue of the initial value (GH-27)
2 parents 7c9001f + 4037825 commit 47a6b96

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
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.2",
2+
"version": "0.1.3",
33
"name": "antd-phone-input",
44
"description": "Advanced Phone Number Input for Ant Design",
55
"keywords": [

src/index.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ const parsePhoneNumber: ParsePhoneNumber = (value, data, formattedNumber) => {
2323
const countryCodePattern = /\+\d+/;
2424
const areaCodePattern = /\((\d+)\)/;
2525

26-
/** Parse the matching partials of the phone number by predefined regex patterns */
26+
/** Parses the matching partials of the phone number by predefined regex patterns */
2727
const countryCodeMatch = formattedNumber ? (formattedNumber.match(countryCodePattern) || []) : [];
2828
const areaCodeMatch = formattedNumber ? (formattedNumber.match(areaCodePattern) || []) : [];
2929

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

34-
/** Parse the phone number by removing the country and area codes from the formatted value */
34+
/** Parses the phone number by removing the country and area codes from the formatted value */
3535
const phoneNumberPattern = new RegExp(`^${countryCode}${(areaCode || "")}(\\d+)`);
3636
const phoneNumberMatch = value ? (value.match(phoneNumberPattern) || []) : [];
3737
const phoneNumber = phoneNumberMatch.length > 1 ? phoneNumberMatch[1] : null;
@@ -76,7 +76,7 @@ const PhoneInput = ({
7676
const code = metadata.isoCode as ISO2Code;
7777

7878
if (code !== currentCode) {
79-
/** Clear phone number when the country is selected manually */
79+
/** Clears phone number when the country is selected manually */
8080
handleChange({...metadata, areaCode: null, phoneNumber: null}, event);
8181
setCurrentCode(code);
8282
return;
@@ -87,6 +87,8 @@ const PhoneInput = ({
8787

8888
const onMount: ReactPhoneOnMount = (rawValue, {countryCode, ...event}, formattedNumber) => {
8989
const metadata = parsePhoneNumber(rawValue, {countryCode}, formattedNumber);
90+
/** Initiates the current country code with the code of initial value */
91+
setCurrentCode(metadata.isoCode as ISO2Code);
9092
/** Initializes the existing value */
9193
handleChange(metadata, event);
9294
handleMount(metadata);

tests/common.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,16 @@ describe("Checks the basic rendering and functionality", () => {
8484
</Form>);
8585
await userEvent.click(screen.getByTestId("button"));
8686
})
87+
88+
it("Checks form with initial value", async () => {
89+
render(<Form initialValues={{phone: {countryCode: 1, areaCode: 702}}}>
90+
<FormItem name="phone">
91+
<PhoneInput/>
92+
</FormItem>
93+
<Button data-testid="button" htmlType="submit">Submit</Button>
94+
</Form>);
95+
const input = screen.getByDisplayValue("+1 (702)");
96+
await userEvent.type(input, "1234567");
97+
assert(input.getAttribute("value") === "+1 (702) 123 4567");
98+
})
8799
})

0 commit comments

Comments
 (0)