$npx @dinachi/cli@latest add otp-field
lengthOTPFieldSeparatorvalidationTypenumeric
alphanumeric
maskdisabledverification-code.tsx
import { OTPField, OTPFieldGroup, OTPFieldInput } from "@/components/ui/otp-field"
export function VerificationCode() {
return (
<OTPField length={6} aria-label="Verification code">
<OTPFieldGroup>
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</OTPFieldGroup>
</OTPField>
)
}Usage
import { OTPField, OTPFieldGroup, OTPFieldInput } from "@/components/ui/otp-field"
export function VerificationCode() {
return (
<OTPField length={6} aria-label="Verification code">
<OTPFieldGroup>
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</OTPFieldGroup>
</OTPField>
)
}Examples
Six digits that advance as you type and fill on paste.
import { OTPField, OTPFieldGroup, OTPFieldInput } from '@/components/ui/otp-field';
export function Example() {
return (
<OTPField length={6}>
<OTPFieldGroup>
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</OTPFieldGroup>
</OTPField>
);
}-
The code split into two groups.
import { OTPField, OTPFieldGroup, OTPFieldInput, OTPFieldSeparator } from '@/components/ui/otp-field';
export function Example() {
return (
<OTPField length={6}>
<OTPFieldGroup>
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</OTPFieldGroup>
<OTPFieldSeparator>-</OTPFieldSeparator>
<OTPFieldGroup>
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</OTPFieldGroup>
</OTPField>
);
}Hides each character like a password field.
import { OTPField, OTPFieldGroup, OTPFieldInput } from '@/components/ui/otp-field';
export function Example() {
return (
<OTPField length={4} mask>
<OTPFieldGroup>
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</OTPFieldGroup>
</OTPField>
);
}Value: (empty)
Tracks the value and reacts once the code is complete.
import * as React from 'react';
import { OTPField, OTPFieldGroup, OTPFieldInput } from '@/components/ui/otp-field';
export function Example() {
const [value, setValue] = React.useState('');
const [completed, setCompleted] = React.useState<string | null>(null);
return (
<div className="space-y-3">
<OTPField
length={6}
value={value}
onValueChange={(next) => setValue(next)}
onValueComplete={(next) => setCompleted(next)}
>
<OTPFieldGroup>
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</OTPFieldGroup>
</OTPField>
<p className="text-sm text-muted-foreground">Value: {value || '(empty)'}</p>
{completed ? <p className="text-sm text-success">Code complete: {completed}</p> : null}
</div>
);
}Blocks input and keeps the value visible.
import { OTPField, OTPFieldGroup, OTPFieldInput } from '@/components/ui/otp-field';
export function Example() {
return (
<OTPField length={4} defaultValue="1234" disabled>
<OTPFieldGroup>
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</OTPFieldGroup>
</OTPField>
);
}Notes
Slots
Render exactly length OTPFieldInputs, split across any number of groups. Separate groups with OTPFieldSeparator.
API
length
Required. Number of slots. Longer codes are refused, for agents too.agent
numbervaluestring
Controlled value.defaultValuestring = ""
Starting value when uncontrolled.onValueChange
Runs for people and agents. agent
(value, details) => voiddetails.cancel() refuses the change.onValueComplete(value, details) => void
Runs once every slot is filled.onValueInvalid(value, details) => void
Runs when typed or pasted characters are rejected.validationType
Allowed characters. Agent codes are checked too.agent
"numeric" | "alpha" | "alphanumeric" | "none" = "numeric"normalizeValue(value: string) => string
Rewrites the value after filtering, such as uppercasing. Must be idempotent.mask
Hides each character. The agent reads the value as redacted.agent
boolean = falseautoComplete
Set on the first slot. While agent
string = "one-time-code"one-time-code, the agent reads the value as redacted.autoSubmitboolean = false
Submits the owning form once the code is complete.inputModestring
On-screen keyboard. Defaults per validationType.disabled
Blocks people and agents.agent
boolean = falsereadOnly
Shows the code, refuses any change.agent
boolean = falsenamestring
Submits the code with a form.requiredboolean = false
Must be filled before the form submits.Takes the props of the element it renders.
typestring
Overrides the input type for this slot.childrenReactNode
What to show, such as a dash.Agent
Inside an AgentProvider, it appears in agent.snapshot() as input.
Reads
labelThe root's aria-label or aria-labelledby.
valueThe code, or [redacted] for a one-time code or a masked field.
lengthHow many characters the code takes.
disabledTrue when the field is blocked.
Actions
setValue("4321")Fills the code. Spaces are dropped; too long or invalid characters fails.
clear()Empties every slot.
Listed once, not per slot. An agent's change runs through onValueChange; isAgentChange(details) is true for it, and details.cancel() refuses it. The agent is told the app refused. Disabled or readOnly fields refuse on their own.
await agent.act({
steps: [{ ref: "input", action: "setValue", args: ["482913"] }],
})Accessibility
- The root is a group. Name it with aria-label or aria-labelledby; the agent reads it.
- One slot is in the tab order; arrows move between slots.
- The first slot has
autocomplete="one-time-code"so browsers can offer received codes. - Disabled fields show their value but take no input.
Keyboard
←→Moves to the previous or next slot
HomeEndMoves to the first slot, or the first empty one
BackspaceDeletes the character and moves back
DeleteDeletes the character in this slot
CtrlBackspaceClears the whole code. Cmd on macOS
←→Moves to the previous or next slot
HomeEndMoves to the first slot, or the first empty one
BackspaceDeletes the character and moves back
DeleteDeletes the character in this slot
CtrlBackspaceClears the whole code. Cmd on macOS