Giter VIP home page Giter VIP logo

react-phonenr-input's Introduction

React-PhoneNr-Input

NPM license npm

Overview

React-PhoneNr-Input is a simple to use phonenumber input field with country selection, that by default, intuitively guesses the country for- and formats the entered phonenumber

For International phonenumbers a dropdown menu is available to select ya country from.

By passing the prop format='NATIONAL' and a default country e.g. defaultCountry='DE' a simple input field is shown that formats the entered phonenumber with the national format declared by the defaultCountry prop.

All written with less than 300 lines of code

Demo and Examples here

Installation

npm:

npm i -S react-phonenr-input

yarn:

yarn add react-phonenr-input

Props:

Name Type Default Description
onChange (data: PhoneNumber) => void required The function/method that returns the entered phonenumber or phonenumber object
withCountryMeta boolean false changes the retuned value into an Object that contains the phonenumber and the country information. eg.:
        {
          phoneNumber: "+49 176 12345678",
          country: {
            name: "Germany (Deutschland)"
            iso2: "DE"
          }
        }
      
className string undefined Adds a custom class to the Phonenumber Input Field
defaultCountry IsoCode undefined Sets the default country (use iso alpha-2 country code e.g 'US', 'GB', 'DE')
disabled boolean false Disables the Phonenumber Input Field
format NumberFormat 'INTERNATIONAL' One of: 'INTERNATIONAL', 'NATIONAL'. Sets the format of the entered phonenumber, in case of 'NATIONAL' the defaultCountry must be set
initialValue string undefined Sets the initial Value of the Phonenumber Input. This is usefull in case you need to set a phonenumber stored for example in a database
placeholder string undefined Sets the Placeholder text
preferredCountries IsoCode[] undefined Lets you restrict the country dropdown to a specific list of countries (use iso alpha-2 country code e.g 'US', 'GB', 'DE')
regions "asia" | "europe" | "africa" | "north-africa" | "oceania" | "america" | "carribean" | "south-america" | "ex-ussr" | "european-union" | "middle-east" | "central-america" | "north-america" | Region[] undefined Lets you restrict the country dropdown to a list of countries in the specified regions
In addition to the here listed Props you can pass all other properties that can be used on a normal Html input field

Code example:

import React, { useState } from 'react'
import { PhoneInput, PhoneNumber } from 'react-phonenr-input';

const Example = () => {
  const [value, setValue] = useState<PhoneNumber>('')

  const handleChange = (phoneNumber: PhoneNumber) => {
    // Do something with the phoneNumber
    setValue(phoneNumber)
  }

  return (
    <div>
      <PhoneInput onChange={handleChange}/>
    </div>
  )
}

Optimized for Mobile usage

mobile

Support

If you like the project and want to support my work, you can buy me a coffee :)

paypal

react-phonenr-input's People

Contributors

dependabot[bot] avatar kaihotz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-phonenr-input's Issues

Seperate country-code from phone input

I just tried the demo and autocomplete with tools such as dashlane will remove the countrycode.
Maybe seperate the country code into a locked input so this cannot happen?

Start with an initial value.

I am trying to pass an initial phone number to the component but it seems that it cannot receive an initial value.

I want to edit a phone number that was already in my database using this component but I cannot manage to do that.

thanks for any help or hint

Country flag is not defaulting if we pass defaultCountry="IN" (India)

For all other countries, it works.
ISO2 code of country India is IN.

Possible reason

In allCountries.ts, we have 2 entries, one with iso2: 'INTL' and iso2: 'IN'.
export const allCountries: ICountry[] = [ { name: '', regions: [], iso2: 'INTL', dialCode: '', isAreaCode: false, hasAreaCodes: false, }, ... ... { name: 'India (भारत)', regions: ['asia'], iso2: 'IN', dialCode: '+91', hasAreaCodes: false, isAreaCode: false, },

In countries-fn.ts, we have
export const findCountryBy: FindCountryBy = (identifyer: Identifyer, item: IsoCode | string) => allCountries.find((country) => country[identifyer] === item || country[identifyer].startsWith(item)) || allCountries[0];

When findCountryBy is called with ('iso2', 'IN'), due to the condition country[identifyer].startsWith(item), it returns entry with iso2 : 'INTL'.

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.