3.8 KiB
3.8 KiB
title, description, component, group
| title | description | component | group |
|---|---|---|---|
| Label 标签 | 渲染与表单控件关联的无障碍标签。 | label | 组件 |
用法
<script setup lang="ts">
import { Label, Input } from "@heroui-vue/components";
</script>
示例
带必填指示符
<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>
禁用状态
<script setup lang="ts">
import { Label, Input } from "@heroui-vue/components";
</script>
<template>
<Label for="username" is-disabled>用户名</Label>
<Input id="username" disabled />
</template>
无效状态
<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
全局 CSS
若要自定义组件类,可使用 @layer components 指令。
@layer components {
.hv-label {
@apply font-semibold text-foreground;
}
.hv-label--required::after {
@apply text-accent;
}
}
样式参考
HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 hv-。
CSS 类
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>元素 - 与表单控件关联时支持键盘导航
- 向屏幕阅读器传达必填与无效状态
- 点击标签可聚焦/激活关联的表单控件