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:
parent
af5e3b416e
commit
c6384bed84
4 changed files with 33 additions and 11 deletions
|
@ -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>
|
||||||
|
|
|
@ -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));
|
||||||
|
|
|
@ -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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue