https://github.com/mukeshsoni/react-telephone-input
React component for entering and validating international telephone numbers
https://github.com/mukeshsoni/react-telephone-input
input international-telephone react react-component
Last synced: 5 months ago
JSON representation
React component for entering and validating international telephone numbers
- Host: GitHub
- URL: https://github.com/mukeshsoni/react-telephone-input
- Owner: mukeshsoni
- License: mit
- Created: 2014-11-28T07:58:11.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2021-08-02T23:44:11.000Z (about 4 years ago)
- Last Synced: 2025-05-08T21:57:52.596Z (5 months ago)
- Topics: input, international-telephone, react, react-component
- Language: Less
- Homepage: https://unstack.in/react-telephone-input
- Size: 20.3 MB
- Stars: 266
- Watchers: 7
- Forks: 167
- Open Issues: 33
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Reactjs Component for International Telephone Input
[![Travis][build-badge]][build]
[![npm package][npm-badge]][npm]_IMP_: From 5.\*, the withStyles file will not be bundled in dist. If you want
to use the css, you can import the css file directly to your module -```JavaScript
import 'react-telephone-input/css/default.css'
```_IMP_: From 3.\*, the styles won't be included by default. Can use `require('react-telephone-input/lib/withStyles')` to get the styles.
Inspired from the awesome jquery plugin for International Telephone Input.
This one is written as a [reactjs](http://facebook.github.io/react/) component.
Live Demo here - [http://unstack.in/react-telephone-input/](http://unstack.in/react-telephone-input/)

```js
function handleInputChange(telNumber, selectedCountry) {
console.log('input changed. number: ', telNumber, 'selected country: ', selectedCountry);
}// Use declaratively within another react components render method
var ReactTelInput = require('react-telephone-input');
// var ReactTelInput = require('react-telephone-input/lib/withStyles'), if you need the styles
// or render standalone
var ReactTelInput = require('react-telephone-input');
React.render(,
document.getElementById('my-container'));
```The component accepts other props to customize it -
#### value and initialValue
Both the props have the same purpose - for setting the initial value of the input programatically.
#### initialValue
See `value` above.
#### autoFormat
`true` by default. Setting it to false will turn off all auto formatting.
#### defaultCountry
The component selects the country code of a country by default. You can change it by passing the iso2 name of a country. E.g. for United States, set `defaultCountry` to 'us'.
#### onlyCountries
If you don't want all countries to be shown in the dropdown list, you can pass an array of objects representing the countries. The structure of each country object can be seen here [https://github.com/mukeshsoni/country-telephone-data/blob/master/country_telephone_data.js](https://github.com/mukeshsoni/country-telephone-data/blob/master/country_telephone_data.js)
E.g.
```
```
#### preferredCountries
Preferred countries show up at the top of the list. This prop also accepts an array of country iso2 names.
E.g.
```
```
#### classNames
You can send in additional classes to be applied to the top container div of the component.
#### onChange
Function, which is called whenever there is a change of value in the input box.
#### onEnterKeyPress
Function, which is called when user presses the 'enter' key when the input box is in focus.
#### onBlur
Function, which is called when the focus goes away from the input box.
#### onFocus
Function, which is called when the input box gets the focus.
#### disabled
Boolean value. When set to true, the input box is disabled and clicking on flag dropdown does nothing.
#### pattern
String. This is passed on to the input field and works like it should for any other input field.
#### inputProps
Object. If you want to set some property on the input element, you can pass in an object with attributes. E.g.
```javascript
let inputProps={
readOnly: true,
autoFocus: true
}```
#### autoComplete
string. Specify the type of autocomplete for the input element.
Default value - 'tel'
#### listStyle
Object. Style applied to the dropdown list container. There is a default value for it
```
listStyle = {
zIndex: 20,
backgroundColor: 'white'
}
```#### listItemClassName
string. Classname which will be applied to each item in the dropdown list. The default is `country`
## How to use it
* If you install it from `npm install`, you can just do `var ReactTelephoneInput = require('react-telephone-input');`
* You will need to copy flags.png from example/src folder to see the flag icons for each country.
* Set the path to the flags image using the prop `flagsImagePath````js
function handleInputChange(telNumber, selectedCountry) {
console.log('input changed. number: ', telNumber, 'selected country: ', selectedCountry)
}function handleInputBlur(telNumber, selectedCountry) {
console.log(
'Focus off the ReactTelephoneInput component. Tel number entered is: ',
telNumber,
' selected country is: ',
selectedCountry
)
}```
The default value for `flagsImagePath` is 'flags.png'
## Features
* Automatically format the number as the user types
* Navigate the country dropdown by typing a country's name, or using up/down keys
* Selecting a country from the dropdown will update the dial code in the input
* Typing a different dial code will automatically update the displayed flag
* Country names in the dropdown also include localised versions in brackets
* Dropdown appears above or below the input depending on available space/scroll position### Contributing
You can try the app by downloading everything and running the commands given below -
```
$ npm install
$ npm startgo to your browser and type http://localhost:3000
```### Running tests
```
$ npm run test
$ npm run test -- --watch # to run tests in watch mode
```[build-badge]: https://img.shields.io/travis/mukeshsoni/react-telephone-input/master.png?style=flat-square
[build]: https://travis-ci.org/mukeshsoni/react-telephone-input
[npm-badge]: https://img.shields.io/npm/v/react-telephone-input.svg?style=flat-square
[npm]: https://www.npmjs.org/package/react-telephone-input