caplin / react-phone-number-input

React component for international phone number input

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-phone-number-input

npm version npm downloads

International phone number <input/> (and output) (in React) (iPhone style)

See Demo

Screenshot #1

With custom autocomplete/select:

Screenshot #2

With native <select/>:

Native <select/>

Installation

npm install react-phone-number-input --save

Usage

import Phone from 'react-phone-number-input'

return (
	<Phone
		placeholder="Enter phone number"
		value={ this.state.phone }
		onChange={ phone => this.setState({ phone }) } />
)

The international phone number input utilizes libphonenumber-js international phone number parsing and formatting library (used in Google Android phones). The size of the library is about 70 KiloBytes, so it's suitable for public internet usage (for example, the size of react package is about 50 KiloBytes).

The countries dropdown with autocomplete is taken from react-responsive-ui library.

I could easily include all country flags in a form of <svg/> React elements as part of this library but the overall size of the bundle would then be about 3 MegaBytes (yeah, those SVGs turned out to be really huge) which is too much for a public internet website. Therefore the default behaviour is a compromise: instead of pleloading the flags for all countries in the list only the flag for the currently selected country is shown. This way the user only downloads a single SVG image, and is not forced to download the whole international flag bundle.

CSS

This component is styled using CSS. Copy styles/style.css to your project folder and include it on a page:

<head>
  <link rel="stylesheet" href="/css/react-phone-number-input.css"/>
</head>

This CSS file is meant as a base one and a developer should override the CSS rules defined in it (this can be done in a separate file) to better suit the project's needs.

An alternative way of including the base CSS file when using Webpack would be:

require('react-phone-number-input/styles/style.css')

API

React component

The available props are

  • value — the phone number holding variable, will contain the phone number in international plaintext format (e.g. +12223333333 for USA)

  • onChange — the function writing the phone number to the value variable

  • country — (optional) the default country; if this property changes and the user hasn't entered a phone number yet then this new country is selected

  • countries — (optional) only these countries will be allowed (e.g. ['RU', 'KZ', 'UA'])

  • flagsPath — (optional) A base URL path for national flag SVG icons. By default it loads flag icons from flag-icon-css github repo. You might want to download those SVG flag icons and host them yourself.

  • flags — (optional) Custom national flag icon React elements (a map of country codes, or just false for no flags at all)

  • nativeExpanded — if set to true will render native <select/> when country selector is expanded instead of the custom one with autocomplete

For the full list of all possible props see the source code.

isValidPhoneNumber

libphonenumber.isValidNumber

(is exported just for convenience, if anyone needs that for whatever purpose)

formatPhoneNumber

libphonenumber.format

(is exported just for convenience, if anyone needs that for whatever purpose)

parsePhoneNumber

libphonenumber.parse

(is exported just for convenience, if anyone needs that for whatever purpose)

Webpack

If you're using Webpack 1 then make sure that

  • You have json-loader set up for *.json files in Webpack configuration
  • json-loader doesn't exclude /node_modules/
  • If you override resolve.extensions in Webpack configuration then make sure .json extension is present in the list

Webpack 2 sets up json-loader by default so there's no need for any special configuration.

Reducing bundle size

By default all countries are included which means that libphonenumber-js loads the complete metadata set having the size of 75 KiloBytes. This really isn't much but for those who still want to reduce that to a lesser size there is a special exported <Input/> creator which takes custom metadata as an argument.

For a "tree-shaking" ES6-capable bundler (e.g. Webpack 2) that would be

import { Input } from 'react-phone-number-input'
import metadata from './metadata.min.json'

export default function Phone(props) {
	return <Input { ...props } metadata={ metadata }/>
}

And for Common.js environment that would be

var Input = require('react-phone-number-input/custom')
var metadata = require('./metadata.min.json')

module.exports = function Phone(props) {
	return <Input { ...props } metadata={ metadata }/>
}

For generating custom metadata see the guide in libphonenumber-js repo.

Contributing

After cloning this repo, ensure dependencies are installed by running:

npm install

This module is written in ES6 and uses Babel for ES5 transpilation. Widely consumable JavaScript can be produced by running:

npm run build

Once npm run build has run, you may import or require() directly from node.

After developing, the full test suite can be evaluated by running:

npm test

When you're ready to test your new functionality on a real project, you can run

npm pack

It will build, test and then create a .tgz archive which you can then install in your project folder

npm install [module name with version].tar.gz

License

MIT

About

React component for international phone number input

License:MIT License


Languages

Language:JavaScript 82.3%Language:CSS 17.7%