Blocks Design System

Board item edit

A card for an item on a project board: views switched by keyboard, an edit dialog with a validated title, a column Select and an Urgent checkbox, a Cancel that never submits, and a toast when the save lands. Everything comes from the package root; the card’s content and the save logic belong to the app.

Write release notes

Doing

Draft the notes for 0.1.0 and link the tarball digest.

Source · recipes/board-item-edit.tsx
recipes/board-item-edit.tsx
'use client'

import {
  Badge,
  Body,
  Button,
  Card,
  Checkbox,
  Field,
  Heading,
  Modal,
  Select,
  Stack,
  Tabs,
  toastSuccess,
} from '@blocks/design-system'
import { type FormEvent, useRef, useState } from 'react'

const columns = [
  { value: 'todo', label: 'To do' },
  { value: 'doing', label: 'Doing' },
  { value: 'done', label: 'Done' },
]

interface Item {
  title: string
  column: string
  urgent: boolean
}

// An item on a project board: views by keyboard, an edit dialog with a validated form, a
// cancel that never submits, and a notice when the save lands.
export default function BoardItemEdit() {
  const [item, setItem] = useState<Item>({ title: 'Write release notes', column: 'doing', urgent: false })
  const [editing, setEditing] = useState(false)
  const titleInput = useRef<HTMLInputElement>(null)

  function save(event: FormEvent<HTMLFormElement>) {
    event.preventDefault()
    if (!editing) return
    const data = new FormData(event.currentTarget)
    const next = { title: String(data.get('title')), column: String(data.get('column')), urgent: data.get('urgent') === 'on' }
    setItem(next)
    setEditing(false)
    toastSuccess(`Saved “${next.title}”`)
  }

  const columnLabel = columns.find((column) => column.value === item.column)?.label

  return (
    <Card as="article" aria-labelledby="board-item-title">
      <Stack gap="md">
        <Stack direction="row" justify="space-between" align="center" wrap="wrap" gap="sm">
          <Heading as="h3" id="board-item-title" fontSize="lg">
            {item.title}
          </Heading>
          <Stack direction="row" gap="xs">
            <Badge tone="info">{columnLabel}</Badge>
            {item.urgent && <Badge tone="danger">Urgent</Badge>}
          </Stack>
        </Stack>
        <Tabs defaultValue="details">
          <Tabs.List aria-label="Item views" activateOnFocus>
            <Tabs.Tab value="details">Details</Tabs.Tab>
            <Tabs.Tab value="activity">Activity</Tabs.Tab>
          </Tabs.List>
          <Tabs.Panel value="details">
            <Body margin="none">Draft the notes for 0.1.0 and link the tarball digest.</Body>
          </Tabs.Panel>
          <Tabs.Panel value="activity">
            <Body margin="none">Moved to Doing this morning.</Body>
          </Tabs.Panel>
        </Tabs>
        <Modal open={editing} onOpenChange={setEditing}>
          <Modal.Trigger render={<Button type="button" />}>Edit item</Modal.Trigger>
          <Modal.Content initialFocus={titleInput}>
            <Modal.Header>Edit item</Modal.Header>
            <Stack as="form" gap="md" onSubmit={save}>
              <Field name="title" validationMode="onBlur">
                <Field.Label>Title</Field.Label>
                <Field.Control ref={titleInput} defaultValue={item.title} required maxLength={60} />
                <Field.Error match="valueMissing">Give the item a title.</Field.Error>
              </Field>
              <Select name="column" items={columns} defaultValue={item.column}>
                <Select.Label>Column</Select.Label>
                <Select.Trigger />
                <Select.Content>
                  {columns.map((column) => (
                    <Select.Item key={column.value} value={column.value}>
                      {column.label}
                    </Select.Item>
                  ))}
                </Select.Content>
              </Select>
              <Checkbox name="urgent" defaultChecked={item.urgent}>
                Urgent
              </Checkbox>
              <Modal.Footer>
                <Modal.Close render={<Button type="button" variant="ghost" />}>Cancel</Modal.Close>
                <Button type="submit" variant="primary">
                  Save
                </Button>
              </Modal.Footer>
            </Stack>
          </Modal.Content>
        </Modal>
      </Stack>
    </Card>
  )
}
Board item

What it uses

  • Card, Heading and Badge for the item.
  • Tabs with activateOnFocus, so a view opens as soon as the arrow keys reach it.
  • Modal with initialFocus on the title and a guard against a second submit while it closes.
  • Field with validationMode="onBlur", Select and Checkbox, all submitting through the native form’s FormData.
  • Button with an explicit type="button" on Cancel, and a toast for the result.