# Checkbox

Checkbox allows users to select or mark a single item as selected.

To use multiple checkboxes together, use the `checkbox_group` component.

## Example

```python
from deephaven import ui

cb = ui.checkbox("Unsubscribe")
```

## UI recommendations

Recommendations for creating checkboxes:

1. Use emphasized checkboxes for forms, settings, or to highlight selected items like cards or table rows. Use non-emphasized checkboxes in application panels with monochrome components to keep the focus on the main content.
2. Use standalone checkboxes when the context is clear without a text label, such as when a checkbox is associated with other controls within a panel.
3. Checkboxes and radio buttons should not be used interchangably. Use checkboxes to allow multiple selections (or none) from a list. Use radio buttons to select only one option from a list of mutually exclusive choices.
4. Checkboxes should be used when selecting (ie., multiple table rows), whereas switches should be used for activation (ie., on/off states).

Consider using a [`checkbox`]() for individual selections or when marking a single item as selected. For a set of related checkboxes, use a [`checkbox_group`](checkbox_group.md) to manage multiple selections within a group. If you need to display a list of checkboxes driven by a Deephaven table, use a [`list_view`](list_view.md) to dynamically generate the checkboxes.

## Content

Checkbox’s accept a child, which is rendered as the label of the checkbox.

```python
from deephaven import ui

my_checkbox_basic = ui.checkbox("Basic Checkbox")
```

## Value

Checkboxes are not selected by default. Use the `default_selected` prop to set the initial state (uncontrolled) or the `is_selected` prop to control the selected state.

```python
from deephaven import ui


@ui.component
def ui_checkbox_content_examples():
    selected, set_selected = ui.use_state(False)
    return ui.flex(
        ui.checkbox("Subscribe (uncontrolled)", default_selected=True),
        ui.checkbox(
            "Subscribe (uncontrolled)", is_selected=selected, on_change=set_selected
        ),
        direction="column",
    )


my_checkbox_content_examples = ui_checkbox_content_examples()
```

## Indeterminate state

A checkbox can be set to an indeterminate state using the `is_indeterminate` prop, which overrides its appearance. The Checkbox remains visually indeterminate until the prop is set to false, regardless of user interaction.

```python
from deephaven import ui


my_checkbox_is_indeterminate_example = ui.checkbox(
    "Indeterminate State", is_indeterminate=True
)
```

## HTML Forms

Checkbox’s can support a `name` prop for integration with HTML forms, allowing for easy identification of a value on form submission.

```python
from deephaven import ui


my_checkbox_name_example = ui.form(ui.checkbox("Sample Label", name="Sample Name"))
```

## Events

Checkboxes accept an `on_change` prop, which is triggered whenever the Checkbox is clicked

```python
from deephaven import ui


@ui.component
def ui_checkbox_event_example():
    selected, set_selected = ui.use_state(False)
    return ui.flex(
        ui.checkbox("Subscribe", is_selected=selected, on_change=set_selected),
        ui.text(value="Subscribed!" if selected else "Not subscribed!"),
        direction="column",
    )


my_checkbox_event_example = ui_checkbox_event_example()
```

## Validation

Checkboxes can indicate a validation state to show if the current value is invalid, via the `is_invalid` prop. Since the invalid state is only shown through a color change, ensure the validation error is also communicated in another accessible way.

```python
from deephaven import ui


@ui.component
def ui_checkbox_validation_example():
    invalid, set_invalid = ui.use_state(False)
    return [
        ui.button(
            "Make checkbox valid" if invalid else "Make checkbox invalid",
            on_press=lambda: set_invalid(not invalid),
        ),
        ui.checkbox("I accept the terms and conditions", is_invalid=invalid),
    ]


my_checkbox_validation_example = ui_checkbox_validation_example()
```

## API reference

Checkboxes allow users to select multiple items from a list of individual items, or to mark one individual item as selected.

**Returns:** `Element` The rendered checkbox.

