Coder Social home page Coder Social logo

suleymanbariseser / react-otp-input Goto Github PK

View Code? Open in Web Editor NEW

This project forked from devfolioco/react-otp-input

0.0 1.0 0.0 2.86 MB

:heavy_check_mark: OTP Input Component for React

Home Page: http://devfolioco.github.io/react-otp-input

License: MIT License

JavaScript 2.40% TypeScript 73.71% CSS 20.92% HTML 2.97%

react-otp-input's Introduction

react-otp-input

npm version npm npm All Contributors

A fully customizable, one-time password input component for the web built with React.

see here

Live Demo

Installation

NPM

To install the latest stable version:

npm install --save react-otp-input

Still using v2?

No problem! You can find the documentation for v2 here

Basic usage:

import React, { useState } from 'react';
import OtpInput from 'react-otp-input';

export default function App() {
  const [otp, setOtp] = useState('');

  return (
    <OtpInput
      value={otp}
      onChange={setOtp}
      numInputs={4}
      renderSeparator={<span>-</span>}
      renderInput={(props) => <input {...props} />}
    />
  );
}

API

You can also pass a function that returns a component, where the function will get the index of the separator being rendered as an argument.
Name
Type Required Default Description
numInputs number true 4 Number of OTP inputs to be rendered.
renderInput function true none A function that returns the input that is supposed to be rendered for each of the input fields. The function will get two arguments: inputProps and index. inputProps is an object that contains all the props that should be passed to the input being rendered (Overriding these props is not recommended because it might lead to some unexpected behaviour). index is the index of the input being rendered.
onChange function true console.log Returns OTP code typed in inputs.
value string / number true '' The value of the OTP passed into the component.
placeholder string false none Specify an expected value of each input. The length of this string should be equal to numInputs.
renderSeparator component / function
false none Provide a custom separator between inputs by passing a component. For instance, <span>-</span> would add - between each input.
containerStyle style (object) / className (string) false none Style applied or class passed to container of inputs.
inputStyle style (object) / className (string) false none Style applied or class passed to each input.
inputType <input> type false text The type of the input that will be passed to the input element being rendered. In v2 isInputNum used to set the input type as tel and prevented non numerical entries, so as to avoid the spin buttons added to the inputs with input type number. That behaviour is still supported if you pass tel to the inputType prop.
shouldAutoFocus boolean false false Auto focuses input on initial page load.

Migrating from v2

The v3 of react-otp-input is a complete rewrite of the library. Apart from making the API more customizable and flexible, this version is a complete rewrite of the library using TypeScript and React Hooks. Here are the breaking changes that you need to be aware of:

  • You now need to pass your own custom input component that will be rendered for each of the input fields via renderInput prop. This gives you the flexibility to customize the input fields as you desire. This also means that props like focusStyle, isDisabled, disabledStyle, hasErrored, errorStyle, isInputNum, isInputSecure, data-cy and data-testid are no longer supported. You can achieve the same functionality and more by passing the relevant props directly to the input component that you return from the renderInput prop.

  • The separator prop has now been renamed to renderSeparator. This prop now apart from accepting a component that will be rendered as a separator between inputs like it used to, now also accepts a function that returns a component. The function will get the index of the separator being rendered as an argument.

  • A new prop called inputType has been added to the component. This prop can be used to specify the type of the input that will be passed to the input element being rendered. The default value of this prop is number.

Migrating from v1

react-otp-input is now a controlled component to facilitate functionalities that weren't possible before from the application using it, such as clearing or pre-assigning values. For v1.0.0 and above, a value prop needs to be passed in the component for it to function as expected.

Development

To run the vite example:

cd example
npm run dev

Checklist

PRs Welcome Open Source Love

  • Write tests
  • Add actions for lint checks and tests

Contributing

GitHub issues GitHub pull requests

Feel free to open issues and pull requests!

License

NPM

Contributors โœจ

Thanks goes to these wonderful people (emoji key):

Abhishek Warokar
Abhishek Warokar

๐Ÿ’ป ๐ŸŽจ ๐Ÿšง ๐Ÿค” ๐Ÿ‘€
Aj
Aj

๐Ÿ’ป ๐ŸŽจ ๐Ÿค”
Aromal Anil
Aromal Anil

๐Ÿ’ป ๐Ÿ”ง
Gabriele Corti
Gabriele Corti

๐Ÿ’ป ๏ธ๏ธ๏ธ๏ธโ™ฟ๏ธ
Anoop
Anoop

๐Ÿ’ป
Dewansh Rawat
Dewansh Rawat

๐Ÿ›
Ishan Chhabra
Ishan Chhabra

๐Ÿ’ป
yaojie
yaojie

๐Ÿ’ป
Prateek Surana
Prateek Surana

๐Ÿ’ป ๐Ÿšง ๐Ÿค” ๐Ÿ“– ๐ŸŽจ

This project follows the all-contributors specification. Contributions of any kind welcome!

react-otp-input's People

Contributors

ajayns avatar apollonian avatar dependabot[bot] avatar aromalanil avatar allcontributors[bot] avatar vinitshahdeo avatar ishan-chhabra avatar parikshit-hooda avatar borntofrappe avatar utkarsh22garg avatar binu42 avatar ferdox2 avatar blenderskool avatar nikhilgupta30 avatar ctrlaltdylan avatar dhruvmisra avatar akashnimare avatar causztic avatar nestoralonso avatar mon53r avatar vishalpolley avatar vishalpolley-sureify avatar surajv311 avatar shubhankarkg avatar saisrinivaspaila avatar robintom avatar rishabh510 avatar pratik-276 avatar prateek3255 avatar nirmalhk7 avatar

Watchers

 avatar

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.