# Checkbox

> A checkbox for one boolean field value, with an indeterminate state.

```tsx title="hero.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import { Form } from '@langgenius/dify-ui/form'
import * as React from 'react'

type ExportOptions = {
  includeSecrets: boolean
  stripLogs: boolean
}

export function CheckboxHeroDemo() {
  const [exported, setExported] = React.useState<ExportOptions | null>(null)

  return (
    <Form<ExportOptions>
      aria-label="Export DSL options"
      className="flex w-full max-w-80 flex-col items-start gap-4"
      onChange={() => {
        if (exported) setExported(null)
      }}
      onFormSubmit={(values) => setExported(values)}
    >
      <div className="flex w-full flex-col gap-3">
        <Field name="includeSecrets">
          <FieldLabel className="flex items-start gap-2">
            <Checkbox className="mt-0.5" />
            Include secret environment variables
          </FieldLabel>
          <FieldDescription className="ps-6">
            Export secret values in plain text inside the DSL file.
          </FieldDescription>
        </Field>
        <Field name="stripLogs">
          <FieldLabel className="flex items-start gap-2">
            <Checkbox defaultChecked className="mt-0.5" />
            Strip past run logs
          </FieldLabel>
          <FieldDescription className="ps-6">
            Omit execution traces from the exported package.
          </FieldDescription>
        </Field>
      </div>
      <Button type="submit" size="small">
        Export DSL
      </Button>
      <p role="status" className={exported ? 'system-xs-regular text-text-tertiary' : 'sr-only'}>
        {exported
          ? `Exported with secrets ${exported.includeSecrets ? 'included' : 'excluded'} and run logs ${exported.stripLogs ? 'stripped' : 'kept'}.`
          : ''}
      </p>
    </Form>
  )
}
```

## Usage guidelines

- Use `Checkbox` for one boolean field value saved with its form, or for one option inside `CheckboxGroup`. Use `CheckboxGroup` for several values from a visible set, `Switch` for an on/off setting that takes effect immediately, and `Toggle` for the pressed state of a button.
- Label the checkbox with a visible `FieldLabel` that wraps it, so clicking the text toggles it. Use `aria-label` only when no visible label exists, because a non-blank `aria-label` overrides the associated label.
- Use `indeterminate` when the checkbox summarizes a partial selection. It is controlled by the owner, which clears it once the user decides. Inside `CheckboxGroup`, a `parent` checkbox derives the mixed state on its own.
- `Checkbox` renders a `<span>` with a hidden `<input>` beside it. Set `name`, `value` and `uncheckedValue` for native form submission.
- Use `disabled` for a value the user cannot change, and say why in `FieldDescription`. `readOnly` keeps the value visible and focusable without allowing changes.

## Anatomy

```tsx
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
```

```tsx
<Field>
  <FieldLabel>
    <Checkbox />
    Label
  </FieldLabel>
  <FieldDescription />
</Field>
```

`Checkbox` owns its indicator: a check mark when checked and a dash when indeterminate.

## Examples

### Indeterminate

`indeterminate` shows the mixed state and sets `aria-checked="mixed"`. The owner turns it off when the user picks a side.

```tsx title="indeterminate.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import * as React from 'react'

type GrantState = boolean | 'mixed'

export function CheckboxIndeterminateDemo() {
  const [grant, setGrant] = React.useState<GrantState>('mixed')

  return (
    <div className="flex w-full max-w-80 flex-col items-start gap-3">
      <Field name="grantEditorRole" className="w-full">
        <FieldLabel className="flex items-start gap-2">
          <Checkbox
            className="mt-0.5"
            checked={grant === true}
            indeterminate={grant === 'mixed'}
            onCheckedChange={setGrant}
          />
          Grant the editor role to all members
        </FieldLabel>
        <FieldDescription className="ps-6">
          {grant === 'mixed'
            ? '3 of 8 members already have the editor role.'
            : grant
              ? 'All 8 members get the editor role.'
              : 'No member gets the editor role.'}
        </FieldDescription>
      </Field>
      <Button
        size="small"
        disabled={grant === 'mixed'}
        onClick={() => setGrant('mixed')}
        className="ms-6"
      >
        Reset to mixed
      </Button>
    </div>
  )
}
```

