Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/mobile/src/invite/JoinCelo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class JoinCelo extends React.Component<Props, State> {
setIsValidNumber={this.setIsValidNumber}
onInputChange={this.onChangePhoneInput}
inputCountryPlaceholder={t('chooseCountry')}
inputPhonePlaceholder={t('phoneNumber')}
initialInputPhonePlaceholder={t('phoneNumber')}
callingCode={true}
lng={language}
defaultCountryCode={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ exports[`JoinCeloScreen renders correctly 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
placeholder="phoneNumber"
placeholder="(000) 000-0000"
placeholderTextColor="#D1D5D8"
rejectResponderTermination={true}
style={
Expand Down Expand Up @@ -881,7 +881,7 @@ exports[`JoinCeloScreen renders with an error 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
placeholder="phoneNumber"
placeholder="(000) 000-0000"
placeholderTextColor="#D1D5D8"
rejectResponderTermination={true}
style={
Expand Down
7 changes: 5 additions & 2 deletions packages/react-components/components/PhoneNumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Props {
onEndEditingPhoneNumber?: () => void
onEndEditingCountryCode?: () => void
inputCountryPlaceholder?: string
inputPhonePlaceholder?: string
initialInputPhonePlaceholder?: string
lng?: string
callingCode?: boolean
defaultCountryCode?: string
Expand All @@ -38,6 +38,7 @@ interface State {
regionCode: string
phoneNumber: string
countries: Countries
inputPhonePlaceholder?: string
country?: string
}

Expand All @@ -49,6 +50,7 @@ export default class PhoneNumberInput extends React.Component<Props, State> {
phoneNumber: '',
// country data should be fetched before mounting to prevent a second render
countries: new Countries(this.props.lng),
inputPhonePlaceholder: this.props.initialInputPhonePlaceholder,
}

componentDidMount() {
Expand Down Expand Up @@ -138,6 +140,7 @@ export default class PhoneNumberInput extends React.Component<Props, State> {
countryQuery,
countryCallingCode,
regionCode,
inputPhonePlaceholder: country.countryPhonePlaceholder.national,
},
// Reparse phone number in case user entered that first
() => this.onChangePhoneNumber(this.state.phoneNumber)
Expand Down Expand Up @@ -266,7 +269,7 @@ export default class PhoneNumberInput extends React.Component<Props, State> {
onEndEditing={this.props.onEndEditingPhoneNumber}
value={this.state.phoneNumber}
underlineColorAndroid="transparent"
placeholder={this.props.inputPhonePlaceholder}
placeholder={this.state.inputPhonePlaceholder}
keyboardType="phone-pad"
testID="PhoneNumberField"
validator={ValidatorKind.Phone}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ exports[`when defaultCountry is truthy does not render an AutoComplete 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
placeholder="(000) 000-0000"
placeholderTextColor="#D1D5D8"
rejectResponderTermination={true}
style={
Expand Down
11 changes: 11 additions & 0 deletions packages/utils/src/countries.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getExampleNumber } from './phoneNumbers'
const esData = require('@umpirsky/country-list/data/es/country.json')
import countryData from 'country-data'
import { notEmpty } from './collections'
Expand All @@ -9,6 +10,10 @@ interface CountryNames {
export interface LocalizedCountry extends countryData.Country {
displayName: string
names: CountryNames
countryPhonePlaceholder: {
national?: string | undefined
international?: string | undefined
}
}

const EMPTY_COUNTRY: LocalizedCountry = {
Expand All @@ -23,6 +28,7 @@ const EMPTY_COUNTRY: LocalizedCountry = {
name: '',
names: {},
status: '',
countryPhonePlaceholder: { national: '', international: '' },
}

const removeDiacritics = (word: string) =>
Expand Down Expand Up @@ -140,6 +146,11 @@ export class Countries {
const localizedCountry = {
names,
displayName: names[this.language],
countryPhonePlaceholder: {
national: getExampleNumber(country.countryCallingCodes[0]),
// Not needed right now
// international: getExampleNumber(country.countryCallingCodes[0], true, true),
},
...country,
}

Expand Down
21 changes: 21 additions & 0 deletions packages/utils/src/phoneNumbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
getCountryCode,
getDisplayPhoneNumber,
getE164Number,
getExampleNumber,
getRegionCode,
getRegionCodeFromCountryCode,
isE164Number,
Expand Down Expand Up @@ -259,4 +260,24 @@ describe('Phone number formatting and utilities', () => {
expect(isE164Number(TEST_PHONE_NUMBERS.VALID_US_4)).toBe(false)
})
})

describe('Example phones', () => {
it('gets example by country showing zeros', () => {
expect(getExampleNumber(COUNTRY_CODES.AR)).toBe('000 0000-0000')
expect(getExampleNumber(COUNTRY_CODES.DE)).toBe('000 000000')
expect(getExampleNumber(COUNTRY_CODES.US)).toBe('(000) 000-0000')
})

it('gets example by country', () => {
expect(getExampleNumber(COUNTRY_CODES.AR, false)).toBe('011 2345-6789')
expect(getExampleNumber(COUNTRY_CODES.DE, false)).toBe('030 123456')
expect(getExampleNumber(COUNTRY_CODES.US, false)).toBe('(201) 555-0123')
})

it('gets example by country showing zeros in international way', () => {
expect(getExampleNumber(COUNTRY_CODES.AR, true, true)).toBe('+54 00 0000-0000')
expect(getExampleNumber(COUNTRY_CODES.DE, true, true)).toBe('+49 00 000000')
expect(getExampleNumber(COUNTRY_CODES.US, true, true)).toBe('+1 000-000-0000')
})
})
})
28 changes: 28 additions & 0 deletions packages/utils/src/phoneNumbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,34 @@ export function anonymizedPhone(phoneNumber: string) {
return phoneNumber.slice(0, -4) + 'XXXX'
}

export function getExampleNumber(
regionCode: string,
useOnlyZeroes: boolean = true,
isInternational: boolean = false
) {
const examplePhone = phoneUtil.getExampleNumber(getRegionCodeFromCountryCode(
regionCode
) as string)

if (!examplePhone) {
return
}

const formatedExample = phoneUtil.format(
examplePhone,
isInternational ? PhoneNumberFormat.INTERNATIONAL : PhoneNumberFormat.NATIONAL
)

if (useOnlyZeroes) {
if (isInternational) {
return formatedExample.replace(/(^\+[0-9]{1,3} |[0-9])/g, (value, _, i) => (i ? '0' : value))
Comment thread
Pedro-vk marked this conversation as resolved.
Outdated
}
return formatedExample.replace(/[0-9]/g, '0')
}

return formatedExample
}

export const PhoneNumberUtils = {
getPhoneHash,
getCountryCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class SetupAccountScreen extends React.Component<Props, State> {
onEndEditingPhoneNumber={this.onChangePhoneEndEditing}
onEndEditingCountryCode={this.onSetCountryCodeEndEditing}
inputCountryPlaceholder={t('country')}
inputPhonePlaceholder={t('phoneNumber')}
initialInputPhonePlaceholder={t('phoneNumber')}
/>
</View>
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ exports[`PhoneNumberInput when defaultCountry renders defaults 1`] = `
onBlur={[Function]}
onChangeText={[Function]}
onFocus={[Function]}
placeholder="000 0000-0000"
placeholderTextColor="#D1D5D8"
rejectResponderTermination={true}
style={
Expand Down