<ParamTable param={{"module_name": "deephaven.ui.", "name": "checkbox", "parameters": [{"name": "*children", "type": "Any", "description": "The checkbox label."}, {"name": "is_emphasized", "type": "bool | None", "description": "This prop sets the emphasized style which provides visual prominence.", "default": "None"}, {"name": "is_indeterminate", "type": "bool | None", "description": "Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction.", "default": "None"}, {"name": "default_selected", "type": "bool | None", "description": "Whether the element should be selected (uncontrolled).", "default": "None"}, {"name": "is_selected", "type": "bool | None", "description": "Whether the element should be selected (controlled).", "default": "None"}, {"name": "value", "type": "str | None", "description": "The value of the input element, used when submitting a form.", "default": "None"}, {"name": "is_disabled", "type": "bool | None", "description": "Whether the element is disabled.", "default": "None"}, {"name": "is_read_only", "type": "bool | None", "description": "Whether the element is read-only.", "default": "None"}, {"name": "is_required", "type": "bool | None", "description": "Whether the element is required before form submission.", "default": "None"}, {"name": "is_invalid", "type": "bool | None", "description": "Whether the element is invalid.", "default": "None"}, {"name": "auto_focus", "type": "bool | None", "description": "Whether the element should automatically get focus on render.", "default": "None"}, {"name": "name", "type": "str | None", "description": "The name of the input element, used when submitting a form.", "default": "None"}, {"name": "on_change", "type": "Callable[[bool], None] | None", "description": "Handler that is called when the element is selected or deselected.", "default": "None"}, {"name": "on_focus", "type": "Callable[[FocusEvent], None] | None", "description": "Handler that is called when the element receives focus.", "default": "None"}, {"name": "on_blur", "type": "Callable[[FocusEvent], None] | None", "description": "Handler that is called when the element loses focus.", "default": "None"}, {"name": "on_focus_change", "type": "Callable[[bool], None] | None", "description": "Handler that is called when the element receives or loses focus.", "default": "None"}, {"name": "on_key_down", "type": "Callable[[KeyboardEvent], None] | None", "description": "Handler that is called when a key is pressed down.", "default": "None"}, {"name": "on_key_up", "type": "Callable[[KeyboardEvent], None] | None", "description": "Handler that is called when a key is released.", "default": "None"}, {"name": "flex", "type": "str | float | bool | None", "description": "When used in a flex layout, specifies how the element will grow or shrink to fit the space available.", "default": "None"}, {"name": "flex_grow", "type": "float | None", "description": "When used in a flex layout, specifies how the element will grow to fit the space available.", "default": "None"}, {"name": "flex_shrink", "type": "float | None", "description": "When used in a flex layout, specifies how the element will shrink to fit the space available.", "default": "None"}, {"name": "flex_basis", "type": "str | float | None", "description": "When used in a flex layout, specifies the initial main size of the element.", "default": "None"}, {"name": "align_self", "type": "Literal['auto', 'normal', 'start', 'end', 'center', 'flex-start', 'flex-end', 'self-start', 'self-end', 'stretch'] | None", "description": "Overrides the alignItems property of a flex or grid container.", "default": "None"}, {"name": "justify_self", "type": "Literal['auto', 'normal', 'start', 'end', 'flex-start', 'flex-end', 'self-start', 'self-end', 'center', 'left', 'right', 'stretch'] | None", "description": "Species how the element is justified inside a flex or grid container.", "default": "None"}, {"name": "order", "type": "int | None", "description": "The layout order for the element within a flex or grid container.", "default": "None"}, {"name": "grid_area", "type": "str | None", "description": "When used in a grid layout specifies, specifies the named grid area that the element should be placed in within the grid.", "default": "None"}, {"name": "grid_row", "type": "str | None", "description": "When used in a grid layout, specifies the row the element should be placed in within the grid.", "default": "None"}, {"name": "grid_column", "type": "str | None", "description": "When used in a grid layout, specifies the column the element should be placed in within the grid.", "default": "None"}, {"name": "grid_row_start", "type": "str | None", "description": "When used in a grid layout, specifies the starting row to span within the grid.", "default": "None"}, {"name": "grid_row_end", "type": "str | None", "description": "When used in a grid layout, specifies the ending row to span within the grid.", "default": "None"}, {"name": "grid_column_start", "type": "str | None", "description": "When used in a grid layout, specifies the starting column to span within the grid.", "default": "None"}, {"name": "grid_column_end", "type": "str | None", "description": "When used in a grid layout, specifies the ending column to span within the grid.", "default": "None"}, {"name": "margin", "type": "str | float | None", "description": "The margin for all four sides of the element.", "default": "None"}, {"name": "margin_top", "type": "str | float | None", "description": "The margin for the top side of the element.", "default": "None"}, {"name": "margin_bottom", "type": "str | float | None", "description": "The margin for the bottom side of the element.", "default": "None"}, {"name": "margin_start", "type": "str | float | None", "description": "The margin for the logical start side of the element, depending on layout direction.", "default": "None"}, {"name": "margin_end", "type": "str | float | None", "description": "The margin for the logical end side of the element, depending on layout direction.", "default": "None"}, {"name": "margin_x", "type": "str | float | None", "description": "The margin for the left and right sides of the element.", "default": "None"}, {"name": "margin_y", "type": "str | float | None", "description": "The margin for the top and bottom sides of the element.", "default": "None"}, {"name": "width", "type": "str | float | None", "description": "The width of the element.", "default": "None"}, {"name": "min_width", "type": "str | float | None", "description": "The minimum width of the element.", "default": "None"}, {"name": "max_width", "type": "str | float | None", "description": "The maximum width of the element.", "default": "None"}, {"name": "height", "type": "str | float | None", "description": "The height of the element.", "default": "None"}, {"name": "min_height", "type": "str | float | None", "description": "The minimum height of the element.", "default": "None"}, {"name": "max_height", "type": "str | float | None", "description": "The maximum height of the element.", "default": "None"}, {"name": "position", "type": "Literal['static', 'relative', 'absolute', 'fixed', 'sticky'] | None", "description": "The position of the element.", "default": "None"}, {"name": "top", "type": "str | float | None", "description": "The distance from the top of the containing element.", "default": "None"}, {"name": "bottom", "type": "str | float | None", "description": "The distance from the bottom of the containing element.", "default": "None"}, {"name": "left", "type": "str | float | None", "description": "The distance from the left of the containing element.", "default": "None"}, {"name": "right", "type": "str | float | None", "description": "The distance from the right of the containing element.", "default": "None"}, {"name": "start", "type": "str | float | None", "description": "The distance from the start of the containing element, depending on layout direction.", "default": "None"}, {"name": "end", "type": "str | float | None", "description": "The distance from the end of the containing element, depending on layout direction.", "default": "None"}, {"name": "z_index", "type": "int | None", "description": "The stack order of the element.", "default": "None"}, {"name": "is_hidden", "type": "bool | None", "description": "Whether the element is hidden.", "default": "None"}, {"name": "id", "type": "str | None", "description": "The unique identifier of the element.", "default": "None"}, {"name": "exclude_from_tab_order", "type": "bool | None", "description": "Whether the element should be excluded from the tab order. If true, the element will not be focusable via the keyboard by tabbing.", "default": "None"}, {"name": "aria_controls", "type": "str | None", "description": "The id of the element that the current element controls.", "default": "None"}, {"name": "aria_label", "type": "str | None", "description": "The label for the element.", "default": "None"}, {"name": "aria_labelledby", "type": "str | None", "description": "The id of the element that labels the current element.", "default": "None"}, {"name": "aria_describedby", "type": "str | None", "description": "The id of the element that describes the current element.", "default": "None"}, {"name": "aria_details", "type": "str | None", "description": "The id of the element that provides additional information about the current element.", "default": "None"}, {"name": "aria_errormessage", "type": "str | None", "description": "The id of the element that provides error information for the current element.", "default": "None"}, {"name": "UNSAFE_class_name", "type": "str | None", "description": "A CSS class to apply to the element.", "default": "None"}, {"name": "UNSAFE_style", "type": "Dict[str, Any] | None", "description": "A CSS style to apply to the element.", "default": "None"}, {"name": "key", "type": "str | None", "description": "A unique identifier used by React to render elements in a list.", "default": "None"}]}} />
