# Table

A plain, semantic table on a board: grey sentence-case headers, no header band, no zebra rows, numbers right-aligned.

Docs: https://cozy-ui.solo.engineer/docs/components/table/

Install: `npx shadcn@latest add @cozy/table`

### Usage

```tsx
import { Table, TableHeader, TableBody, TableFooter, TableRow, TableHead, TableCell, TableCaption } from "@/components/ui/table"
```

For sorting, filtering, selection and paging use Data Table. Add `is-num` to numeric cells and their head.

### Example

```tsx
import { Badge } from "@/components/ui/badge";
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "@/components/ui/table";

const rows = [
  { plot: "Plot 1", crop: "Rice", status: "Ripe", coins: 120 },
  { plot: "Plot 2", crop: "Sweet potato", status: "Growing", coins: 260 },
  { plot: "Plot 3", crop: "Luffa gourd", status: "Growing", coins: 640 },
  { plot: "Plot 4", crop: "Bananas", status: "Ripe", coins: 380 },
];

export default function TableDemo() {
  return (
    <Table>
      <TableCaption>Today’s fields.</TableCaption>
      <TableHeader>
        <TableRow>
          <TableHead>Plot</TableHead>
          <TableHead>Crop</TableHead>
          <TableHead>Status</TableHead>
          <TableHead className="is-num">Yield</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {rows.map((r) => (
          <TableRow key={r.plot}>
            <TableCell>{r.plot}</TableCell>
            <TableCell>{r.crop}</TableCell>
            <TableCell><Badge variant={r.status === "Ripe" ? "success" : "secondary"}>{r.status}</Badge></TableCell>
            <TableCell className="is-num">{r.coins} coins</TableCell>
          </TableRow>
        ))}
      </TableBody>
      <TableFooter>
        <TableRow>
          <TableCell colSpan={3}>Total</TableCell>
          <TableCell className="is-num">{rows.reduce((s, r) => s + r.coins, 0).toLocaleString("en")} coins</TableCell>
        </TableRow>
      </TableFooter>
    </Table>
  );
}
```

### Source (components/ui/table.tsx)

```tsx
import * as React from "react";
import { cn } from "@/lib/utils";
import { ArrowUpGlyph } from "./glyphs";

function Table({ className, ...props }: React.ComponentProps<"table">) {
  return (
    <div data-slot="table-container" className="cz-table-wrap">
      <table data-slot="table" className={cn("cz-table", className)} {...props} />
    </div>
  );
}
function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
  return <thead data-slot="table-header" className={cn("cz-table__header", className)} {...props} />;
}
function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
  return <tbody data-slot="table-body" className={cn("cz-table__body", className)} {...props} />;
}
function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
  return <tfoot data-slot="table-footer" className={cn("cz-table__footer", className)} {...props} />;
}
function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
  return <tr data-slot="table-row" className={cn("cz-table__row", className)} {...props} />;
}
function TableHead({ className, ...props }: React.ComponentProps<"th">) {
  return <th data-slot="table-head" className={cn("cz-table__head", className)} {...props} />;
}
function TableCell({ className, ...props }: React.ComponentProps<"td">) {
  return <td data-slot="table-cell" className={cn("cz-table__cell", className)} {...props} />;
}
function TableCaption({ className, ...props }: React.ComponentProps<"caption">) {
  return <caption data-slot="table-caption" className={cn("cz-table__caption", className)} {...props} />;
}

/** A header button that shows and cycles sort direction. Put it inside a TableHead. */
function TableSortButton({
  direction = "none",
  className,
  children,
  ...props
}: React.ComponentProps<"button"> & { direction?: "asc" | "desc" | "none" }) {
  return (
    <button type="button" data-dir={direction} className={cn("cz-table__sort", className)} {...props}>
      {children}
      <ArrowUpGlyph />
    </button>
  );
}

export { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell, TableCaption, TableSortButton };
```

npm: `import { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell, TableCaption, TableSortButton } from "@0xtrou/cozy-ui"` (after `npm install @0xtrou/cozy-ui` and `import "@0xtrou/cozy-ui/styles.css"`).
