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 playgroundThe 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
npm i @ashutoshswamy/smouiUsage
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.
numberdefault6- 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.
booleandefaultfalse- 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
More inputs components
- AddressAutocompleteType a street and pick from live suggestions. Lookups wait for a pause, show a spinner and drop stale answers. Picking confirms the full address underneath.
- AvatarPickerPick an avatar from a grid. One accent ring glides to your choice, the big preview swaps on a spring, arrow keys move across the grid and Shuffle rolls a random one.
- AvatarUploadDrop or choose a photo, then frame it under a crop circle. The stage shows the whole photo, the circle shows what you keep, and three live previews follow every move. Drag, zoom or use the arrow keys.
- CheckboxBox squashes on press, fills with the accent and draws its tick in.
- ChipA selectable pill. Selecting it tints it and slides a tick in at the front while the pill grows; an optional close button removes it.
- ColorPickerSaturation field and hue strip with jelly thumbs on springs. Edit the hex, pick a swatch, or use arrow keys.