-
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.
feat(usephoneinput): usePhoneInput supports national format (#77)
- Loading branch information
1 parent
df77bac
commit 5642c4e
Showing
13 changed files
with
236 additions
and
22 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
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
4 changes: 4 additions & 0 deletions
4
packages/phone-input/src/helpers/formatInternational/formatInternational.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
19 changes: 19 additions & 0 deletions
19
packages/phone-input/src/helpers/formatNational/formatNational.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,19 @@ | ||
import { formatNational } from './formatNational' | ||
|
||
describe('formatNational', () => { | ||
it('Should leave as-is when the value is already in national format.', () => { | ||
expect(formatNational('+123456')).toBe('123456') | ||
}) | ||
|
||
it('Should auto remove + sign.', () => { | ||
expect(formatNational('123456')).toBe('123456') | ||
}) | ||
|
||
it('Should auto remove + sign and remove first 0 digit.', () => { | ||
expect(formatNational('0123456')).toBe('123456') | ||
}) | ||
|
||
it('Should return empty string when input value is not valid', () => { | ||
expect(formatNational('')).toBe('') | ||
}) | ||
}) |
12 changes: 12 additions & 0 deletions
12
packages/phone-input/src/helpers/formatNational/formatNational.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,12 @@ | ||
import { parseIncompletePhoneNumber } from 'libphonenumber-js' | ||
|
||
export const formatNational = (phoneValue: string) => { | ||
if (!phoneValue) return '' | ||
if (phoneValue.startsWith('+')) | ||
return parseIncompletePhoneNumber(phoneValue.replace(/\+/g, '')) | ||
|
||
if (phoneValue.startsWith('0')) | ||
return parseIncompletePhoneNumber(phoneValue.slice(1)) | ||
|
||
return 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.