# Checkbox Group

> Shared multiple-selection state for the checkboxes it renders.

```tsx title="hero.tsx"
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { CheckboxGroup } from '@langgenius/dify-ui/checkbox-group'
import { Field, FieldDescription, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'

const fileTypes = [
  { value: 'markdown', label: 'Markdown' },
  { value: 'pdf', label: 'PDF' },
  { value: 'html', label: 'HTML' },
  { value: 'docx', label: 'DOCX' },
]

export function CheckboxGroupHeroDemo() {
  return (
    <Field name="allowedFileTypes" className="w-full max-w-80">
      <Fieldset
        render={<CheckboxGroup defaultValue={['markdown', 'pdf']} className="grid gap-1" />}
      >
        <FieldsetLegend>Allowed file types</FieldsetLegend>
        {fileTypes.map((fileType) => (
          <FieldItem key={fileType.value}>
            <FieldLabel className="flex items-center gap-2">
              <Checkbox value={fileType.value} />
              {fileType.label}
            </FieldLabel>
          </FieldItem>
        ))}
      </Fieldset>
      <FieldDescription>Uploads of other types are rejected.</FieldDescription>
    </Field>
  )
}
```

## Usage guidelines

- Use `CheckboxGroup` for several values from a visible set. Use `Combobox` when the set is long enough to need search, `RadioGroup` when only one value applies, and a single `Checkbox` for one boolean value.
- Compose it as a form field: `Field` holds the name, `Fieldset render={<CheckboxGroup />}` provides group semantics, `FieldsetLegend` names the group, and each option sits in a `FieldItem` with its own `FieldLabel`.
- Pass `value`, `defaultValue`, `onValueChange` and `disabled` to `CheckboxGroup`, not to `Fieldset`. Each `Checkbox` only states which value it stands for.
- The group holds the selection of the checkboxes it renders. Selection beyond them, such as other pages or filtered-out rows, stays in the owner's domain state, which hands the group only the part it can show.
- The value is a `string[]`. Map stronger domain IDs at the boundary that owns them.

## Anatomy

```tsx
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { CheckboxGroup } from '@langgenius/dify-ui/checkbox-group'
import { Field, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
```

```tsx
<Field>
  <Fieldset render={<CheckboxGroup />}>
    <FieldsetLegend />
    <FieldItem>
      <FieldLabel>
        <Checkbox />
        Label
      </FieldLabel>
    </FieldItem>
  </Fieldset>
</Field>
```

## Examples

### Parent checkbox

Add a `Checkbox` with `parent` and pass every child value to `allValues` on a controlled group. The parent shows the mixed state when only some children are selected. From a partial selection, it cycles through all, none, and back to that selection.

```tsx title="parent.tsx"
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { CheckboxGroup } from '@langgenius/dify-ui/checkbox-group'
import { Field, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import * as React from 'react'

const tools = [
  { value: 'google-search', label: 'Google Search' },
  { value: 'wikipedia', label: 'Wikipedia' },
  { value: 'web-scraper', label: 'Web Scraper' },
  { value: 'code-interpreter', label: 'Code Interpreter' },
]

const toolValues = tools.map((tool) => tool.value)

export function CheckboxGroupParentDemo() {
  const [enabledTools, setEnabledTools] = React.useState(['google-search'])

  return (
    <Field name="agentTools" className="w-full max-w-80">
      <Fieldset
        render={
          <CheckboxGroup
            value={enabledTools}
            onValueChange={setEnabledTools}
            allValues={toolValues}
            className="grid gap-1"
          />
        }
      >
        <FieldsetLegend>Agent tools</FieldsetLegend>
        <FieldItem>
          <FieldLabel className="flex items-center gap-2">
            <Checkbox parent />
            All tools
          </FieldLabel>
        </FieldItem>
        {tools.map((tool) => (
          <FieldItem key={tool.value} className="ps-6">
            <FieldLabel className="flex items-center gap-2">
              <Checkbox value={tool.value} />
              {tool.label}
            </FieldLabel>
          </FieldItem>
        ))}
      </Fieldset>
    </Field>
  )
}
```

