fix(phone-parser): use default country code

This commit is contained in:
Corentin Thomasset 2023-05-07 13:04:55 +02:00
parent 83a7b3bae9
commit a43c546e34
No known key found for this signature in database
GPG key ID: DBD997E935996158
2 changed files with 4 additions and 4 deletions

View file

@ -1,4 +1,4 @@
import type { NumberType } from 'libphonenumber-js/types'; import type { CountryCode, NumberType } from 'libphonenumber-js/types';
import lookup from 'country-code-lookup'; import lookup from 'country-code-lookup';
export { formatTypeToHumanReadable, getFullCountryName, getDefaultCountryCode }; export { formatTypeToHumanReadable, getFullCountryName, getDefaultCountryCode };
@ -32,10 +32,10 @@ function getFullCountryName(countryCode: string | undefined) {
function getDefaultCountryCode({ function getDefaultCountryCode({
locale = window.navigator.language, locale = window.navigator.language,
defaultCode = 'FR', defaultCode = 'FR',
}: { locale?: string; defaultCode?: string } = {}): string { }: { locale?: string; defaultCode?: CountryCode } = {}): CountryCode {
const countryCode = locale.split('-')[1]?.toUpperCase(); const countryCode = locale.split('-')[1]?.toUpperCase();
if (!countryCode) return defaultCode; if (!countryCode) return defaultCode;
return lookup.byIso(countryCode)?.iso2 ?? defaultCode; return (lookup.byIso(countryCode)?.iso2 ?? defaultCode) as CountryCode;
} }

View file

@ -50,7 +50,7 @@ const validation = useValidation({
const parsedDetails = computed(() => { const parsedDetails = computed(() => {
if (!validation.isValid) return undefined; if (!validation.isValid) return undefined;
const parsed = withDefaultOnError(() => parsePhoneNumber(rawPhone.value, 'FR'), undefined); const parsed = withDefaultOnError(() => parsePhoneNumber(rawPhone.value, defaultCountryCode.value), undefined);
if (!parsed) return undefined; if (!parsed) return undefined;