Files
heroui-vue/apps/docs/components/input-otp.md
T
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
HD-04 scope, against heroui ce90ea3 cn sources:

- stale/fictional content: input.md no longer claims TextField is
  unmigrated; color-field.md no longer claims ColorPicker is private;
  header.md drops the fictional Header.Root subcomponent and the stale
  'menus not migrated' wording (MenuSection label registration is now
  documented from HeaderRoot source); radio-group.md drops three copied
  CheckboxGroup wordings (variant override is Radio's, scalar emits,
  native single-value submit); input-otp.md drops the fictional
  data-complete state and lists the real missing upstream props
- toggle-button disabled row now matches the real CSS hooks
  (:disabled / [aria-disabled], with data-disabled as a marker only)
- part API / CSS classes / interactive states restored per pinned
  sources: button (full class lists + real variable-based global CSS),
  accordion (per-part tables + variant/state lists), card (per-part
  tables + variant list), switch (SwitchGroup snippet + Switch/
  SwitchGroup class lists + states), color-field Group (class list +
  states), input-otp (variant notes, global CSS, class/state lists);
  input-group gains the two upstream usage chapters
- RelatedComponents restored from the pinned componentRelationships
  (first-3, filtered against componentsMap): 64 pages rewritten,
  list-box/slider sections added, 7 empty-mapping pages' fabricated
  entries removed; RelatedShowcases left unmigrated with its real data
  inventory recorded (2 cases, showcase-site cards)
- defaults unified on implementation evidence: granularity (inferred
  day/minute) and visibleYears (full min-max span, no "or 20" fallback);
  HD-03 hand-offs resolved (trigger focus-visible noted for HD-06,
  width guards kept)
- new demos: header-collection-grouping (MenuSection/ListBoxSection
  label wiring), empty-state-collection (collection empty slot);
  empty-state page deduplicated with a boundary compatibility note
- ComponentPreview minHeight/isBgSolid restored per the 30 upstream
  marks; tabs-overflow max-w-full kept after measuring internal scroll
- tests: +3 header registration/demo cases, +1 empty-state collection
  case; full suite 110 files / 1630 tests, typecheck and build:docs
  pass
2026-10-28 22:32:10 +08:00

