🌐 Nodejs.cn

输入 OTP

具有复制粘贴功能的可访问一次性密码组件。

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

关于

🌐 About

输入 OTP 构建在 input-otp 的基础上,由 @guilherme_rodz 开发。

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

安装

🌐 Installation

pnpm dlx shadcn@latest add input-otp

用法

🌐 Usage

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

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

🌐 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>
"use client"

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

示例

🌐 Examples

分隔符

🌐 Separator

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

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

import {
  InputOTP,
  InputOTPGroup,

已禁用

🌐 Disabled

使用 disabled 属性来禁用输入。

🌐 Use the disabled prop to disable the input.

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

受控

🌐 Controlled

使用 valueonChange 属性来控制输入值。

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

Enter your one-time password.
"use client"

import * as React from "react"

无效

🌐 Invalid

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

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

0
0
0
0
0
0
"use client"

import * as React from "react"

四位数字

🌐 Four Digits

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

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

"use client"

import {

字母数字

🌐 Alphanumeric

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

🌐 Use REGEXP_ONLY_DIGITS_AND_CHARS to accept both letters and numbers.

"use client"

import {

表单

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

从右到左

🌐 RTL

要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南

🌐 To enable RTL support in shadcn/ui, see the RTL configuration guide.

1
2
3
4
5
6
"use client"

import * as React from "react"

API参考

🌐 API Reference

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

🌐 See the input-otp documentation for more information.