### Disabled

Set `disabled` on `Field` to disable the checkbox and its label together. The description explains why the value is fixed.

```tsx title="disabled.tsx"
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'

export function CheckboxDisabledDemo() {
  return (
    <div className="flex w-full max-w-80 flex-col gap-3">
      <Field name="allowSignUp" disabled>
        <FieldLabel className="flex items-start gap-2">
          <Checkbox className="mt-0.5" />
          Allow members to sign up with email
        </FieldLabel>
        <FieldDescription className="ps-6">
          Your workspace signs in through SSO only.
        </FieldDescription>
      </Field>
      <Field name="auditLog" disabled>
        <FieldLabel className="flex items-start gap-2">
          <Checkbox defaultChecked className="mt-0.5" />
          Keep an audit log of workspace changes
        </FieldLabel>
        <FieldDescription className="ps-6">Required by your Enterprise plan.</FieldDescription>
      </Field>
    </div>
  )
}
```

### Read-only

`readOnly` keeps the checkbox focusable, readable and included in form submission without allowing changes.

```tsx title="read-only.tsx"
import { Checkbox } from '@langgenius/dify-ui/checkbox'
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'

export function CheckboxReadOnlyDemo() {
  return (
    <Field name="requireCitations" className="w-full max-w-80">
      <FieldLabel className="flex items-start gap-2">
        <Checkbox defaultChecked readOnly className="mt-0.5" />
        Require source citations in published replies
      </FieldLabel>
      <FieldDescription className="ps-6">
        Locked by workspace policy. Focusable and submitted with the form, but cannot be toggled.
      </FieldDescription>
    </Field>
  )
}
```

## Keyboard

| Key     | Action                                                           |
| ------- | ---------------------------------------------------------------- |
| `Space` | Toggles the focused checkbox.                                    |
| `Tab`   | Moves focus to the next control. A disabled checkbox is skipped. |

## API reference

### Checkbox

A checkbox control supporting checked, unchecked, and indeterminate states. Renders a `<span>` element and a hidden `<input>` beside.

Pair with a visible `FieldLabel` or `<label>`; use `aria-label` only when no visible label exists.

Inherits `className`, `render`, `style`, `nativeButton`, `id`, `name`, `form`, `checked`, `defaultChecked`, `disabled`, `onCheckedChange`, `readOnly`, `required`, `indeterminate`, `inputRef`, `parent`, `uncheckedValue`, `value` from [Base UI Checkbox.Root](https://base-ui.com/react/components/checkbox#root).

**Data attributes**

| Attribute            | Description                                                                    |
| -------------------- | ------------------------------------------------------------------------------ |
| `data-checked`       | Present when the checkbox is checked.                                          |
| `data-unchecked`     | Present when the checkbox is not checked.                                      |
| `data-indeterminate` | Present when the checkbox is in an indeterminate state.                        |
| `data-disabled`      | Present when the checkbox is disabled.                                         |
| `data-readonly`      | Present when the checkbox is readonly.                                         |
| `data-required`      | Present when the checkbox is required.                                         |
| `data-valid`         | Present when the checkbox is in a valid state (when wrapped in Field.Root).    |
| `data-invalid`       | Present when the checkbox is in an invalid state (when wrapped in Field.Root). |
| `data-touched`       | Present when the checkbox has been touched (when wrapped in Field.Root).       |
| `data-dirty`         | Present when the checkbox's value has changed (when wrapped in Field.Root).    |
| `data-filled`        | Present when the checkbox is checked (when wrapped in Field.Root).             |
| `data-focused`       | Present when the checkbox is focused (when wrapped in Field.Root).             |

### CheckboxSkeleton

Accepts the props of the element it renders.
