Files
heroui-vue/apps/docs/components/checkbox-group.md
T
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

204 lines
9.0 KiB
Markdown
Raw 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: CheckboxGroup 复选框组
description: 用于管理多个复选框选择的复选框组组件
icon: updated
component: checkbox-group
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Checkbox,
CheckboxContent,
CheckboxControl,
CheckboxIndicator,
Label,
Description,
FieldError,
CheckboxGroup,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview
name="checkbox-group-basic"
/>
## 组件结构
```vue
<script setup lang="ts">
import {
Checkbox,
CheckboxContent,
CheckboxControl,
CheckboxIndicator,
Label,
Description,
FieldError,
CheckboxGroup,
} from "@heroui-vue/components";
</script>
<template>
<CheckboxGroup>
<Label>选择选项</Label>
<Checkbox value="option">
<CheckboxContent>
<CheckboxControl><CheckboxIndicator /></CheckboxControl>
<Label>选项</Label>
</CheckboxContent>
<Description />
</Checkbox>
<FieldError />
</CheckboxGroup>
</template>
```
## 示例
### 表面样式
在 [Surface](/components/surface) 组件内使用时,请使用 `variant="secondary"` 以应用适合 Surface 背景的低强调变体。
<ComponentPreview
name="checkbox-group-on-surface"
/>
### 禁用
<ComponentPreview
name="checkbox-group-disabled"
/>
### 半选状态
<ComponentPreview
name="checkbox-group-indeterminate"
/>
### 受控组件
<ComponentPreview
name="checkbox-group-controlled"
/>
### 表单校验
<ComponentPreview
name="checkbox-group-validation"
/>
### 功能扩展示例
<ComponentPreview
name="checkbox-group-features-and-addons"
/>
### 自定义指示器
<ComponentPreview
name="checkbox-group-with-custom-indicator"
/>
### 渲染函数
<ComponentPreview
name="checkbox-group-render-function"
/>
## 自定义样式
### Tailwind CSS
<ComponentPreview name="checkbox-group-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 CheckboxGroup 组件类。
[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
```css
@layer components {
.hv-checkbox-group {
@apply flex flex-col gap-2;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
CheckboxGroup 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/checkbox-group.css)):
#### 基础类
- `.hv-checkbox-group` - 复选框组根容器
## API 参考
本库采用原生 label/input 承载选择与提交,复用共享字段、Form、Fieldset 禁用和错误协议。原生输入是唯一的提交值来源;组内没有 Reka 的重复隐藏值或带索引字段名。组校验另有无 name、不可 Tab 聚焦的约束代理,不产生 FormData 条目。
### CheckboxGroup
| 属性 | 类型 | 默认值 | 描述 |
| -------------------------- | ---------------------------------------------------------------------- | ---------------------- | -------------------------------------- |
| `modelValue` | `string[]` | 非受控 | 当前值,使用 v-model |
| `defaultValue` | `string[]` | `[]` | 非受控初始值副本 |
| `name` / `form` / `id` | `string` | - / - / 自动 | 统一原生提交键、表单归属及组 ID |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 组视觉样式;Checkbox 成员可显式覆盖 |
| `isDisabled` / `disabled` | `boolean` | `false` | 合并 Fieldset 禁用,后代不能解除 |
| `isReadOnly` | `boolean` | `false` | 阻止成员点击、空格和方向键修改 |
| `isRequired` / `isInvalid` | `boolean` | `false` | 要求至少一项可用选择 / 错误展示 |
| `validate` | `(value: string[]) => string \| string[] \| true \| null \| undefined` | - | 同步组校验 |
| `validationBehavior` | `'native' \| 'aria'` | 继承 Form,否则 native | 原生提交拦截或仅错误语义 |
| `class` / `style` | Vue class/style 绑定 | - | 合并根样式,接受字符串/数组/对象 class |
| 默认插槽 | 作用域插槽 | - | 标签、说明、成员、错误 |
### 事件与暴露
| 接口 | 行为 |
| ------------------------------ | ---------------------------------------------------------------------- |
| `update:modelValue` / `change` | 发出新的数组或标量;不修改传入数组;父级拒绝后恢复所有原生 checked |
| `focus()` / `element` | 聚焦选中可用成员或首个可用成员;空组聚焦组根。element 是组 HTMLElement |
CheckboxGroup 按原生约定提交同名重复条目,通过 FormData.getAll(name) 获取;RadioGroup 只提交一个选中可用值。未指定 name 不产生提交值。每个成员 value 必须唯一;重复或动态改成重复值会抛出明确错误。不为移除的选择自动选择别的成员;模型保留调用者的值,FormData 只包含实际存在的可用成员。
required 检查整个组,不能让每个 Checkbox 都变成必选;native 无效提交聚焦第一个可用成员,空组聚焦根。只读保留提交值并跳过必填/自定义约束。reset 尊重 preventDefault,受控值不覆盖,非受控恢复初始副本。动态 name/form 会更新提交和 reset 归属。
Label 在组中渲染 span 并注册 aria-labelledby;不要手写 for 指向组 div。每个成员独立隔离 Description/FieldError,同时合并组描述和当前组错误。后端错误使用 Form.validationErrors 按 name 映射,编辑后忽略当前响应,新对象可恢复错误。暂不提供异步 validate 或完整 validationDetails。
### 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 或逐像素验收。
## 相关组件
[Checkbox 复选框](./checkbox) · [Label 标签](./label) · [Fieldset 字段组](./fieldset)