Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sabrydawood/v-phone-input
https://github.com/sabrydawood/v-phone-input
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/sabrydawood/v-phone-input
- Owner: sabrydawood
- License: mit
- Created: 2023-09-21T23:08:51.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-21T23:09:00.000Z (over 1 year ago)
- Last Synced: 2024-04-13T08:36:10.072Z (9 months ago)
- Language: JavaScript
- Homepage: https://virgel1995.github.io/v-phone-input/
- Size: 113 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# v-react-phone
Highly customizable phone input component with auto formatting.[![npm version](https://img.shields.io/npm/v/v-react-phone.svg?style=flat)](https://www.npmjs.com/package/v-react-phone)
[![npm downloads](https://img.shields.io/npm/dm/v-react-phone.svg?style=flat)](https://www.npmjs.com/package/v-react-phone)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/virgel1995/v-react-phone#contributing)
[![travis build](https://travis-ci.org/virgel1995/v-react-phone.svg?branch=master)](https://travis-ci.org/virgel1995/v-react-phone)![animation](https://media.giphy.com/media/xiORAWnqoTJDsH0UOI/giphy.gif)
## Installation
```shell-script
npm install v-react-phone --save
```## Usage
```jsx
import PhoneInput from 'v-react-phone'
import 'v-react-phone/lib/style.css'this.setState({ phone })}
/>
```
**available styles** - style • high-res • material • bootstrap • semantic-ui • plain#### [Demo 1 (UI)](https://virgel1995.github.io/v-react-phone.html) - [Demo 2 (CSS)](https://virgel1995.github.io/v-react-phone-css.html)
![screenshot](https://raw.githubusercontent.com/virgel1995/v-react-phone/master/test/screenshot.png)
## Options
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
```jsx
```
### Contents
- [Style](#style)
- [Events](#events)
- [Regions](#regions)
- [Localization](#predefined-localization)
- [Local area codes](#local-area-codes)
- [Custom masks](#custom-masks)
- [Custom area codes](#custom-area-codes)
- [Other props](#other-props)
- [Custom localization](#custom-localization)
- [Guides](#guides)
- [Phone without dialCode](#phone-without-dialcode)
- [Check validity of the phone number](#check-validity-of-the-phone-number)
- [CDN](#cdn)
- [Contributing](#contributing)### Style
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
### Events
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) }
### Regions
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']
```jsx
```
### Predefined localization
`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```jsx
import es from 'v-react-phone/lang/es.json'```
### Local area codes
```jsx```
#### Flexible mask
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)`### Custom masks
```jsx```
### Custom area codes
```jsx```
### Other props
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
### Custom localization
```jsx```
### Preserve countries order
```jsx```
## Guides
### Phone without dialCode
```jsx
handleOnChange(value, data, event, formattedValue) {
this.setState({ rawPhone: value.slice(data.dialCode.length) })
}
```### Check validity of the phone number
`isValid(value, country, countries, hiddenAreaCodes)````jsx
{
if (value.match(/12345/)) {
return 'Invalid value: '+value+', '+country.name;
} else if (value.match(/1234/)) {
return false;
} else {
return true;
}
}}
/>
``````jsx
import startsWith from 'lodash.startswith';{
return countries.some((country) => {
return startsWith(inputNumber, country.dialCode) || startsWith(country.dialCode, inputNumber);
});
}}
/>
```### Clear country
To clear `country`, pass `null` as `value`.### Dynamic placeholder
Show
```jsx
const phoneCountryFormat = useMemo(() => phoneCountry?.format || undefined, [phoneCountry]);
const placeholder = useMemo(() => {
if (phoneCountryFormat) {
const words = phoneCountryFormat.split(' ');
words.shift(); // I'm using only local numbers so here I remove the country code from the format
let text = words.join(' ');
// first digit is special on french numbers, these 3 lines could be removed
if (country === 'fr') {
text = text.replace('.', '6');
}
while (text.indexOf('.') > -1) {
text = text.replace('.', `${Math.min(9, Math.max(1, Math.floor(Math.random() * 10)))}`);
}
return text;
}
return '';
}, [phoneCountryFormat, country]);
```### CDN
```html```
## Contributing
- Code style changes not allowed
- Do not create issues about incorrect or missing country masks (of already present countries) or absent area codes (they will be closed). Only create issues if the subject is an actual mechanism that is not present in the component. Otherwise create a PR with a link that proves the correctness of your newly suggested mask or list of area codes
- Do not send new languages## License
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/virgel1995/v-react-phone/blob/master/LICENSE)Based on [v-react-phone-input](https://github.com/razagill/v-react-phone-input)