Gathering detailed insights and metrics for react-native-paper-phone-number-input
Gathering detailed insights and metrics for react-native-paper-phone-number-input
Gathering detailed insights and metrics for react-native-paper-phone-number-input
Gathering detailed insights and metrics for react-native-paper-phone-number-input
A performant phone number input component for react-native-paper with country picker
npm install react-native-paper-phone-number-input
46.1
Supply Chain
60.9
Quality
75.9
Maintenance
50
Vulnerability
94.8
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
6 Stars
63 Commits
1 Forks
2 Watching
1 Branches
3 Contributors
Updated on 29 Jul 2024
Minified
Minified + Gzipped
TypeScript (91.88%)
JavaScript (4.16%)
HTML (3.95%)
Cumulative downloads
Total Downloads
Last day
100%
16
Compared to previous day
Last week
14.3%
64
Compared to previous week
Last month
-27%
290
Compared to previous month
Last year
0%
2,694
Compared to previous year
5
🔥 A material design Phone number input component for React Native Paper that has a country code picker with emoji flags for speed and low footprint. Performant and beautiful!
Theme | Number Input | Country Picker |
---|---|---|
Light | ||
Dark |
Install with your favorite package manager.
Using Yarn:
yarn add react-native-paper-phone-number-input
Using NPM:
npm i react-native-paper-phone-number-input
This package should work out of the box on most desktop and mobile browsers on Web. However, Microsoft Windows does not ship with Flags Emoji by default. While the flags will display correctly on Firefox and its derivatives, Chromium based browsers like Chrome and Edge will not display the flags correctly and will show the two-letter country code instead.
To fix this, you should install country-flag-emoji-polyfill and import it in your project. This polyfill will add support for Flag Emojis on Windows. The setup process is described in the readme of the library. Briefly:
For expo users, you can add the polyfill by adding the following to your web/index.html
file:
1<script type="module" defer> 2 import { polyfillCountryFlagEmojis } from 'https://cdn.skypack.dev/country-flag-emoji-polyfill'; 3 polyfillCountryFlagEmojis(); 4</script>
For other setups, you can either add the above snippet in your index.html
file or import the polyfill in your web entrypoint.
yarn add country-flag-emoji-polyfill
1// App.web.tsx 2import { polyfillCountryFlagEmojis } from 'country-flag-emoji-polyfill'; 3 4polyfillCountryFlagEmojis();
See the example
directory for a complete example. For further information including using local fonts instead of loading it from the CDN, please refer to the API Documentation of the library.
Note that this package uses the default font family Twemoji Country Flags
for the relevant components. If you want to use a different font, in addition to using the proper parameters in polyfillCountryFlagEmojis
call, you need to pass the relevant font families in the theme
prop of the PhoneNumberInput
or CountryPicker
components.
1<PhoneNumberInput 2 code={countryCode} 3 setCode={setCountryCode} 4 phoneNumber={phoneNumber} 5 setPhoneNumber={setPhoneNumber} 6 theme={{ 7 fonts: { 8 // For MD3Theme. 9 bodyLarge: { 10 fontFamily: '"YourFontFamily", ...', 11 }, 12 default: { 13 fontFamily: '"YourFontFamily", ...', 14 }, 15 // For MD2Theme. 16 regular: { 17 fontFamily: '"YourFontFamily", ...', 18 }, 19 }, 20 }} 21/>
This package also exports defaultFlagsFont
which is the default font family used for the flags. You can use this to display flag emojis correctly in other components in your application on Windows.
1import { Text } from 'react-native'; 2import { defaultFlagsFont } from 'react-native-paper-phone-number-input'; 3 4// Use defaultFlagsFont in your components. 5<Text 6 style={{ 7 fontFamily: defaultFlagsFont, 8 }} 9> 10 {selectedCountry.flag} 11</Text>;
Import the PhoneNumberInput
component from the library. You can then use it in your application like so:
1// App.tsx 2import React, { useState } from 'react'; 3import { PhoneNumberInput, getCountryByCode } from 'react-native-paper-phone-number-input'; 4 5// `showFirstOnList`, `includeCountries` and `excludeCountries` should be defined outsude 6// the component to prevent uncessary recomputations and re-renders. 7const includeCountries = ['AZ', 'BD', 'CA', 'GB', 'IN', 'NZ', 'US', 'TR']; 8 9export default function App() { 10 const [countryCode, setCountryCode] = useState<string>('BD'); // Default country code 11 const [phoneNumber, setPhoneNumber] = useState<string>(); 12 13 const { name, flag, dialCode } = getCountryByCode(countryCode); // Get country details 14 15 return ( 16 <PhoneNumberInput 17 code={countryCode} 18 setCode={setCountryCode} 19 phoneNumber={phoneNumber} 20 setPhoneNumber={setPhoneNumber} 21 includeCountries={includeCountries} 22 /> 23 ); 24}
A more complete example can be found in the example
directory.
PhoneNumberInput
Prop | Type | Description | Notes |
---|---|---|---|
code | string | The country code. | Optional. By default, the country code is set to ## which shows a world icon. |
setCode | (code: string) => void | A function that sets the country code. | Required. |
phoneNumber | string | The phone number. | Optional. By default, no phone number is set. |
setPhoneNumber | (phoneNumber: string) => void | A function that sets the phone number. | Required. |
showFirstOnList | string[] | A list of country codes that should be shown on top of the list. | Optional. By default, countries are shown alphabetically. |
includeCountries | string[] | A list of country codes that specifies which countries can be selected. | Optional. By default, shows all countries. |
excludeCountries | string[] | A list of country codes that specifies which countries cannot be selected. | Optional. By default, does not exclude any countries. |
limitMaxLength | boolean | Limit the maximum length of the phone number for the country as defined in E.164. | Optional. By default, the maximum length of the phone number is not limited. |
modalStyle | StyleProp<ViewStyle> | The style of the modal that shows the country code picker. | Optional. |
modalContainerStyle | StyleProp<ViewStyle> | The style of the container of the modal that shows the country code picker. | Optional. |
...rest | ...TextInputProps | Any other props that you want to pass to the TextInput component of React Native Paper. | Optional. |
[!TIP]
The props that accepts a list o f country codes such asshowFirstOnList
,includeCountries
andexcludeCountries
should be defined outside the component or memoized to prevent unnecessary recomputations and re-renders!
[!CAUTION] If you set contradictory prop values in
includeCountries
andexcludeCountries
props, theexcludeCountries
prop will take precedence over theincludeCountries
prop. ie. If you set the same country code in bothincludeCountries
andexcludeCountries
, the country will be excluded.
Method Name | Description |
---|---|
openCountryPicker | Opens the country code picker. |
closeCountryPicker | Closes the country code picker. |
CountryPicker
Prop | Type | Description | Notes |
---|---|---|---|
country | string | The country | Optional. |
setCountry | (code: string) => void | A function that sets the country code. | Required. |
...rest | ...TextInputProps | Any other props that you want to pass to the TextInput component of React Native Paper. | Optional. |
Method Name | Description |
---|---|
openCountryPicker | Opens the country picker. |
closeCountryPicker | Closes the country picker. |
getCountryByCode
1type CountryWithoutCode = { 2 name: string; 3 flag: string; 4 dialCode: string; 5}; 6 7const getCountryByCode: (code?: string) => CountryWithoutCode;
You can set the default country by setting the code
prop. This prop accepts a country code. For example, if you want to set the default country to Bangladesh, you can set the code
prop to BD
.
Multiple countries can have the same dial code. For example, the dial code for United States is +1 and the dial code for Canada is also +1. So, if we were to set the dial code instead of the country code, wrong flag would be shown for the country.
To solve this, this library uses the country code instead of the dial code. This library also exposes a getCountryByCode
method that can be used to get the country details from the country code such as the dial code, country name and flag.
This project integrates with react-native-builder-bob
. To get started:
yarn
.yarn example android
, yarn example ios
or yarn example web
.This package is licensed under the MIT License.
Any kind of contribution is welcome. Thanks!
No vulnerabilities found.
No security vulnerabilities found.