Gathering detailed insights and metrics for @emberly/react-phone-input-2
Gathering detailed insights and metrics for @emberly/react-phone-input-2
Gathering detailed insights and metrics for @emberly/react-phone-input-2
Gathering detailed insights and metrics for @emberly/react-phone-input-2
📞 Highly customizable phone input component with auto formatting
npm install @emberly/react-phone-input-2
Typescript
Module System
Node Version
NPM Version
JavaScript (98.85%)
Less (1.13%)
HTML (0.01%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
388 Commits
1 Branches
2 Contributors
Updated on Mar 02, 2024
Latest Version
2.15.9
Package Id
@emberly/react-phone-input-2@2.15.9
Unpacked Size
417.32 kB
Size
196.40 kB
File Count
26
NPM Version
10.2.4
Node Version
20.11.1
Published on
Mar 02, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
This is a fork of [react-phone-input-2] This fixes a few minor issues with the original package, since it seems to be slow to accept PRs. However, it's still the best phone input package I've found for React, unfortunately.
Highly customizable phone input component with auto formatting.
1npm install react-phone-input-2 --save
1import PhoneInput from 'react-phone-input-2' 2import 'react-phone-input-2/lib/style.css' 3 4<PhoneInput 5 country={'us'} 6 value={this.state.phone} 7 onChange={phone => this.setState({ phone })} 8/>
available styles - style • high-res • material • bootstrap • semantic-ui • plain
Name | Type | Description | Example |
---|---|---|---|
country | string | initial country | 'us' | 1 |
value | string | input state value | |
onlyCountries | array | country codes to be included | ['cu','cw','kz'] |
preferredCountries | array | country codes to be at the top | ['cu','cw','kz'] |
excludeCountries | array | array of country codes to be excluded | ['cu','cw','kz'] |
placeholder | string | custom placeholder | |
inputProps | object | props to pass into the input |
Booleans | Default | Description |
---|---|---|
autoFormat | true | on/off phone formatting |
disabled | false | disable input and dropdown |
disableDropdown | false | disable dropdown only |
disableCountryCode | false | |
enableAreaCodes | false | enable local codes for all countries |
enableTerritories | false | enable dependent territories with population of ~100,000 or lower |
enableLongNumbers | false | boolean/number |
countryCodeEditable | true | |
enableSearch | false | enable search in the dropdown |
disableSearchIcon | false | hide icon for the search field |
1<PhoneInput 2 inputProps={{ 3 name: 'phone', 4 required: true, 5 autoFocus: true 6 }} 7/>
containerClass | string | class for container | |
inputClass | string | class for input | |
buttonClass | string | class for dropdown button | |
dropdownClass | string | class for dropdown container | |
searchClass | string | class for search field | |
containerStyle | object | styles for container | |
inputStyle | object | styles for input | |
buttonStyle | object | styles for dropdown button | |
dropdownStyle | object | styles for dropdown container | |
searchStyle | object | styles for search field |
onChange | onFocus | onBlur | onClick | onKeyDown |
onChange(value, country, e, formattedValue)
Country data object not returns from onKeyDown event
Data | Type | Description |
---|---|---|
value/event | string/object | event or the phone number |
country data | object | country object { name, dialCode, countryCode (iso2) } |
Name | Type | Description |
---|---|---|
regions | array/string | to show countries only from specified regions |
Regions |
---|
['america', 'europe', 'asia', 'oceania', 'africa'] |
Subregions |
['north-america', 'south-america', 'central-america', 'carribean', 'eu-union', 'ex-ussr', 'ex-yugos', 'baltic', 'middle-east', 'north-africa'] |
1<PhoneInput 2 country='de' 3 regions={'europe'} 4/> 5 6<PhoneInput 7 country='us' 8 regions={['north-america', 'carribean']} 9/>
es
Spanish, de
Deutsch, ru
Russian, fr
French
jp
Japanese, cn
Chinese, pt
Portuguese, it
Italian
ir
Iranian, ar
Arabic, tr
Turkish, id
Indonesian
hu
Hungarian, pl
Polish, ko
Korean
1import es from 'react-phone-input-2/lang/es.json' 2 3<PhoneInput 4 localization={es} 5/>
1<PhoneInput 2 enableAreaCodes={true} 3 enableAreaCodes={['us', 'ca']} 4 enableAreaCodeStretch 5/>
If enableAreaCodeStretch
is added, the part of the mask with the area code will not stretch to length of the respective section of phone mask.
Example: +61 (2), +61 (02)
1<PhoneInput 2 onlyCountries={['fr', 'at']} 3 masks={{fr: '(...) ..-..-..', at: '(....) ...-....'}} 4/>
1<PhoneInput 2 onlyCountries={['gr', 'fr', 'us']} 3 areaCodes={{gr: ['2694', '2647'], fr: ['369', '463'], us: ['300']}} 4/>
defaultMask | ... ... ... ... .. |
alwaysDefaultMask | false |
prefix | + |
searchPlaceholder | 'search' |
searchNotFound | 'No entries to show' |
copyNumbersOnly | true |
renderStringAsFlag | string |
autocompleteSearch | false |
jumpCursorToEnd | false |
priority | {{ca: 0, us: 1, kz: 0, ru: 1}} |
enableClickOutside | true |
showDropdown | false |
defaultErrorMessage | string |
specialLabel | string |
disableInitialCountryGuess | false |
disableCountryGuess | false |
1<PhoneInput 2 onlyCountries={['de', 'es']} 3 localization={{de: 'Deutschland', es: 'España'}} 4/> 5 6<PhoneInput 7 onlyCountries={['de', 'es']} 8 localization={{'Germany': 'Deutschland', 'Spain': 'España'}} 9/>
1<PhoneInput 2 onlyCountries={['fr', 'at']} 3 preserveOrder={['onlyCountries', 'preferredCountries']} 4/>
1handleOnChange(value, data, event, formattedValue) {
2 this.setState({ rawPhone: value.slice(data.dialCode.length) })
3}
isValid(value, country, countries, hiddenAreaCodes)
1<PhoneInput 2 isValid={(value, country) => { 3 if (value.match(/12345/)) { 4 return 'Invalid value: '+value+', '+country.name; 5 } else if (value.match(/1234/)) { 6 return false; 7 } else { 8 return true; 9 } 10 }} 11/>
1import startsWith from 'lodash.startswith'; 2 3<PhoneInput 4 isValid={(inputNumber, country, countries) => { 5 return countries.some((country) => { 6 return startsWith(inputNumber, country.dialCode) || startsWith(country.dialCode, inputNumber); 7 }); 8 }} 9/>
To clear country
, pass null
as value
.
1const phoneCountryFormat = useMemo(() => phoneCountry?.format || undefined, [phoneCountry]); 2const placeholder = useMemo(() => { 3 if (phoneCountryFormat) { 4 const words = phoneCountryFormat.split(' '); 5 words.shift(); // I'm using only local numbers so here I remove the country code from the format 6 let text = words.join(' '); 7 // first digit is special on french numbers, these 3 lines could be removed 8 if (country === 'fr') { 9 text = text.replace('.', '6'); 10 } 11 while (text.indexOf('.') > -1) { 12 text = text.replace('.', `${Math.min(9, Math.max(1, Math.floor(Math.random() * 10)))}`); 13 } 14 return text; 15 } 16 return ''; 17}, [phoneCountryFormat, country]);
1<script src="https://unpkg.com/react-phone-input-2@2.x/dist/lib.js"></script>
Based on react-phone-input
No vulnerabilities found.
No security vulnerabilities found.