106k
New

输入 OTP

具有复制粘贴功能的无障碍一次性密码组件。

1
2
3
4
5
6
import {
  InputOTP,
  InputOTPGroup,

关于

Input OTP 是基于 input-otp@guilherme_rodz 构建的。

安装

pnpm dlx shadcn@latest add input-otp

使用方法

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>

模式

使用 pattern 属性为 OTP 输入定义自定义的模式。

import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp"
 
;<InputOTP maxLength={6} pattern={REGEXP_ONLY_DIGITS_AND_CHARS}>
  ...
</InputOTP>
"use client"

import { Field, FieldLabel } from "@/components/ui/field"

示例

分隔符

使用 <InputOTPSeparator /> 组件在输入组之间添加分隔符。

import {
  InputOTP,
  InputOTPGroup,

禁用

使用 disabled 属性禁用输入。

1
2
3
4
5
6
import { Field, FieldLabel } from "@/components/ui/field"
import {
  InputOTP,

受控

使用 valueonChange 属性控制输入值。

Enter your one-time password.
"use client"

import * as React from "react"

无效

在插槽上使用 aria-invalid 显示错误状态。

0
0
0
0
0
0
"use client"

import * as React from "react"

四位数字

PIN 码的常见模式。这里使用了 pattern={REGEXP_ONLY_DIGITS} 属性。

"use client"

import {

字母数字混合

使用 REGEXP_ONLY_DIGITS_AND_CHARS 以接受字母和数字。

"use client"

import {

表单

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
import { Button } from "@/components/ui/button"
import {
  Card,

API 参考

更多信息请参阅 input-otp 文档。