ورودی OTP
ورودی کد یکبار مصرف برای احراز هویت دو مرحلهای، تأیید ورود و صفحات پین — اسلاتهای حساس به چسباندن با حاشیههای نئوبروتالیست و سایههای تند.
import {
InputOTP,
InputOTPGroup,Input OTP یک فیلد کد یکبارمصرف segmented است: اسلاتهای بصری روی یک input بومی واحد رندر میشوند تا paste و autofill کد موبایل از جعبه کار کند. هر دو variant بکاند همان کتابخانهٔ input-otp از @guilherme_rodz را میپیچند — primitiveِ Radix یا Base زیرین نیست — و اسلاتها با دستور پخت نئوبروتالیستی استایل شدهاند: حاشیههای ضخیم، سایههای سخت و تایپوگرافی پررنگ.
برای این موارد از آن استفاده کنید:
- احراز هویت دو عاملی — کد 6 رقمی TOTP یا SMS بعد از ورود با رمز.
- تأیید ایمیل و تلفن — تأیید مالکیت هنگام ثبتنام، checkout یا بازیابی حساب.
- ورود PIN — کدهای 4 رقمی برای قفل صفحه یا تأیید پرداخت (نمونهٔ Four Digits را ببینید).
درباره#
Input OTP بر پایهٔ input-otp ساختهٔ @guilherme_rodz ساخته شده است.
نصب#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/input-otp.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/input-otp.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/input-otp.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/input-otp.json
وابستگیهای زیر را نصب کنید:
pnpm add input-otpnpm install input-otpyarn add input-otpbun add input-otp
کد زیر را کپی کرده و در پروژهٔ خود جایگذاری کنید.
"use client"
import * as React from "react"
import { OTPInput, OTPInputContext } from "input-otp"
import { MinusIcon } from "lucide-react"
import { cn } from "@/lib/utils"
function InputOTP({
className,
containerClassName,
...props
}: React.ComponentProps<typeof OTPInput> & {
containerClassName?: string
}) {
return (
<OTPInput
data-slot="input-otp"
containerClassName={cn(
"cn-input-otp flex items-center has-disabled:opacity-50",
containerClassName
)}
spellCheck={false}
className={cn("disabled:cursor-not-allowed", className)}
{...props}
/>
)
}
function InputOTPGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="input-otp-group"
className={cn(
"flex items-center rounded has-aria-invalid:border-destructive",
className
)}
{...props}
/>
)
}
function InputOTPSlot({
index,
className,
...props
}: React.ComponentProps<"div"> & {
index: number
}) {
const inputOTPContext = React.useContext(OTPInputContext)
const { char, hasFakeCaret, isActive } = inputOTPContext?.slots[index] ?? {}
return (
<div
data-slot="input-otp-slot"
data-active={isActive}
className={cn(
"relative flex size-8 items-center justify-center border-y-2 border-r-2 bg-input text-sm shadow-sm transition-all outline-none first:rounded-l first:border-l-2 last:rounded-r aria-invalid:border-destructive data-[active=true]:z-10 data-[active=true]:outline-2 data-[active=true]:outline-primary data-[active=true]:aria-invalid:border-destructive",
className
)}
{...props}
>
{char}
{hasFakeCaret && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" />
</div>
)}
</div>
)
}
function InputOTPSeparator({ ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="input-otp-separator"
className="flex items-center [&_svg:not([class*='size-'])]:size-4"
role="separator"
{...props}
>
<MinusIcon />
</div>
)
}
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator }
مسیرهای import را مطابق با پیکربندی پروژهٔ خود بهروزرسانی کنید.
استفاده#
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
└── InputOTPSlotPattern#
برای تعریف یک الگوی سفارشی برای ورودی OTP از prop pattern استفاده کنید.
import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"
;<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS_AND_CHARS}>
...
</InputOTP>"use client"
import { REGEXP_ONLY_DIGITS } from "input-otp"نمونهها#
جداکننده#
برای افزودن یک جداکننده بین گروههای ورودی از کامپوننت <InputOTPSeparator /> استفاده کنید.
import {
InputOTP,
InputOTPGroup,غیرفعال#
برای غیرفعال کردن ورودی از prop disabled استفاده کنید.
import { Field, FieldLabel } from "@/components/ui/field"
import {
InputOTP,کنترلشده#
برای کنترل مقدار ورودی از propهای value و onChange استفاده کنید.
"use client"
import * as React from "react"نامعتبر#
برای نمایش یک حالت خطا از aria-invalid روی اسلاتها استفاده کنید.
"use client"
import * as React from "react"چهار رقم#
یک الگوی رایج برای کدهای PIN. از prop pattern={REGEXP_ONLY_DIGITS} استفاده میکند.
"use client"
import { REGEXP_ONLY_DIGITS } from "input-otp"حروف و اعداد#
برای پذیرش هم حروف و هم اعداد از REGEXP_ONLY_DIGITS_AND_CHARS استفاده کنید.
"use client"
import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"فرم#
import { RefreshCwIcon } from "lucide-react"
import { Button } from "@/components/ui/button"RTL#
برای فعالسازی پشتیبانی RTL در Neobrutalism، راهنمای پیکربندی RTL را ببینید.
"use client"
import * as React from "react"دسترسیپذیری#
الگوی WAI-ARIA APG برای فیلد OTP وجود ندارد و لازم هم نیست: input-otp یک <input> واقعی با autocomplete="one-time-code" پشت اسلاتهای بصری رندر میکند تا فناوری کمکی یک فیلد متنی واحد اعلام کند و صفحهکلید موبایل بتواند کد ورودی را پیشنهاد دهد. اسلاتها presentationalاند — خود فیلد را برچسب بزنید (FormLabel یا aria-label) و aria-invalid را برای استایل خطا به اسلاتها بدهید.
تعاملات صفحهکلید از رفتار بومی فیلد متنی پیروی میکنند و در variantهای Radix و Base یکساناند:
| کلید | عمل |
|---|---|
Tab / Shift + Tab | ورود / خروج فوکوس به فیلد (یک tab stop) |
ArrowLeft / ArrowRight | حرکت caret بین اسلاتها |
Home / End | پرش به اولین / آخرین اسلات |
Backspace / Delete | حذف نویسهٔ قبلی / بعدی |
Ctrl/Cmd + V | Paste — اگر با pattern جور باشد کد اسلاتها را پر میکند |
مرجع API#
برای اطلاعات بیشتر، مستندات input-otp را ببینید.