- Old Versions
- Installation
- Features
- Basic Usage
- Intermediate Usage
- Advanced Usage
- Customizing Lib
- Lib Props
- Lib Functions
- Supported languages
- Contributing
- License
$ npm i --save react-native-international-phone-numberOR
$ yarn add react-native-international-phone-numberAND
Update your
metro.config.jsfile:module.exports = { ... resolver: { sourceExts: ['jsx', 'js', 'ts', 'tsx', 'cjs', 'json'], }, };
- π± Works with iOS, Android (Cross-platform) and Expo;
- π¨ Lib with UI customizable;
- π Phone Input Mask according with the selected country;
- π¨βπ» Functional and class component support;
- πΆ 18 languages supported.
import React from 'react';
import { View, Text } from 'react-native';
import PhoneInput from 'react-native-international-phone-number';
export class App extends React.Component {
constructor(props) {
super(props)
this.state = {
selectedCountry: null,
inputValue: ''
}
}
function handleSelectedCountry(country) {
this.setState({
selectedCountry: country
})
}
function handleInputValue(phoneNumber) {
this.setState({
inputValue: phoneNumber
})
}
render(){
return (
<View style={{ width: '100%', flex: 1, padding: 24 }}>
<PhoneInput
value={this.state.inputValue}
onChangePhoneNumber={this.handleInputValue}
selectedCountry={this.state.selectedCountry}
onChangeSelectedCountry={this.handleSelectedCountry}
/>
<View style={{ marginTop: 10 }}>
<Text>
Country:{' '}
{`${this.state.selectedCountry?.name?.en} (${this.state.selectedCountry?.cca2})`}
</Text>
<Text>
Phone Number: {`${this.state.selectedCountry?.callingCode} ${this.state.inputValue}`}
</Text>
</View>
</View>
);
}
}import React, { useState } from 'react';
import { View, Text } from 'react-native';
import PhoneInput from 'react-native-international-phone-number';
export default function App() {
const [selectedCountry, setSelectedCountry] = useState(null);
const [inputValue, setInputValue] = useState('');
function handleInputValue(phoneNumber) {
setInputValue(phoneNumber);
}
function handleSelectedCountry(country) {
setSelectedCountry(country);
}
return (
<View style={{ width: '100%', flex: 1, padding: 24 }}>
<PhoneInput
value={inputValue}
onChangePhoneNumber={handleInputValue}
selectedCountry={selectedCountry}
onChangeSelectedCountry={handleSelectedCountry}
/>
<View style={{ marginTop: 10 }}>
<Text>
Country:{' '}
{`${selectedCountry?.name?.en} (${selectedCountry?.cca2})`}
</Text>
<Text>
Phone Number:{' '}
{`${selectedCountry?.callingCode} ${inputValue}`}
</Text>
</View>
</View>
);
}import React, { useState } from 'react';
import { View, Text } from 'react-native';
import PhoneInput, {
ICountry,
} from 'react-native-international-phone-number';
export default function App() {
const [selectedCountry, setSelectedCountry] =
useState<null | ICountry>(null);
const [inputValue, setInputValue] = useState<string>('');
function handleInputValue(phoneNumber: string) {
setInputValue(phoneNumber);
}
function handleSelectedCountry(country: ICountry) {
setSelectedCountry(country);
}
return (
<View style={{ width: '100%', flex: 1, padding: 24 }}>
<PhoneInput
value={inputValue}
onChangePhoneNumber={handleInputValue}
selectedCountry={selectedCountry}
onChangeSelectedCountry={handleSelectedCountry}
/>
<View style={{ marginTop: 10 }}>
<Text>
Country:{' '}
{`${selectedCountry?.name?.en} (${selectedCountry?.cca2})`}
</Text>
<Text>
Phone Number:{' '}
{`${selectedCountry?.callingCode} ${inputValue}`}
</Text>
</View>
</View>
);
}import React, { useRef } from 'react';
import { View, Text } from 'react-native';
import PhoneInput, {
ICountry,
IPhoneInputRef,
} from 'react-native-international-phone-number';
export default function App() {
const phoneInputRef = useRef<IPhoneInputRef>(null);
function onSubmitRef() {
Alert.alert(
'Intermediate Result',
`${phoneInputRef.current?.selectedCountry?.callingCode} ${phoneInputRef.current?.value}`
);
}
return (
<View style={{ width: '100%', flex: 1, padding: 24 }}>
<PhoneInput ref={phoneInputRef} />
<TouchableOpacity
style={{
width: '100%',
paddingVertical: 12,
backgroundColor: '#2196F3',
borderRadius: 4,
marginTop: 10,
}}
onPress={onSubmit}
>
<Text
style={{
color: '#F3F3F3',
textAlign: 'center',
fontSize: 16,
fontWeight: 'bold',
}}
>
Submit
</Text>
</TouchableOpacity>
</View>
);
}Observation: Don't use the useRef hook combined with the useState hook to manage the phoneNumber and selectedCountry values. Instead, choose to use just one of them (useRef or useState).
import React, { useState, useEffect } from 'react';
import { View, Text, TouchableOpacity, Alert } from 'react-native';
import PhoneInput, {
ICountry,
} from 'react-native-international-phone-number';
import { Controller, FieldValues } from 'react-hook-form';
interface FormProps extends FieldValues {
phoneNumber: string;
}
export default function App() {
const [selectedCountry, setSelectedCountry] = useState<
undefined | ICountry
>(undefined);
function handleSelectedCountry(country: ICountry) {
setSelectedCountry(country);
}
function onSubmit(form: FormProps) {
Alert.alert(
'Advanced Result',
`${selectedCountry?.callingCode} ${form.phoneNumber}`
);
}
return (
<View style={{ width: '100%', flex: 1, padding: 24 }}>
<Controller
name="phoneNumber"
control={control}
render={({ field: { onChange, value } }) => (
<PhoneInput
defaultValue="+12505550199"
value={value}
onChangePhoneNumber={onChange}
selectedCountry={selectedCountry}
onChangeSelectedCountry={handleSelectedCountry}
/>
)}
/>
<TouchableOpacity
style={{
width: '100%',
paddingVertical: 12,
backgroundColor: '#2196F3',
borderRadius: 4,
}}
onPress={handleSubmit(onSubmit)}
>
<Text
style={{
color: '#F3F3F3',
textAlign: 'center',
fontSize: 16,
fontWeight: 'bold',
}}
>
Submit
</Text>
</TouchableOpacity>
</View>
);
}Observations:
- You need to use a default value with the following format:
+(country callling code)(area code)(number phone)- The lib has the mechanism to set the flag and mask of the supplied
defaultValue. However, if the supplieddefaultValuedoes not match any international standard, theinput mask of the defaultValuewill be set to "BR" (please make sure that the default value is in the format mentioned above).
...
<PhoneInput
...
theme="dark"
/>
... ...
<PhoneInput
...
inputStyle={{
color: '#F3F3F3',
}}
containerStyle={{
backgroundColor: '#575757',
borderWidth: 1,
borderStyle: 'solid',
borderColor: '#F3F3F3',
}}
flagContainerStyle={{
borderTopLeftRadius: 7,
borderBottomLeftRadius: 7,
backgroundColor: '#808080',
justifyContent: 'center',
}}
flagTextStyle={{
fontSize: 16,
fontWeight: 'bold',
color: '#F3F3F3',
}}
modalStyle={{
modal: {
backgroundColor: '#333333',
borderWidth: 1,
},
backdrop: {},
divider: {
backgroundColor: 'transparent',
},
countriesList: {},
searchInput: {
borderRadius: 8,
borderWidth: 1,
borderColor: '#F3F3F3',
color: '#F3F3F3',
backgroundColor: '#333333',
paddingHorizontal: 12,
height: 46,
},
countryButton: {
borderWidth: 1,
borderColor: '#F3F3F3',
backgroundColor: '#666666',
marginVertical: 4,
paddingVertical: 0,
},
noCountryText: {},
noCountryContainer: {},
flag: {
color: '#FFFFFF',
fontSize: 20,
},
callingCode: {
color: '#F3F3F3',
},
countryName: {
color: '#F3F3F3',
},
}}
/>
... ...
<PhoneInput
...
modalHeight="80%"
/>
... ...
<PhoneInput
...
modalDisabled
/>
... ...
<PhoneInput
...
disabled
/>
... const [isDisabled, setIsDisabled] = useState<boolean>(true)
...
<PhoneInput
...
containerStyle={ isDisabled ? { backgroundColor: 'yellow' } : {} }
disabled={isDisabled}
/>
... ...
<PhoneInput
...
language="pt"
/>
... ...
<PhoneInput
...
customMask={['#### ####', '##### ####']}
/>
... ...
<PhoneInput
...
defaultCountry="CA"
/>
... ...
<PhoneInput
...
defaultValue="+12505550199"
/>
...Observations:
- You need to use a default value with the following format:
+(country callling code)(area code)(number phone)- The lib has the mechanism to set the flag and mask of the supplied
defaultValue. However, if the supplieddefaultValuedoes not match any international standard, theinput mask of the defaultValuewill be set to "BR" (please make sure that the default value is in the format mentioned above).
Component Props (PhoneInputProps)
language?:ILanguage;customMask?:string[];defaultValue?:string;value?:string;onChangePhoneNumber?:(phoneNumber: string) => void;selectedCountry?:ICountry;onChangeSelectedCountry?:(country: ICountry) => void;disabled?:boolean;modalDisabled?:boolean;modalHeight?:number | string;theme?:ITheme;containerStyle?:StyleProp<ViewStyle>;flagContainerStyle?:StyleProp<ViewStyle>;flagTextStyle?:StyleProp<TextStyle>;inputStyle?:StyleProp<TextStyle>;modalStyle?:IModalStyle;ref?:Ref<IPhoneInputRef>
getAllCountries:() => ICountry[];getCountriesByCallingCode:(callingCode: string) => ICountry[] | undefined;getCountryByCca2:(cca2: string) => ICountry | undefined;getCountriesByName:(name: string, language: ILanguage) => ICountry[] | undefined;getCountryByPhoneNumber:(phoneNumber: string) => ICountry | undefined;
"name": {
"en": "English",
"ru": "Russian",
"pl": "Polish",
"ua": "Ukrainian",
"cz": "Czech",
"by": "Belarusian",
"pt": "Portuguese",
"es": "Espanol",
"ro": "Romanian",
"bg": "Bulgarian",
"de": "German",
"fr": "French",
"nl": "Dutch",
"it": "Italian",
"cn": "Chinese",
"ee": "Estonian",
"jp": "Japanese",
"he": "Hebrew"
},- Fork or clone this repository
$ git clone https://github.com/AstrOOnauta/react-native-international-phone-number.git-
Repair, Update and Enjoy π οΈπ§βοΈ
-
Create a new PR to this repository
Thanks for stopping by! π

