OtpInput

One-time code field on a single real input, so SMS autofill, paste and the mobile keyboard just work. A highlight glides between cells, characters pop in, and an error shakes the row. Inputs.

Open playground

The right code is 123456. Try pasting it.

About

OtpInput is a React component in the Inputs category of smoui. It is typed, ships no stylesheet, follows your theme through CSS variables, and honors prefers-reduced-motion. Import it with import { OtpInput } from "@ashutoshswamy/smoui" and set length, value, onChange among its 10 props.

Install

Terminal
npm i @ashutoshswamy/smoui

Usage

TSX
import { OtpInput } from "@ashutoshswamy/smoui";

<OtpInput
  length={6}
  groupSize={3}
  onComplete={(code) => verify(code)}
  error={wrong ? "That code is not right." : false}
/>

Props 10

length

Number of cells.

numberdefault 6
value / defaultValue

Controlled or starting code.

string
onChange

Fires on every change with the code so far.

(value: string) => void
onComplete

Fires once every cell is filled.

(code: string) => void
type

Digits only, or letters and digits (uppercased).

"numeric""alphanumeric"default "numeric"
mask

Show dots instead of characters.

booleandefault false
groupSize

Dash after every n cells, e.g. 3 for 123-456.

number
error

Red cells and a shake. A string is also shown below as the reason.

booleanstring
disabled / autoFocus / name / label

Usual input behaviour. name lets a plain form post the code.

boolean / boolean / string / string
cellClassName

Class on each cell.

string