Skip to content

Forms

OTPInput

tsx
"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

tsx
"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

tsx
"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

tsx
"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

lengthnumber

Number of slots. Defaults to 6.

valuestring

Controlled value.

defaultValuestring

Initial value when uncontrolled.

onValueChange(value: string) => void

Called with the joined code on every change.

onComplete(value: string) => void

Called once every slot is filled.

mode"numeric" | "alphanumeric"

Accepted characters. Defaults to "numeric".

separatorboolean

Show a divider between the two halves. Defaults to false.

size"sm" | "md" | "lg"

Slot size. Defaults to "md".

invalidboolean

Error state; set automatically inside a <Field error>.

disabledboolean

Disables every slot.

requiredboolean

Marks the code as required.

namestring

Form field name; the joined code is submitted.

autoFocusboolean

Focus the first slot on mount.

slotLabel(index: number, length: number) => string

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.

valuestringrequired

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.