## Import

```jsx
import { Checkbox } from "@cfa/react-core";
```

## Live Editor

```jsx
<Checkbox>Label</Checkbox>
```

## Guidelines

### When to use

- Checkbox is for selecting and deselecting options. People can select several independently, such as in a form, settings, or to confirm a single choice.

### Do

- Write standalone checkbox labels as `Subscribe to updates`, not `Should I subscribe?`.
- Use helper text only when it adds context, not for redundant information.
- For multiple related options, use CheckboxGroup with a visible group label.

## Examples

### Default Selected

The `defaultSelected` prop allows you to set the initial state of the checkbox.

```jsx
<Checkbox defaultSelected>Label</Checkbox>
```

### Description

The `description` prop allows you to add helper text to the checkbox.

```jsx
<Checkbox description="Additional helper text">Label</Checkbox>
```

### Disabled

Use the `isDisabled` prop to disable the checkbox.

```jsx
<div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
  <Checkbox isDisabled>Disabled</Checkbox>
  <Checkbox isDisabled defaultSelected>
    Disabled selected
  </Checkbox>
</div>
```

### Indeterminate

A checkbox input can only have two states in a form: checked or unchecked. It either submits its value or doesn't. However, visually there are three states a checkbox can be in: checked, unchecked, or indeterminate. Below is an example of the indeterminate state, passing the `isIndeterminate` prop.

```jsx
<Checkbox isIndeterminate defaultSelected>
  Indeterminate
</Checkbox>
```

### Invalid

The `isInvalid` prop applies the following outline error color to a validated Checkbox.

```jsx
<div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
  <Checkbox isInvalid>I accept the terms and conditions</Checkbox>
  <Checkbox isInvalid defaultSelected>
    I accept the terms and conditions
  </Checkbox>
</div>
```

### Error Message

Use `errorMessage` with `isInvalid` to display validation text below the checkbox.

```jsx
<Checkbox isInvalid errorMessage="You must accept before continuing">
  I accept the terms and conditions
</Checkbox>
```

### Readonly

The `isReadOnly` prop applies the following visual style and prevents user from making a selection.

Readonly inputs are still focusable ().

```jsx
<div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
  <Checkbox isReadOnly>Read-only</Checkbox>
  <Checkbox isReadOnly defaultSelected>
    Read-only selected
  </Checkbox>
</div>
```

### Required

The `isRequired` prop applies a red asterisk to the label, visually indicating that the checkbox is required.

```jsx
<Checkbox isRequired>Required</Checkbox>
```

### Controlled

Use the `isSelected` or `defaultSelected` prop to set the selection state, and `onChange` to handle selection changes.

```jsx
function ControlledExample() {
  let [selected, setSelected] = React.useState(false);

  return (
    <>
      <Checkbox isSelected={selected} onChange={setSelected}>
        Subscribe
      </Checkbox>
      <p>You are {selected ? "subscribed" : "unsubscribed"}</p>
    </>
  );
}
```

## Props

### Checkbox

