Checkbox

Row selection in tables and lists. The header checkbox uses Indeterminate when only some rows are selected.

Open in Figma

Usage

import { Checkbox } from '@pilar-amoda/ui/checkbox';

States

checked={false | true | "indeterminate"} and disabled. Hover (ring border) and focus (focus ring) come from :hover and :focus-visible. 16 × 16, radius 4, border 1.25.

import { Checkbox } from '@pilar-amoda/ui/checkbox';

/** Empty, Checked, Indeterminate and Disabled. Hover and focus are :hover / :focus-visible. */
export const CheckboxStates = () => (
  <>
    <Checkbox aria-label="Kosong" data-testid="chkEmpty_Checkbox" />
    <Checkbox aria-label="Dipilih" defaultChecked data-testid="chkChecked_Checkbox" />
    <Checkbox aria-label="Sebagian" checked="indeterminate" data-testid="chkPartial_Checkbox" />
    <Checkbox aria-label="Nonaktif" disabled data-testid="chkDisabled_Checkbox" />
    <Checkbox
      aria-label="Nonaktif, dipilih"
      disabled
      defaultChecked
      data-testid="chkDisabledChecked_Checkbox"
    />
  </>
);

Select all

The header checkbox is Checked only when every row is selected, Indeterminate when some are.

Pilih semua RO
Renovasi Gudang CikarangRO-1026-0004
Renovasi Kantor PusatRO-1026-0007
Pengadaan Rak Teluk NagaRO-1026-0009
'use client';

import { useState } from 'react';

import { Checkbox } from '@pilar-amoda/ui/checkbox';

const requestOrders = [
  { id: 'RO-1026-0004', name: 'Renovasi Gudang Cikarang' },
  { id: 'RO-1026-0007', name: 'Renovasi Kantor Pusat' },
  { id: 'RO-1026-0009', name: 'Pengadaan Rak Teluk Naga' },
] as const;

/** The header checkbox is Indeterminate while only some rows are selected. */
export const CheckboxSelectAll = () => {
  const [selected, setSelected] = useState<readonly string[]>([requestOrders[0].id]);

  const allSelected = selected.length === requestOrders.length;
  const headerState = allSelected ? true : selected.length > 0 ? 'indeterminate' : false;

  const toggleAll = () => {
    setSelected(allSelected ? [] : requestOrders.map((ro) => ro.id));
  };
  const toggle = (id: string, checked: boolean) => {
    setSelected((current) => (checked ? [...current, id] : current.filter((item) => item !== id)));
  };

  return (
    <div className="text-pilar-xs flex flex-col gap-3">
      <div className="text-pilar-muted-foreground flex items-center gap-3 font-medium">
        <Checkbox
          aria-label="Pilih semua RO"
          checked={headerState}
          onCheckedChange={toggleAll}
          data-testid="chkAll_Checkbox"
        />
        Pilih semua RO
      </div>
      {requestOrders.map((ro) => (
        <div key={ro.id} className="flex items-center gap-3">
          <Checkbox
            aria-label={`Pilih ${ro.name}`}
            checked={selected.includes(ro.id)}
            onCheckedChange={(checked) => {
              toggle(ro.id, checked);
            }}
            data-testid={`chkRo_${ro.id}`}
          />
          {ro.name}
          <span className="font-pilar-mono text-pilar-muted-foreground">{ro.id}</span>
        </div>
      ))}
    </div>
  );
};

Do and don't

Do: use Indeterminate in the header when only some rows are selected.
Don't: show Checked in the header when not every row is selected.