OTP Field

A one-time code in separate slots.

Source
$npx @dinachi/cli@latest add otp-field
length
OTPFieldSeparator
validationType
mask
disabled

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>
  );
}

Notes

Slots

Render exactly length OTPFieldInputs, split across any number of groups. Separate groups with OTPFieldSeparator.

API

length
agent
number
Required. Number of slots. Longer codes are refused, for agents too.
valuestring
Controlled value.
defaultValuestring = ""
Starting value when uncontrolled.
onValueChange
agent
(value, details) => void
Runs for people and agents. details.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
agent
"numeric" | "alpha" | "alphanumeric" | "none" = "numeric"
Allowed characters. Agent codes are checked too.
normalizeValue(value: string) => string
Rewrites the value after filtering, such as uppercasing. Must be idempotent.
mask
agent
boolean = false
Hides each character. The agent reads the value as redacted.
autoComplete
agent
string = "one-time-code"
Set on the first slot. While 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
agent
boolean = false
Blocks people and agents.
readOnly
agent
boolean = false
Shows the code, refuses any change.
namestring
Submits the code with a form.
requiredboolean = false
Must be filled before the form submits.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as input.

Reads

label

The root's aria-label or aria-labelledby.

value

The code, or [redacted] for a one-time code or a masked field.

length

How many characters the code takes.

disabled

True 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.