Skip to content

Forms

Checkbox

tsx
"use client";
import { Checkbox, Field } from "@cherry-design-system/react";

export function Terms() {
  return (
    <Field label="Terms" required>
      <Checkbox name="terms" label="I accept the terms of service" />
    </Field>
  );
}

A checkbox on a native <input type="checkbox">: it submits, resets and handles keyboard and assistive tech natively. The checkmark stroke draws in; indeterminate shows a dash. Use it for independent yes/no choices in a form. Use Toggle for settings that apply immediately and Radio for one choice out of several.

Examples

Sizes

sm, md and lg are 16, 18 and 20 px boxes.

tsx
"use client";
import { Checkbox, Field } from "@cherry-design-system/react";

export function Sizes() {
  return (
    <>
      <Field label="Small">
        <Checkbox size="sm" label="Remember me" />
      </Field>
      <Field label="Medium">
        <Checkbox size="md" label="Remember me" />
      </Field>
      <Field label="Large">
        <Checkbox size="lg" label="Remember me" />
      </Field>
    </>
  );
}

Controlled

tsx
"use client";
import { useState } from "react";
import { Checkbox, Field } from "@cherry-design-system/react";

export function Newsletter() {
  const [checked, setChecked] = useState(false);
  return (
    <Field label="Newsletter">
      <Checkbox
        label="Send me product updates"
        checked={checked}
        onChange={(e) => setChecked(e.target.checked)}
      />
    </Field>
  );
}

Indeterminate

Use indeterminate for a "select all" box when only some children are checked. It is announced as "mixed" and cleared by the next click.

tsx
"use client";
import { useState } from "react";
import { Checkbox, Field } from "@cherry-design-system/react";

const items = ["Email", "SMS", "Push"];

export function Channels() {
  const [on, setOn] = useState<string[]>(["Email"]);
  const all = on.length === items.length;
  return (
    <Field label="Channels">
      <Checkbox
        label="All channels"
        checked={all}
        indeterminate={on.length > 0 && !all}
        onChange={() => setOn(all ? [] : items)}
      />
    </Field>
  );
}

Error

tsx
"use client";
import { Checkbox, Field } from "@cherry-design-system/react";

export function Consent() {
  return (
    <Field label="Consent" error="You must accept the terms to continue.">
      <Checkbox name="terms" label="I accept the terms of service" />
    </Field>
  );
}

Properties

labelReactNode

Visible label. Omit only when an aria-label is given.

size"sm" | "md" | "lg"

16 / 18 / 20 px box. Defaults to "md".

invalidboolean

Error state; set automatically inside a <Field error>.

indeterminateboolean

Mixed state (a dash). Announced as "mixed"; cleared by the next click. Defaults to false.

wrapperClassNamestring

Class for the outer <label> (className goes to the <input>).

The ref and all native <input> attributes (except size and type) go to the <input>.

Styling

Root class cherry-checkbox (the outer <label>) with data-size (sm, md, lg) and states data-invalid, data-disabled, plus :checked, :indeterminate and :disabled on the input. Parts: cherry-checkbox__control, __input, __icon, __check, __dash, __label.

Reads --cherry-primary-default and --cherry-primary-fg when checked, --cherry-border-strong, --cherry-danger-solid, --cherry-ring and --cherry-ring-danger.

Accessibility

Native checkbox: Space toggles, the label is clickable, and indeterminate is announced as mixed. Inside a Field, the Field's hint or error is linked to the input through aria-describedby.