Logobaseui-cn

OTP Field

A one-time password input composed of individual character slots.

Previewformprimitiveinteractive

Preview primitive

This installable wraps the current Base UI OTPFieldPreview API from @base-ui/react/otp-field. The underlying Base UI primitive is still in preview, so expect small API adjustments as it matures.

Loading preview...

Installation

Run the following command to add the component to your project:

npx baseui-cn add otp-field

Usage

import { OTPField, OTPFieldInput, OTPFieldSeparator } from "@/components/ui/otp-field"
<OTPField length={6}>
  <OTPFieldInput />
  <OTPFieldInput aria-label="Character 2 of 6" />
  <OTPFieldInput aria-label="Character 3 of 6" />
  <OTPFieldSeparator aria-hidden="true">-</OTPFieldSeparator>
  <OTPFieldInput aria-label="Character 4 of 6" />
  <OTPFieldInput aria-label="Character 5 of 6" />
  <OTPFieldInput aria-label="Character 6 of 6" />
</OTPField>

Examples

Form integration

Use the OTP field inside Form and Field when you want one named value submitted to your action.

Loading preview...

Auto submit with feedback

Use autoSubmit when the form should verify immediately after the last slot is filled. This example keeps the feedback inside your existing Base UI stack with button loading state and a success toast.

Loading preview...

Alphanumeric codes

Use validationType="alphanumeric" for recovery, invite, or backup codes that mix letters and numbers.

Loading preview...

Grouped layout

Wrap subsets of inputs in your own layout elements and use OTPFieldSeparator when you want a chunked visual format like 123-456.

Loading preview...

Connected slots

Use a zero-gap layout with shared borders when you want the OTP field to read like one combined control.

Loading preview...

Reset and refocus

Use a controlled value when you want to clear the current code and move focus back to the first slot for another attempt.

Loading preview...

API

OTPField

PropTypeDefaultDescription
size"sm" | "default" | "lg""default"Height and width of each input cell; shared with children via context
autoSubmitbooleanfalseAutomatically submit the nearest <form> when all cells are filled

OTPFieldInput and OTPFieldSeparator inherit size from the parent context and accept a size prop to override locally.

API Reference

See the Base UI OTPFieldPreview documentation for the full API reference, including all props, data attributes, and CSS classes.