import { useState, type FormEvent } from 'react' import { Badge, Button, Card, ConfirmDialog, DescriptionItem, DescriptionList, Drawer, Form, FormActions, FormControl, FormField, FormLabel, FormMessage, Input, PageHeader, Textarea, ToastContainer, toast, } from '../../index' interface Customer { name: string email: string company: string notes: string } const INITIAL_CUSTOMER: Customer = { name: 'Ada Chen', email: 'ada@northwind.example', company: 'Northwind Trading', notes: 'Prefers invoices in USD. Renewal due in Q4.', } /** * Recipe: a detail page with drawer editing. * * Composition: PageHeader (back + Edit action) → DescriptionList inside a * Card for the read view → Drawer holding the edit form (Form family) → * ConfirmDialog guarding unsaved changes → toast confirming the save. * Every close path (X, Escape, backdrop, Cancel) funnels through one * `requestClose` so the dirty guard can never be bypassed. */ export function DetailDrawerPage() { const [customer, setCustomer] = useState(INITIAL_CUSTOMER) const [drawerOpen, setDrawerOpen] = useState(false) const [draft, setDraft] = useState(INITIAL_CUSTOMER) const [nameError, setNameError] = useState(undefined) const [discardOpen, setDiscardOpen] = useState(false) const isDirty = draft.name !== customer.name || draft.email !== customer.email || draft.company !== customer.company || draft.notes !== customer.notes const openEditor = () => { setDraft(customer) setNameError(undefined) setDrawerOpen(true) } // The single gate for closing the drawer: dirty → confirm, clean → close. const requestClose = () => { if (isDirty) { setDiscardOpen(true) } else { setDrawerOpen(false) } } const discardChanges = () => { setDiscardOpen(false) setDrawerOpen(false) } const save = (event: FormEvent) => { event.preventDefault() if (!draft.name.trim()) { setNameError('Name is required.') return } setCustomer(draft) setDrawerOpen(false) toast.success('Customer updated', { description: 'Your changes have been saved.' }) } return (
Active} onBack={() => toast.info('Back', { description: 'Wire onBack to your router.' })} actions={} /> Active
Name { setDraft({ ...draft, name: e.target.value }) setNameError(undefined) }} /> Email setDraft({ ...draft, email: e.target.value })} /> Company setDraft({ ...draft, company: e.target.value })} /> Notes