# Sheet widget

A small table the assistant hands over to be corrected: edit a cell, watch totals follow, and send exactly what changed back.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Sheet widget. Trip costs to split. Move with the arrows and type; totals follow, and Send hands the edits back. Preview: https://chatcn.dev/preview/sheet-widget-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/sheet-widget
```

## Usage

```tsx
import { SheetWidget } from "@/components/ui/chatcn/pro/sheet-widget"

<SheetWidget
  title="Lisbon trip costs"
  columns={[
    { id: "item", header: "Cost" },
    { id: "amount", header: "Amount", type: "currency", total: "sum" },
    { id: "share", header: "Each of 4", type: "currency", total: "sum",
      compute: (row) => Number(row.amount) / 4 },
  ]}
  defaultRows={rows}
  currency="EUR"
  onSubmit={(changes) => sendToAssistant(changes)}
/>
```

This is not a spreadsheet to live in; for that there is the AI spreadsheet. It is the table an answer comes with, small enough to read at a glance and quick to fix. Move with the arrow keys and type to replace a cell, or press Enter to change it; Enter saves and moves down, Tab moves across, Escape cancels. Worked out columns (`compute`, marked ƒ) and totals follow at once, and a changed total turns over softly. A cell that doesn't read as a number stays open with the reason under the table. Every edited cell keeps a blue corner, and with `onSubmit` a bar counts the edits with Undo all and Send, which passes the list of `{ rowId, columnId, from, to }`. `formulas` lets number cells take arithmetic such as `=120*3+40`, parsed without eval; it is off by default so an equals sign is never a surprise. `baseRows` restores a draft against what the assistant sent.

## Examples

### States

Example: States. Seats and shares with no change bar, and a restored draft with two edits waiting to be sent. Preview: https://chatcn.dev/preview/sheet-widget-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `columns` | `SheetWidgetColumn[]` | `{ id, header, type?, compute?, total?, width? }` |
| `defaultRows` | `SheetRow[]` | `{ id, ...cells }` |
| `baseRows` | `SheetRow[]` | What edits count against. Default `defaultRows` |
| `currency`, `locale` | `string` | For currency cells. Default USD |
| `formulas` | `boolean` | Arithmetic in number cells. Off by default |
| `addable` | `boolean` | Shows Add row |
| `onRowsChange` | `(rows) => void` | Every saved edit |
| `onSubmit` | `(changes, rows) => void` | Shows the change bar |
| `labels` | `Partial<SheetWidgetLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Arrow keys, Home, End | Move between cells; Ctrl with Home or End goes to the corners |
| Enter, F2, or typing | Edit the cell |
| Enter, Tab | Save and move down or across; Shift goes back |
| Escape | Cancel the edit |
| Delete | Clear the cell |

## Accessibility

- A real table with the grid pattern: one tab stop, arrow keys inside.
- Edited and worked out cells say so to screen readers; an error is an alert tied to the input.
- The change count is a polite live region.

## Motion

A changed total fades and unblurs in 260 ms. The error and the Sent note rise in. Reduced motion shows them in place.

Source: https://chatcn.dev/docs/components/sheet-widget