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 完整消费验收尚未进行。