### Selection across pages

The owner keeps the selection for every page. The group receives the current page's share as `value` and its values as `allValues`, and `onValueChange` merges the page's new selection back. Key the group by page so the parent checkbox does not restore a partial selection from another page.

```tsx title="paged-selection.tsx"
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { CheckboxGroup } from '@langgenius/dify-ui/checkbox-group'
import { Field, FieldItem, FieldLabel } from '@langgenius/dify-ui/field'
import { Fieldset, FieldsetLegend } from '@langgenius/dify-ui/fieldset'
import {
  Pagination,
  PaginationNavigation,
  PaginationNext,
  PaginationPageJump,
  PaginationPrevious,
} from '@langgenius/dify-ui/pagination'
import * as React from 'react'

const documents = [
  'onboarding-guide.pdf',
  'pricing-faq.md',
  'release-notes.txt',
  'api-reference.html',
  'security-whitepaper.pdf',
  'support-playbook.docx',
  'glossary.md',
  'changelog-2026.md',
  'sla-terms.pdf',
]

const pageSize = 3
const totalPages = Math.ceil(documents.length / pageSize)

export function CheckboxGroupPagedSelectionDemo() {
  const [page, setPage] = React.useState(1)
  const [selectedDocuments, setSelectedDocuments] = React.useState([
    'pricing-faq.md',
    'glossary.md',
  ])

  const pageDocuments = documents.slice((page - 1) * pageSize, page * pageSize)
  const pageSelection = selectedDocuments.filter((name) => pageDocuments.includes(name))

  return (
    <div className="flex w-full max-w-80 flex-col gap-2">
      <Field name="reindexDocuments">
        <Fieldset
          key={page}
          render={
            <CheckboxGroup
              value={pageSelection}
              onValueChange={(nextPageSelection) => {
                setSelectedDocuments((current) => [
                  ...current.filter((name) => !pageDocuments.includes(name)),
                  ...nextPageSelection,
                ])
              }}
              allValues={pageDocuments}
              className="grid gap-1"
            />
          }
        >
          <FieldsetLegend>Documents to re-index</FieldsetLegend>
          <FieldItem>
            <FieldLabel className="flex items-center gap-2">
              <Checkbox parent />
              All on this page
            </FieldLabel>
          </FieldItem>
          {pageDocuments.map((name) => (
            <FieldItem key={name} className="ps-6">
              <FieldLabel className="flex items-center gap-2">
                <Checkbox value={name} />
                {name}
              </FieldLabel>
            </FieldItem>
          ))}
        </Fieldset>
      </Field>
      <div className="flex flex-wrap items-center justify-between gap-2">
        <span className="shrink-0 system-xs-regular text-text-tertiary tabular-nums">
          {`${selectedDocuments.length} of ${documents.length} selected`}
        </span>
        <Pagination
          aria-label="Documents pagination"
          page={page}
          totalPages={totalPages}
          onPageChange={setPage}
          className="min-w-28 flex-1 justify-end px-0 py-0"
        >
          <PaginationNavigation>
            <PaginationPrevious />
            <PaginationPageJump />
            <PaginationNext />
          </PaginationNavigation>
        </Pagination>
      </div>
    </div>
  )
}
```

## Keyboard

| Key     | Action                                                                                                     |
| ------- | ---------------------------------------------------------------------------------------------------------- |
| `Tab`   | Moves focus to the next checkbox in the group.                                                             |
| `Space` | Toggles the focused checkbox. On the parent, cycles through all, none, and the previous partial selection. |

## API reference

### CheckboxGroup

Provides shared state to a series of checkboxes, including `allValues` and parent checkbox coordination. Renders a `<div>` element.

Inherits `className`, `value`, `defaultValue`, `onValueChange`, `allValues`, `disabled`, `render`, `style` from [Base UI CheckboxGroup](https://base-ui.com/react/components/checkbox-group).

**Data attributes**

| Attribute       | Description                                  |
| --------------- | -------------------------------------------- |
| `data-disabled` | Present when the checkbox group is disabled. |
