Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

✔️ OTP Input Component for React

License

NotificationsYou must be signed in to change notification settings

devfolioco/react-otp-input

Repository files navigation

npm versionnpmnpmAll 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 v2here

Basic usage:

importReact,{useState}from'react';importOtpInputfrom'react-otp-input';exportdefaultfunctionApp(){const[otp,setOtp]=useState('');return(<OtpInputvalue={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
TypeRequiredDefaultDescription
numInputsnumbertrue4Number of OTP inputs to be rendered.
renderInputfunctiontruenoneA function that returns the input that is supposed to be rendered for each of the input fields. The function will get two arguments:inputProps andindex.inputProps is an object that contains all the propsthat 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.
onChangefunctiontrueconsole.logReturns OTP code typed in inputs.
onPastefunctionfalsenoneProvide a custom onPaste event handler scoped to the OTP inputs container. Executes when content is pasted into any OTP field.

Example:
const handlePaste: React.ClipboardEventHandler = (event) => {  const data = event.clipboardData.getData('text');  console.log(data)};
valuestring / numbertrue''The value of the OTP passed into the component.
placeholderstringfalsenoneSpecify an expected value of each input. The length of this string should be equal tonumInputs.
renderSeparatorcomponent / function
falsenoneProvide a custom separator between inputs by passing a component. For instance,<span>-</span> would add- between each input.
containerStylestyle (object) / className (string)falsenoneStyle applied or class passed to container of inputs.
inputStylestyle (object) / className (string)falsenoneStyle applied or class passed to each input.
inputType<input> typefalsetextThe type of the input that will be passed to the input element being rendered. In v2isInputNum used to set the input type astel and prevented non numerical entries, so as to avoid the spin buttons added to the inputs with input typenumber. That behaviour is still supported if you passtel to the inputType prop.
shouldAutoFocusbooleanfalsefalseAuto focuses input on initial page load.
skipDefaultStylesbooleanfalsefalseThe component comes with default styles for legacy reasons. Passtrue to skip those styles. This prop will be removed in the next major release.

⚠️ Warning

Do not override the following props on the input component that you return from therenderInput prop. Doing so might lead to unexpected behaviour.

  • ref
  • value
  • onChange
  • onFocus
  • onBlur
  • onKeyDown
  • onPaste
  • onInput
  • type
  • inputMode

Migrating from v2

The v3 ofreact-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 viarenderInput prop. This gives you the flexibility to customize the input fields as you desire. This also means that props likefocusStyle,isDisabled,disabledStyle,hasErrored,errorStyle,isInputNum,isInputSecure,data-cy anddata-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 therenderInput prop.

  • Theseparator prop has now been renamed torenderSeparator. 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 calledinputType 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 isnumber.

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. Forv1.0.0 and above, avalue prop needs to be passed in the component for it to function as expected.

Development

To run the vite example:

cd examplenpm run dev

Checklist

PRs WelcomeOpen Source Love

  • Write tests
  • Add actions for lint checks and tests

Contributing

GitHub issuesGitHub pull requests

Feel free to openissues andpull 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

💻🚧🤔📖🎨
Süleyman Barış Eser
Süleyman Barış Eser

🐛
Steve Domino
Steve Domino

💻
Stephen Hogan
Stephen Hogan

💻

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


[8]ページ先頭

©2009-2025 Movatter.jp