Clawscale

Checkbox

A Checkbox turns one option on or off. A third, indeterminate state shows a partial selection.

Usage#

import { Checkbox } from "@clawscale/react";
<Checkbox checked={retry} label="Retry failed tasks" onChange={(event) => setRetry(event.currentTarget.checked)} />

Examples#

Basic#

defaultChecked sets the initial state of an uncontrolled checkbox. Pass checked and onChange to control it.

import { Checkbox } from "@clawscale/react";

export default function CheckboxBasic() {
  return (
    <div>
      <Checkbox defaultChecked label="Retry failed tasks" />
      <Checkbox label="Notify owners on failure" />
      <Checkbox defaultChecked label="Skip weekends" />
    </div>
  );
}

Indeterminate#

indeterminate shows a dash instead of a check. Use it on a select-all checkbox when only some items are selected.

import { Checkbox } from "@clawscale/react";
import { useState } from "react";

const DATASETS = ["orders_daily", "customer_profiles", "events_raw", "inventory_snapshots"];

export default function CheckboxIndeterminate() {
  const [selected, setSelected] = useState(["orders_daily", "events_raw"]);
  const toggle = (name: string, checked: boolean) =>
    setSelected((current) => (checked ? [...current, name] : current.filter((item) => item !== name)));
  return (
    <div>
      <Checkbox
        checked={selected.length === DATASETS.length}
        indeterminate={selected.length > 0 && selected.length < DATASETS.length}
        label={`Select all (${selected.length} of ${DATASETS.length})`}
        onChange={(event) => setSelected(event.currentTarget.checked ? DATASETS : [])}
      />
      <div style={{ paddingLeft: 26 }}>
        {DATASETS.map((name) => (
          <Checkbox
            key={name}
            checked={selected.includes(name)}
            label={name}
            onChange={(event) => toggle(name, event.currentTarget.checked)}
          />
        ))}
      </div>
    </div>
  );
}

Inline#

inline places checkboxes on one line.

import { Checkbox } from "@clawscale/react";

const DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];

export default function CheckboxInline() {
  return (
    <div>
      {DAYS.map((day, index) => (
        <Checkbox key={day} defaultChecked={index < 5} inline label={day} />
      ))}
    </div>
  );
}

Disabled#

disabled blocks changes in every state.

import { Checkbox } from "@clawscale/react";

export default function CheckboxDisabled() {
  return (
    <div>
      <Checkbox defaultChecked disabled label="Encrypt at rest (required by policy)" />
      <Checkbox disabled label="Allow public read access" />
      <Checkbox disabled indeterminate label="Apply to 3 of 8 buckets" />
    </div>
  );
}

Align right#

alignIndicator="end" moves the box after the label. Use it in settings lists with a fixed width.

import { Checkbox } from "@clawscale/react";

export default function CheckboxAlignRight() {
  return (
    <div style={{ width: 260 }}>
      <Checkbox alignIndicator="end" defaultChecked label="Daily email digest" />
      <Checkbox alignIndicator="end" defaultChecked label="Failure alerts" />
      <Checkbox alignIndicator="end" label="Page the on-call engineer" />
    </div>
  );
}

Interactive playground#

Props
View source on GitHub

Props interface#

Checkbox also accepts the HTML attributes of an <input>.

interface CheckboxProps extends ControlProps@clawscale/react

Checkbox component props.

PropsDescription
alignIndicator
Alignment = Alignment.START
Alignment of the indicator within container.
Inherited from ControlProps
checked
boolean
Whether the control is checked.
Inherited from CheckedControlProps
children
React.ReactNode
JSX label for the control.
Inherited from ControlProps
className
string
A space-delimited list of class names to pass along to a child element.
Inherited from Props
defaultChecked
boolean
Whether the control is initially checked (uncontrolled mode).
Inherited from CheckedControlProps
defaultIndeterminate
boolean
Whether this checkbox is initially indeterminate (uncontrolled mode).
disabled
boolean
Whether the control is non-interactive.
Inherited from ControlProps
indeterminate
boolean
Whether this checkbox is indeterminate, or "partially checked." The checkbox will appear with a small dash instead of a tick to indicate that the value is not exactly true or false.
inline
boolean
Whether the control should appear as an inline element.
Inherited from ControlProps
inputRef
React.Ref<HTMLInputElement>
Ref attached to the HTML <input> element backing this component.
Inherited from ControlProps
label
string
Text label for the control.
Inherited from ControlProps
labelElement
React.ReactNode
JSX element label for the control.
Inherited from ControlProps
largedeprecated
boolean = false
Whether this control should use large styles.
use size="large" instead
Inherited from ControlProps
onChange
React.ChangeEventHandler<HTMLInputElement>
Event handler invoked when input value is changed.
Inherited from CheckedControlProps
size
NonSmallSize = "medium"
Size of the control.
Inherited from ControlProps
tagName
keyof React.JSX.IntrinsicElements = "label"
Name of the HTML tag that wraps the checkbox.
Inherited from ControlProps