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
+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)