Input OTP

복사/붙여넣기를 지원하는 접근성 있는 일회용 비밀번호 입력 컴포넌트입니다.

1
2
3
4
5
6
examples/krds/input-otp-demo.tsx
import {
  InputOTP,
  InputOTPGroup,

개요

Input OTP is built on top of input-otp by @guilherme_rodz.

설치

pnpm dlx shadcn@latest add https://krdscn.gwlee.kr/r/input-otp.json

사용법

import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from "@/components/ui/input-otp"
<InputOTP maxLength={6}>
  <InputOTPGroup>
    <InputOTPSlot index={0} />
    <InputOTPSlot index={1} />
    <InputOTPSlot index={2} />
  </InputOTPGroup>
  <InputOTPSeparator />
  <InputOTPGroup>
    <InputOTPSlot index={3} />
    <InputOTPSlot index={4} />
    <InputOTPSlot index={5} />
  </InputOTPGroup>
</InputOTP>

구성

다음 조합으로 InputOTP:

InputOTP
├── InputOTPGroup
│   ├── InputOTPSlot
│   ├── InputOTPSlot
│   └── InputOTPSlot
├── InputOTPSeparator
├── InputOTPGroup
│   ├── InputOTPSlot
│   ├── InputOTPSlot
│   └── InputOTPSlot
├── InputOTPSeparator
└── InputOTPGroup
    ├── InputOTPSlot
    └── InputOTPSlot

패턴

Use the pattern prop to define a custom pattern for the OTP input.

import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"

;<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS_AND_CHARS}>
  ...
</InputOTP>
examples/krds/input-otp-pattern.tsx
"use client"

import { REGEXP_ONLY_DIGITS } from "input-otp"

예제

Separator

Use the <InputOTPSeparator /> component to add a separator between input groups.

examples/krds/input-otp-separator.tsx
import {
  InputOTP,
  InputOTPGroup,

Disabled

Use the disabled prop to disable the input.

1
2
3
4
5
6
examples/krds/input-otp-disabled.tsx
import {
  InputOTP,
  InputOTPGroup,

Controlled

Use the value and onChange props to control the input value.

Enter your one-time password.
examples/krds/input-otp-controlled.tsx
"use client"

import * as React from "react"

Invalid

Use aria-invalid on the slots to show an error state.

0
0
0
0
0
0
examples/krds/input-otp-invalid.tsx
"use client"

import * as React from "react"

Four Digits

A common pattern for PIN codes. This uses the pattern={REGEXP_ONLY_DIGITS} prop.

examples/krds/input-otp-four-digits.tsx
"use client"

import { REGEXP_ONLY_DIGITS } from "input-otp"

Alphanumeric

Use REGEXP_ONLY_DIGITS_AND_CHARS to accept both letters and numbers.

examples/krds/input-otp-alphanumeric.tsx
"use client"

import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"

Form

Verify your login
Enter the verification code we sent to your email address: m@example.com.

I no longer have access to this email address.

Having trouble signing in? Contact support
examples/krds/input-otp-form.tsx
import { RefreshCwIcon } from "lucide-react"

import { Button } from "@/components/ui/button"

RTL

RTL 지원을 활성화하려면 RTL 설정 가이드를 참고하세요.

1
2
3
4
5
6
examples/krds/input-otp-rtl.tsx
"use client"

import * as React from "react"

API 참조

input-otp 문서를 참고하세요.