# PinInput

> 按固定长度填写验证码、访问码等短字符串。

网页来源：/components/pin-input

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

## 基本用法

`v-model` 保存完整字符串，`length` 设置格数，默认 4。数字输入会自动前进，支持方向键移动与退格修改；`valueComplete` 在从未填满变为填满时通知调用方。完成输入后保留焦点，提交由表单或按钮处理。

### 示例：pin-input/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { computed, ref } from 'vue';
import { QuireButton, QuireField, QuirePinInput } from '@quirekit/ui/vue';

const code = ref('');
const confirmed = ref('');
const complete = computed(() => code.value.length === 6);

/** Record this local example's explicit confirmation. */
function confirm(): void { if (complete.value) confirmed.value = code.value; }
</script>

<template>
  <div class="flex flex-col items-start gap-qk-surface p-qk-surface" data-pin-basic>
    <QuireField v-slot="{ controlProps }" label="验证码" description="填写 6 位数字，也可以直接粘贴。" width="100%">
      <QuirePinInput v-bind="controlProps" v-model="code" :length="6" label="验证码" otp />
    </QuireField>
    <div class="flex flex-wrap items-center gap-qk-inline">
      <QuireButton color="primary" variant="solid" :disabled="!complete" @click="confirm">确认验证码</QuireButton>
      <QuireButton variant="ghost" @click="code = ''; confirmed = ''">重新填写</QuireButton>
    </div>
    <p class="m-0 text-qk-helper text-qk-text-secondary" role="status">{{ confirmed ? `已确认：${confirmed}` : complete ? '已填满，可以确认。' : `已填写 ${code.length} / 6 位` }}</p>
  </div>
</template>
```

## 字符类型与粘贴

`type` 支持 `numeric`、`alphabetic`、`alphanumeric`。可直接粘贴整段代码，超出的字符会截断；需要接受带空格或连字符的代码时，用 `sanitizeValue` 先处理粘贴内容。`otp` 开启验证码自动填写，`mask` 隐藏显示字符。

### 示例：pin-input/modes

文件：`modes.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireField, QuirePinInput } from '@quirekit/ui/vue';

const invitation = ref('');
const rejected = ref(false);

/** Accept readable separators while preserving the code's character case.
 * @param value - Clipboard text provided to the component.
 * @returns The unseparated code for normal validation.
 */
function normalizeCode(value: string): string { return value.replace(/[\s-]/g, ''); }
</script>

<template>
  <div class="flex flex-col gap-qk-surface p-qk-surface" data-pin-modes>
    <QuireField v-slot="{ controlProps }" label="邀请代码" width="100%" :invalid="rejected"
      description="试试粘贴 AB-12 CD；接受字母和数字。" error-text="请只填写字母和数字。">
      <QuirePinInput v-bind="controlProps" v-model="invitation" label="邀请代码" type="alphanumeric" :length="6"
        :sanitize-value="normalizeCode" @value-invalid="rejected = true" @value-change="rejected = false" />
    </QuireField>
    <QuireField v-slot="{ controlProps }" label="字母代码" width="100%" description="保留输入的大小写。">
      <QuirePinInput v-bind="controlProps" label="字母代码" type="alphabetic" default-value="Ab" />
    </QuireField>
    <QuireField v-slot="{ controlProps }" label="隐藏访问码" width="100%" description="隐藏字符仅影响显示。">
      <QuirePinInput v-bind="controlProps" label="隐藏访问码" mask default-value="2468" />
    </QuireField>
  </div>
</template>
```

## 尺寸

`size` 使用与 Input、Button 相同的五档控件尺寸，默认 `sm`。每一格保持正方形；长代码超过容器宽度时在组内横向滚动。

### 示例：pin-input/sizes

文件：`sizes.vue`

```vue
<script setup lang="ts">
import { QuirePinInput } from '@quirekit/ui/vue';

const sizes = ['xs', 'sm', 'md', 'lg', 'xl'] as const;
</script>

