A totally rad React component for one-time passwords!
Best viewed with: or
Version 3.2.1 • Public • Published 3 years ago
React Auth Code Input - One-time password (OTP) React component, zero dependencies, fully tested.
Demo
npm install --save react-auth-code-input
or yarn add react-auth-code-input
import React, { useState } from 'react';
import AuthCode from 'react-auth-code-input';
const App = () => {
const [result, setResult] = useState();
const handleOnChange = (res: string) => {
setResult(res);
};
return <AuthCode onChange={handleOnChange} />;
};
By default you can type numbers and letters in the inputs as the allowedCharacters prop is defaulted to alphanumeric but you can also choose between allowing only letters or only numbers by setting the prop to alpha or numeric respectively.
import React, { useState } from 'react';
import AuthCode from 'react-auth-code-input';
const App = () => {
const [result, setResult] = useState();
const handleOnChange = (res: string) => {
setResult(res);
};
return <AuthCode allowedCharacters='numeric' onChange={handleOnChange} />;
};
By default the first input is focused when the component is mounted, you can opt-out from this by setting the autoFocus prop to false, and then you can handle the focus manually by passing a reference.
import React, { useRef, useState } from 'react';
import AuthCode, { AuthCodeRef } from 'react-auth-code-input';
const App = () => {
const AuthInputRef = useRef<AuthCodeRef>(null);
const [result, setResult] = useState();
const handleOnChange = (res: string) => {
setResult(res);
};
return (
<>
<AuthCode autoFocus={false} onChange={handleOnChange} ref={AuthInputRef} />
<button onClick={() => AuthInputRef.current?.focus()}>Focus</button>
</>
);
};
You can clear all the inputs by passing a reference and then calling the clear method.
import React, { useRef, useState } from 'react';
import AuthCode, { AuthCodeRef } from 'react-auth-code-input';
const App = () => {
const AuthInputRef = useRef<AuthCodeRef>(null);
const [result, setResult] = useState();
const handleOnChange = (res: string) => {
setResult(res);
};
return (
<>
<AuthCode onChange={handleOnChange} ref={AuthInputRef} />
<button onClick={() => AuthInputRef.current?.clear()}>Clear</button>
</>
);
};
This component supports autofill from SMS's received, tested on Safari and Chrome in iOS.
Prop | Type | Description | Default Value | Observations |
---|---|---|---|---|
allowedCharacters | String | Type of allowed characters for your code. | alphanumeric | Valid values: alpha, alphanumeric, numeric |
ariaLabel | String | Accessibility. | ||
autoFocus | Boolean | Wether the first input is focused on mount or not. | true | Since version 3.1.0 |
length | Number | The number of inputs to display. | 6 | |
containerClassName | String | The styles to be applied to the container. | ||
inputClassName | String | The styles to be applied to each input. | ||
onChange | Function(value: String) | Callback function called every time an input value changes. | Required | |
isPassword | Boolean | Whether to display the inputs as passwords or not. | false | |
disabled | Boolean | Makes all the inputs disabled if true. | false | Since version 3.1.1 |
placeholder | String | Displays a placeholder in all the inputs. | Since version 3.2.0 |
License: Licensed under the MIT License, Copyright © 2020-present Luis Guerrerodrac94. See LICENSE for more information.
Readme Keywords: auth, react, 2FA, apple, input
Install: npm i react-auth-code-input
Repository: github.com/drac94/react-auth-code-input
Homepage: github.com/drac94/react-auth-code-input#readme
Downloads:
Version: 3.2.1
License: MIT
Unpacked Size: 43.8 kB
Total Files: 10
Last publish: 3 years ago
Collaborators:
Visitors since 1998!