246 lines
11 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: InputOTP 验证码输入
description: 一次性验证码输入:分格槽位、分组分隔、模式过滤与完成事件。
component: input-otp
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
} from "@heroui-vue/components";
</script>
<template>
<InputOTP :max-length="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>
</template>
```
<ComponentPreview name="input-otp-basic" />
## 组件结构
上游交互来自 React 的 `input-otp` 包(单隐藏 input)。Vue 版以安装版 **Reka PinInput** 为底座(多 input),内部 DOM 与上游不同,但可感知行为、表单值与可访问名称按基线验收。公开值为**字符串**;内部为**按索引的字符数组**,空位保留原索引,后续字符不会左移。
导出 `InputOTP`(即 `InputOTPRoot`)、`InputOTPGroup`、`InputOTPSlot`、`InputOTPSeparator`,以及正则常量 `REGEXP_ONLY_DIGITS`、`REGEXP_ONLY_CHARS`、`REGEXP_ONLY_DIGITS_AND_CHARS`。
### 字符串与字符数组的转换规则
- 外部新字符串按字符下标展开到 `InputOTPSlot :index="i"`;编辑产生内部空位后,公开紧凑字符串不再承载这些空位位置,使用下方回写政策。
- 内部数组长度为 `maxLength`,空槽为 `""`;删除中间槽位时后续字符仍在原索引,UI 不左移。
- 序列化为公开字符串时按索引 `join("")`,空槽在字符串中被省略(字符串紧凑)。槽位 UI 始终读数组,不从紧凑字符串反推,因此中间空位不会把后面的字符拉到前面。
- 父组件回写我们发出的同一字符串时保留内部数组(含中间空位);父组件写入**不同**字符串时按左到右密排展开。
## 示例
### 变体
InputOTP 组件支持两种视觉变体:
- **`primary`**(默认)- 标准样式带阴影,适用于大多数场景
- **`secondary`** - 低强调变体无阴影,适用于 Surface 组件内
<ComponentPreview name="input-otp-variants" />
### Surface 上的次要变体
<ComponentPreview name="input-otp-on-surface" />
### 禁用
<ComponentPreview name="input-otp-disabled" />
### 四位 PIN
<ComponentPreview name="input-otp-four-digits" />
### 受控
<ComponentPreview name="input-otp-controlled" />
`v-model` 为字符串。父级拒绝更新时实际输入会恢复。
### 完成事件
<ComponentPreview name="input-otp-on-complete" />
`@complete` 在**首次填满**时触发一次;替换已满值、重复同值或父级同步同一值不会重复触发。示例使用模拟验证(定时器),不发送真实验证码。
### 表单示例
<ComponentPreview name="input-otp-form-example" />
表单提交一份完整字符串值(`name` 映射)。模拟校验 `123456`;卸载时清理定时器。
### 模式过滤
<ComponentPreview name="input-otp-with-pattern" />
`pattern` 过滤单字符输入,可使用导出的正则常量。
### 表单验证
<ComponentPreview name="input-otp-with-validation" />
### 自定义样式
<ComponentPreview name="input-otp-custom-styles" />
## 自定义样式
`inputClassName` 落在真实 `<input>` 上;视觉槽位用 `class` 落在 `InputOTPSlot` 根节点。二者不会合并到同一节点。
### 全局 CSS
可使用 `@layer components` 指令自定义 InputOTP 组件类;下面的类与状态选择器都来自当前样式表:
```css
@layer components {
.hv-input-otp {
@apply gap-3;
}
.hv-input-otp__slot {
@apply size-12 rounded-xl border-2 font-bold;
}
.hv-input-otp__slot[data-active="true"] {
@apply border-accent ring-2 ring-accent/30;
}
.hv-input-otp__separator {
@apply h-1 w-2 rounded-full bg-border-strong;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
InputOTP 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/input-otp.css)):
#### 基础类 [!toc]
- `.hv-input-otp` - 基础容器
- `.hv-input-otp__group` - slot 组
- `.hv-input-otp__slot` - 单个输入 slot
- `.hv-input-otp__slot-value` - slot 内的字符
- `.hv-input-otp__caret` - 闪烁光标指示器
- `.hv-input-otp__separator` - 组之间的视觉分隔符
- `.hv-input-otp__input` - 真实 `<input>` 的透明覆盖层(Reka PinInput 多 input 底座的适配类,替代上游 `input-otp` 库的 `__container`)
#### 变体类 [!toc]
- `.hv-input-otp--primary` - 标准变体(默认)
- `.hv-input-otp--secondary` - 低强调变体
#### 状态类 [!toc]
- `.hv-input-otp__slot[data-active="true"]` - 当前激活的 slot
- `.hv-input-otp__slot[data-filled="true"]` - 含字符的 slot
- `.hv-input-otp__slot[data-disabled="true"]` - 禁用的 slot
- `.hv-input-otp__slot[data-invalid="true"]` - 无效的 slot
- `.hv-input-otp[data-disabled="true"]` / `[data-invalid="true"]` - 根容器的禁用与错误标记
### 交互状态
组件同时支持 CSS 伪类与 data 属性:
- **Hover**:slot 上 `:hover` 或 `[data-hovered="true"]`
- **Active**:slot 上 `[data-active="true"]`(当前聚焦)
- **Filled**:slot 上 `[data-filled="true"]`(含字符)
- **Disabled**:根与 slot 上 `[data-disabled="true"]`
- **Invalid**:根与 slot 上 `[data-invalid="true"]`(slot 另输出 `[data-readonly]`)
## API 参考
### InputOTP / InputOTPRoot
| Prop | 类型 | 默认值 | 描述 |
| ------------------ | -------------------------- | ----------- | ---------------------------------------------------------------- |
| `modelValue` | `string` | - | 受控字符串值;下标对应槽位 |
| `defaultValue` | `string` | `''` | 非受控初始值 |
| `maxLength` | `number` | 必填 | 槽位数量 / 最大长度 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 外观变体 |
| `isDisabled` | `boolean` | `false` | 禁用 |
| `isInvalid` | `boolean` | `false` | 无效展示状态 |
| `validationErrors` | `string[]` | `[]` | 校验错误消息 |
| `inputClassName` | `string` | - | 附加到真实 input 的类 |
| `name` | `string` | - | 表单字段名(隐藏 input 提交一份完整值) |
| `required` | `boolean` | `false` | 原生必填 |
| `pattern` | `string \| RegExp` | - | 字符过滤正则 |
| `mask` | `boolean` | `false` | 密码样式(Reka `mask`) |
| `otp` | `boolean` | `false` | 一次性验证码自动填充(`autocomplete="one-time-code"`) |
| `placeholder` | `string` | `''` | 空位占位符 |
| `id` | `string` | 自动 | 提交桥 id;真实输入派生为 id-slot-index,内部 Label 自动关联首格 |
事件:`update:modelValue` / `change`(字符串)、`complete`(首次填满)。
### InputOTPGroup / InputOTPSeparator
| Prop | 类型 | 默认值 | 描述 |
| -------- | -------- | ------ | ----------- |
| `class` | `string` | - | 附加 CSS 类 |
| 默认插槽 | - | - | Slot 或内容 |
### InputOTPSlot
| Prop | 类型 | 默认值 | 描述 |
| ------- | -------- | ------ | -------------------- |
| `index` | `number` | 必填 | 槽位索引,从 0 开始 |
| `class` | `string` | - | 附加到视觉槽位根节点 |
### 上游差异
| 上游 React 能力 | Vue 实现 |
| ------------------------------------ | ------------------------------------------------------------------------------------- |
| `input-otp` 单隐藏 input | Reka PinInput 多 input + 隐藏提交 input |
| `value` / `onChange` 字符串 | `v-model` / `update:modelValue` / `change` |
| `onComplete` 跨过长度阈值 | `complete` 仅在首次填满触发 |
| `pattern` 整串校验 | 单字符过滤(同样接受导出常量) |
| `render` / `OTPInputContext` | 具名插槽部件;槽位状态经上下文 |
| `pushPasswordManagerStrategy` | 未提供;`otp` 覆盖 one-time-code 自动填充 |
| `validationDetails`(ValidityState) | 未提供;校验结果为 `string \| string[]` 文本 |
| `textAlign` / `pasteTransformer` | 未提供(能力缺口,见 HD 队列后续能力卡) |
| `autoFocus` / `containerClassName` | 未提供专门 prop;`containerClassName` 被上游公共类型显式 Omit,`autoFocus` 属能力缺口 |
## 字段与表单
- 复用既有 `fieldContextKey`:Label `for` 指向控件 id,Description / FieldError 可关联;`isInvalid` / `validationErrors` 驱动展示。
- 不复制第二套字段、错误或 reset 内核。真实 `FormData` 只有一份完整字符串值;禁用时省略。
- 受控 reset 保留父级值;非受控恢复初始值。取消原生 reset 保留当前值;专项回归断言真实 input 与唯一 FormData 值。
Slot 的 keydown/paste 用户处理器在底座编辑前的 capture 边界协调取消,事件阶段与普通 div 的 bubble 处理存在差异;其他 DOM 事件正常透传。
## 无障碍
- 每个槽位是真实 `<input>`,`autocomplete="one-time-code"`(或 `otp` 时更完整);隐藏 input 供表单提交,`aria-hidden`。
- Label 通过 `for` 关联首格,并通过 `aria-labelledby` 给所有实际输入命名;外置标签使用显式 `aria-labelledby`。底座默认名称不会覆盖字段名称。
- 真实设备上的短信/密码管理器自动填充、IME 与触屏行为**未在本轮验证**,不能按属性存在宣布验收。
- 读屏表现未验证。
## 相关组件
[Input 输入框](./input) · [Form 表单](./form) · [Surface 表面](./surface)