| Name | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| children | `ReactElement<unknown, string \ | JSXElementConstructor<any>> \ | ReactNode` | - | The label of the checkbox. |
| description | `string` | - | Additional helper text for the checkbox. |
| errorMessage | `string \ | ((validation: ValidationResult) => string)` | - | Custom error message to display when the checkbox is invalid. |
| ref | `Ref<HTMLLabelElement>` | - | Requires React 19 |
| aria-controls | `string` | - | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
| aria-describedby | `string` | - | Identifies the element (or elements) that describes the object. |
| aria-details | `string` | - | Identifies the element (or elements) that provide a detailed, extended description for the object. |
| aria-errormessage | `string` | - | Identifies the element that provides an error message for the object. |
| aria-label | `string` | - | Defines a string value that labels the current element. |
| aria-labelledby | `string` | - | Identifies the element (or elements) that labels the current element. |
| autoFocus | `boolean` | - | Whether the element should receive focus on render. |
| className | `ClassNameOrFunction<CheckboxFieldRenderProps>` | `'react-aria-CheckboxField'` | The CSS \[className]\(https\://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state. |
| defaultSelected | `boolean` | - | Whether the element should be selected (uncontrolled). |
| dir | `string` | - | - |
| excludeFromTabOrder | `boolean` | - | Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. |
| form | `string` | - | The \`\<form>\` element to associate the input with. The value of this attribute must be the id of a \`\<form>\` in the same document. See \[MDN]\(https\://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input#form). |
| hidden | `boolean` | - | - |
| id | `string` | - | The element's unique identifier. See \[MDN]\(https\://developer.mozilla.org/en-US/docs/Web/HTML/Global\_attributes/id). |
| inert | `boolean` | - | - |
| inputRef | `Ref<HTMLInputElement \ | null>` | - | A ref for the HTML input element. |
| isDisabled | `boolean` | - | Whether the input is disabled. |
| isIndeterminate | `boolean` | - | Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction. |
| isInvalid | `boolean` | - | Whether the input value is invalid. |
| isReadOnly | `boolean` | - | Whether the input can be selected but not changed by the user. |
| isRequired | `boolean` | - | Whether user input is required on the input before form submission. |
| isSelected | `boolean` | - | Whether the element should be selected (controlled). |
| lang | `string` | - | - |
| name | `string` | - | The name of the input element, used when submitting an HTML form. See \[MDN]\(https\://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname). |
| onAnimationEnd | `AnimationEventHandler<HTMLDivElement>` | - | - |
| onAnimationEndCapture | `AnimationEventHandler<HTMLDivElement>` | - | - |
| onAnimationIteration | `AnimationEventHandler<HTMLDivElement>` | - | - |
| onAnimationIterationCapture | `AnimationEventHandler<HTMLDivElement>` | - | - |
| onAnimationStart | `AnimationEventHandler<HTMLDivElement>` | - | - |
| onAnimationStartCapture | `AnimationEventHandler<HTMLDivElement>` | - | - |
| onAuxClick | `MouseEventHandler<HTMLDivElement>` | - | - |
| onAuxClickCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onBlur | `((e: FocusEvent<Element, Element>) => void)` | - | Handler that is called when the element loses focus. |
| onChange | `((isSelected: boolean) => void)` | - | Handler that is called when the element's selection state changes. |
| onClick | `((e: MouseEvent<FocusableElement, MouseEvent>) => void)` | - | \*\*Not recommended – use \`onPress\` instead.\*\* \`onClick\` is an alias for \`onPress\` provided for compatibility with other libraries. \`onPress\` provides additional event details for non-mouse interactions. |
| onClickCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onContextMenu | `MouseEventHandler<HTMLDivElement>` | - | - |
| onContextMenuCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onDoubleClick | `MouseEventHandler<HTMLDivElement>` | - | - |
| onDoubleClickCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onFocus | `((e: FocusEvent<Element, Element>) => void)` | - | Handler that is called when the element receives focus. |
| onFocusChange | `((isFocused: boolean) => void)` | - | Handler that is called when the element's focus status changes. |
| onGotPointerCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onGotPointerCaptureCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onKeyDown | `((e: KeyboardEvent) => void)` | - | Handler that is called when a key is pressed. |
| onKeyUp | `((e: KeyboardEvent) => void)` | - | Handler that is called when a key is released. |
| onLostPointerCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onLostPointerCaptureCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onMouseDown | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseDownCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseEnter | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseLeave | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseMove | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseMoveCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseOut | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseOutCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseOver | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseOverCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseUp | `MouseEventHandler<HTMLDivElement>` | - | - |
| onMouseUpCapture | `MouseEventHandler<HTMLDivElement>` | - | - |
| onPointerCancel | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerCancelCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerDown | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerDownCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerEnter | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerLeave | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerMove | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerMoveCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerOut | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerOutCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerOver | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerOverCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerUp | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPointerUpCapture | `PointerEventHandler<HTMLDivElement>` | - | - |
| onPress | `((e: PressEvent) => void)` | - | Handler that is called when the press is released over the target. |
| onPressChange | `((isPressed: boolean) => void)` | - | Handler that is called when the press state changes. |
| onPressEnd | `((e: PressEvent) => void)` | - | Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. |
| onPressStart | `((e: PressEvent) => void)` | - | Handler that is called when a press interaction starts. |
| onPressUp | `((e: PressEvent) => void)` | - | Handler that is called when a press is released over the target, regardless of whether it started on the target or not. |
| onScroll | `UIEventHandler<HTMLDivElement>` | - | - |
| onScrollCapture | `UIEventHandler<HTMLDivElement>` | - | - |
| onTouchCancel | `TouchEventHandler<HTMLDivElement>` | - | - |
| onTouchCancelCapture | `TouchEventHandler<HTMLDivElement>` | - | - |
| onTouchEnd | `TouchEventHandler<HTMLDivElement>` | - | - |
| onTouchEndCapture | `TouchEventHandler<HTMLDivElement>` | - | - |
| onTouchMove | `TouchEventHandler<HTMLDivElement>` | - | - |
| onTouchMoveCapture | `TouchEventHandler<HTMLDivElement>` | - | - |
| onTouchStart | `TouchEventHandler<HTMLDivElement>` | - | - |
| onTouchStartCapture | `TouchEventHandler<HTMLDivElement>` | - | - |
| onTransitionCancel | `TransitionEventHandler<HTMLDivElement>` | - | - |
| onTransitionCancelCapture | `TransitionEventHandler<HTMLDivElement>` | - | - |
| onTransitionEnd | `TransitionEventHandler<HTMLDivElement>` | - | - |
| onTransitionEndCapture | `TransitionEventHandler<HTMLDivElement>` | - | - |
| onTransitionRun | `TransitionEventHandler<HTMLDivElement>` | - | - |
| onTransitionRunCapture | `TransitionEventHandler<HTMLDivElement>` | - | - |
| onTransitionStart | `TransitionEventHandler<HTMLDivElement>` | - | - |
| onTransitionStartCapture | `TransitionEventHandler<HTMLDivElement>` | - | - |
| onWheel | `WheelEventHandler<HTMLDivElement>` | - | - |
| onWheelCapture | `WheelEventHandler<HTMLDivElement>` | - | - |
| render | `DOMRenderFunction<"div", CheckboxFieldRenderProps>` | - | Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.&#xA;&#xA;Requirements:&#xA;&#xA;- You must render the expected element type (e.g. if \`\<button>\` is expected, you cannot render an \`\<a>\`).&#xA;- Only a single root DOM element can be rendered (no fragments).&#xA;- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate. |
| slot | `string \ | null` | - | A slot name for the component. Slots allow the component to receive props from a parent component. An explicit \`null\` value indicates that the local props completely override all props received from a parent. |
| style | `StyleOrFunction<CheckboxFieldRenderProps>` | - | The inline \[style]\(https\://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state. |
| suppressHydrationWarning | `boolean` | - | A React-only flag that suppresses the warning React normally logs when an element's server-rendered content doesn't match the client render during hydration. See \[React docs]\(https\://react.dev/reference/react-dom/client/hydrateRoot#suppressing-unavoidable-hydration-mismatch-errors). |
| translate | `"no" \ | "yes"` | - | - |
| validate | `((value: boolean) => true \ | ValidationError \ | null)` | - | A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if \`validationBehavior="native"\`. For realtime validation, use the \`isInvalid\` prop instead. |
| validationBehavior | `"aria" \ | "native"` | `'native'` | Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. |
| value | `string` | - | The value of the input element, used when submitting an HTML form. See \[MDN]\(https\://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefvalue). |