0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(ui): format phone number with country calling code (#1551)

format phone number with country calling code
This commit is contained in:
simeng-li 2022-07-15 14:08:59 +08:00 committed by GitHub
parent af5e3b416e
commit c6384bed84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 11 deletions

View file

@ -7,6 +7,7 @@ import { WebModal, MobileModal } from '@/components/ConfirmModal';
import useApi from '@/hooks/use-api'; import useApi from '@/hooks/use-api';
import usePlatform from '@/hooks/use-platform'; import usePlatform from '@/hooks/use-platform';
import { UserFlow } from '@/types'; import { UserFlow } from '@/types';
import { formatPhoneNumberWithCountryCallingCode } from '@/utils/country-code';
type Props = { type Props = {
className?: string; className?: string;
@ -56,7 +57,7 @@ const PasswordlessConfirmModal = ({ className, isOpen, type, method, value, onCl
: 'description.sign_in_id_does_not_exists', : 'description.sign_in_id_does_not_exists',
{ {
type: t(`description.${method === 'email' ? 'email' : 'phone_number'}`), type: t(`description.${method === 'email' ? 'email' : 'phone_number'}`),
value: `${method === 'sms' ? '+' : ''}${value}`, value: method === 'sms' ? formatPhoneNumberWithCountryCallingCode(value) : value,
} }
)} )}
</ConfirmModal> </ConfirmModal>

View file

@ -6,13 +6,16 @@
import { import {
parsePhoneNumberWithError, parsePhoneNumberWithError,
CountryCallingCode, CountryCallingCode,
E164Number,
ParseError, ParseError,
CountryCode, CountryCode,
} from 'libphonenumber-js/mobile'; } from 'libphonenumber-js/mobile';
import { useState } from 'react'; import { useState } from 'react';
import { getDefaultCountryCallingCode, getCountryList } from '@/utils/country-code'; import {
getDefaultCountryCallingCode,
getCountryList,
parseE164Number,
} from '@/utils/country-code';
export type { CountryCallingCode } from 'libphonenumber-js/mobile'; export type { CountryCallingCode } from 'libphonenumber-js/mobile';
@ -26,14 +29,6 @@ type PhoneNumberData = {
nationalNumber: string; nationalNumber: string;
}; };
const parseE164Number = (value: string): E164Number | '' => {
if (!value || value.startsWith('+')) {
return value;
}
return `+${value}`;
};
const isValidPhoneNumber = (value: string): boolean => { const isValidPhoneNumber = (value: string): boolean => {
try { try {
const phoneNumber = parsePhoneNumberWithError(parseE164Number(value)); const phoneNumber = parsePhoneNumberWithError(parseE164Number(value));

View file

@ -5,6 +5,7 @@ import {
getDefaultCountryCode, getDefaultCountryCode,
getDefaultCountryCallingCode, getDefaultCountryCallingCode,
getCountryList, getCountryList,
formatPhoneNumberWithCountryCallingCode,
} from './country-code'; } from './country-code';
describe('country-code', () => { describe('country-code', () => {
@ -76,4 +77,9 @@ describe('country-code', () => {
); );
expect(countryList[0]?.countryCallingCode).toEqual('86'); expect(countryList[0]?.countryCallingCode).toEqual('86');
}); });
it('formatPhoneNumberWithCountryCallingCode', async () => {
expect(formatPhoneNumberWithCountryCallingCode('18888888888')).toBe('+1 8888888888');
expect(formatPhoneNumberWithCountryCallingCode('8618888888888')).toBe('+86 18888888888');
});
}); });

View file

@ -3,7 +3,9 @@ import {
getCountries, getCountries,
CountryCode, CountryCode,
CountryCallingCode, CountryCallingCode,
E164Number,
getCountryCallingCode, getCountryCallingCode,
parsePhoneNumberWithError,
} from 'libphonenumber-js/mobile'; } from 'libphonenumber-js/mobile';
export const fallbackCountryCode = 'US'; export const fallbackCountryCode = 'US';
@ -78,3 +80,21 @@ export const getCountryList = (): CountryMetaData[] => {
...countryList, ...countryList,
]; ];
}; };
export const parseE164Number = (value: string): E164Number | '' => {
if (!value || value.startsWith('+')) {
return value;
}
return `+${value}`;
};
export const formatPhoneNumberWithCountryCallingCode = (number: string) => {
try {
const phoneNumber = parsePhoneNumberWithError(parseE164Number(number));
return `+${phoneNumber.countryCallingCode} ${phoneNumber.nationalNumber}`;
} catch {
return number;
}
};