overwrite country code if possible

This commit is contained in:
Samuel Newman
2025-12-11 20:32:22 +02:00
parent f59b6a25cd
commit 4344119d80
2 changed files with 32 additions and 11 deletions
+12 -8
View File
@@ -19,6 +19,7 @@ export function processPhoneNumber(
| {
valid: true
formatted: string
countryCode: CountryCode
}
| {
valid: false
@@ -42,15 +43,18 @@ export function processPhoneNumber(
reason: t`Number should be a mobile number`,
}
}
if (phoneNumber.country !== country) {
return {
valid: false,
reason: t`Country code does not match`,
let countryCode = country
if (phoneNumber.country && phoneNumber.country !== country) {
if (phoneNumber.country === 'AC' || phoneNumber.country === 'TA') {
countryCode = 'SH'
} else {
countryCode = phoneNumber.country
}
}
return {
valid: true,
formatted: formatInternationalWithoutCountryCode(phoneNumber),
formatted: formatE164lWithoutCountryCode(phoneNumber),
countryCode,
}
} catch (error) {
if (error instanceof ParseError) {
@@ -65,8 +69,8 @@ export function processPhoneNumber(
* Format a phone number as the international format with the prefix
* removed.
*/
function formatInternationalWithoutCountryCode(phoneNumber: PhoneNumber) {
const intl = phoneNumber.formatInternational()
function formatE164lWithoutCountryCode(phoneNumber: PhoneNumber) {
const intl = phoneNumber.format('E.164')
const prefix = '+' + phoneNumber.countryCallingCode
return intl.replace(prefix, '').trim()
}
@@ -113,7 +117,7 @@ export function getCountryCodeFromPastedNumber(
if (countryCode && countryCode !== 'AC' && countryCode !== 'TA') {
return {
countryCode,
rest: formatInternationalWithoutCountryCode(phoneNumber),
rest: formatE164lWithoutCountryCode(phoneNumber),
}
} else {
return undefined
+20 -3
View File
@@ -13,7 +13,14 @@ import {
import {cleanError, isNetworkError} from '#/lib/strings/errors'
import {logger} from '#/logger'
import {useAgent} from '#/state/session'
import {android, atoms as a, tokens, useGutters, useTheme} from '#/alf'
import {
android,
atoms as a,
platform,
tokens,
useGutters,
useTheme,
} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import * as TextField from '#/components/forms/TextField'
import {InternationalPhoneCodeSelect} from '#/components/InternationalPhoneCodeSelect'
@@ -102,7 +109,14 @@ export function PhoneInput({
const result = processPhoneNumber(phoneNumber, countryCode)
if (result.valid) {
setPhoneNumber(result.formatted)
submit({phoneCountryCode: countryCode, phoneNumber: result.formatted})
setCountryCode(result.countryCode)
if (!isFindContactsFeatureEnabled(result.countryCode)) return
submit({
phoneCountryCode: result.countryCode,
phoneNumber: result.formatted,
})
} else {
setFormatError(result.reason ?? _(msg`Invalid phone number`))
}
@@ -182,7 +196,10 @@ export function PhoneInput({
setPhoneNumber(text)
}}
placeholder={null}
keyboardType="number-pad" // we don't want people entering +() etc
keyboardType={platform({
ios: 'number-pad',
android: 'phone-pad',
})}
autoComplete="tel"
returnKeyType={android('next')}
onSubmitEditing={onSubmitNumber}