# Checklist

A to-do list: tick an item and it's struck through with the pen; untick it and the line pulls back out.

Docs: https://ballpoint.st9wd.com/docs/checklist

```bash
npx shadcn@latest add @ballpoint/checklist
```

```tsx
import { Checklist, ChecklistItem } from "@/components/ui/checklist"

<Checklist defaultValue={["pens"]}>
  <ChecklistItem value="pens">Buy more blue pens</ChecklistItem>
  <ChecklistItem value="ship">Ship it</ChecklistItem>
</Checklist>
```

Props:

- `value / defaultValue`: `string[]`. The done items, controlled or not.
- `onValueChange`: `(value: string[]) => void`. Called as items are ticked and unticked.
- `ChecklistItem value`: `string`. What the list reports when this item is done.
- `ChecklistItem disabled`: `boolean`. Can't be ticked or unticked.

Every other prop goes to Base UI's CheckboxGroup: https://base-ui.com/react/components/checkbox-group

## Example

```tsx
import { Checklist, ChecklistItem } from "@/components/ui/checklist";

export default function ChecklistDemo() {
  return (
    <Checklist defaultValue={["pens"]} aria-label="Before the launch">
      <ChecklistItem value="pens" seed="checklist-pens">
        Buy more blue pens
      </ChecklistItem>
      <ChecklistItem value="docs" seed="checklist-docs">
        Write the docs
      </ChecklistItem>
      <ChecklistItem value="ship" seed="checklist-ship">
        Ship it
      </ChecklistItem>
    </Checklist>
  );
}
```

## With progress

Controlled with value, feeding a Progress bar, with check-all and clear.

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Checklist, ChecklistItem } from "@/components/ui/checklist";
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";

const tasks = [
  ["pens", "Buy more blue pens"],
  ["docs", "Write the docs"],
  ["tests", "Run the test suite"],
  ["ship", "Ship it"],
] as const;

export default function ChecklistProgress() {
  const [done, setDone] = useState<string[]>(["pens", "docs"]);
  return (
    <div className="flex w-full max-w-sm flex-col gap-5">
      <Progress value={(done.length / tasks.length) * 100} seed="cp-progress">
        <ProgressLabel>Launch checklist</ProgressLabel>
        <ProgressValue>{() => `${done.length} of ${tasks.length}`}</ProgressValue>
      </Progress>
      <Checklist value={done} onValueChange={setDone} aria-label="Launch checklist">
        {tasks.map(([value, label]) => (
          <ChecklistItem key={value} value={value} seed={`cp-${value}`}>
            {label}
          </ChecklistItem>
        ))}
      </Checklist>
      <div className="flex gap-3">
        <Button variant="outline" size="sm" onClick={() => setDone(tasks.map(([v]) => v))} seed="cp-all">
          Check all
        </Button>
        <Button variant="ghost" size="sm" onClick={() => setDone([])} seed="cp-none">
          Clear
        </Button>
      </div>
    </div>
  );
}
```

## Disabled items

Items that can't be ticked yet are paler; a disabled item can still arrive done.

```tsx
import { Checklist, ChecklistItem } from "@/components/ui/checklist";

export default function ChecklistStates() {
  return (
    <Checklist defaultValue={["sketch", "lock"]} aria-label="Release steps">
      <ChecklistItem value="sketch" seed="cs-sketch">
        Sketch the layout
      </ChecklistItem>
      <ChecklistItem value="draw" seed="cs-draw">
        Draw the final version
      </ChecklistItem>
      <ChecklistItem value="lock" disabled seed="cs-lock">
        Lock the registry (done by CI)
      </ChecklistItem>
      <ChecklistItem value="publish" disabled seed="cs-publish">
        Publish (waits for the lock)
      </ChecklistItem>
    </Checklist>
  );
}
```

## Getting started

A card that counts down what is left and washes over when everything is done.

```tsx
"use client";

import { useState } from "react";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Checklist, ChecklistItem } from "@/components/ui/checklist";
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";

const tasks = [
  ["account", "Make an account"],
  ["pen", "Pick a pen"],
  ["paper", "Pick a paper"],
  ["write", "Write your first line"],
  ["share", "Show someone"],
] as const;

export default function ChecklistOnboarding() {
  const [done, setDone] = useState<string[]>(["account", "pen"]);
  const finished = done.length === tasks.length;
  return (
    <Card className="w-full max-w-sm" variant={finished ? "wash" : "outline"} seed="co-card">
      <CardHeader>
        <CardTitle>{finished ? "All set." : "Getting started"}</CardTitle>
        <CardDescription>{finished ? "That is everything. Now go and write." : `${tasks.length - done.length} to go.`}</CardDescription>
      </CardHeader>
      <CardContent className="flex flex-col gap-5">
        <Progress value={(done.length / tasks.length) * 100} size="sm" seed="co-progress">
          <ProgressLabel>Done so far</ProgressLabel>
          <ProgressValue>{() => `${done.length} / ${tasks.length}`}</ProgressValue>
        </Progress>
        <Checklist value={done} onValueChange={setDone} aria-label="Getting started">
          {tasks.map(([value, label]) => (
            <ChecklistItem key={value} value={value} seed={`co-${value}`}>
              {label}
            </ChecklistItem>
          ))}
        </Checklist>
      </CardContent>
    </Card>
  );
}
```
