Giter VIP home page Giter VIP logo

benaubin / react-headless-phone-input Goto Github PK

View Code? Open in Web Editor NEW
39.0 2.0 4.0 1.26 MB

Headless phone number input component for React. Because phone numbers are hard.

Home Page: https://codesandbox.io/s/react-headless-phone-input-demo-ygow2?file=/src/App.js

License: MIT License

JavaScript 41.24% TypeScript 58.76%
react input phone-number phone-number-validation phone-number-input e164 phone-input telephone-number-parser phone-formatting

react-headless-phone-input's Introduction

React Headless Phone Input

A headless phone number input component built for usability.

Phone numbers are hard. Users expect to be able to enter phone numbers in the format they're used to. Here's the problem: most people are used to national - or even local phone number formats. If you offload phone number validation to your backend (or an API), resolving the ambiguity becomes difficult or even impossible.

This component helps you build a UI that gracefully guides your users towards unambiguous phone number formats. And you get the result in standard e164 format: ready for use with any telephony service.

Other libraries are generally heavy (phone number rulesets can be big - 99.1% of this library's footprint is due to libphonenumber-js), force you to use their UI, and can't handle copy & paste or edit-in-place. react-headless-phone-input is designed for usability-first, and lets you bring your own input components. In fact, your existing input fields will almost certainly work with no modifications. Plus, it supports optional lazy-loading with progressive enhancement powered by React Suspense.

Built with React Hooks.

Demo

Install

Install both react-headless-input and libphonenumber-js:

npm i --save react-headless-phone-input libphonenumber-js

or

yarn add react-headless-phone-input libphonenumber-js

Features

  • 100% headless: Bring your own UI. You can use almost any input component you already have
  • Lets users copy & paste phone numbers of any format
  • Typescript support
  • Built-in lazy-loading with progressive enhancement (clocks in at 40KB without lazy-loading)
  • Detects the associated country, enabling international phone input.
  • Lets users copy & paste phone numbers of any format
  • Acts like a normal input: Doesn’t glitch if a user edits in-place or deletes template characters
  • Validates number plausibility
  • External state is standard e164 format

Example

This library is headless: you bring your own UI, but it's almost as easy as using regular inputs.

Here's an example using tiny-flag-react to show the flag associated with the number's country:

import TinyFlagReact from "tiny-flag-react";
import PhoneFormatter from "react-headless-phone-input";
// import PhoneFormatter from "react-headless-phone-input/lazy"; RECOMMENDED

const [e164, setE164] = useState("");

<PhoneFormatter defaultCountry="US" value={e164} onChange={setE164}>
  {({ country, impossible, onBlur, onInputChange, inputValue }) => {
    return (
      <>
        <div style={{ display: "flex", alignItems: "center" }}>
          <span
            style={{
              fontSize: "24px",
            }}>
            {country ? (
              <TinyFlagReact
                country={country}
                alt={country + " flag"}
                fallbackImageURL={`https://cdn.jsdelivr.net/npm/[email protected]/img/SVG/${country}.svg`}
              />
            ) : (
              <></>
            )}
          </span>
          <input
            type="tel"
            value={inputValue}
            onBlur={onBlur}
            onChange={(e) => onInputChange(e.target.value)}
          />
        </div>
        {impossible && (
          <div style={{ color: "red" }}>Impossible phone number</div>
        )}
      </>
    );
  }}
</PhoneFormatter>;

Demo

Performance

Due to this library's dependence on libphonenumber-js, it clocks in at 38.7KB minified + gzipped. To improve your user's experience, react-headless-phone-component supports lazy loading with React Suspense with progressive auto-enachement. If your bundler supports dynamic imports and are using a compatible version of React, just swap react-headless-phone-input for react-headless-phone-input/lazy.

Your UI will render and can be used immediately. Once react-headless-phone-input loads, the component will be automatically upgraded. No other changes are required.

import PhoneFormatter from "react-headless-phone-input/lazy";

License

MIT

react-headless-phone-input's People

Contributors

benaubin avatar dependabot[bot] 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

Watchers

 avatar  avatar

react-headless-phone-input's Issues

Lazy loading issues with Jest when used in create-react-app

As part of a create react app, react-headless-phone-input/lazy works fine on dev server, but when running our test suite, the module is not found.

Jest can find react-headless-phone-input just fine, only /lazy is affected.

This SO answer got me part of the way there: https://stackoverflow.com/a/60025441/12848423

I can monkey patch the node_modules and rename lazy.d.ts to lazy.ts and it seems to work, but obviously that's not a great solution in production.

Any thoughts about workarounds or fixes you might know of dearly appreciated!

Setting a default value

Being able to assign a defaultValue for the field would be great.

How could that be done?

Let's say we have a Redux store that has phone number values already -- how can we pre-populate the input field with that data?

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.