1.0.4 • Published 4 years ago

react-phone-lib v1.0.4

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

React Phone Lib

Encapsuling libphonenumber in React.js.

https://github.com/google/libphonenumber

Installation

$ yarn add react-phone-lib

or

$ npm install react-phone-lib

Usage

import the component:

import PhoneInput from "react-phone-lib";
    handleChangeNumber = val => this.setState({ phone: val });

    handleOnBlur = () => console.log("onBlur called");

    handleOnFocus = () => console.log("onFocus called");

    validateNumber = (obj) => {
        if (obj && obj.isValid) {
            console.log("The number is valid");
        }
    };

    render() {
        const { phone } = this.state;
        return (
            <div>
                <h2>React Phone lib - Demo</h2>
                <PhoneInput
                debug
                autoFormat
                value={phone}
                onChange={this.handleChangeNumber}
                onBlur={this.handleOnBlur}
                callBack={this.validateNumber}
                onFocus={this.handleOnFocus}
                format="INTERNATIONAL"
                language="es"
                label="Phone"
                inputId="phone"
                defaultCountry="ES"
                inputClass="input-class"
                selectClass="select-class"
                containerClass="container-class"
                />
            </div>
        );
    }

Development

Build

$ yarn build

Running on localhost

$ cd demo && yarn start

Properties

propertypropTyperequireddeafultdescription
debugbool-falseshows a console log with the current state after each key pressed
onBlurfunc-nullexecute a callback function when the event onBlur occurs on phone input
onFocusfunc-nullexecute a callback function when the event onFocus occurs on phone input
onChangefunc-nullexecute a callback function when the event onChange occurs on phone input
callBackfucn-nullexecute a callback function passing the actual state when 'onBlur, onFocus and onChange' events occurs
labelstring-nullset the label
valuestring-''set the input value
inputIdstring-nullset the input ID
formatstring-'INTERNATIONAL'controls how the input value will be formatted: E164: "+5511999999999" INTERNATIONAL: "+55 11 99999-9999" NATIONAL: "(11) 99999-9999" RFC3966: "(11) 99999-9999"
languagestring-'pt'set the language to translate country labels current available languages: 'de', 'el', 'en', 'es', 'fi', 'fr', 'it', 'nb', 'pl', 'pt', 'ru', 'sv', 'vi'
autoFormatbool-trueenables the auto formatting
defaultCountrystring-'BR'set the initial country
inputClassstring-nullset a class css for the number field
selectClassstring-nullset a class css for the select field
containerClassstring-nullset a class css for the container div

Demonstration