A table that makes its own records in place

TableCreateRow

2 min readWritten for your agentMarkdown

A ledger, an order's lines, a list of line items: the records are made one after another at the table's foot, never in a dialog. useCreateRow holds the row; what it returns:

  • create.row: spread it on the row, <TableCreateRow {...create.row} label="New line" onStart={() => create.start()}>;
  • create.start(values) opens the row (with these values over defaultValues), create.toggle(values) opens or closes it from a button of yours (aria-expanded={create.open}), create.cancel() closes it as Escape does;
  • create.values and create.setValue(name, value): your editors' values, by name (a Select chip, a number field);
  • create.landed(key): spread on every record's TableRow, so the new one grows in, holds its tint and is announced.

onCreate receives { title, summary, values }: the title trimmed (never empty: Create waits for one), the summary ('' in a table: it has none) and your editors' values. Add the record at once and return its key; throw an Error to refuse (its message shows on the row, the words kept); return a promise for a server's save (the words wait until it answers). After a create the row stays open for the next record: the title emptied and focused, the editors' values KEPT (the next line is often the same kind); keepOpen: false closes it instead. onUndo (⌘/Ctrl+Z, the toast's Undo) takes the record out again.

One TableCell per column, in the columns' order: the first holds CreateRowTitle, the last CreateRowActions (Cancel and Create; give the table a column for them, align="end" on its head and cells), the others your editors. A column marked hide: 'narrow' leaves the create row as it leaves every other row, so its editor goes with it on a narrow table: give that column's value a default, as a person on a phone cannot set it there.

Show the code62 lines
TSX
import { useState } from 'react';
import { Table, TableBody, TableHead, TableHeader, TableRow } from '@/components/ui/goop/table';
import { TableCell, TableCellLabel } from '@/components/ui/goop/table-cell';
import { CreateRowActions, CreateRowTitle, TableCreateRow, useCreateRow } from '@/components/ui/goop/create-row';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/goop/select';

type Line = { id: number; item: string; kind: string };

export function OrderLines() {
  const [lines, setLines] = useState<Line[]>([{ id: 1, item: 'Field notebooks', kind: 'Goods' }]);
  const create = useCreateRow<{ kind: string | undefined }>({
    noun: 'Line',
    defaultValues: { kind: 'Goods' },
    onCreate: ({ title, values }) => {
      const id = Date.now();
      setLines((all) => [...all, { id, item: title, kind: values.kind ?? 'Goods' }]);
      return id;
    },
    onUndo: (key) => setLines((all) => all.filter((line) => line.id !== key)),
  });
  return (
    <Table columns="minmax(160px, 1fr) 140px auto" aria-label="Order lines">
      <TableHeader>
        <TableRow>
          <TableHead>Item</TableHead>
          <TableHead>Kind</TableHead>
          <TableHead align="end" />
        </TableRow>
      </TableHeader>
      <TableBody>
        {lines.map((line) => (
          <TableRow key={line.id} {...create.landed(line.id)}>
            <TableCell>
              <TableCellLabel>{line.item}</TableCellLabel>
            </TableCell>
            <TableCell>{line.kind}</TableCell>
            <TableCell align="end" />
          </TableRow>
        ))}
        <TableCreateRow {...create.row} label="New line" onStart={() => create.start()}>
          <TableCell>
            <CreateRowTitle placeholder="Item" />
          </TableCell>
          <TableCell>
            <Select value={create.values.kind} onValueChange={(kind) => create.setValue('kind', kind)}>
              <SelectTrigger shape="chip" aria-label={`Kind: ${create.values.kind ?? 'none'}`}>
                <SelectValue />
              </SelectTrigger>
              <SelectContent aria-label="Kind">
                <SelectItem value="Goods">Goods</SelectItem>
                <SelectItem value="Service">Service</SelectItem>
              </SelectContent>
            </Select>
          </TableCell>
          <TableCell align="end">
            <CreateRowActions />
          </TableCell>
        </TableCreateRow>
      </TableBody>
    </Table>
  );
}

Where would you like to start?

Free · now

The Goop plugin

A design system for your AI coding agent, so screen ten matches screen one.

/plugin marketplace add goop-so/skillsthen /plugin install goop@goopCodex, Cursor, ChatGPT, the Claude app and more: Connect Goop
FreePro

The components

Every part live, with its props, its keys and what an agent sees. Whole page and app templates, working with sample data.