145a208687
HD-04 scope, against heroui ce90ea3 cn sources: - stale/fictional content: input.md no longer claims TextField is unmigrated; color-field.md no longer claims ColorPicker is private; header.md drops the fictional Header.Root subcomponent and the stale 'menus not migrated' wording (MenuSection label registration is now documented from HeaderRoot source); radio-group.md drops three copied CheckboxGroup wordings (variant override is Radio's, scalar emits, native single-value submit); input-otp.md drops the fictional data-complete state and lists the real missing upstream props - toggle-button disabled row now matches the real CSS hooks (:disabled / [aria-disabled], with data-disabled as a marker only) - part API / CSS classes / interactive states restored per pinned sources: button (full class lists + real variable-based global CSS), accordion (per-part tables + variant/state lists), card (per-part tables + variant list), switch (SwitchGroup snippet + Switch/ SwitchGroup class lists + states), color-field Group (class list + states), input-otp (variant notes, global CSS, class/state lists); input-group gains the two upstream usage chapters - RelatedComponents restored from the pinned componentRelationships (first-3, filtered against componentsMap): 64 pages rewritten, list-box/slider sections added, 7 empty-mapping pages' fabricated entries removed; RelatedShowcases left unmigrated with its real data inventory recorded (2 cases, showcase-site cards) - defaults unified on implementation evidence: granularity (inferred day/minute) and visibleYears (full min-max span, no "or 20" fallback); HD-03 hand-offs resolved (trigger focus-visible noted for HD-06, width guards kept) - new demos: header-collection-grouping (MenuSection/ListBoxSection label wiring), empty-state-collection (collection empty slot); empty-state page deduplicated with a boundary compatibility note - ComponentPreview minHeight/isBgSolid restored per the 30 upstream marks; tabs-overflow max-w-full kept after measuring internal scroll - tests: +3 header registration/demo cases, +1 empty-state collection case; full suite 110 files / 1630 tests, typecheck and build:docs pass
131 lines
3.7 KiB
Markdown
131 lines
3.7 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" invalid />
|
||
</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>` 元素
|
||
- 与表单控件关联时支持键盘导航
|
||
- 向屏幕阅读器传达必填与无效状态
|
||
- 点击标签可聚焦/激活关联的表单控件
|
||
|
||
## 相关组件
|
||
|
||
[Input 输入框](./input) · [TextArea 多行文本框](./textarea) · [Fieldset 字段组](./fieldset)
|