Giter Club home page Giter Club logo

react-phone-input-auto-format's Introduction

React Phone Input Auto Format

This is an auto-formatting phone input component.

If an Input component is passed as the inputComponent prop, it will be rendered instead of the default html input which allows easy integration with Styled Components

Also exposes utility functions to normalize and format the phone number.

Pros:

  • United States only (if you are only dealing with US numbers)
  • Easily integrates with styled components

Phone Input Video

If you wish to use the styling displayed above, check out the demo source

Installation

npm install react-phone-input-auto-format --save

Usage

Basic

import React from "react";
import PhoneInput from "react-phone-input-auto-format";

const onChange = phoneNumber => {
  // do something with the phone number
};
const Form = () => {
  return <PhoneInput onChange={onChange} />;
};

export default Form;

With Styled Component

Also see the demo

import React from "react";
import PhoneInput from "react-phone-input-auto-format";
import styled from "styled-components";

const Input = styled.input`
  border: 1px solid blue;
  font-size: 2em;
`;
const onChange = phoneNumber => {
  // do something with phone number
};
const Form = () => {
  return <PhoneInput onChange={onChange} inputComponent={Input} />;
};
export default Form;

With Utility Functions

Also see the demo and demo source

import React from "react";
import PhoneInput, { format, normalize } from "react-phone-input-auto-format";

const onChange = phoneNumber => {
  const formatted = format(phoneNumber); // (123) 456-7890
  const normalized = normalize(phoneNumber); // 1234567890

  // do something with the formatted or normalized number
};

const Input = () => {
  return <PhoneInput onChange={onChange} />;
};

export default Input;

Props

name type description
onChange function Returns the value of the input field
value string Can set a default and / or custom value if desired
inputComponent React Component Will be used as the input if given, otherwise will use a default HTML input

Other properties (not documented) are applied to the root element.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.