BNA

BNA UI
22

Input OTP

A secure input component for one-time passwords and verification codes.

Installation

pnpm dlx bna-ui add input-otp

Usage

import { InputOTP, InputOTPWithSeparator } from '@/components/ui/input-otp';
<InputOTP
  length={6}
  value={otp}
  onChangeText={setOtp}
  onComplete={(value) => console.log('OTP Complete:', value)}
/>

Examples

Default

Different Lengths

With Separator

Masked Input

Error State

Disabled State

Custom Styling

Without Cursor

API Reference

InputOTP

The main OTP input component for handling one-time passwords and verification codes.

PropTypeDefaultDescription
lengthnumber6Number of OTP digits to display.
valuestring''Current OTP value.
onChangeText(value: string) => void-Called when OTP value changes.
onComplete(value: string) => void-Called when OTP is complete (all digits filled).
errorstring-Error message to display below the input.
disabledbooleanfalseWhether the input is disabled.
maskedbooleanfalseWhether to mask digits with dots for security.
showCursorbooleantrueWhether to show cursor in the active slot.
separatorReactNode-Custom separator component between slots.
containerStyleViewStyle-Additional styles for the container.
slotStyleViewStyle-Additional styles for individual digit slots.
errorStyleTextStyle-Additional styles for the error message.

InputOTPWithSeparator

A preset variant of InputOTP that includes dash separators between digits.

PropTypeDefaultDescription
All props from InputOTP except separator--Inherits all InputOTP props except separator which is preset to a dash.

InputOTPRef

Reference object that provides programmatic control over the InputOTP component.

MethodTypeDescription
focus() => voidFocuses the input.
blur() => voidBlurs the input.
clear() => voidClears all entered digits.
getValue() => stringReturns the current OTP value.

Usage with Ref

import { useRef } from 'react';
import { InputOTP, InputOTPRef } from '@/components/ui/input-otp';
 
export function MyComponent() {
  const otpRef = useRef<InputOTPRef>(null);
 
  const handleClear = () => {
    otpRef.current?.clear();
  };
 
  const handleFocus = () => {
    otpRef.current?.focus();
  };
 
  return (
    <InputOTP
      ref={otpRef}
      length={6}
      onComplete={(value) => {
        console.log('OTP entered:', value);
      }}
    />
  );
}

Accessibility

The InputOTP component is built with accessibility in mind:

  • Uses a hidden TextInput for proper keyboard handling and screen reader support
  • Supports dynamic text sizing for better readability
  • Provides proper focus management between slots
  • Error messages are announced by screen readers
  • Maintains proper contrast ratios for all states
  • Supports keyboard navigation and input

Security Considerations

  • Use the masked prop when dealing with sensitive verification codes
  • Always validate OTP values on the server side
  • Consider implementing rate limiting for OTP attempts
  • Clear sensitive OTP values from memory when no longer needed