-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from trinhthinh388/master
fix: usePhoneInput result not in national format (#84)
- Loading branch information
Showing
11 changed files
with
317 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/phone-input/src/helpers/formatWithFixedCountry/formatWithFixedCountry.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { formatWithFixedCountry } from './formatWithFixedCountry' | ||
|
||
describe('formatWithFixedCountry', () => { | ||
it('Should leave as-is when the value is already has valid country code.', () => { | ||
expect(formatWithFixedCountry('+123456', 'US')).toBe('+123456') | ||
}) | ||
|
||
it('Should format to the correct country code.', () => { | ||
expect(formatWithFixedCountry('+123456', 'VN')).toBe('+84123456') | ||
}) | ||
}) |
25 changes: 25 additions & 0 deletions
25
packages/phone-input/src/helpers/formatWithFixedCountry/formatWithFixedCountry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { | ||
CountryCode, | ||
getCountryCallingCode, | ||
parseIncompletePhoneNumber, | ||
} from 'libphonenumber-js' | ||
|
||
export const formatWithFixedCountry = ( | ||
phoneValue: string, | ||
country: CountryCode, | ||
) => { | ||
if (!phoneValue) return '' | ||
|
||
const prefix = `+${getCountryCallingCode(country)}` | ||
|
||
if (phoneValue.startsWith(prefix)) | ||
return parseIncompletePhoneNumber(phoneValue) | ||
|
||
if (phoneValue.startsWith('+')) | ||
return `${prefix}${parseIncompletePhoneNumber(phoneValue.replace(/\+/g, ''))}` | ||
|
||
if (phoneValue.startsWith('0')) | ||
return `${prefix}${parseIncompletePhoneNumber(phoneValue.slice(1))}` | ||
|
||
return `${prefix}${parseIncompletePhoneNumber(phoneValue)}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.