---
title: "Checkbox Group"
description: "A group of checkboxes that binds to FormField context."
url: https://weme-ui.github.io/slim/components/checkbox-group.md
---
# Checkbox Group

复选框组。可以单独用，也可以放进 `FormField`。有 `FormField` 时会合并 `id` / `name` / `aria-*` / `data-*`。基于 Reka UI `CheckboxGroupRoot`，按 `items` 渲染多个 `Checkbox`，支持外观变体、方向、最大可选数与 roving focus。

## Installation

```bash
pnpm dlx @weme-ui/weme-ui add checkbox-group
```

## Usage

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import * as v from 'valibot'
import Button from '../../button/button.vue'
import FormField from '../../form-field/form-field.vue'
import Form from '../../form/form.vue'
import CheckboxGroup from '../checkbox-group.vue'

const schema = v.object({
  interests: v.pipe(v.array(v.string()), v.minLength(1, '至少选一项')),
})

const defaultValues = {
  interests: [] as string[],
}

const items: CheckboxGroupItem[] = [
  { label: 'Design', value: 'design' },
  { label: 'Engineering', value: 'engineering' },
  { label: 'Product', value: 'product' },
]

function onFormSubmit(values: { interests: string[] }) {
  // eslint-disable-next-line no-alert
  alert(JSON.stringify(values))
}
</script>

<template>
  <Form
    class="max-w-sm w-full"
    :default-values="defaultValues"
    :schema="schema"
    @form-submit="onFormSubmit"
  >
    <FormField v-slot="{ field }" name="interests" label="Interests" :native-label="false" required>
      <CheckboxGroup
        :model-value="field.state.value"
        :items="items"
        @update:model-value="field.api.handleChange"
      />
    </FormField>
    <Button type="submit"> Submit </Button>
  </Form>
</template>
```

## Props

| 属性                     | 说明                                                                                                                                                 | 类型                               | 默认值               |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------- | -------------------- |
| `as`                     | 根节点渲染为的元素或组件（Reka UI `Primitive` / RovingFocus）                                                                                        | `AsTag \| Component`               | `'div'`              |
| `items`                  | 选项列表；单项含 `value`（`string \| number`）、`label`、`description`、`disabled`、`class`，以及该项 `checkedIcon` / `indeterminateIcon` 及其 class | `CheckboxGroupItem[]`              | —                    |
| `modelValue`             | 受控选中值数组；可用 `v-model`                                                                                                                       | `CheckboxGroupValue[]`             | —                    |
| `defaultValue`           | 非受控初始选中值数组                                                                                                                                 | `CheckboxGroupValue[]`             | —                    |
| `max`                    | 最大可选数量；达到后未选项会 `aria-disabled`                                                                                                         | `number \| null`                   | —                    |
| `rovingFocus`            | 是否启用组内方向键漫游焦点                                                                                                                           | `boolean`                          | `true`               |
| `loop`                   | roving focus 是否循环                                                                                                                                | `boolean`                          | —                    |
| `dir`                    | 阅读方向（影响水平漫游）                                                                                                                             | `'ltr' \| 'rtl'`                   | —                    |
| `variant`                | 外观变体（下传到各项 `Checkbox`）                                                                                                                    | `'soft' \| 'outline' \| 'card'`    | `'soft'`             |
| `size`                   | 尺寸                                                                                                                                                 | `'sm' \| 'md' \| 'lg'`             | `'md'`               |
| `orientation`            | 布局与漫游方向                                                                                                                                       | `'horizontal' \| 'vertical'`       | `'vertical'`         |
| `align`                  | 指示器与文案交叉轴对齐                                                                                                                               | `'start' \| 'center' \| 'end'`     | `'start'`            |
| `indicator`              | 指示器位置；`none` 时仅保留无障碍指示器（可视隐藏）；下传到各项 `Checkbox`                                                                           | `'start' \| 'end' \| 'none'`       | `'start'`            |
| `checkedIcon`            | 选中态图标（Iconify）；下传到各项 `Checkbox`                                                                                                         | `IconifyIconProps['icon']`         | `'ri:check-line'`    |
| `checkedIconClass`       | 选中态图标额外 class                                                                                                                                 | `any`                              | —                    |
| `indeterminateIcon`      | 半选态图标（Iconify）；下传到各项 `Checkbox`                                                                                                         | `IconifyIconProps['icon']`         | `'ri:subtract-line'` |
| `indeterminateIconClass` | 半选态图标额外 class                                                                                                                                 | `any`                              | —                    |
| `inverted`               | 反色样式变体                                                                                                                                         | `boolean`                          | —                    |
| `unstyled`               | 移除样式变体类，仅保留结构                                                                                                                           | `boolean`                          | —                    |
| `id`                     | 控件 `id` 前缀；未传时回退到 FormField Context；各项为 `` `${id}-${value}` ``                                                                        | `string`                           | —                    |
| `name`                   | 控件 `name`；未传时回退到 FormField Context；各项为 `` `${name}[]` ``                                                                                | `string`                           | —                    |
| `required`               | 是否必填；会与 FormField Context 的 `required` 合并                                                                                                  | `boolean`                          | —                    |
| `disabled`               | 是否禁用整组；会与 FormField Context 的 `disabled` 合并                                                                                              | `boolean`                          | —                    |
| `invalid`                | 是否无效；会与 FormField Context 的 `invalid` 合并                                                                                                   | `boolean`                          | —                    |
| `class`                  | 根节点额外 class                                                                                                                                     | `any`                              | —                    |
| `ui`                     | 覆盖 slots 样式（`root` / `item` / `indicator` / `checkedIcon` / `indeterminateIcon` / `content` / `label` / `description`）                         | `Partial<CheckboxGroupStyleSlots>` | —                    |

## Events

| 事件                | 说明                                                                 | 参数                          |
| ------------------- | -------------------------------------------------------------------- | ----------------------------- |
| `update:modelValue` | 选中集合变更时触发（含 `v-model`）；由 Reka `CheckboxGroupRoot` 转发 | `value: CheckboxGroupValue[]` |
| `change`            | `update:modelValue` 时同步触发                                       | `value: CheckboxGroupValue[]` |

## Slots

| 插槽          | 说明                                                                                    |
| ------------- | --------------------------------------------------------------------------------------- |
| `indicator`   | 覆盖各项指示器；作用域参数 `{ state }`（`'checked' \| 'unchecked' \| 'indeterminate'`） |
| `label`       | 覆盖各项标签；作用域参数 `{ state }`                                                    |
| `description` | 覆盖各项描述；作用域参数 `{ state }`                                                    |

## Accessibility

基于 Reka UI `CheckboxGroupRoot`（默认 `rovingFocus`）。组内各项为 `Checkbox`（`role="checkbox"`、`aria-checked`），并以 `in-group` 渲染：不合并 FormField bindings、不单独注册控件，标签为 `span`（无 `for`）。组根通过 `useFormFieldControl` 合并 `id` / `name` / `disabled` / `required` / `aria-*` 与字段 `data-*`，并向 FormField 注册为控件。根节点设置 `data-slot="checkbox-group"`。达到 `max` 时未选项会带 `aria-disabled`。

### Keyboard Interactions

| Key                        | Description                                           |
| -------------------------- | ----------------------------------------------------- |
| `Tab`                      | 进入 / 离开组（roving focus 时组内仅一项在 Tab 序中） |
| `ArrowUp` / `ArrowDown`    | 垂直方向时在组内移动焦点（`rovingFocus` 为真）        |
| `ArrowLeft` / `ArrowRight` | 水平方向时在组内移动焦点（`rovingFocus` 为真）        |
| `Space`                    | 切换当前焦点项的选中状态                              |

## Examples

### Variants

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import CheckboxGroup from '../checkbox-group.vue'

const items: CheckboxGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
]
</script>

<template>
  <div class="flex flex-col gap-6 max-w-sm w-full">
    <CheckboxGroup variant="soft" :items="items" />

    <CheckboxGroup variant="outline" :items="items" />

    <CheckboxGroup variant="card" :items="items" />
  </div>
</template>
```

