Skip to content

Input OTP

⋯

InputOtp renders one native input with separate visual slots. The slots follow the Figma six-cell row: 46px square cells, 12px gaps, and the same semantic border, type, and focus tokens as Input.

import { InputOtp } from "@nesra/ui/input-otp";
export function CodeField() {
return (
<InputOtp aria-label="Verification code" maxLength={6} name="code" required>
<InputOtp.Group>
{Array.from({ length: 6 }, (_, index) => (
<InputOtp.Slot key={index} index={index} />
))}
</InputOtp.Group>
</InputOtp>
);
}

InputOtp is the only component export from @nesra/ui/input-otp and @nesra/ui. Use its parts through InputOtp.Group, InputOtp.Slot, and InputOtp.Separator. The matching prop types (InputOtpProps, InputOtpGroupProps, InputOtpSlotProps, and InputOtpSeparatorProps) are available from either entrypoint for typed wrappers.

InputOtp
└─ InputOtp.Group
├─ InputOtp.Slot index={0}
├─ InputOtp.Slot index={1}
└─ …

maxLength is the number of characters. Render one slot for each zero-based index. Groups and separators change the visual arrangement without adding form fields.

Click a cell to inspect the focus ring. aria-invalid gives every cell the destructive border and ring, including the active cell. The caret still marks the active cell. disabled removes the input from keyboard navigation and form submission and renders digits in text-disabled. Colors follow the field state colors in Light and Dark.

Partially filled
Complete
Error
Disabled

onChange receives the complete string. onComplete fires when it first reaches maxLength.

0 of 6 digits entered

Set pattern={REGEXP_ONLY_DIGITS_AND_CHARS} and inputMode="text" to accept letters and digits. Import the pattern constant from @nesra/ui/input-otp.

The form reads one code value from FormData. It checks for six digits on submit, shows an associated error for an incomplete code, and clears that error on the next edit. Paste accepts codes with spaces or hyphens. This demo stops at local validation; the application must verify the code with its server.

Enter the six digits from your message.

  • The single native input has one tab stop and accepts typing, selection, paste, and SMS autofill. Use aria-label or associate a visible <label> with its id.
  • name, required, disabled, and other native input attributes reach the real input. Its ref points to that input. The visual slots are hidden from screen readers.
  • The default pattern accepts ASCII digits. Set pattern and inputMode="text" for other code formats, including alphanumeric codes. Use pasteTransformer when copied codes include separators. Invalid keystrokes and untransformed invalid pastes are rejected.
  • The consuming form owns validation timing and error text. Set aria-invalid and an associated aria-describedby on the root when showing an error. maxLength limits entry but does not by itself reject a partial code at form submission.
Prop Type Default Effect
maxLength number required Number of characters in the code.
value string — Controlled value.
defaultValue string — Initial uncontrolled value.
onChange (value: string) => unknown — Receives the new code after an edit.
onComplete (value: string) => unknown — Fires when the code transitions to full length.
pattern string | RegExp digits only Validates each new code.
pasteTransformer (value: string) => string — Transforms pasted text before validation.
containerClassName string — Adds classes to the visible layout container.

InputOtp inherits the input-otp root and native input props. className applies to the real input. The ref points to that input. REGEXP_ONLY_DIGITS_AND_CHARS is exported from both @nesra/ui/input-otp and @nesra/ui for alphanumeric codes.

Accepts native div props and a ref. It arranges adjacent slots with a 12px gap.

Prop Type Default Effect
index number required Zero-based character position.

Accepts other native div props and a ref. It renders the character or an optional placeholder. Empty slots stay blank until focused, when they show the caret.

Accepts native div props, children, and a ref. It renders a minus icon by default.

Visual values come from the color roles, typography roles, layout scale, and shape and elevation tokens.