<template>
  <div class="flex flex-col gap-qk-surface p-qk-surface" data-pin-sizes>
    <div v-for="size in sizes" :key="size" class="flex flex-wrap items-center gap-qk-inline">
      <span class="w-8 text-qk-helper text-qk-text-secondary">{{ size }}</span>
      <QuirePinInput :size="size" :label="`${size} 尺寸代码`" default-value="12" />
    </div>
  </div>
</template>
```

## 状态与字段反馈

`disabled` 禁止操作，`readOnly` 保留聚焦和读取；`invalid` 与 Field 的错误信息共同表达校验结果。把 Field 的 `controlProps` 绑定到 PinInput，可将标签、说明和错误关联到各个输入格。

### 示例：pin-input/states

文件：`states.vue`

```vue
<script setup lang="ts">
import { QuireField, QuirePinInput } from '@quirekit/ui/vue';
</script>

<template>
  <div class="flex flex-col gap-qk-surface p-qk-surface" data-pin-states>
    <QuireField v-slot="{ controlProps }" label="尚不可填写" disabled disabled-reason="先完成上一项信息。" width="100%">
      <QuirePinInput v-bind="controlProps" label="尚不可填写" default-value="12" />
    </QuireField>
    <QuireField v-slot="{ controlProps }" label="已保存的代码" read-only description="可以聚焦查看，不能修改。" width="100%">
      <QuirePinInput v-bind="controlProps" label="已保存的代码" default-value="2468" />
    </QuireField>
    <QuireField v-slot="{ controlProps }" label="代码已失效" invalid error-text="请获取新代码后重新填写。" width="100%">
      <QuirePinInput v-bind="controlProps" label="代码已失效" default-value="1357" />
    </QuireField>
  </div>
</template>
```

## 表单校验与重置

FormField 通过同一个字符串模型管理校验和重置。按实际长度提供校验规则；填写完成只表示输入格式完整，业务是否接受由 `onSubmit` 决定。下面的示例仅在当前页面保存提交结果。

### 示例：pin-input/form

文件：`form.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireButton, QuireForm, QuireFormField, QuirePinInput } from '@quirekit/ui/vue';
import type { QuireKitFormRule, QuireKitFormValues } from '@quirekit/ui/vue';

const saved = ref('');
const initialValues = { code: '' };

/** Require a complete six-digit code before this local save.
 * @param value - The form-owned draft.
 * @returns Acceptance or a recovery message.
 */
const codeRule: QuireKitFormRule = value => /^[0-9]{6}$/.test(String(value ?? '')) || '请填写完整的 6 位验证码。';

/** Keep this example's saved result on the current page.
 * @param values - The form's validated snapshot.
 */
function save(values: QuireKitFormValues): void { saved.value = String(values.code); }
</script>

<template>
  <div class="flex flex-col gap-qk-surface p-qk-surface" data-pin-form>
    <QuireForm :initial-values="initialValues" :on-submit="save" aria-label="验证码提交示例">
      <QuireFormField v-slot="{ controlProps, value }" name="code" label="提交验证码"
        required required-message="请填写验证码。" :rules="codeRule" description="此示例只保存到当前页面。">
        <QuirePinInput v-bind="controlProps" :model-value="String(value ?? '')" label="提交验证码" :length="6" otp />
      </QuireFormField>
      <template #actions="{ resetForm }">
        <QuireButton type="submit" variant="solid" color="primary">保存验证码</QuireButton>
        <QuireButton variant="outline" @click="resetForm(); saved = ''">重置表单</QuireButton>
      </template>
    </QuireForm>
    <p role="status" class="m-0 text-qk-helper text-qk-text-secondary">{{ saved ? `本页已保存：${saved}` : '尚未提交' }}</p>
  </div>
</template>
```

原生表单使用 `name` 提交一个字符串；`required` 要求填满，`invalid` 事件可显示恢复说明。`defaultValue` 是原生重置基线，禁用字段不进入 FormData。

### 示例：pin-input/native-form

文件：`native-form.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireButton, QuireField, QuirePinInput } from '@quirekit/ui/vue';

const code = ref('12');
const invalid = ref(false);
const submitted = ref('');

/** Read the actual serialized value after native validation succeeds.
 * @param event - The submitted native form.
 */
function submit(event: Event): void {
  submitted.value = String(new FormData(event.currentTarget as HTMLFormElement).get('accessCode'));
}
</script>

