OTPInput
"use client";
import { Field, OTPInput } from "@cherry-design-system/react";
export function VerifyCode({ verify }: { verify: (code: string) => void }) {
return (
<Field label="Verification code">
<OTPInput name="code" length={6} separator onComplete={verify} />
</Field>
);
}A one-time code input made of single-character slots. Typing advances, Backspace goes back, arrows move, and pasting (or SMS autofill) fills every slot. Use it for verification and two-factor codes.
Examples
Alphanumeric
"use client";
import { Field, OTPInput } from "@cherry-design-system/react";
export function RecoveryCode() {
return (
<Field label="Recovery code" hint="Letters and numbers, from your backup sheet.">
<OTPInput length={8} mode="alphanumeric" separator />
</Field>
);
}Controlled
"use client";
import { useState } from "react";
import { Field, OTPInput } from "@cherry-design-system/react";
export function Pin() {
const [pin, setPin] = useState("");
return (
<Field label="PIN" error={pin.length === 4 && pin === "0000" ? "Choose a less obvious PIN." : undefined}>
<OTPInput length={4} size="lg" value={pin} onValueChange={setPin} />
</Field>
);
}Translated slot labels
"use client";
import { Field, OTPInput } from "@cherry-design-system/react";
export function Codigo() {
return (
<Field label="Código">
<OTPInput slotLabel={(index, length) => `Carácter ${index + 1} de ${length}`} />
</Field>
);
}Properties
OTPInput
Number of slots. Defaults to 6.
Controlled value.
Initial value when uncontrolled.
Called with the joined code on every change.
Called once every slot is filled.
Accepted characters. Defaults to "numeric".
Show a divider between the two halves. Defaults to false.
Slot size. Defaults to "md".
Error state; set automatically inside a <Field error>.
Disables every slot.
Marks the code as required.
Form field name; the joined code is submitted.
Focus the first slot on mount.
Accessible name per slot. Defaults to "Character {n} of {length}".
Renders a <div> group, forwards its ref to it and passes through native <div> attributes.
OTPSlot
One character cell: a single-character input with a blinking caret while it is focused and empty. Exported for custom layouts; OTPInput renders these for you.
The slot's character ("" when empty).
The ref and native <input> attributes (except size and type) go to the <input>.
Styling
Root class cherry-otp-input with data-size (sm, md, lg) and states data-invalid, data-disabled, data-separator. Slots carry data-filled. Parts: cherry-otp-input__slot, __char, __caret, __separator.
Reads --cherry-bg-canvas, --cherry-border-default, --cherry-border-focus, --cherry-focus-ring and --cherry-danger-fg.
Accessibility
A named group; each slot is labelled "Character n of length" (slotLabel). Inside a Field, the Field label names the group. Typing advances, Backspace goes back, arrows, Home and End move, and paste fills every slot.