control-flow
A todo list with <show>/<otherwise> filters, a keyed list with an index, derived counts, bind:value, a <form> with on:submit, toggles and a long press.
Run it
cargo run -p duolith-cli -- dev examples/control-flow # live, reloads on save
cargo run -p duolith-cli -- build examples/control-flow # static output in examples/control-flow/dist
The folder on the web: examples/control-flow.
Source
pages/index.duo
---
import { derived, state } from "@duolith/client";
type Todo = { id: number; title: string; done: boolean; starred: boolean };
type Filter = "all" | "active" | "done";
let todos = state<Todo[]>([
{ id: 1, title: "Buy oat milk", done: false, starred: false },
{ id: 2, title: "Water the plants", done: true, starred: false },
{ id: 3, title: "Call the bank", done: false, starred: true },
]);
let draft = state("");
let filter = state<Filter>("all");
let isTyping = state(false);
let nextId = 4;
let remaining = derived(() => todos.filter((todo) => !todo.done).length);
let completed = derived(() => todos.length - remaining);
let visible = derived(() =>
todos.filter((todo) => filter === "all" || (filter === "done") === todo.done),
);
let summary = derived(() => (remaining === 1 ? "1 thing left" : `${remaining} things left`));
function add() {
const title = draft.trim();
if (title === "") return;
todos = [...todos, { id: nextId++, title, done: false, starred: false }];
draft = "";
}
function update(todo: Todo, change: Partial<Todo>) {
todos = todos.map((each) => (each.id === todo.id ? { ...each, ...change } : each));
}
function remove(todo: Todo) {
todos = todos.filter((each) => each.id !== todo.id);
}
function clearDone() {
todos = todos.filter((todo) => !todo.done);
}
function reverse() {
todos = [...todos].reverse();
}
---
<screen title="Todos" class="p-4 gap-4 bg-canvas">
<view class="gap-1">
<text class="text-title text-ink">Todos</text>
<text class="text-caption text-muted">{summary} · {completed} done</text>
</view>
<form class="row gap-2 p-3 rounded-lg bg-surface border" on:submit={add}>
<input
class="grow"
placeholder="What needs doing?"
label="New todo"
bind:value={draft}
on:focus={() => (isTyping = true)}
on:blur={() => (isTyping = false)}
/>
<button
submit
class="px-4 py-2 rounded-md bg-primary text-on-primary text-label pressed:bg-ink"
>
Add
</button>
</form>
<show when={isTyping && draft.trim() !== ""}>
<text class="text-caption text-muted">Press Enter to add “{draft.trim()}”.</text>
</show>
<view class="row gap-2 items-center">
<show when={filter === "all"}>
<button class="chip chosen" on:press={() => (filter = "all")}>All</button>
</show>
<otherwise>
<button class="chip" on:press={() => (filter = "all")}>All</button>
</otherwise>
<show when={filter === "active"}>
<button class="chip chosen" on:press={() => (filter = "active")}>Active</button>
</show>
<otherwise>
<button class="chip" on:press={() => (filter = "active")}>Active</button>
</otherwise>
<show when={filter === "done"}>
<button class="chip chosen" on:press={() => (filter = "done")}>Done</button>
</show>
<otherwise>
<button class="chip" on:press={() => (filter = "done")}>Done</button>
</otherwise>
<view class="grow" />
<button class="text-label text-primary" label="Reverse the order" on:press={reverse}>
⇅ Reverse
</button>
</view>
<show when={visible.length > 0}>
<list each={visible} as="todo" key={todo.id} index="i" class="gap-2">
<view
class="row items-center gap-3 p-3 rounded-lg bg-surface border"
on:longpress={() => update(todo, { starred: !todo.starred })}
>
<text class="text-caption text-muted">{i + 1}.</text>
<toggle
label={`Done: ${todo.title}`}
checked={todo.done}
on:change={(event) => update(todo, { done: event.checked ?? false })}
/>
<view class="grow gap-1">
<text class="text-body text-ink">{todo.title}</text>
<show when={todo.starred}>
<text class="text-caption text-primary">★ Starred · hold to unstar</text>
</show>
</view>
<button
class="text-label text-danger"
label={`Remove ${todo.title}`}
on:press={() => remove(todo)}
>
✕
</button>
</view>
</list>
</show>
<otherwise>
<view class="p-6 items-center rounded-lg border">
<text class="text-body text-muted">Nothing here.</text>
</view>
</otherwise>
<show when={completed > 0}>
<view class="row justify-center">
<button class="text-label text-muted" on:press={clearDone}>Clear {completed} done</button>
</view>
</show>
<text class="text-caption text-muted">Hold a todo for half a second to star it.</text>
</screen>
<style>
.chip {
padding: 6px 14px;
border-radius: 9999px;
border: 1px solid var(--color-border);
background: var(--color-surface);
color: var(--color-ink);
font-weight: 600;
}
.chosen {
background: var(--color-ink);
border-color: var(--color-ink);
color: var(--color-canvas);
}
</style>pages/index.test.ts
// Tests for the todo list, run with: duolith test examples/control-flow
import { describe, expect, render, test } from "@duolith/testing";
describe("todos", () => {
test("adding a todo shows it in the list and counts it", async () => {
const screen = await render("/");
await screen.byLabel("New todo").type("Feed the cat");
await screen.byRole("button", { name: "Add", exact: true }).press();
await expect(screen.byText("Feed the cat")).toBeShown();
await expect(screen.byText("3 things left · 1 done")).toBeShown();
await expect(screen.byLabel("New todo")).toHaveText("");
});
test("the Done filter shows only finished todos", async () => {
const screen = await render("/");
await screen.byRole("button", { name: "Done", exact: true }).press();
await expect(screen.byText("Water the plants")).toBeShown();
await expect(screen.byText("Buy oat milk")).toBeHidden();
});
test("ticking a todo moves it to done, and clearing removes it", async () => {
const screen = await render("/");
await screen.byRole("switch", { name: "Done: Buy oat milk" }).press();
await expect(screen.byRole("switch", { name: "Done: Buy oat milk" })).toBeChecked();
await screen.byRole("button", { name: "Clear 2 done" }).press();
await expect(screen.byText("Buy oat milk")).toBeHidden();
await expect(screen.byRole("switch")).toHaveCount(1);
});
test("holding a todo stars it", async () => {
const screen = await render("/");
await screen.byText("Buy oat milk").hold();
await expect(screen.byText("★ Starred · hold to unstar")).toHaveCount(2);
});
});