Skip to content
العربية
Data display

Table

A styled native table that scrolls sideways on small screens. Put numbers in cells with align="end" so the digits line up. TableHead is a column header (scope="col": text-xs, muted, one line, h-10). scope="row" makes a row header (a form line label) and looks like a body cell: py-3, text-sm, text-fg, normal weight, and it wraps. wrap lets a column header wrap onto more lines, and its height follows the text.

import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from "@percentsa/ui";

Examples

Invoice lines
ItemQuantityAmount
Design sprint118,000
Development (hours)12054,000
Hosting (1 year)12,400
Code
export function TableExample() {
  const t = useT();
  const lines = [
    { item: t("Design sprint", "مرحلة التصميم"), qty: 1, amount: 18000 },
    { item: t("Development (hours)", "التطوير (ساعات)"), qty: 120, amount: 54000 },
    { item: t("Hosting (1 year)", "الاستضافة (سنة)"), qty: 1, amount: 2400 },
  ];
  return (
    <Table caption={t("Invoice lines", "بنود الفاتورة")} className="max-w-lg">
      <TableHeader>
        <TableRow>
          <TableHead>{t("Item", "البند")}</TableHead>
          <TableHead align="end">{t("Quantity", "الكمية")}</TableHead>
          <TableHead align="end">{t("Amount", "المبلغ")}</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {lines.map((l) => (
          <TableRow key={l.item}>
            <TableCell>{l.item}</TableCell>
            <TableCell align="end">{l.qty}</TableCell>
            <TableCell align="end">
              <Money amount={l.amount} />
            </TableCell>
          </TableRow>
        ))}
      </TableBody>
    </Table>
  );
}

type Invoice = { id: string; client: readonly [string, string]; status: "paid" | "due" | "overdue"; amount: number };

const CLIENTS = [
  ["Masar", "مسار"],
  ["Nakhla Foods", "نخلة للأغذية"],
  ["Raha Clinics", "عيادات راحة"],
  ["Sadeem", "سديم"],
  ["Wadi Tech", "وادي التقنية"],
  ["Bayt Coffee", "قهوة بيت"],
  ["Najm Logistics", "نجم للخدمات اللوجستية"],
] as const;
const STATUSES = ["paid", "due", "overdue"] as const;
const INVOICES: Invoice[] = Array.from({ length: 23 }, (_, i) => ({
  id: `INV-${1040 + i}`,
  client: CLIENTS[i % CLIENTS.length]!,
  status: STATUSES[(i * 5) % 3]!,
  amount: 1500 + ((i * 7919) % 40) * 250,
}));

function useInvoiceColumns(): DataTableColumn<Invoice>[] {
  const t = useT();
  const lang = useLang();
  const status = { paid: t("Paid", "مدفوعة"), due: t("Due", "مستحقة"), overdue: t("Overdue", "متأخرة") };
  const client = (r: Invoice) => r.client[lang === "ar" ? 1 : 0];
  return [
    { key: "id", header: t("Invoice", "الفاتورة"), sortable: true, cell: (r) => <span dir="ltr" className="font-mono text-xs">{r.id}</span> },
    { key: "client", header: t("Client", "العميل"), sortable: true, cell: client, sortValue: client },
    { key: "status", header: t("Status", "الحالة"), cell: (r) => <Badge variant={r.status === "overdue" ? "solid" : r.status === "due" ? "outline" : "subtle"}>{status[r.status]}</Badge> },
    { key: "amount", header: t("Amount", "المبلغ"), sortable: true, align: "end", cell: (r) => <Money amount={r.amount} /> },
  ];
}
VAT return
LineAmount ()VAT amount ()
Standard rated sales120,00018,000
Zero rated sales30,0000
Code
export function TableRowHeadersExample() {
  const t = useT();
  const lines = [
    { label: t("Standard rated sales", "المبيعات الخاضعة للنسبة الأساسية"), base: 120000, vat: 18000 },
    { label: t("Zero rated sales", "المبيعات الخاضعة لنسبة الصفر"), base: 30000, vat: 0 },
  ];
  return (
    <Table caption={t("VAT return", "الإقرار الضريبي")} className="max-w-lg">
      <TableHeader>
        <TableRow>
          <TableHead>{t("Line", "البند")}</TableHead>
          <TableHead wrap align="end">{t("Amount (", "المبلغ (")}<RiyalSign />)</TableHead>
          <TableHead wrap align="end">{t("VAT amount (", "مبلغ الضريبة (")}<RiyalSign />)</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {lines.map((l) => (
          <TableRow key={l.label}>
            <TableHead scope="row">{l.label}</TableHead>
            <TableCell align="end">
              <Money amount={l.base} />
            </TableCell>
            <TableCell align="end">
              <Money amount={l.vat} />
            </TableCell>
          </TableRow>
        ))}
      </TableBody>
    </Table>
  );
}