JSPM

react-phone-number-input

0.2.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 751453
  • Score
    100M100P100Q197431F
  • License MIT

Telephone input for React

Package Exports

  • react-phone-number-input

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-phone-number-input) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-phone-number-input

[![NPM Version][npm-badge]][npm] [![Build Status][travis-badge]][travis] [![Test Coverage][coveralls-badge]][coveralls]

International phone number <input/> in React

screenshot of react-phone-number-input

Installation

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

Usage

import Phone, { phoneNumberFormat, isValidPhoneNumber } from 'react-phone-number-input'

...

state = { phone: '+79991234567' }

...

return (
    <div>
        +7
        <Phone
            format={ phoneNumberFormat.RU }
            value={ this.state.phone }
            onChange={ phone => this.setState({ phone }) }
            className={ classnames('phone', {
                'phone--invalid': !isValidPhoneNumber(this.state.phone, phoneNumberFormat.RU)
            }) } />
    </div>
)

// Outputs "(999) 123-45-67" in the <input/> field.
// `this.state.phone` is plaintext: "+79991234567".

API

default

A React component with the following props

{
    format : PropTypes.oneOfType
    ([
        PropTypes.shape
        ({
            country  : PropTypes.string.isRequired,
            template : PropTypes.string.isRequired
        }),
        PropTypes.shape
        ({
            template : PropTypes.func.isRequired
        })
    ])
    .isRequired,
    value     : PropTypes.string.isRequired,
    onChange  : PropTypes.func.isRequired
}

All other props are passed directly to the underlying <input/> component (and so it can be used with things like redux-form).

phone_number_format

(aka phoneNumberFormat)

A map with phone number format examples

{
    // +7 | (123) 456-78-90
    RU:
    {
        country  : '7',
        template : '(xxx) xxx-xx-xx'
    },

    // +1 | (123) 456-7890
    US:
    {
        country  : '1',
        template : '(xxx) xxx-xxxx'
    },

    // Supports any custom phone number format logic
    custom:
    {
        // Generates a proper phone number template for the given input digits,
        // where each digit is designated with an "x" symbol.
        //
        // E.g.: "71234567890" -> "+x (xxx) xxx-xx-xx"
        //
        template(digits)
        {
            // Template for "+7 (123) 456-78-90" is:
            return "+x (xxx) xxx-xx-xx"

            // More complex logic can be implemented here
            // for countries with non-trivial phone number formatting rules.
        }
    }
}

is_valid_phone_number(plaintext, format)

(aka isValidPhoneNumber)

Returns true if plaintext phone number is valid given the format.

format_phone_number(plaintext, format)

(aka formatPhoneNumber)

Formats plaintext phone number given the format. The output format is local (without country code).

  • +79991234567(999) 123-45-67
  • 9991234567(999) 123-45-67

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 [npm]: https://www.npmjs.org/package/react-phone-number-input [npm-badge]: https://img.shields.io/npm/v/react-phone-number-input.svg?style=flat-square [travis]: https://travis-ci.org/halt-hammerzeit/react-phone-number-input [travis-badge]: https://img.shields.io/travis/halt-hammerzeit/react-phone-number-input/master.svg?style=flat-square [coveralls]: https://coveralls.io/r/halt-hammerzeit/react-phone-number-input?branch=master [coveralls-badge]: https://img.shields.io/coveralls/halt-hammerzeit/react-phone-number-input/master.svg?style=flat-square