feat: complete field documentation and TextArea examples

This commit is contained in:
maoyingjie
2026-10-21 23:41:57 +08:00
parent 37e11e6330
commit 116056832c
40 changed files with 2071 additions and 15 deletions
+5
View File
@@ -53,15 +53,20 @@ const groups = [
{ title: "Card 卡片", url: "/components/card" },
{ title: "Chip 标签", url: "/components/chip" },
{ title: "CloseButton 关闭按钮", url: "/components/close-button" },
{ title: "Description 描述", url: "/components/description" },
{ title: "FieldError 字段错误", url: "/components/field-error" },
{ title: "Input 输入框", url: "/components/input" },
{ title: "InputGroup 输入框组", url: "/components/input-group" },
{ title: "Kbd 键盘按键", url: "/components/kbd" },
{ title: "Label 标签", url: "/components/label" },
{ title: "Link 链接", url: "/components/link" },
{ title: "Separator 分隔符", url: "/components/separator" },
{ title: "Skeleton 骨架屏", url: "/components/skeleton" },
{ title: "Spinner 加载指示器", url: "/components/spinner" },
{ title: "Surface 表面", url: "/components/surface" },
{ title: "Switch 开关", url: "/components/switch" },
{ title: "TextArea 多行文本框", url: "/components/textarea" },
{ title: "TextField 文本输入框", url: "/components/text-field" },
],
},
];
+5
View File
@@ -11,12 +11,17 @@ description: 已迁移组件及文档
- [Card 卡片](./components/card)
- [Chip 标签](./components/chip)
- [CloseButton 关闭按钮](./components/close-button)
- [Description 描述](./components/description)
- [FieldError 字段错误](./components/field-error)
- [Input 输入框](./components/input)
- [InputGroup 输入框组](./components/input-group)
- [Kbd 键盘按键](./components/kbd)
- [Label 标签](./components/label)
- [Link 链接](./components/link)
- [Separator 分隔符](./components/separator)
- [Skeleton 骨架屏](./components/skeleton)
- [Spinner 加载指示器](./components/spinner)
- [Surface 表面](./components/surface)
- [Switch 开关](./components/switch)
- [TextArea 多行文本框](./components/textarea)
- [TextField 文本输入框](./components/text-field)
+112
View File
@@ -0,0 +1,112 @@
---
title: Description 描述
description: 为表单字段及其他组件提供补充说明文本
component: description
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Description, Input, Label } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="description-basic" />
## 示例
### 搭配表单字段
```vue
<script setup lang="ts">
import { Description, Input, Label } from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-1">
<Label for="password">密码</Label>
<Input
id="password"
type="password"
aria-describedby="password-description"
/>
<Description id="password-description">
至少 8 个字符,且包含一个大写字母
</Description>
</div>
</template>
```
### 与 TextField 配合
```vue
<script setup lang="ts">
import { TextField, Label, Input, Description } from "@heroui-vue/components";
</script>
<template>
<TextField type="email">
<Label>邮箱</Label>
<Input placeholder="输入你的邮箱" />
<Description>我们不会分享你的邮箱</Description>
</TextField>
</template>
```
使用 [TextField](/components/text-field) 时,`aria-describedby` 会自动合并到控件;支持多个描述与动态 id。
## 自定义样式
### Tailwind CSS
<ComponentPreview name="description-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 Description 组件类。
```css
@layer components {
.hv-description {
@apply text-muted;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
Description 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/description.css)):
#### 基础类 [!toc]
- `.hv-description` - 基础描述样式,使用 `muted` 文本颜色
## API 参考
### Description
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | --------------------------------- |
| `id` | `string` | 自动 | 显式 id;未指定时生成并与控件关联 |
| 默认插槽 | `Component` | - | 描述内容 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
## 无障碍
Description 组件通过以下方式增强无障碍性:
- 使用屏幕阅读器可识别的语义化 HTML(`p`)
- 在 TextField 内自动注册到控件的 `aria-describedby`
- 支持多个描述、外部 `aria-describedby` 合并与卸载清理
任意用户 Vue 包装组件不能被 TextField 静态展开做 SSR 预规划;需要首屏 SSR 关联时请显式设置控件 `aria-describedby` 与描述 id。
## 相关组件
[TextField 文本输入框](./text-field) · [Label 标签](./label) · [FieldError 字段错误](./field-error)
+212
View File
@@ -0,0 +1,212 @@
---
title: FieldError 字段错误
description: 展示表单字段校验错误消息
component: field-error
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { FieldError, TextField, Label, Input } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="field-error-basic" />
FieldError 组件展示表单字段的校验错误消息。当父字段标记为无效时自动显示,并提供平滑的透明度过渡。
## 示例
### 基础校验
```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const value = ref("");
const isInvalid = computed(
() => value.value.length > 0 && value.value.length < 3,
);
</script>
<template>
<TextField v-model="value" class="w-64" :is-invalid="isInvalid">
<Label for="username">用户名</Label>
<Input id="username" placeholder="输入用户名" />
<FieldError>用户名至少需要 3 个字符</FieldError>
</TextField>
</template>
```
### 动态错误信息
使用 **FieldError** 作用域插槽读取当前字段错误。`is-invalid` 只负责展示;阻止提交请使用 `validate` 或 Form 的 `validation-errors`。下面示例用 `validate` + `validation-behavior="aria"`,输入时即可看到动态消息:
```vue
<script setup lang="ts">
import { ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const password = ref("");
function validatePassword(value: string) {
if (value.length === 0) return true;
return value.length < 8 ? "密码至少需要 8 个字符" : true;
}
</script>
<template>
<TextField
v-model="password"
name="password"
validation-behavior="aria"
:validate="validatePassword"
>
<Label>密码</Label>
<Input type="password" placeholder="至少 8 个字符" />
<FieldError>
<template #default="{ validationErrors }">
{{ validationErrors.join(",") }}
</template>
</FieldError>
</TextField>
</template>
```
上游 `FieldError` 的 render 函数 `(validation) => …` 在 Vue 中为 **FieldError** 的作用域插槽 `#default="{ validationErrors, isInvalid }"`。当前未提供完整 React Aria `validationDetails` 对象。
### 自定义校验逻辑
```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const email = ref("");
const isInvalid = computed(
() => email.value.length > 0 && !email.value.includes("@"),
);
</script>
<template>
<TextField v-model="email" :is-invalid="isInvalid">
<Label>邮箱</Label>
<Input type="email" />
<FieldError>邮箱必须包含 @ 符号</FieldError>
</TextField>
</template>
```
也可以用 `validate` 做同步提交拦截:
```vue
<TextField
v-model="email"
:validate="(v) => (v.includes('@') ? true : '邮箱必须包含 @ 符号')"
name="email"
>
<Label>邮箱</Label>
<Input type="email" />
<FieldError />
</TextField>
```
### 多条错误信息
默认插槽展示合并后的错误文本;需要逐条渲染时使用 FieldError 作用域插槽。下面示例的 `validate` 会返回多条错误:
```vue
<script setup lang="ts">
import { ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const password = ref("");
function validatePassword(value: string) {
if (value.length === 0) return true;
const errors: string[] = [];
if (value.length < 8) errors.push("密码至少需要 8 个字符");
if (!/[A-Z]/.test(value)) errors.push("需要包含一个大写字母");
return errors.length ? errors : true;
}
</script>
<template>
<TextField
v-model="password"
name="password"
validation-behavior="aria"
:validate="validatePassword"
>
<Label>密码</Label>
<Input type="password" placeholder="试试 abc" />
<FieldError>
<template #default="{ validationErrors }">
<div v-for="(error, i) in validationErrors" :key="i">{{ error }}</div>
</template>
</FieldError>
</TextField>
</template>
```
## 自定义样式
### Tailwind CSS
<ComponentPreview name="field-error-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 FieldError 组件类。
```css
@layer components {
.hv-field-error {
@apply font-medium text-danger;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
FieldError 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/field-error.css)):
#### 基础类 [!toc]
- `.hv-field-error` - 基础错误样式,danger 色
- 仅在存在 `data-visible` 属性时显示
- 长消息自动换行(`wrap-break-word`)
## API 参考
### FieldError
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ---------------------------------------------------- |
| `id` | `string` | 自动 | 显式 id;未指定时生成并关联到控件 `aria-describedby` |
| 默认插槽 | `Component` | - | 错误消息;作用域提供 `validationErrors`、`isInvalid` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
字段无效时渲染;默认展示错误数组(空格连接)。在 Switch 等 toggle 字段内也可使用。
## 无障碍
FieldError 组件通过以下方式确保无障碍性:
- 使用 `role="alert"` 播报错误
- 语义化 HTML 支持屏幕阅读器
- 提供视觉与程序化错误指示(`aria-describedby` 关联)
- 根据校验状态自动管理可见性
当前不提供完整 React Aria `validationDetails` 对象;第三方契约请勿据此宣称完全兼容。
## 相关组件
[TextField 文本输入框](./text-field) · [Label 标签](./label) · [Description 描述](./description)
+130
View File
@@ -0,0 +1,130 @@
---
title: Label 标签
description: 渲染与表单控件关联的无障碍标签。
component: label
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Label, Input } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="label-basic" />
## 示例
### 带必填指示符
```vue
<script setup lang="ts">
import { Label, Input } from "@heroui-vue/components";
</script>
<template>
<Label for="email" is-required>邮箱地址</Label>
<Input id="email" type="email" />
</template>
```
### 禁用状态
```vue
<script setup lang="ts">
import { Label, Input } from "@heroui-vue/components";
</script>
<template>
<Label for="username" is-disabled>用户名</Label>
<Input id="username" disabled />
</template>
```
### 无效状态
```vue
<script setup lang="ts">
import { Label, Input } from "@heroui-vue/components";
</script>
<template>
<Label for="password" is-invalid>密码</Label>
<Input id="password" type="password" />
</template>
```
在 TextField 内使用时,`is-required` / `is-disabled` / `is-invalid` 会自动继承字段状态,无需重复标注。
## 自定义样式
### Tailwind CSS
<ComponentPreview name="label-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-label {
@apply font-semibold text-foreground;
}
.hv-label--required::after {
@apply text-accent;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
Label 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/label.css)):
#### 基础类 [!toc]
- `.hv-label` — 基础标签文本样式
#### 状态修饰类 [!toc]
- `.hv-label--required` 或 `[data-required="true"] > .hv-label` — 显示必填星号
- `.hv-label--disabled` 或 `[data-disabled="true"] .hv-label` — 禁用状态样式
- `.hv-label--invalid` 或 `[data-invalid="true"] .hv-label` — 无效状态样式(危险/红色文本)
**说明:** 必填星号会基于 role 与 `data-slot` 智能应用,并排除 `role="group"` / `radiogroup` / `checkboxgroup` 以及 `data-slot="radio"` / `checkbox` 的元素,避免分组内重复星号。
## API 参考
### Label
| Prop | 类型 | 默认值 | 描述 |
| ------------ | --------------------------- | ------- | ------------------------------------- |
| `for` | `string` | - | 标签所关联元素的 id(Vue 使用 `for`) |
| `isRequired` | `boolean` | `false` | 是否显示必填指示符 |
| `isDisabled` | `boolean` | `false` | 标签是否处于禁用状态 |
| `isInvalid` | `boolean` | `false` | 标签是否处于无效状态 |
| 默认插槽 | `Component` | - | 标签内容 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
在 TextField 内且未显式传 `for` 时,自动关联当前控件 id。Switch 等 toggle 字段内渲染为非交互 `span`,避免嵌套交互。
## 无障碍
Label 基于原生 HTML `<label>`,并遵循 WAI-ARIA 最佳实践:
- 使用 `for` 与表单控件关联(上游 `htmlFor` 的 Vue 等价)
- 提供语义化的 `<label>` 元素
- 与表单控件关联时支持键盘导航
- 向屏幕阅读器传达必填与无效状态
- 点击标签可聚焦/激活关联的表单控件
## 相关组件
[TextField 文本输入框](./text-field) · [Description 描述](./description) · [FieldError 字段错误](./field-error)
+239
View File
@@ -0,0 +1,239 @@
---
title: TextField 文本输入框
description: 便于组合的文本字段,包含标签、说明与内联校验。
component: textfield
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { TextField, Label, Input } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="textfield-basic" />
## 组件结构
```vue
<script setup lang="ts">
import {
TextField,
Label,
Input,
Description,
FieldError,
} from "@heroui-vue/components";
</script>
<template>
<TextField>
<Label />
<Input />
<Description />
<FieldError />
</TextField>
</template>
```
> **TextField** 将标签、输入、说明与错误信息整合为单个无障碍组件。若只需独立输入,请使用 **[Input](/components/input)** 或 **[TextArea](/components/textarea)**。
## 示例
### 表面样式
在 [Surface](/components/surface) 内使用时,请在 Input 或 TextArea 组件上使用 `variant="secondary"`,以应用适合表面背景的低强调变体。
<ComponentPreview name="textfield-on-surface" />
### 带描述
<ComponentPreview name="textfield-with-description" />
### 必填字段
<ComponentPreview name="textfield-required" />
### 禁用状态
<ComponentPreview name="textfield-disabled" />
### 宽度充满
<ComponentPreview name="textfield-full-width" />
### 表单校验
将 `is-invalid` 与 `FieldError` 配合使用,以展示校验消息。`is-invalid` 只负责错误展示;阻止提交请使用 `validate` 或 Form 的 `validation-errors`。
<ComponentPreview name="textfield-validation" />
### 受控组件
使用 `v-model` 控制值,以同步计数器、预览或格式化逻辑。
<ComponentPreview name="textfield-controlled" />
### 错误信息
<ComponentPreview name="textfield-with-error" />
### 文本域
多行内容请使用 [TextArea](/components/textarea),而非 [Input](/components/input)。
<ComponentPreview name="textfield-textarea" />
### Input 类型
<ComponentPreview name="textfield-input-types" />
### 渲染函数
上游 `render` 自定义根元素;Vue 将 `class`/`data-*` 等透传到 TextField 根节点,暂不支持替换根元素类型。
<ComponentPreview name="textfield-render-function" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="text-field-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-textfield {
@apply flex flex-col gap-1;
}
/* When invalid, the description is hidden automatically */
.hv-textfield[data-invalid="true"] [data-slot="description"],
.hv-textfield[aria-invalid="true"] [data-slot="description"] {
@apply hidden;
}
/* Description has default padding */
.hv-textfield [data-slot="description"] {
@apply px-1;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
- `.hv-textfield` – 根容器,样式极少(`flex flex-col gap-1`)
> **提示:** 子组件([Label](/components/label)、[Input](/components/input)、[TextArea](/components/textarea)、[Description](/components/description)、[FieldError](/components/field-error))各自拥有 CSS 类与样式,定制方式请参见对应文档。
### 交互状态
TextField 会根据状态自动管理以下 data 属性:
- **无效**:`[data-invalid="true"]` — 无效时自动隐藏 description 插槽
- **禁用**:`[data-disabled="true"]` — 在 `is-disabled` 为 true 时应用
- **必填**:`[data-required="true"]`
- **只读**:`[data-readonly="true"]`
## API 参考
### TextField
| Prop | 类型 | 默认值 | 描述 |
| ------------------------------------- | -------------------------------------------------------------------- | -------------------------- | -------------------------------------------------- |
| 默认插槽 | `Component` | - | 子组件(Label、Input、Description、FieldError 等) |
| `modelValue` | `string` | - | 当前值(受控;`v-model`) |
| `defaultValue` | `string` | - | 默认值(非受控) |
| `name` | `string` | - | 用于 HTML 表单提交的 input 名称 |
| `id` | `string` | - | 控件 id;子控件显式 id 优先 |
| `type` | `string` | - | 下发到 Input 的 type |
| `isDisabled` | `boolean` | `false` | 是否禁用输入 |
| `isReadOnly` | `boolean` | `false` | 只读 |
| `isRequired` | `boolean` | `false` | 提交表单前是否必须填写 |
| `isInvalid` | `boolean` | - | 错误展示状态;不单独阻止提交 |
| `validate` | `(value: string) => string \| string[] \| true \| null \| undefined` | - | 同步校验函数 |
| `validationBehavior` | `'native' \| 'aria'` | 继承 Form,否则 `'native'` | 原生约束拦截或仅 ARIA 语义 |
| `fullWidth` | `boolean` | `false` | 是否占满容器宽度 |
| `variant` | `'primary' \| 'secondary'` | - | 视觉变体,继承到子控件 |
| `minLength` / `maxLength` / `pattern` | `number` / `number` / `string` | - | 下发到真实文本控件的原生约束 |
| `autoComplete` | `string` | - | 浏览器自动完成提示(下发 `autocomplete`) |
| `inputMode` | `string` | - | 移动键盘类型提示(下发 `inputmode`) |
| `form` | `string` | - | 关联外部 form 的 id(下发到真实控件) |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
值使用 `v-model`(`update:modelValue`)。受控值由父级决定,拒绝更新时恢复实际 DOM 值。`name`、原生约束、`autoComplete` / `inputMode` / `form` 与 ARIA 落在真实 `input`/`textarea` 上。服务端/外部错误通过 Form 的 `validation-errors`(按 name 映射)接入,不是 TextField 的 `validationErrors` 数组。
### 事件
| 事件 | 参数 | 描述 |
| ------------------- | -------- | ---------------------------- |
| `update:modelValue` | `string` | 值变化时发出;配合 `v-model` |
### 暴露方法
通过模板 ref 调用:
| 名称 | 类型 | 描述 |
| --------- | ------------------------------------------------------ | ------------------------------------------------- |
| `focus` | `() => void` | 聚焦当前文本控件 |
| `element` | `HTMLInputElement \| HTMLTextAreaElement \| undefined` | 底层控件(模板 ref 的公开实例会自动解包内部 Ref) |
### 组合子组件
- **[Label](/components/label)** — 字段标签
- **[Input](/components/input)** — 单行文本输入
- **[TextArea](/components/textarea)** — 多行文本输入
- **[Description](/components/description)** — 辅助说明
- **[FieldError](/components/field-error)** — 校验错误
```vue
<script setup lang="ts">
import {
TextField,
Label,
Input,
Description,
FieldError,
} from "@heroui-vue/components";
import { ref } from "vue";
const email = ref("");
const hasError = ref(false);
</script>
<template>
<TextField v-model="email" :is-invalid="hasError" is-required>
<Label>邮箱地址</Label>
<Input type="email" placeholder="you@example.com" />
<Description>我们不会分享你的邮箱。</Description>
<FieldError>请输入有效的邮箱地址。</FieldError>
</TextField>
</template>
```
### 作用域插槽状态
默认插槽提供以下状态,可用于条件渲染:
| 参数 | 类型 | 描述 |
| ------------------ | ---------- | ------------ |
| `isDisabled` | `boolean` | 是否禁用 |
| `isReadOnly` | `boolean` | 是否只读 |
| `isRequired` | `boolean` | 是否必填 |
| `isInvalid` | `boolean` | 是否无效 |
| `validationErrors` | `string[]` | 当前错误列表 |
与上游 `TextFieldRenderProps` 的差异:Vue 使用作用域插槽而非 render prop;无 `isFocusWithin` / `isFocusVisible`(聚焦状态由控件自身管理)。
## 相关组件
[Input 输入框](./input) · [TextArea 多行文本框](./textarea) · [Label 标签](./label) · [FieldError 字段错误](./field-error)
+128
View File
@@ -0,0 +1,128 @@
---
title: TextArea 多行文本框
description: 原语级多行文本输入组件,可接受标准 HTML 属性。
component: textarea
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { TextArea } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="textarea-basic" />
> 校验、标签与错误消息请参阅 **[TextField](/components/text-field)**。
## 示例
### 变体
TextArea 组件支持两种视觉变体:
- **`primary`**(默认)— 带阴影的标准样式,适用于大多数场景
- **`secondary`** — 低强调、无阴影,适合在 Surface 等表面背景上使用
<ComponentPreview name="textarea-variants" />
### 表面样式
在 [Surface](/components/surface) 内使用时,请使用 `variant="secondary"`,以应用适合表面背景的低强调变体。
<ComponentPreview name="textarea-on-surface" />
### 宽度充满
<ComponentPreview name="textarea-full-width" />
### 受控组件
<ComponentPreview name="textarea-controlled" />
### 行数与尺寸调整
<ComponentPreview name="textarea-rows" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="textarea-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-textarea {
@apply rounded-xl border border-border bg-surface px-4 py-3 text-sm leading-6 shadow-sm;
&:hover,
&[data-hovered="true"] {
@apply bg-surface-secondary border-border/80;
}
&:focus-visible,
&[data-focus-visible="true"] {
@apply border-accent ring-2 ring-accent/20;
}
&[data-invalid="true"] {
@apply border-danger bg-danger-soft text-danger;
}
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
#### 基础类 [!toc]
- `.hv-textarea` – 底层 `<textarea>` 元素样式
### 交互状态
- **悬停**:`:hover` 或 `[data-hovered="true"]`
- **可见焦点**:`:focus-visible` 或 `[data-focus-visible="true"]`
- **无效**:`[data-invalid="true"]`
- **禁用**:`:disabled` 或 `[aria-disabled="true"]`
## API 参考
### TextArea
TextArea 接受所有标准 HTML `<textarea>` 属性,以及以下属性:
| Prop | 类型 | 默认值 | 描述 |
| -------------- | --------------------------- | ----------- | -------------------------------- |
| `modelValue` | `string` | - | 受控值(`v-model`) |
| `defaultValue` | `string` | - | 非受控初始值 |
| `rows` | `number` | `3` | 可见文本行数 |
| `fullWidth` | `boolean` | `false` | 是否占满容器宽度 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体 |
| `invalid` | `boolean` | - | 展示无效状态;与父级字段状态合并 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
原生属性(`name`、`placeholder`、`disabled`、`readonly`、`required`、`cols`、`wrap`、`autocomplete`、`minlength`、`maxlength` 等)透传到真实 `<textarea>`。在 TextField 内时,`name`/禁用/只读/必填/校验/ARIA 由字段上下文统一管理。
### 事件与暴露
| 接口 | 说明 |
| ------------------- | ---------------------------------------------- |
| `update:modelValue` | 值变化时发出;配合 `v-model` |
| `focus()` | 模板 ref 暴露,聚焦真实 `<textarea>` |
| `element` | 模板 ref 暴露的底层 `HTMLTextAreaElement` 引用 |
> 对于 `isInvalid`、`isRequired` 等校验 prop 以及错误处理,请将 TextArea 作为子组件与 **[TextField](/components/text-field)** 一起使用。
## 相关组件
[TextField 文本输入框](./text-field) · [Input 输入框](./input) · [InputGroup 输入框组](./input-group)
+19
View File
@@ -0,0 +1,19 @@
<script setup lang="ts">
import { Description, Input, Label } from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-1">
<Label for="email">邮箱</Label>
<Input
id="email"
aria-describedby="email-description"
class="w-64"
placeholder="you@example.com"
type="email"
/>
<Description id="email-description">
我们不会将你的邮箱分享给任何人。
</Description>
</div>
</template>
@@ -0,0 +1,13 @@
<script setup lang="ts">
import { Description, Input, Label } from "@heroui-vue/components";
</script>
<template>
<div class="flex w-64 flex-col gap-1">
<Label for="workspace-slug">工作区 URL</Label>
<Input id="workspace-slug" placeholder="acme" type="text" />
<Description id="workspace-slug-hint" class="leading-relaxed tracking-wide">
仅支持小写字母和连字符。将用于 app.heroui.com/acme
</Description>
</div>
</template>
+17
View File
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const value = ref("jr");
const isInvalid = computed(
() => value.value.length > 0 && value.value.length < 3,
);
</script>
<template>
<TextField v-model="value" class="w-64" :is-invalid="isInvalid">
<Label for="username">用户名</Label>
<Input id="username" placeholder="输入用户名" />
<FieldError>用户名至少需要 3 个字符</FieldError>
</TextField>
</template>
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const value = ref("jr");
const isInvalid = computed(
() => value.value.length > 0 && value.value.length < 3,
);
</script>
<template>
<TextField v-model="value" class="w-64" :is-invalid="isInvalid">
<Label for="handle">用户名</Label>
<Input id="handle" class="bg-field font-mono" placeholder="至少 3 个字符" />
<FieldError class="font-medium">用户名至少需要 3 个字符</FieldError>
</TextField>
</template>
+10
View File
@@ -0,0 +1,10 @@
<script setup lang="ts">
import { Input, Label } from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-1">
<Label for="name">姓名</Label>
<Input id="name" class="w-64" placeholder="输入你的姓名" type="text" />
</div>
</template>
+15
View File
@@ -0,0 +1,15 @@
<script setup lang="ts">
import { Input, Label } from "@heroui-vue/components";
</script>
<template>
<div class="flex flex-col gap-1.5">
<Label
class="text-xs font-semibold tracking-wide text-accent uppercase"
for="repo"
>
仓库
</Label>
<Input id="repo" class="w-64 bg-field" placeholder="heroui/react" />
</div>
</template>
@@ -0,0 +1,21 @@
<script setup lang="ts">
import { Input, Label, TextField } from "@heroui-vue/components";
const fieldClass =
"rounded-xl border border-border/80 bg-surface shadow-sm ring-1 ring-black/5 transition-[box-shadow,border-color] focus-visible:ring-2 focus-visible:ring-neutral-400/25 dark:ring-white/10 dark:focus-visible:ring-neutral-500/30";
</script>
<template>
<TextField class="w-full max-w-64 gap-1.5" name="email" type="email">
<Label class="font-medium text-neutral-800 dark:text-neutral-100">
邮箱
</Label>
<Input
:class="[
'text-sm text-neutral-800 placeholder:text-neutral-400 dark:text-neutral-100 dark:placeholder:text-neutral-500',
fieldClass,
]"
placeholder="输入你的邮箱"
/>
</TextField>
</template>
+11
View File
@@ -0,0 +1,11 @@
<script setup lang="ts">
import { TextArea } from "@heroui-vue/components";
</script>
<template>
<TextArea
aria-label="快速项目更新"
class="h-32 w-96"
placeholder="分享一条简短的项目更新…"
/>
</template>
+20
View File
@@ -0,0 +1,20 @@
<script setup lang="ts">
import { ref } from "vue";
import { Description, TextArea } from "@heroui-vue/components";
const value = ref("");
</script>
<template>
<div class="flex w-96 flex-col gap-2">
<TextArea
v-model="value"
aria-describedby="textarea-controlled-description"
aria-label="公告"
placeholder="撰写公告…"
/>
<Description id="textarea-controlled-description">
字符数: {{ value.length }} / 280
</Description>
</div>
</template>
@@ -0,0 +1,17 @@
<script setup lang="ts">
import { TextArea } from "@heroui-vue/components";
const fieldClass =
"rounded-xl border border-border/80 bg-surface shadow-sm ring-1 ring-black/5 transition-[box-shadow,border-color] focus-visible:ring-2 focus-visible:ring-neutral-400/25 dark:ring-white/10 dark:focus-visible:ring-neutral-500/30";
</script>
<template>
<TextArea
aria-label="备注"
:class="[
'h-28 w-full max-w-xs text-sm text-neutral-800 placeholder:text-neutral-400 dark:text-neutral-100 dark:placeholder:text-neutral-500',
fieldClass,
]"
placeholder="添加备注…"
/>
</template>
+9
View File
@@ -0,0 +1,9 @@
<script setup lang="ts">
import { TextArea } from "@heroui-vue/components";
</script>
<template>
<div class="w-[400px] space-y-3">
<TextArea full-width placeholder="全宽文本域" />
</div>
</template>
+13
View File
@@ -0,0 +1,13 @@
<script setup lang="ts">
import { Surface, TextArea } from "@heroui-vue/components";
</script>
<template>
<Surface class="w-full rounded-3xl p-6">
<TextArea
class="w-full min-w-[280px]"
placeholder="描述你的产品"
variant="secondary"
/>
</Surface>
</template>
+27
View File
@@ -0,0 +1,27 @@
<script setup lang="ts">
import { Label, TextArea } from "@heroui-vue/components";
</script>
<template>
<div class="flex w-96 flex-col gap-4">
<div class="flex flex-col gap-2">
<Label for="textarea-rows-3">简短反馈</Label>
<TextArea
id="textarea-rows-3"
aria-label="简短反馈"
placeholder="本周亮点…"
:rows="3"
/>
</div>
<div class="flex flex-col gap-2">
<Label for="textarea-rows-6">详细笔记</Label>
<TextArea
id="textarea-rows-6"
aria-label="详细笔记"
placeholder="写下完整的会议记录…"
:rows="6"
style="resize: vertical"
/>
</div>
</div>
</template>
+10
View File
@@ -0,0 +1,10 @@
<script setup lang="ts">
import { TextArea } from "@heroui-vue/components";
</script>
<template>
<div class="flex w-[280px] flex-col gap-2">
<TextArea full-width placeholder="主要文本域" variant="primary" />
<TextArea full-width placeholder="次要文本域" variant="secondary" />
</div>
</template>
+28
View File
@@ -0,0 +1,28 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Description,
Input,
Label,
TextArea,
TextField,
} from "@heroui-vue/components";
const name = ref("");
const bio = ref("");
</script>
<template>
<div class="flex w-full max-w-64 flex-col gap-4">
<TextField v-model="name" name="name">
<Label>显示名称</Label>
<Input placeholder="Jane" />
<Description>字符数: {{ name.length }}</Description>
</TextField>
<TextField v-model="bio" name="bio">
<Label>个人简介</Label>
<TextArea placeholder="介绍一下你自己…" />
<Description>字符数: {{ bio.length }} / 200</Description>
</TextField>
</div>
</template>
+16
View File
@@ -0,0 +1,16 @@
<script setup lang="ts">
import { Description, Input, Label, TextField } from "@heroui-vue/components";
</script>
<template>
<TextField
class="w-full max-w-64"
default-value="USR-12345"
is-disabled
name="accountId"
>
<Label>账户 ID</Label>
<Input placeholder="自动生成" />
<Description>此字段不可编辑</Description>
</TextField>
</template>
+23
View File
@@ -0,0 +1,23 @@
<script setup lang="ts">
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
</script>
<template>
<div class="w-[400px] space-y-4">
<TextField full-width name="name">
<Label>你的姓名</Label>
<Input placeholder="John" />
</TextField>
<TextField
full-width
is-invalid
is-required
name="password"
type="password"
>
<Label>密码</Label>
<Input />
<FieldError>密码长度必须超过 8 个字符</FieldError>
</TextField>
</div>
</template>
+32
View File
@@ -0,0 +1,32 @@
<script setup lang="ts">
import { Input, Label, TextField } from "@heroui-vue/components";
</script>
<template>
<div class="flex w-full max-w-64 flex-col gap-4">
<TextField name="password" type="password">
<Label>密码</Label>
<Input placeholder="••••••••" />
</TextField>
<TextField name="age" type="number">
<Label>年龄</Label>
<Input max="150" min="0" placeholder="21" />
</TextField>
<TextField name="email" type="email">
<Label>邮箱</Label>
<Input placeholder="user@example.com" />
</TextField>
<TextField name="website" type="url">
<Label>网站</Label>
<Input placeholder="https://example.com" />
</TextField>
<TextField name="phone" type="tel">
<Label>电话</Label>
<Input placeholder="+1 (555) 000-0000" />
</TextField>
</div>
</template>
+29
View File
@@ -0,0 +1,29 @@
<script setup lang="ts">
import {
Description,
Input,
Label,
Surface,
TextArea,
TextField,
} from "@heroui-vue/components";
</script>
<template>
<Surface class="flex w-full min-w-[340px] flex-col gap-4 rounded-3xl p-6">
<TextField name="name" variant="secondary">
<Label>你的姓名</Label>
<Input class="w-full" placeholder="John" />
<Description>我们绝不会与他人分享此信息</Description>
</TextField>
<TextField name="email" type="email" variant="secondary">
<Label>邮箱</Label>
<Input class="w-full" placeholder="john@example.com" />
</TextField>
<TextField name="bio" variant="secondary">
<Label>个人简介</Label>
<TextArea class="w-full" placeholder="介绍一下你自己…" :rows="4" />
<Description>至少 4 行</Description>
</TextField>
</Surface>
</template>
@@ -0,0 +1,16 @@
<script setup lang="ts">
import { Input, Label, TextField } from "@heroui-vue/components";
</script>
<template>
<!-- 上游 render 自定义根节点;Vue 将 data-* 透传到 TextField 根,不支持替换元素类型。 -->
<TextField
class="w-full max-w-64"
data-custom="foo"
name="email"
type="email"
>
<Label>邮箱</Label>
<Input placeholder="输入你的邮箱" />
</TextField>
</template>
+11
View File
@@ -0,0 +1,11 @@
<script setup lang="ts">
import { Description, Input, Label, TextField } from "@heroui-vue/components";
</script>
<template>
<TextField class="w-full max-w-64" is-required name="fullName">
<Label>全名</Label>
<Input placeholder="John Doe" />
<Description>此字段为必填项</Description>
</TextField>
</template>
+16
View File
@@ -0,0 +1,16 @@
<script setup lang="ts">
import {
Description,
Label,
TextArea,
TextField,
} from "@heroui-vue/components";
</script>
<template>
<TextField class="w-full max-w-64" name="message">
<Label>消息</Label>
<TextArea placeholder="在此输入你的消息…" :rows="4" />
<Description>最多 500 个字符</Description>
</TextField>
</template>
+11
View File
@@ -0,0 +1,11 @@
<script setup lang="ts">
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
</script>
<template>
<TextField class="w-full max-w-64" is-invalid name="email" type="email">
<Label>邮箱</Label>
<Input placeholder="user@example.com" />
<FieldError>请输入有效的邮箱地址</FieldError>
</TextField>
</template>
+9 -1
View File
@@ -57,7 +57,15 @@ Form 的 `validation-errors` 是以字段 name 为键、字符串或字符串数
## 当前边界
本页交付共享协议和验证场景,尚未宣称完整迁移 Form、TextField 的所有上游文档。已迁移的三个 TextField 基线示例在此展示,其余示例后续按清单补齐。
本页交付共享协议和验证场景,是字段/表单基础设施指南,不替代独立组件文档。TextField、Label、Description、FieldError、TextArea 的完整 API 与基线示例见各自组件页:
- [TextField 文本输入框](/components/text-field)
- [Label 标签](/components/label)
- [Description 描述](/components/description)
- [FieldError 字段错误](/components/field-error)
- [TextArea 多行文本框](/components/textarea)
已迁移的三个 TextField 基线示例仍在此展示,便于联调验证。
SSR 支持标准的直接子部件、原生元素/Fragment 包裹的 Description/FieldError,即使说明放在控件之后也能生成完整 ID 关联。自定义 Vue 包装组件中的描述在客户端注册;若首屏 SSR 必须关联,请显式提供控件 aria-describedby 和对应描述 id。Nuxt 完整消费验收尚未进行。
+14 -6
View File
@@ -55,14 +55,14 @@
| tooltip | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 |
| input | 验证中(字段接入) | 接入 TextField;复用值恢复/reset/ARIA;独立使用保持兼容;完整消费验收未完成 |
| input-group | 验证中(字段接入) | Root/Input/TextArea/Prefix/Suffix;复用 useTextControl;变体继承 TextField 可覆盖;前后缀非表单字段 |
| textarea | 验证中(字段接入) | 接入 TextField 与共享 useTextControl;独立文档/全部示例仍待补齐 |
| textfield | 验证中(基础设施) | 一个文本控件;值/状态/约束/校验/ID;3 个基线示例;完整文档与其余示例待补齐 |
| textarea | 验证中(字段接入) | rows 默认 3;变体/fullWidth/受控/FormData/reset;独立页 7/7 示例;接入 useTextControl |
| textfield | 验证中(字段接入) | 值/状态/约束/校验/ID;独立页 13/13 示例;render 自定义根类型未支持(data-* 透传) |
| search-field | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 |
| typography | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 |
| label | 验证中(通用字段) | 文本字段自动 for,Switch 内 span;继承 required/disabled/invalid;完整独立文档待补齐 |
| description | 验证中(通用字段) | 通用字段描述注册与动态 ID 清理;外部 describedby 合并;SSR 包装限制见基础协议 |
| label | 验证中(通用字段) | 自动 for、Switch 内 span、状态继承;独立页 2/2 示例 |
| description | 验证中(通用字段) | 动态 ID 清理、外部 describedby 合并;独立页 2/2 示例;SSR 包装限制见基础协议 |
| empty-state | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 |
| field-error | 验证中(通用字段) | 通用 FieldContext;TextField 与 Switch 错误/动态 ID;validationDetails 等差异见基础协议 |
| field-error | 验证中(通用字段) | 作用域插槽 validationErrors;独立页 2/2 示例;无完整 validationDetails(见基础协议) |
| fieldset | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 |
| form | 验证中(基础设施) | native/aria 校验、FormData/reset、服务端错误;完整基线文档与生态集成未完成 |
| header | 计划 | 待实施时核验子部件、属性、事件、键盘行为与替代底座 |
@@ -100,7 +100,7 @@ React Aria 专属 rac 导出与 React 图标类型不移植;Vue 版 ConfigProv
## 已实现组件的文档清单复核
Button 14 / 14,Card 7 / 7,Input 7 / 7,Switch 15 / 15,Surface 4 / 4,Link 6 / 6,Spinner 5 / 5,CloseButton 4 / 4,Avatar 7 / 7,AvatarGroup 7 / 7,Skeleton 8 / 8,Separator 7 / 7,Kbd 7 / 7,Chip 5 / 5,Badge 8 / 8,Alert 2 / 2,InputGroup 21 / 21,基线预览名称与顺序均已对齐。没有用空壳补足数量;预览与复制代码共用 SFC。
Button 14 / 14,Card 7 / 7,Input 7 / 7,Switch 15 / 15,Surface 4 / 4,Link 6 / 6,Spinner 5 / 5,CloseButton 4 / 4,Avatar 7 / 7,AvatarGroup 7 / 7,Skeleton 8 / 8,Separator 7 / 7,Kbd 7 / 7,Chip 5 / 5,Badge 8 / 8,Alert 2 / 2,InputGroup 21 / 21,TextField 13 / 13,Label 2 / 2,Description 2 / 2,FieldError 2 / 2,TextArea 7 / 7,基线预览名称与顺序均已对齐。没有用空壳补足数量;预览与复制代码共用 SFC。
Card 图片、头像、横向布局、表单与自定义样式已补齐;Input 的标签、类型、宽度与自定义样式已对齐。Switch 补齐图标、位置、分组、表单、状态插槽与自定义根容器。Surface 覆盖变体与表单组合;Link 覆盖图标位置、文本装饰、自定义图标、asChild 与自定义样式;Spinner 覆盖颜色、尺寸、速度与自定义样式。CloseButton 覆盖默认、交互、自定义图标与自定义样式。Avatar 覆盖基本、尺寸、颜色、变体、回退、自定义图片组件与自定义样式;AvatarGroup 覆盖基本、max、自定义计数、尺寸、网格、叠放样式与自定义样式;Skeleton 覆盖文本、用户资料、列表、网格、单次闪烁、动画类型与自定义样式。Separator 覆盖基本、变体、Surface 组合、垂直、带内容布局、asChild 自定义根与自定义样式;Kbd 覆盖基本、变体、导航键、行内、说明文字、特殊键与自定义样式;Chip 覆盖基本、变体矩阵、状态图标、带图标与自定义样式。Badge 覆盖基本、变体、尺寸、颜色、四角位置、点状、带内容与自定义样式;Alert 覆盖六种组合的基本示例与自定义样式。
@@ -153,3 +153,11 @@ Card 图片、头像、横向布局、表单与自定义样式已补齐;Input
- 显式 `variant` 优先于 TextField 继承;fullWidth/role 可配置。点击组合空白聚焦真实 input/textarea(按钮等可交互子元素除外)。
- Prefix/Suffix 为展示容器,不进入 FormData;嵌入 Button 默认 `type="button"`。
- 与上游差异:`with-textarea` 的 Tooltip 以原生 `title`/`aria-label` 适配(Tooltip 未迁移);React `render`/`className`/`children` 改为 Vue 插槽与 `class`。
## M13–M14 说明
- TextField / Label / Description / FieldError / TextArea 均有独立中文文档页;`/fields` 保留为基础设施指南并链接到组件页。
- TextField 13 例含 on-surface、required、disabled、full-width、controlled、with-error、textarea、input-types、render-function、custom-styles;复用已有 basic / with-description / validation。
- Label/Description/FieldError 正文中的必填/禁用/无效、表单组合、动态/多条错误示例已改为可运行 Vue 写法(v-model、作用域插槽)。
- TextArea:补 `rows` 默认 3;7 例齐全。值/reset/ARIA 仍由 useTextControl 管理。
- 差异:TextField `render` 不支持替换根元素类型(data-* 透传);FieldError 无 validationDetails,动态错误用作用域插槽。
+47
View File
@@ -0,0 +1,47 @@
# M13–M14 独立复核
日期:2026-10-21。起点 `37e11e6`。首轮结论:**待修正,未提交**。以下发现保留为历史,最终结论见文末。
## P2:FieldError 两个正文示例既不能独立编译,也没有错误数据来源
位置:`apps/docs/components/field-error.md:48–64`(动态错误信息)、`108–124`(多条错误信息)。
两段完整 SFC 的 script 只有组件导入,但模板引用 `hasErrors`。把正文完整 SFC 提取为临时 Vue 文件,运行 `pnpm typecheck`,两处均报 TS2339:`hasErrors` 不存在。
此外,单纯添加 `const hasErrors = ref(true)` 不能修好演示。`isInvalid` 只改变错误展示状态,不会向 `validationErrors` 填入任何文本;当前既没有 validate 也没有 Form.validationErrors 数据源,所以动态 join 和多条 v-for 都没有内容。前一个示例还把作用域挂在 TextField,后文却说明 FieldError 的作用域插槽,需统一实际演示对象。
修正要求:给两例完整、明确的错误源,例如 Form 的按 name 映射错误,或带 v-model / validate / validationBehavior 的可操作字段。动态例实际使用 FieldError 作用域插槽;多条例实际产生至少两条错误。完整 SFC 能复制编译,交互或初始状态确实显示预期消息。补充正文代码块编译检查和消息内容行为验证,不能只断言 Markdown 包含 validationErrors 字符串。
## P2:TextField API 表未完整描述已实现的公共属性
位置:`apps/docs/components/text-field.md` API 参考(约 148–170 行)。
当前 TextField.vue 已明确声明 `autoComplete`、`inputMode`、`form`,并通过字段上下文下发到实际输入控件,但新独立页未列出这三项,也没有指向完整契约。它们直接影响自动完成、移动键盘与外部表单归属,不能在“字段文档补齐”时静默省略。
修正要求:逐项核对 TextField 当前全部公开 props、update:modelValue 事件、focus/element 暴露接口、作用域插槽和属性落点;补齐缺失项和适用默认值。TextArea 页也明确说明 focus/element 与值更新事件,原生属性可以有清楚的统一说明,无需重复制造别名。使用最小消费用例确认表中接口真实生效。
## 已通过的独立检查与边界
- `pnpm test`:13 文件 / 201 项(含 node SSR 3 项)。
- `pnpm test:unit`:12 文件 / 198 项(不含 node SSR)。
- 常规 `pnpm typecheck`、`pnpm build:docs`、`pnpm format` 通过;这不覆盖 Markdown 正文 SFC 的变量与行为。
- 额外正文 SFC 类型探针失败,见第一项;临时文件已移除,不混入交付。
- TextArea 改动限于 rows 的显式类型、默认值和 DOM 绑定,未复制或改动共享字段协议;默认 3 依据上游 API 表,未独立验证 React Aria 运行时默认值。
- TextField 13、Label/Description/FieldError 各 2、TextArea 7 个预览均存在。抽查受控、行数、自定义样式与上游源码;render-function 例的实际上游根也是 div + data-custom,本例通过根属性透传保留效果,根元素替换能力仍明确未支持。
- 未重新逐页操作浏览器,不把实施者浏览器记录算作独立复测;未做逐像素、Nuxt、RTL、读屏或独立安装。
## 最短返工范围
优先修正文档/API、必要的示例与验证,不动 shared field/TextField/Form/Switch/InputGroup 核心。同步实施者报告、兼容矩阵中“可运行 Vue 示例”等结论,以实际结果为准。完成后交回复核,不自行提交或改写本报告结论为验收通过。
## 返工后独立复核(2026-10-21)
结论:**上述两项已修正,通过本批复核,可以提交。**
- FieldError 两个完整正文示例均以 v-model + 同步 validate + aria 模式提供真实错误,并使用 FieldError 作用域插槽。
- 独立提取五个文档页中包含 script 和 template 的完整 Vue 代码块,由 vue-tsc 实际检查,通过;另将 Markdown 原文中的动态/多条错误 SFC 直接导入 Vitest(不是手写一份副本),输入 abc 显示预期的一条/两条错误,合法输入后错误消失,两项通过。临时探针已删除。
- TextField autoComplete/inputMode/form、值更新事件、focus/element 和 TextArea 的事件/暴露说明补齐。集成人校正 element 在公开组件实例中自动解包的类型说明,并去掉暴露测试中会掩盖失败的 DOM 回退,直接断言真实元素与实际焦点。
- 既有正文绑定检查是正则静态检查,集成人更正其测试名称,避免将它描述为完整编译器;本次真正的正文类型/行为检查单独记录在上文。
- dist TextAreaProps 探针经 vue-tsc 验证:rows 数字可用、字符串被拒绝;临时探针已移除。
- 最终 pnpm test:13 文件 / 208 项(含 node SSR 3 项);pnpm test:unit:12 文件 / 205 项(不含 node SSR)。pnpm typecheck、pnpm build:docs、pnpm format、git diff --check 通过。
- 本轮没有重跑逐页浏览器或视觉对比,不扩大原有浏览器自测、SSR、主题与独立安装的验收范围。完整基建与根元素替换差异仍按既有协议记录。
+105
View File
@@ -0,0 +1,105 @@
# M13 字段文档交接报告
实施日期:2026-10-21。状态:实施者自测完成,待集成人复核。**未提交、未推送。**
## 1. 起点与范围
| 项 | 值 |
| --------- | ---------------------------------------------------------------------------------------- |
| 起始 HEAD | `37e11e6`(feat: add InputGroup and verify shared field integration) |
| 工作区 | 干净;未改 field/useFieldPart/useTextControl/TextField/Form guard/Switch/InputGroup 核心 |
| 上游 | HeroUI 3.2.6 / `ce90ea3`(`../heroui` 只读) |
| 范围 | TextField、Label、Description、FieldError 四个独立中文文档页及全部基线内容 |
## 2. 交付
### 文档页(新建)
- `apps/docs/components/text-field.md`(13 预览)
- `apps/docs/components/label.md`(2 预览)
- `apps/docs/components/description.md`(2 预览)
- `apps/docs/components/field-error.md`(2 预览)
### 演示(新建 16;复用 3)
复用:`textfield-basic` / `textfield-with-description` / `textfield-validation`(FORM-01 已有)。
新建 TextField 10:on-surface、required、disabled、full-width、controlled、with-error、textarea、input-types、render-function、text-field-custom-styles。
新建 Label 2、Description 2、FieldError 2。
### 其他
- 导航与 `components.md` 增加四页;`fields.md` 保留基础设施用途并链接组件页。
- `tests/field-docs-textarea.test.ts`(与 M14 共用,见 M14 报告)。
## 3. 上游来源
| 路径 | 用途 |
| --------------------------------------------------------------------------- | ---------------------------------------- |
| `.../(forms)/{text-field,label,description,field-error}.mdx` | 标题、结构、示例次序、正文代码、API 叙述 |
| `apps/docs/src/demos/cn/{textfield,label,description,field-error}/` | 16 个新演示的文案与布局 |
| `packages/react/src/components/{textfield,label,description,field-error}/*` | API 语义对照 |
| `packages/styles/components/{textfield,label,description,field-error}.css` | 样式类说明(hv- 已存在) |
## 4. 示例与正文对照
- TextField 13/13:名称与 CN 文档顺序一致;custom-styles 预览名保持上游 `text-field-custom-styles`。
- Label/Description/FieldError 各 2/2。
- 正文代码示例已改为 Vue:Label 必填/禁用/无效;Description 搭配表单与 TextField;FieldError 基础校验、动态错误(作用域插槽)、自定义校验(含 validate)、多条错误(v-for)。
## 5. API 差异(Vue 契约)
| 上游 | Vue |
| ---------------------------------- | ------------------------------------------------------------------------------------------------------- |
| `htmlFor` | `for` |
| `value`/`onChange` | `v-model` / `update:modelValue` |
| `className`/`children` | `class` / 默认插槽 |
| `render` 自定义根 | 未支持元素类型替换;`data-*` 透传到根(render-function 示例已注明) |
| FieldError render `(validation)=>` | 作用域插槽 `validationErrors` / `isInvalid` |
| `validationErrors` 数组 prop | Form `validation-errors`(按 name 映射) |
| TextFieldRenderProps | 作用域插槽 isDisabled/isReadOnly/isRequired/isInvalid/validationErrors;无 isFocusWithin/isFocusVisible |
不提供完整 `validationDetails`;文档已注明。
## 6. 回归与命令
| 要求 | 结果 |
| --------------------------------- | --------------------------- |
| 文档契约(无 className/children) | 测试通过 |
| 预览清单 13+2+2+2 | 测试通过 |
| 三份既有示例与 /fields 保留 | 测试通过 |
| Label/Description/FieldError 关联 | field-foundation + 本批测试 |
`pnpm test` 13 文件 / **201**(含 SSR 3);`pnpm test:unit` 12 文件 / **198**(不含 SSR);typecheck / build:docs / format 通过。
## 7. 浏览器证据
`http://127.0.0.1:5173`:text-field 22 字段 / data-custom 1;field-error 输入 "ab" 显示错误;四页 API 表桌面与 390 窄屏均无溢出。
## 8. 未验证
浅/深切换交互、位图、Nuxt、RTL、读屏、独立安装、逐像素。
## 9. 最短复核步骤
1. `pnpm test && pnpm typecheck && pnpm build:docs`
2. 打开 `/components/text-field` 数预览 13;核对 render 示例注释。
3. `/components/label`、`description`、`field-error`:对照正文代码是否 Vue 且可运行。
4. `/fields` 仍为指南页并链接四个组件页。
## 10. 需集成人确认
- TextField 根元素类型自定义(render/asChild)是否列入后续协议扩展。
- custom-styles 预览名 `text-field-custom-styles` 与 `textfield-*` 前缀不一致(对齐上游命名)。
实施者不宣布验收通过。
## 11. 复核返工记录(实施者,2026-10-21)
独立复核两项 P2 已修:
1. FieldError「动态错误信息」「多条错误信息」正文示例:补 `password` + `validatePassword` 真实错误源,使用 FieldError 作用域插槽与 `validation-behavior="aria"`;多条示例返回两条错误并 v-for 渲染。完整 SFC 可编译,输入 `abc` 显示预期消息。
2. TextField API 表补齐 `autoComplete`、`inputMode`、`form`,以及 `update:modelValue` 事件与 `focus`/`element` 暴露;TextArea 页补事件与暴露说明。
回归新增:正文 SFC 脚本绑定检查、动态/多条错误消息行为、API 表关键字、`autoComplete`/`inputMode`/`form` 下发到真实控件、focus/element 暴露。`pnpm test` **208**(+7)、typecheck、format 通过。未改共享 field/TextField/Form 核心。
+80
View File
@@ -0,0 +1,80 @@
# M14 TextArea 交接报告
实施日期:2026-10-21。状态:实施者自测完成,待集成人复核。**未提交、未推送。**
## 1. 起点与范围
| 项 | 值 |
| --------- | ---------------------------------------------------------------------------------------------------------------- |
| 起始 HEAD | `37e11e6`;与 M13 同一 checkout、同一交付批次 |
| 边界 | 可改 TextArea 局部封装/CSS/变体;不复制 useTextControl;不改 TextField/Form/Switch/InputGroup 核心 |
| 上游 | `text-area.mdx`、`packages/react/src/components/textarea`、`demos/cn/textarea`、`styles/components/textarea.css` |
## 2. 能力核对与局部缺口
| 能力 | 状态 |
| ------------------------------------ | --------------------------------------------------- |
| 值 v-model / default-value | 已有(useTextControl) |
| FormData / reset / 取消 reset | 已有 |
| 受控拒绝恢复 DOM | 已有 |
| disabled / readonly / required | 已有 |
| variant primary/secondary、fullWidth | 已有 |
| rows | **本批补**:显式 `rows` prop,默认 3(上游 API 表) |
| cols/wrap/autocomplete/maxlength 等 | attrs 透传 |
| Label/Description/FieldError 关联 | 已有 |
| InputGroup 相邻回归 | 本批测试覆盖 |
未改 useTextControl;无第二份值/错误状态。
## 3. 交付
- `apps/docs/components/textarea.md`(7 预览,Vue API 表)
- 演示 7:basic、variants、on-surface、full-width、controlled、rows、textarea-custom-styles(对应上游 `text-area-custom-styles` 预览名)
- `TextArea.vue`:`rows?: number` 默认 3
- 测试(与 M13 共用 `tests/field-docs-textarea.test.ts`):rows 默认与显式、FormData/disabled/readonly/reset/取消 reset、受控拒绝、字段关联、InputGroup 回归、类型约束
## 4. 示例对照(7/7)
| 预览 | 上游文件 | 内容 |
| ---------------------- | ----------------- | ---------------------------- |
| textarea-basic | basic.tsx | aria-label + h-32 w-96 |
| textarea-variants | variants.tsx | primary/secondary fullWidth |
| textarea-on-surface | on-surface.tsx | Surface + secondary |
| textarea-full-width | full-width.tsx | 全宽 |
| textarea-controlled | controlled.tsx | v-model + 字符数 Description |
| textarea-rows | rows.tsx | rows 3/6 + Label |
| textarea-custom-styles | custom-styles.tsx | 自定义圆角/阴影 |
## 5. API 差异
- `value`/`onChange` → `v-model`;`className` → `class`(string|array|object)。
- `rows` 默认 3 与上游一致;校验类 prop 仍通过 TextField。
## 6. 命令
与 M13 同次执行:`pnpm test` 13 文件 / **201**(SSR 3);`pnpm test:unit` 12 文件 / **198**;typecheck / build:docs / format 通过。
## 7. 浏览器
`/components/textarea`:9 个 textarea(含演示外页面代码),rows=3 与 rows=6 生效;表格无溢出;390 窄屏正常。
## 8. 未验证
浅/深切换交互、位图、Nuxt、RTL、读屏、独立安装、resize 手感(仅样式属性)。
## 9. 最短复核步骤
1. `pnpm exec vitest run tests/field-docs-textarea.test.ts`
2. `/components/textarea`:变体、全宽、受控计数、行数 3/6。
3. 在 `/components/text-field` 的 textarea 示例与 InputGroup with-textarea 回归。
## 10. 需集成人确认
- `rows` 默认 3 是否需要与 Input 默认高度策略统一文档说明。
- 自定义样式预览名 `textarea-custom-styles` 对应上游 `text-area-custom-styles`。
实施者不宣布验收通过。
## 11. 复核返工(2026-10-21)
按 M13–M14 复核报告,在 TextArea 文档补充 `update:modelValue`、`focus()`、`element` 暴露说明;行为测试覆盖 focus/element 与 autoComplete 等字段下发。`pnpm test` 208 项通过。
+2
View File
@@ -53,3 +53,5 @@
- Form/字段验证页的 `form-foundation.vue` 为本库基础设施回归场景,未冒充上游完整 Form 示例。
- 原 Switch 专属 FieldError 移到通用 field 目录;复用 fieldContextKey,而非复制一套说明/错误基础组件。具体差异与 SSR 包装边界见 decisions/field-form-foundation.md。
- InputGroup 演示图标由 `@gravity-ui/icons` 替换为 Tabler(IconMail/IconWorld/IconCopy/IconEye/IconEyeOff/IconAt/IconPlus/IconPlugConnected/IconMicrophone/IconArrowUp);with-textarea 的 Tooltip 以原生 title/aria-label 适配。
- M13–M14:`apps/docs/content/docs/cn/react/components/(forms)/{text-field,label,description,field-error,text-area}.mdx` 与 `apps/docs/src/demos/cn/{textfield,textarea,label,description,field-error}/` 的文档结构、正文代码示例与 26 个文档引用预览(TextField 13 + Label 2 + Description 2 + FieldError 2 + TextArea 7)。
- TextArea `rows` 默认值 3 来自上游 API 表;CSS/变体此前已移植为 hv- 命名空间。
+35
View File
@@ -229,3 +229,38 @@
独立复核见 [M12-review.md](./migrations/M12-review.md)。修复 InputGroup 内显式控件 ID 的 SSR Label 关联,补充受控拒绝、取消 reset、native/aria、实际水合与动态插槽回归。多行演示清理定时器,校正文档继承语义与静态复制按钮说明。
最终 `pnpm test` 12 文件 / 189 项(含 node SSR 3 项);`pnpm test:unit` 11 文件 / 186 项(不含 node SSR)。`pnpm typecheck`、`pnpm build:docs`、`pnpm format`、`git diff --check` 通过。CSS 规范化后与固定基线一致。本次未重新逐页浏览器检查;实施者浏览器自测记录不能算作集成人复测。逐像素、读屏、RTL、Nuxt、独立安装仍未验证。
## M13–M14 字段文档与 TextArea(2026-10-21)
起始 HEAD `37e11e6`(M12 InputGroup 及集成人修正已合入),工作区干净。未改 field/context.ts、useFieldPart.ts、useTextControl.ts、TextField.vue、Form guard、Switch、InputGroup 核心。
### 命令(分别记录)
| 入口 | 文件数 | 通过数 | 含 SSR |
| ----------------- | ------ | ------ | ------------------- |
| `pnpm test` | 13 | 201 | 是(ssr.test.ts 3) |
| `pnpm test:unit` | 12 | 198 | 否 |
| `pnpm typecheck` | — | 通过 | — |
| `pnpm build:docs` | — | 通过 | — |
| `pnpm format` | — | 通过 | — |
### 浏览器(Playwright CLI,127.0.0.1:5173)
- `/components/text-field`:22 个 TextField、22 Label、10 Description、2 FieldError;render 示例 `data-custom=foo`;API 表 17 行无溢出。
- `/components/label`、`/components/description`、`/components/field-error`:预览与表格正常;field-error 输入 "ab" 显示错误文案与 data-invalid。
- `/components/textarea`:9 个 textarea,rows=3 与 rows=6 均生效。
- 窄屏 390:表格宽 350,无水平溢出。
### 未验证
浅/深切换交互、位图截图、Nuxt 水合、RTL、读屏、触屏、独立安装、自动视觉对比。
## M13–M14 文档返工(2026-10-21)
独立复核 2 项 P2:FieldError 正文两例缺错误数据源/未定义 `hasErrors`;TextField API 缺 autoComplete/inputMode/form 与事件/暴露。已改为带 `validate` 的完整 Vue 示例并补 API;新增正文 SFC 绑定检查与消息行为测试。`pnpm test` 208(13 文件,SSR 3)、typecheck、format 通过。详见 `docs/migrations/M13-M14-review.md`。
## M13–M14 返工后集成人复核(2026-10-21)
结论见 [独立报告](./migrations/M13-M14-review.md#返工后独立复核2026-10-21):FieldError 正文完整 SFC 经独立提取、vue-tsc 检查及原文动态/多条错误运行检查通过,TextField/ TextArea 公共接口文档补齐。公开 element 直接断言真实 DOM,focus 确认实际焦点。
最终 pnpm test 为 13 文件 / 208 项(含 node SSR 3 项);pnpm test:unit 为 12 文件 / 205 项(不含 node SSR)。pnpm typecheck、pnpm build:docs、pnpm format、git diff --check 通过。另有临时正文原文行为探针 2 项通过、dist 类型探针通过,探针验证后删除,不计入最终套件数量。未重新逐页浏览器检查,未新增逐像素、Nuxt、RTL、读屏或独立安装验收。
+1 -1
View File
@@ -15,7 +15,7 @@
"build:docs": "pnpm build && pnpm --filter @heroui-vue/docs build",
"typecheck": "vue-tsc --noEmit",
"test": "vitest run",
"test:unit": "vitest run tests/components.test.ts tests/composition.test.ts tests/docs-search.test.ts tests/button-demos.test.ts tests/aligned-components.test.ts tests/surface-link-spinner.test.ts tests/close-button-avatar-skeleton.test.ts tests/separator-kbd-chip.test.ts tests/badge-alert.test.ts tests/input-group.test.ts tests/field-foundation.test.ts",
"test:unit": "vitest run tests/components.test.ts tests/composition.test.ts tests/docs-search.test.ts tests/button-demos.test.ts tests/aligned-components.test.ts tests/surface-link-spinner.test.ts tests/close-button-avatar-skeleton.test.ts tests/separator-kbd-chip.test.ts tests/badge-alert.test.ts tests/input-group.test.ts tests/field-docs-textarea.test.ts tests/field-foundation.test.ts",
"test:ssr": "vitest run tests/ssr.test.ts",
"format": "prettier --check .",
"format:fix": "prettier --write .",
@@ -3,13 +3,17 @@ import { useAttrs } from "vue";
import { textAreaVariants, type TextAreaVariants } from "@heroui-vue/styles";
import { useTextControl } from "../field/useTextControl";
defineOptions({ inheritAttrs: false });
const props = defineProps<{
modelValue?: string;
defaultValue?: string;
invalid?: boolean;
variant?: TextAreaVariants["variant"];
fullWidth?: boolean;
}>();
const props = withDefaults(
defineProps<{
modelValue?: string;
defaultValue?: string;
invalid?: boolean;
variant?: TextAreaVariants["variant"];
fullWidth?: boolean;
rows?: number;
}>(),
{ rows: 3 },
);
const emit = defineEmits<{ "update:modelValue": [value: string] }>();
const attrs = useAttrs();
const control = useTextControl(props, attrs, emit);
@@ -23,6 +27,7 @@ defineExpose({ focus: () => element.value?.focus(), element });
:value="value"
:id="control.id()"
:name="control.name()"
:rows="rows"
:disabled="control.disabled()"
:readonly="control.readOnly()"
:required="control.required()"
+534
View File
@@ -0,0 +1,534 @@
import { describe, it, expect, afterEach, vi } from "vitest";
import { mount } from "@vue/test-utils";
import { defineComponent, h, nextTick, ref } from "vue";
import { readFileSync } from "node:fs";
import {
Description,
FieldError,
Form,
Input,
InputGroup,
InputGroupInput,
Label,
TextArea,
TextField,
} from "../packages/vue/src";
afterEach(() => {
document.body.innerHTML = "";
});
const settle = async () => {
await nextTick();
await nextTick();
};
describe("TextArea", () => {
it("renders native textarea with default rows and variant classes", () => {
const w = mount(TextArea, {
props: { rows: 5, variant: "secondary", fullWidth: true },
});
const el = w.get("[data-slot=textarea]");
expect(el.element.tagName).toBe("TEXTAREA");
expect((el.element as HTMLTextAreaElement).rows).toBe(5);
expect(el.classes()).toContain("hv-textarea");
expect(el.classes()).toContain("hv-textarea--secondary");
expect(el.classes()).toContain("hv-textarea--full-width");
});
it("defaults rows to 3", () => {
const w = mount(TextArea);
expect((w.get("textarea").element as HTMLTextAreaElement).rows).toBe(3);
});
it("submits FormData, respects disabled/readonly and resets uncontrolled values", async () => {
const w = mount(
defineComponent({
render: () =>
h(Form, {}, () => [
h(TextField, { name: "bio", defaultValue: "start" }, () => [
h(TextArea),
]),
h(
TextField,
{ name: "off", defaultValue: "no", isDisabled: true },
() => [h(TextArea)],
),
h(
TextField,
{ name: "ro", defaultValue: "yes", isReadOnly: true },
() => [h(TextArea)],
),
]),
}),
{ attachTo: document.body },
);
const areas = w.findAll("textarea");
await areas[0].setValue("changed");
expect(
Object.fromEntries(
new FormData(w.get("form").element as HTMLFormElement),
),
).toEqual({ bio: "changed", ro: "yes" });
(w.get("form").element as HTMLFormElement).reset();
await settle();
expect((areas[0].element as HTMLTextAreaElement).value).toBe("start");
w.unmount();
});
it("restores DOM value when the parent rejects updates", async () => {
const value = ref("fixed");
const w = mount(
defineComponent({
components: { TextField, TextArea },
setup: () => ({ value }),
template: `<TextField :model-value="value"><TextArea /></TextField>`,
}),
{ attachTo: document.body },
);
const area = w.get("textarea");
await area.setValue("typing");
await settle();
expect((area.element as HTMLTextAreaElement).value).toBe("fixed");
w.unmount();
});
it("respects canceled form reset", async () => {
const w = mount(
defineComponent({
render: () =>
h(Form, {}, () => [
h(TextField, { name: "t", defaultValue: "init" }, () => [
h(TextArea),
]),
]),
}),
{ attachTo: document.body },
);
const area = w.get("textarea");
await area.setValue("edited");
const form = w.get("form").element as HTMLFormElement;
form.addEventListener("reset", (e) => e.preventDefault());
form.reset();
await settle();
expect((area.element as HTMLTextAreaElement).value).toBe("edited");
w.unmount();
});
it("associates Label, Description and FieldError through TextField", async () => {
const w = mount(
defineComponent({
render: () =>
h(TextField, { isInvalid: true, name: "x" }, () => [
h(Label, {}, () => "Bio"),
h(TextArea, { rows: 2 }),
h(Description, {}, () => "hint"),
h(FieldError),
]),
}),
{ attachTo: document.body },
);
await settle();
const area = w.get("textarea");
const label = w.get("label");
expect(label.attributes("for")).toBe(area.attributes("id"));
const described = area.attributes("aria-describedby")!.split(" ");
expect(described.length).toBeGreaterThanOrEqual(2);
for (const id of described) {
expect(w.find(`[id="${id}"]`).exists()).toBe(true);
}
expect(w.find("[data-slot=field-error]").exists()).toBe(true);
w.unmount();
});
it("keeps InputGroup integration working next to TextArea", async () => {
const w = mount(
defineComponent({
render: () =>
h(Form, {}, () => [
h(TextField, { name: "email", defaultValue: "a@b.c" }, () => [
h(InputGroup, {}, () => [h(InputGroupInput)]),
]),
h(TextField, { name: "note", defaultValue: "hi" }, () => [
h(TextArea, { rows: 2 }),
]),
]),
}),
{ attachTo: document.body },
);
expect(
Object.fromEntries(
new FormData(w.get("form").element as HTMLFormElement),
),
).toEqual({ email: "a@b.c", note: "hi" });
w.unmount();
});
it("keeps public prop types constrained", async () => {
const { TextArea: Area } = await import("../packages/vue/src");
// @ts-expect-error rows must be a number
const badRows: InstanceType<typeof Area>["$props"]["rows"] = "3";
// @ts-expect-error variant must be primary | secondary
const badVariant: InstanceType<typeof Area>["$props"]["variant"] = "ghost";
void badRows;
void badVariant;
const ok: InstanceType<typeof Area>["$props"] = {
rows: 6,
variant: "secondary",
fullWidth: true,
modelValue: "x",
};
expect(ok.rows).toBe(6);
});
});
describe("M13 field docs demos", () => {
const expected: Record<string, string[]> = {
textfield: [
"basic",
"on-surface",
"with-description",
"required",
"disabled",
"full-width",
"validation",
"controlled",
"with-error",
"textarea",
"input-types",
"render-function",
"text-field-custom-styles",
],
label: ["basic", "custom-styles"],
description: ["basic", "custom-styles"],
"field-error": ["basic", "custom-styles"],
textarea: [
"basic",
"variants",
"on-surface",
"full-width",
"controlled",
"rows",
"custom-styles",
],
};
const docFiles: Record<string, string> = {
textfield: "text-field",
label: "label",
description: "description",
"field-error": "field-error",
textarea: "textarea",
};
it("includes every preview from the pinned upstream documents", () => {
for (const [key, names] of Object.entries(expected)) {
const docName = docFiles[key];
const document = readFileSync(
`apps/docs/components/${docName}.md`,
"utf8",
);
const previewNames = [
...document.matchAll(/<ComponentPreview name="([^"]+)"/g),
].map((m) => m[1]);
const ids =
key === "textfield"
? names.map((n) =>
n === "text-field-custom-styles" ? n : `textfield-${n}`,
)
: names.map((n) => `${key}-${n}`);
expect(previewNames).toEqual(ids);
for (const id of ids) {
const source = readFileSync(`apps/docs/demos/${id}.vue`, "utf8");
expect(source).toContain("<template>");
}
}
});
it("documents Vue class binding and slots instead of React APIs", () => {
for (const docName of [
"text-field",
"label",
"description",
"field-error",
"textarea",
]) {
const document = readFileSync(
`apps/docs/components/${docName}.md`,
"utf8",
);
expect(document).not.toMatch(/\|\s*`className`\s*\|/);
expect(document).not.toMatch(/\|\s*`children`\s*\|/);
expect(document).toContain("`class`");
}
const fieldError = readFileSync(
"apps/docs/components/field-error.md",
"utf8",
);
expect(fieldError).toContain("validationErrors");
expect(fieldError).toContain("不提供完整 React Aria `validationDetails`");
expect(fieldError).toContain("作用域插槽");
});
it("keeps the three shared textfield demos and new pages aligned", () => {
for (const name of [
"textfield-basic",
"textfield-with-description",
"textfield-validation",
]) {
const source = readFileSync(`apps/docs/demos/${name}.vue`, "utf8");
expect(source).toContain("TextField");
}
const fieldsGuide = readFileSync("apps/docs/fields.md", "utf8");
expect(fieldsGuide).toContain("textfield-basic");
// Independent pages exist alongside the infrastructure guide
for (const page of [
"text-field",
"label",
"description",
"field-error",
"textarea",
]) {
expect(readFileSync(`apps/docs/components/${page}.md`, "utf8")).toContain(
"ComponentPreview",
);
}
});
it("uses Tabler-free demos with library parts for field docs", () => {
for (const name of [
"label-basic",
"description-basic",
"field-error-basic",
"textarea-basic",
]) {
const source = readFileSync(`apps/docs/demos/${name}.vue`, "utf8");
expect(source).not.toContain("className");
expect(source).not.toContain("@gravity-ui/icons");
expect(source).toContain("@heroui-vue/components");
}
});
});
function extractVueBlocks(markdown: string): string[] {
return [...markdown.matchAll(/```vue\n([\s\S]*?)```/g)].map((m) => m[1]);
}
function scriptBindings(block: string): Set<string> {
const script =
block.match(/<script setup lang="ts">([\s\S]*?)<\/script>/)?.[1] ?? "";
const names = new Set<string>();
for (const m of script.matchAll(
/(?:const|let|function)\s+([A-Za-z_$][\w$]*)/g,
)) {
names.add(m[1]);
}
return names;
}
function templateIdentifiers(block: string): string[] {
const template = block.match(/<template>([\s\S]*)<\/template>/)?.[1] ?? block;
const ids: string[] = [];
// Only bindings that must come from <script setup>, not slot/v-for locals.
for (const m of template.matchAll(
/(?:v-model|:is-invalid|:validate|:model-value)="([^"]+)"/g,
)) {
const expr = m[1];
// Inline arrows/closures define their own params.
if (expr.includes("=>")) continue;
for (const id of expr.matchAll(/[A-Za-z_$][\w$]*/g)) {
const name = id[0];
if (name === "true" || name === "false" || name === "null") continue;
// Skip property access tails like value.length
const before = expr.slice(Math.max(0, id.index! - 1), id.index!);
if (before === ".") continue;
ids.push(name);
}
}
return ids;
}
describe("M13 field-error body examples", () => {
const markdown = readFileSync("apps/docs/components/field-error.md", "utf8");
const blocks = extractVueBlocks(markdown);
it("checks common script bindings in full SFC blocks", () => {
const fullSfcs = blocks.filter((b) => b.includes("<template>"));
expect(fullSfcs.length).toBeGreaterThanOrEqual(4);
for (const block of fullSfcs) {
const bindings = scriptBindings(block);
for (const id of templateIdentifiers(block)) {
if (id === "true" || id === "false") continue;
expect(
bindings.has(id),
`template identifier "${id}" is not defined in script`,
).toBe(true);
}
expect(block).not.toContain("hasErrors");
}
});
it("dynamic error example uses FieldError slot and a real error source", () => {
const dynamic = blocks.find((b) => b.includes("validatePassword"));
expect(dynamic).toBeTruthy();
expect(dynamic!.includes("validationErrors")).toBe(true);
expect(dynamic!.includes("FieldError")).toBe(true);
expect(dynamic!.includes("validate")).toBe(true);
expect(dynamic!.includes('validation-behavior="aria"')).toBe(true);
});
it("renders dynamic and multi-error messages from validate", async () => {
const DynamicDemo = defineComponent({
components: { TextField, Label, Input, FieldError },
setup() {
const password = ref("");
function validatePassword(value: string) {
if (value.length === 0) return true;
return value.length < 8 ? "密码至少需要 8 个字符" : true;
}
return { password, validatePassword };
},
template: `
<TextField
v-model="password"
name="password"
validation-behavior="aria"
:validate="validatePassword"
>
<Label>密码</Label>
<Input type="password" />
<FieldError>
<template #default="{ validationErrors }">
{{ validationErrors.join(",") }}
</template>
</FieldError>
</TextField>
`,
});
const w = mount(DynamicDemo, { attachTo: document.body });
await w.get("input").setValue("abc");
await settle();
expect(w.get("[data-slot=field-error]").text()).toContain(
"密码至少需要 8 个字符",
);
await w.get("input").setValue("long-enough-Password");
await settle();
expect(w.find("[data-slot=field-error]").exists()).toBe(false);
w.unmount();
const MultiDemo = defineComponent({
components: { TextField, Label, Input, FieldError },
setup() {
const password = ref("");
function validatePassword(value: string) {
if (value.length === 0) return true;
const errors: string[] = [];
if (value.length < 8) errors.push("密码至少需要 8 个字符");
if (!/[A-Z]/.test(value)) errors.push("需要包含一个大写字母");
return errors.length ? errors : true;
}
return { password, validatePassword };
},
template: `
<TextField
v-model="password"
name="password"
validation-behavior="aria"
:validate="validatePassword"
>
<Label>密码</Label>
<Input type="password" />
<FieldError>
<template #default="{ validationErrors }">
<div v-for="(error, i) in validationErrors" :key="i">{{ error }}</div>
</template>
</FieldError>
</TextField>
`,
});
const m = mount(MultiDemo, { attachTo: document.body });
await m.get("input").setValue("abc");
await settle();
const text = m.get("[data-slot=field-error]").text();
expect(text).toContain("密码至少需要 8 个字符");
expect(text).toContain("需要包含一个大写字母");
expect(m.findAll("[data-slot=field-error] div")).toHaveLength(2);
m.unmount();
});
});
describe("M13 TextField and TextArea public API docs", () => {
it("documents autoComplete, inputMode, form, events and expose on TextField", () => {
const doc = readFileSync("apps/docs/components/text-field.md", "utf8");
expect(doc).toContain("`autoComplete`");
expect(doc).toContain("`inputMode`");
expect(doc).toContain("`form`");
expect(doc).toContain("update:modelValue");
expect(doc).toContain("focus");
expect(doc).toContain("element");
});
it("documents focus/element and value updates on TextArea", () => {
const doc = readFileSync("apps/docs/components/textarea.md", "utf8");
expect(doc).toContain("update:modelValue");
expect(doc).toContain("focus");
expect(doc).toContain("element");
expect(doc).toContain("`rows`");
});
it("keeps TextField autoComplete/inputMode/form wired to the real control", async () => {
const w = mount(
defineComponent({
components: { TextField, TextArea },
template: `
<TextField
model-value="x"
name="email"
type="email"
auto-complete="email"
input-mode="email"
form="outer"
>
<TextArea />
</TextField>
`,
}),
{ attachTo: document.body },
);
const area = w.get("textarea");
expect(area.attributes("autocomplete")).toBe("email");
expect(area.attributes("inputmode")).toBe("email");
expect(area.attributes("form")).toBe("outer");
w.unmount();
});
it("exposes focus and element on TextField and TextArea", async () => {
const areaRef = ref();
const fieldRef = ref();
const wrap = mount(
defineComponent({
components: { TextField, TextArea },
setup: () => ({ areaRef, fieldRef }),
template: `
<TextField ref="fieldRef" model-value="a">
<TextArea ref="areaRef" />
</TextField>
`,
}),
{ attachTo: document.body },
);
await settle();
const areaInstance = areaRef.value;
expect(typeof areaInstance?.focus).toBe("function");
const native = wrap.get("textarea").element;
// Vue unwraps exposed refs on the public component instance.
expect(areaInstance.element).toBe(native);
expect(fieldRef.value.element).toBe(native);
areaInstance.focus();
expect(document.activeElement).toBe(native);
(native as HTMLTextAreaElement).blur();
fieldRef.value.focus();
expect(document.activeElement).toBe(native);
wrap.unmount();
});
});