131 lines
3.8 KiB
Markdown
131 lines
3.8 KiB
Markdown
---
|
||
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)
|