### Sizes

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import CheckboxGroup from '../checkbox-group.vue'

const items: CheckboxGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
]
</script>

<template>
  <div class="flex flex-col gap-6 max-w-sm w-full">
    <CheckboxGroup size="sm" :items="items" />

    <CheckboxGroup size="md" :items="items" />

    <CheckboxGroup size="lg" :items="items" />
  </div>
</template>
```

### Orientation

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import CheckboxGroup from '../checkbox-group.vue'

const items: CheckboxGroupItem[] = [
  { label: 'Design', value: 'design' },
  { label: 'Engineering', value: 'engineering' },
  { label: 'Product', value: 'product' },
]
</script>

<template>
  <div class="flex flex-col gap-6 max-w-md w-full">
    <CheckboxGroup orientation="vertical" :items="items" />

    <CheckboxGroup orientation="horizontal" :items="items" />
  </div>
</template>
```

### With Description

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import CheckboxGroup from '../checkbox-group.vue'

const items: CheckboxGroupItem[] = [
  {
    label: 'Email',
    value: 'email',
    description: 'Get notified via email.',
  },
  {
    label: 'Push',
    value: 'push',
    description: 'Get push notifications on your devices.',
  },
  {
    label: 'SMS',
    value: 'sms',
    description: 'Get text messages for important updates.',
  },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <CheckboxGroup :items="items" />
  </div>
</template>
```

### Max

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import CheckboxGroup from '../checkbox-group.vue'

const items: CheckboxGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
  { label: 'Option C', value: 'c' },
  { label: 'Option D', value: 'd' },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <CheckboxGroup :max="2" :items="items" />
  </div>
</template>
```

### Disabled

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import CheckboxGroup from '../checkbox-group.vue'

const items: CheckboxGroupItem[] = [
  { label: 'Design', value: 'design' },
  { label: 'Engineering', value: 'engineering', disabled: true },
  { label: 'Product', value: 'product' },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <CheckboxGroup :items="items" />
  </div>
</template>
```

### Invalid

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import CheckboxGroup from '../checkbox-group.vue'

const items: CheckboxGroupItem[] = [
  { label: 'Design', value: 'design' },
  { label: 'Engineering', value: 'engineering' },
]
</script>

<template>
  <div class="flex flex-col gap-3 max-w-sm w-full">
    <CheckboxGroup :items="items" />
    <CheckboxGroup invalid :items="items" />
  </div>
</template>
```

### Unstyled

```vue
<script lang="ts" setup>
import type { CheckboxGroupItem } from '../checkbox-group.props'
import CheckboxGroup from '../checkbox-group.vue'

const items: CheckboxGroupItem[] = [
  { label: 'Option A', value: 'a' },
  { label: 'Option B', value: 'b' },
]
</script>

<template>
  <div class="max-w-sm w-full">
    <CheckboxGroup unstyled class="flex flex-col gap-2" :items="items" />
  </div>
</template>
```