Skip to Content
Checkbox

Checkbox

Basic control to allow selecting one or more items from a set.

Documentation

import { Checkbox } from "@optiaxiom/react"; export function App() { return <Checkbox>Label</Checkbox>; }

The children prop is used as the label of the checkbox control.

import { Checkbox, Group, Separator } from "@optiaxiom/react"; export function App() { return ( <Group gap="16"> <Checkbox>Label</Checkbox> <Separator orientation="vertical" /> <Checkbox /> </Group> ); }

Use the checked and defaultChecked props to toggle between controlled and uncontrolled usage. And combine it with onCheckedChange to listen for changes to the state.

"use client"; import { Button, Checkbox, Group } from "@optiaxiom/react"; import { useState } from "react"; export function App() { const [value, setValue] = useState(false); return ( <Group gap="16"> <Checkbox checked={value} onCheckedChange={setValue}> Label </Checkbox> <Button disabled={!value} onClick={() => setValue(false)}> Reset </Button> </Group> ); }

Integrate with form libraries like react-hook-form for validation and error handling.

"use client"; import { Button, Checkbox, Field, Group, Input } from "@optiaxiom/react"; import { useForm } from "react-hook-form"; type FormData = { email: string; newsletter: boolean; terms: boolean; }; export function App() { const { formState: { errors }, handleSubmit, register, } = useForm<FormData>(); return ( <form noValidate onSubmit={handleSubmit((data) => { console.log(data); })} > <Group flexDirection="column" gap="16"> <Field error={errors.email?.message} label="Email" required> <Input placeholder="you@example.com" {...register("email", { required: "Email is required", })} /> </Field> <Checkbox {...register("newsletter")}>Subscribe to newsletter</Checkbox> <Field error={errors.terms?.message}> <Checkbox {...register("terms", { required: "You must accept the terms", })} > Accept terms and conditions </Checkbox> </Field> <Button alignSelf="start" type="submit"> Submit </Button> </Group> </form> ); }

Enable the disabled prop to toggle the disabled state of the checkbox control.

import { Checkbox, Group } from "@optiaxiom/react"; export function App() { return ( <Group gap="16"> <Checkbox defaultChecked disabled> Label </Checkbox> <Checkbox disabled>Label</Checkbox> </Group> ); }

Enable the indeterminate prop to toggle the indeterminate state of the checkbox control.

import { Checkbox } from "@optiaxiom/react"; export function App() { return <Checkbox indeterminate>Label</Checkbox>; }

Use the description prop to add helper text after the label.

import { Checkbox } from "@optiaxiom/react"; export function App() { return <Checkbox description="Helper Text">Label</Checkbox>; }

Implement a “select all” checkbox that shows indeterminate state when some items are selected.

"use client"; import { Checkbox, Group } from "@optiaxiom/react"; import { useState } from "react"; const fruits = ["Apple", "Banana", "Orange", "Mango"]; export function App() { const [selected, setSelected] = useState<string[]>([]); const allSelected = selected.length === fruits.length; const someSelected = selected.length > 0 && !allSelected; return ( <Group flexDirection="column" gap="8"> <Checkbox checked={allSelected} indeterminate={someSelected} onCheckedChange={(checked) => { setSelected(checked ? [...fruits] : []); }} > Select All </Checkbox> <Group flexDirection="column" gap="8" pl="24"> {fruits.map((fruit) => ( <Checkbox checked={selected.includes(fruit)} key={fruit} onCheckedChange={(checked) => { setSelected((prev) => checked ? [...prev, fruit] : prev.filter((item) => item !== fruit), ); }} > {fruit} </Checkbox> ))} </Group> </Group> ); }

RadioGroup

Basic control to allow selecting only one item from a set.

Switch

Control to allow toggling between checked and not checked state.

Field

Wrapper for inputs to provide context such as label, help text and required state.

Props

Supports all Box props in addition to its own. Renders a <div> element but forwards all props to a hidden <input> element.

Prop

asChild

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read the Composition guide for more details.

false | true

children

ReactNode

className

string

description

Add secondary text after the label.

ReactNode

indeterminate

Display a partially checked icon instead of the regular checkmark.

false | true

name

The name of the form control element.

string

onCheckedChange

Handler that is called when the checked state changes.

(checked: boolean) => void

required

Whether selecting this input is required.

false | true

value

The value of the form control element.

string | number | readonly string[]

Accessibility

Key

Description

Space

Toggles the checkbox between checked and unchecked

Tab

Moves focus to or away from the checkbox

Shift + Tab

Moves focus to the previous focusable element

Checkbox automatically handles accessibility:

  • Uses proper role="checkbox" and ARIA attributes
  • Supports aria-checked for checked/unchecked/indeterminate states
  • Associates label text with the checkbox control

Changelog

  • Moved indeterminate from checked state to explicit prop
  • Added component
Last updated on