Files
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
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
2026-10-28 22:32:10 +08:00

270 lines
11 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Checkbox 复选框
description: 复选框允许用户从列表中选择多项,或标记单个项目为选中状态
icon: updated
component: checkbox
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Checkbox,
CheckboxContent,
CheckboxControl,
CheckboxIndicator,
Label,
Description,
FieldError,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview
name="checkbox-basic"
/>
## 组件结构
```vue
<script setup lang="ts">
import {
Checkbox,
CheckboxContent,
CheckboxControl,
CheckboxIndicator,
Label,
Description,
FieldError,
} from "@heroui-vue/components";
</script>
<template>
<Checkbox>
<CheckboxContent>
<CheckboxControl><CheckboxIndicator /></CheckboxControl>
<Label>选项</Label>
</CheckboxContent>
<Description />
</Checkbox>
</template>
```
## 示例
### 变体
Checkbox 组件支持两种视觉变体:
- **`primary`**(默认)- 标准样式与默认背景,适用于大多数场景
- **`secondary`** - 低强调变体,适用于 Surface 组件内
<ComponentPreview name="checkbox-variants" />
### 全圆角
<ComponentPreview
name="checkbox-full-rounded"
/>
### 禁用
<ComponentPreview
name="checkbox-disabled"
/>
### 外部标签
<ComponentPreview
name="checkbox-external-label"
/>
### 带描述
<ComponentPreview
name="checkbox-with-description"
/>
### 默认选中
<ComponentPreview
name="checkbox-default-selected"
/>
### 无效状态
<ComponentPreview
name="checkbox-invalid"
/>
### 受控组件
<ComponentPreview
name="checkbox-controlled"
/>
### 半选状态
<ComponentPreview
name="checkbox-indeterminate"
/>
### 表单集成
<ComponentPreview
name="checkbox-form"
/>
### 渲染属性
<ComponentPreview
name="checkbox-render-props"
/>
### 渲染函数
<ComponentPreview
name="checkbox-render-function"
/>
### 自定义指示器
<ComponentPreview
name="checkbox-custom-indicator"
/>
## 自定义样式
### Tailwind CSS
<ComponentPreview name="checkbox-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 Checkbox 组件类。
[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
.hv-checkbox {
@apply inline-flex gap-3 items-center;
}
.hv-checkbox__control {
@apply size-5 border-2 border-gray-400 rounded data-[selected=true]:bg-blue-500 data-[selected=true]:border-blue-500;
/* Animated background indicator */
&::before {
@apply bg-accent pointer-events-none absolute inset-0 z-0 origin-center scale-50 rounded-md opacity-0 content-[''];
transition:
scale 200ms linear,
opacity 200ms linear,
background-color 200ms ease-out;
}
/* Show indicator when selected */
&[data-selected="true"]::before {
@apply scale-100 opacity-100;
}
}
.hv-checkbox__indicator {
@apply text-white;
}
.hv-checkbox__content {
@apply items-center gap-3;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
Checkbox 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/checkbox.css)):
#### 基础类
- `.hv-checkbox` - 基础复选框容器(字段)
- `.hv-checkbox__content` - 包裹控件与标签文本的可点击 label
- `.hv-checkbox__control` - 复选框控件框
- `.hv-checkbox__indicator` - 复选框勾选指示器
### 交互状态
复选框同时支持 CSS 伪类与 data 属性:
- **Selected**:`[data-selected="true"]` 或 `[aria-checked="true"]`(显示勾选与背景色变化)
- **Indeterminate**:`[data-indeterminate="true"]`(显示不确定状态的短横线)
- **Invalid**:`[data-invalid="true"]` 或 `[aria-invalid="true"]`(显示 danger 色错误状态)
- **Hover**:`CheckboxContent`(可点击标签)上的 `:hover` 或 `[data-hovered="true"]`
- **Focus**:按钮上的 `:focus-visible` 或 `[data-focus-visible="true"]`(控件上显示焦点环)
- **Disabled**:字段上的 `[data-disabled="true"]`(降低透明度,包括帮助文本)
- **Pressed**:`:active` 或 `[data-pressed="true"]`
## API 参考
本库采用原生 label/input 承载选择与提交,复用共享字段、Form、Fieldset 禁用和错误协议。原生输入是唯一的提交值来源;组内没有 Reka 的重复隐藏值或带索引字段名。组校验另有无 name、不可 Tab 聚焦的约束代理,不产生 FormData 条目。
### Checkbox
| 属性 | 类型 | 默认值 | 描述 |
| ------------------------------------ | --------------------------------------------------------------------- | -------------------------- | ----------------------------------------------------------------------- |
| `checked` | `boolean` | 非受控 | 当前选择,推荐 `v-model:checked` |
| `isSelected` | `boolean` | - | 保留上游受控属性,支持 `v-model:isSelected`;同时传值时 checked 优先 |
| `defaultChecked` / `defaultSelected` | `boolean` | `false` | 非受控初始值;defaultChecked 优先 |
| `isIndeterminate` | `boolean` | `false` | 独立的半选外观;不将 mixed 字符串提交。点击后是否解除由调用方更新该属性 |
| `value` | `string` | `'on'` | 原生提交值;组内应显式指定唯一 value |
| `name` / `form` / `id` | `string` | - / - / 自动 | 落在真实 checkbox;组内 name/form 由组统一管理 |
| `variant` | `'primary' \| 'secondary'` | 继承组,否则 primary | 显式子变体优先 |
| `isDisabled` / `disabled` | `boolean` | `false` | 禁用,不能解除组/Fieldset 禁用 |
| `isReadOnly` | `boolean` | `false` | 保持可聚焦,阻止鼠标和键盘更新 |
| `isRequired` / `isInvalid` | `boolean` | `false` | 必填约束 / 纯展示错误状态;后者不单独阻止提交 |
| `validate` | `(value: boolean) => string \| string[] \| true \| null \| undefined` | - | 同步校验 |
| `validationBehavior` | `'native' \| 'aria'` | 继承组或 Form,否则 native | native 拦截;aria 只显示语义 |
| `class` / `style` | Vue class/style 绑定 | - | 字段容器;ARIA 和 DOM 监听落在真实 input |
| 默认插槽 | 作用域插槽 | 默认 control/indicator | 字段部件与状态 |
### 事件与暴露
| 接口 | 参数 / 行为 |
| ------------------------------------------------- | -------------------------------------------------------- |
| `update:checked` / `update:isSelected` / `change` | 每次修改请求发出 boolean;受控拒绝后恢复原生 checked |
| `focus()` / `element` | 模板 ref 暴露;实际 HTMLInputElement(公开实例自动解包) |
默认控件空内容时需提供 aria-label;推荐显式组合 Content 与文案。原生 Space 切换;外部 Label 的 for 与 Checkbox id 对应真实输入。禁用项不提交,未选中项不提交,非受控 reset 恢复初始值;取消 reset 保留当前值。
### CheckboxContent / Control / Indicator
| 部件 | 根与插槽 | 属性落点 |
| --------- | ------------------------------------------ | ------------------------------------------ |
| Content | 原生 label,包含实际 input;默认作用域插槽 | class/style/原生 label 属性及事件 |
| Control | span,默认插槽 | class/style/DOM 属性与事件 |
| Indicator | span,默认作用域插槽 | 装饰 aria-hidden;自定义内容替换基线指示器 |
具名导出使用 CheckboxContent、CheckboxControl、CheckboxIndicator;不使用 React 对象式子部件。class 均支持字符串、数组和对象。默认图形精确来自基线,文档自定义图标使用 Tabler。Content 输出 selected/indeterminate/hovered/pressed/focus-visible 状态标记供定制样式使用。
### 作用域插槽状态
| 参数 | 类型 | 说明 |
| ---------------------------------------------------------- | --------- | ------------------------------ |
| `isSelected` / `isIndeterminate` | `boolean` | 选中 / 半选(半选仅 Checkbox) |
| `isDisabled` / `isReadOnly` / `isRequired` / `isInvalid` | `boolean` | 当前状态 |
| `isHovered` / `isPressed` / `isFocused` / `isFocusVisible` | `boolean` | 单成员交互状态 |
组作用域提供 value、validationErrors、isDisabled/isReadOnly/isRequired/isInvalid,以及 state.value/state.setValue;单成员另提供 state.isSelected/state.setSelected/state.toggle。不把组值和单成员状态混用。
### 框架适配与边界
render 自定义根类型未支持;基线 div+data-custom 示例以 Vue 根属性透传保留效果。SSR 预规划直接/原生/Fragment 包裹的标签、说明、错误与直接成员;本库 Content 内的说明也支持。任意用户 Vue 包装组件无法静态展开,需要显式 aria-labelledby/aria-describedby 和 ID 保证首屏关联。没有宣称完整读屏、触屏、Nuxt 或逐像素验收。
## 相关组件
[Label 标签](./label) · [CheckboxGroup 复选框组](./checkbox-group) · [Description 描述](./description)