feat: complete field documentation and TextArea examples
This commit is contained in:
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user