Files
heroui-vue/apps/docs/components/label.md
T
maoyingjie 53e76f676d fix(demos): restore form, overlay and toast demos to the pinned cn baseline
HD-02 scope, all against heroui ce90ea3 cn sources:

- card: add the text-primary class to the coin icons (source parity);
  card-with-form drops the added required validation and inline business
  note, restores the plain href="#" forgot-password link and the baseline
  alert("表单提交成功!") submit feedback (autocomplete kept: no visible
  impact, allowed by the task card)
- input-group-with-textarea: restore the four Tooltips and the Enter Kbd
  hint; drop the stale "Tooltip 组件尚未迁移" note from input-group.md
- alert-dialog: restore the h-px top light line in custom-styles and the
  truncated/missing description text in sizes/custom-portal/
  custom-animations (+ controlled); align the fluid-slide icon with the
  modal mapping (IconArrowBarUp for gravity-ui:arrow-up-from-line)
- toast: restore the upstream demo layout shells (variants/simple/
  custom-indicator/custom-queue) and the action button default variant:
  ToastActionButton now falls back to the Button defaults (primary/md)
  like upstream ToastActionButton, so variants' actions render primary,
  danger stays danger and default stays tertiary; toast.md defaults fixed
- submit demos: restore baseline feedback and structure — alert() for
  card/fieldset/form/checkbox/checkbox-group/switch/select/combo-box/
  autocomplete and console.log for search/number/time field form examples;
  modal-with-form returns to the close-only footer (no submit/result row);
  drop the added inline status/result rows and the max-w-full/
  max-sm:min-w-0 width guards from the restored demos
- radio-group-validation: add the missing text-muted to the result line;
  label.md invalid example gives the real control its invalid state and
  matches the baseline input shape

Tests re-pinned to the baseline contracts (alert/console/close behavior,
four tooltips + Kbd, positional modal footer); full suite 110 files /
1617 tests, typecheck, build:docs and the m35-m36 tarball contract pass.
2026-10-28 14:10:10 +08:00

3.8 KiB
Raw Blame History

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" invalid />
</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> 元素
  • 与表单控件关联时支持键盘导航
  • 向屏幕阅读器传达必填与无效状态
  • 点击标签可聚焦/激活关联的表单控件

相关组件

TextField 文本输入框 · Description 描述 · FieldError 字段错误