Examples
Create, edit, and confirm delete
Keep record identity visible through each local change and confirm destructive actions in a shared Dialog.
Projects
Manage local example records. No service request is made.
- Active
Atlas onboarding
Mai Nguyen
- Draft
Orbit billing
Phuc Tran
Project records with create, edit, and confirmed delete actions
"use client";
import { useId, useRef, useState } from "react";
import type { FormEvent } from "react";
import { Badge } from "@phuctech/ui/components/badge";
import { Button } from "@phuctech/ui/components/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@phuctech/ui/components/card";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@phuctech/ui/components/dialog";
import { EmptyState } from "@phuctech/ui/components/empty-state";
import { IconButton } from "@phuctech/ui/components/icon-button";
import { Input } from "@phuctech/ui/components/input";
import { PencilIcon, PlusIcon, TrashIcon } from "@phuctech/ui/icons";
type Project = { id: string; name: string; owner: string; status: "Active" | "Draft" };
type Editor = { mode: "create" } | { mode: "edit"; projectId: string };
const initialProjects: Project[] = [
{ id: "atlas", name: "Atlas onboarding", owner: "Mai Nguyen", status: "Active" },
{ id: "orbit", name: "Orbit billing", owner: "Phuc Tran", status: "Draft" },
];
export function Example() {
const [projects, setProjects] = useState(initialProjects);
const [query, setQuery] = useState("");
const [editor, setEditor] = useState<Editor | null>(null);
const [deleteTarget, setDeleteTarget] = useState<Project | null>(null);
const [draftName, setDraftName] = useState("");
const [notice, setNotice] = useState("");
const nextProjectNumber = useRef(0);
const inputId = useId();
const currentProject = editor?.mode === "edit" ? projects.find((project) => project.id === editor.projectId) : undefined;
const filteredProjects = projects.filter((project) => `${project.name} ${project.owner}`.toLowerCase().includes(query.trim().toLowerCase()));
function openCreate() {
setDraftName("");
setEditor({ mode: "create" });
}
function openEdit(project: Project) {
setDraftName(project.name);
setEditor({ mode: "edit", projectId: project.id });
}
function saveProject(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const name = draftName.trim();
if (!name || !editor) return;
if (editor.mode === "create") {
nextProjectNumber.current += 1;
setProjects((current) => [{ id: `project-new-${nextProjectNumber.current}`, name, owner: "You", status: "Draft" }, ...current]);
setNotice(`Created “${name}”.`);
} else {
setProjects((current) => current.map((project) => project.id === editor.projectId ? { ...project, name } : project));
setNotice(`Updated “${name}”.`);
}
setEditor(null);
}
function confirmDelete() {
if (!deleteTarget) return;
setProjects((current) => current.filter((project) => project.id !== deleteTarget.id));
setNotice(`Deleted “${deleteTarget.name}”.`);
setDeleteTarget(null);
}
return (
<Card className="w-full max-w-3xl min-w-0">
<CardHeader className="gap-4">
<div className="flex flex-wrap items-start justify-between gap-3">
<div><CardTitle>Projects</CardTitle><CardDescription>Manage local example records. No service request is made.</CardDescription></div>
<Button type="button" onClick={openCreate}><PlusIcon aria-hidden="true" />New project</Button>
</div>
<Input type="search" aria-label="Search projects" placeholder="Search by project or owner" value={query} onChange={(event) => setQuery(event.currentTarget.value)} />
</CardHeader>
<CardContent className="grid gap-3">
{notice ? <p role="status" className="text-sm text-[color:var(--color-success-text)]">{notice}</p> : null}
{filteredProjects.length ? (
<ul aria-label="Projects" className="grid gap-2">
{filteredProjects.map((project) => (
<li key={project.id} className="flex min-w-0 flex-wrap items-center gap-3 rounded-lg border border-border p-3">
<div className="min-w-0 flex-1"><p className="truncate font-medium">{project.name}</p><p className="mt-1 text-sm text-muted-foreground">{project.owner}</p></div>
<Badge variant={project.status === "Active" ? "success" : "secondary"}>{project.status}</Badge>
<div className="flex items-center gap-1">
<IconButton type="button" variant="ghost" size="sm" aria-label={`Edit ${project.name}`} onClick={() => openEdit(project)}><PencilIcon aria-hidden="true" /></IconButton>
<IconButton type="button" variant="ghost" size="sm" aria-label={`Delete ${project.name}`} onClick={() => setDeleteTarget(project)}><TrashIcon aria-hidden="true" /></IconButton>
</div>
</li>
))}
</ul>
) : (
<EmptyState kind="no-results" heading="No projects found" description="Try a different search or clear the current filter." action={<Button type="button" variant="outline" onClick={() => setQuery("")}>Clear search</Button>} />
)}
</CardContent>
<Dialog open={Boolean(editor)} onOpenChange={(open) => { if (!open) setEditor(null); }}>
<DialogContent>
<DialogHeader><DialogTitle>{editor?.mode === "edit" ? `Edit ${currentProject?.name ?? "project"}` : "Create project"}</DialogTitle><DialogDescription>Changes in this preview stay in local example state.</DialogDescription></DialogHeader>
<form id="crud-project-form" onSubmit={saveProject} className="grid gap-2">
<label htmlFor={inputId} className="text-sm font-medium">Project name</label>
<Input id={inputId} value={draftName} onChange={(event) => setDraftName(event.currentTarget.value)} required autoFocus />
</form>
<DialogFooter>
<DialogClose render={<Button type="button" variant="outline">Cancel</Button>} />
<Button type="submit" form="crud-project-form">{editor?.mode === "edit" ? "Save changes" : "Create project"}</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<Dialog open={Boolean(deleteTarget)} onOpenChange={(open) => { if (!open) setDeleteTarget(null); }}>
<DialogContent>
<DialogHeader><DialogTitle>Delete {deleteTarget?.name ?? "project"}?</DialogTitle><DialogDescription>This action cannot be undone. The example removes only its local fixture row.</DialogDescription></DialogHeader>
<DialogFooter>
<DialogClose render={<Button type="button" variant="outline">Cancel</Button>} />
<Button type="button" variant="destructive" onClick={confirmDelete}>Delete project</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</Card>
);
}
When to use
Use CRUD when a person can create or change records and needs to return to a list to compare the result. Start with local or fixture data while the interaction contract is being designed; do not add a service request just to make the documentation look complete.
The loop
- List. Show the identifying fields, status, owner, and last update needed to decide what to open.
- Create or edit. Use a focused form with a title that names the action and preserve the list context behind it.
- Confirm. Before an irreversible action, name the exact record and explain that the action cannot be undone.
- Report. Announce the result near the list and keep the updated record visible when possible.
The runnable Preview and Code/Copy tabs use src/components/design/examples/source/crud-pattern.tsx. The example edits local fixture data only and has no pattern-level property playground.
Destructive actions
Delete is not a menu decoration. Use a shared Dialog, put Cancel first, and disable or close the dialog only after the operation has completed. For bulk deletion, include the count and the scope in the confirmation copy.
“Delete Lumen research?” names the exact record and says the action cannot be undone.
Never delete on row click, on menu open, or through an unlabelled icon with no confirmation step.
Accessibility
- Give each row a stable accessible name and make icon-only edit/delete controls name the record they affect.
- Use a real heading in the edit and delete dialogs, and return focus to the triggering action when the dialog closes.
- Announce create, update, and delete results with a status message that remains readable after the list re-renders.
- Keep the list usable at narrow widths; allow content to wrap or scroll rather than clipping actions.
Do and don’t
Keep the record identity in the edit title, preserve the list context, and make the next action explicit.
Put a full multi-step workflow in a small Dialog or use color as the only record status.
Related
- Dialog provides focus management and the confirmation surface.
- Badge labels status with readable text, not color alone.
- IconButton supports compact row actions with required accessible names.