<template>
  <form class="flex flex-col items-start gap-qk-surface p-qk-surface" data-pin-native aria-label="原生访问码表单" @submit.prevent="submit">
    <QuireField v-slot="{ controlProps }" label="访问码" required :invalid="invalid" error-text="请补全 4 位访问码。" width="100%">
      <QuirePinInput v-bind="controlProps" v-model="code" name="accessCode" label="访问码" default-value="12"
        @invalid="invalid = true" @value-change="invalid = false" />
    </QuireField>
    <div class="flex flex-wrap gap-qk-inline">
      <QuireButton type="submit" variant="solid" color="primary">提交访问码</QuireButton>
      <QuireButton type="reset" variant="outline" @click="invalid = false; submitted = ''">恢复初始代码</QuireButton>
    </div>
    <p role="status" class="m-0 text-qk-helper text-qk-text-secondary">{{ submitted ? `FormData：${submitted}` : '等待提交' }}</p>
  </form>
</template>
```

## API

### QuirePinInput

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| modelValue | 否 | — | Controlled code; its characters map to successive inputs. |
| defaultValue | 否 | "" | Initial code and native form-reset baseline. |
| length | 否 | 4 | Positive number of cells; invalid values fall back to four. |
| type | 否 | "numeric" | Accepted character family. |
| label | 否 | — | Accessible field name used in each cell's positional label; defaults to the language pack's code name. |
| id | 否 | — | Native id of the first visible input, for Field and external labels. |
| name | 否 | — | Native form field name; submits one string. |
| form | 否 | — | Optional id of the owning native form. |
| size | 否 | "sm" | Shared control height and typography; cells remain square. |
| disabled | 否 | undefined | Disable focus and changes. |
| readOnly | 否 | undefined | Preserve focus and reading while preventing changes. |
| required | 否 | undefined | Require a complete code during native form submission. |
| invalid | 否 | undefined | Show an externally determined validation error. |
| otp | 否 | false | Request the browser's one-time-code autocomplete. |
| mask | 否 | false | Visually mask characters without changing the model. |
| placeholder | 否 | "" | Placeholder shown in each unfocused empty cell. |
| sanitizeValue | 否 | — | Normalize pasted text before Ark validates and distributes it. |

**modelValue**

```ts
string | undefined
```

```ts
undefined | string
```

**defaultValue**

```ts
string | undefined
```

```ts
undefined | string
```

**length**

```ts
number | undefined
```

```ts
undefined | number
```

**type**

```ts
QuireKitPinInputType | undefined
```

```ts
undefined | "numeric" | "alphabetic" | "alphanumeric"
```

**label**

```ts
string | undefined
```

```ts
undefined | string
```

**id**

```ts
string | undefined
```

```ts
undefined | string
```

**name**

```ts
string | undefined
```

```ts
undefined | string
```

**form**

```ts
string | undefined
```

```ts
undefined | string
```

**size**

```ts
QuireKitControlSize | undefined
```

```ts
undefined | "sm" | "xs" | "md" | "lg" | "xl"
```

**disabled**

```ts
boolean | undefined
```

**readOnly**

```ts
boolean | undefined
```

**required**

```ts
boolean | undefined
```

**invalid**

```ts
boolean | undefined
```

**otp**

```ts
boolean | undefined
```

**mask**

```ts
boolean | undefined
```

**placeholder**

```ts
string | undefined
```

```ts
undefined | string
```

**sanitizeValue**

```ts
((value: string) => string) | undefined
```

```ts
undefined | { (value: string): string; }
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| invalid | 否 | — |  |
| update\:modelValue | 否 | — |  |
| valueChange | 否 | — |  |
| valueComplete | 否 | — |  |
| valueInvalid | 否 | — |  |

**invalid**

```ts
[event: Event]
```

**update:modelValue**

```ts
[value: string]
```

**valueChange**

```ts
[value: string]
```

**valueComplete**

```ts
[value: string]
```

**valueInvalid**

```ts
[details: QuireKitPinInputValueInvalidDetails]
```

#### Slots

无。

#### 公开实例成员

无。
