# Table

Tabel data dari kolom dan baris, dengan opsi garis tiap sel, baris selang-seling, ukuran ringkas, dan baris footer. Digulir horizontal di layar sempit.

- Kategori: Data
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/table.json
- CLI: `npx markasui@latest add table`

## Kapan dipakai

Menampilkan data terstruktur yang perlu dibandingkan per kolom, misalnya daftar pesanan atau anggota.

- Selalu isi caption atau label agar region punya nama
- Ratakan kanan kolom angka dan nominal dengan align right
- Untuk sel berisi badge atau tombol pakai renderCell di React dan slot cell-<key> di Vue
- Pakai bordered dengan footer untuk laporan dan total, size sm untuk data padat
- Kirim baris total lewat footer, bukan ditambahkan ke rows

## Kode

### Blade

Simpan di `resources/views/components/markas/table.blade.php`. Tanpa dependensi tambahan.

```blade
@props([
    'columns' => [],
    'rows' => [],
    'caption' => null,
    'showCaption' => false,
    'label' => null,
    'bordered' => false,
    'square' => false,
    'striped' => false,
    'size' => 'md',
    'footer' => null,
])

@php
    $wrapper = 'overflow-x-auto border border-line focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary';
    $tableClass = 'w-full border-collapse text-sm';
    $captionClasses = [
        'hidden' => 'sr-only',
        'visible' => 'px-4 py-3 text-left font-medium text-ink',
    ];
    $headRow = 'bg-subtle text-left text-sm font-medium text-muted';
    $rounded = 'rounded-xl';
    $borderedHeadRow = 'border-b-2 border-line';
    $headCell = 'font-medium whitespace-nowrap';
    $row = 'border-t border-line transition-colors duration-150 ease-out hover:bg-subtle/50 motion-reduce:transition-none';
    $stripedRow = 'even:bg-subtle/40';
    $footRow = 'border-t-2 border-line bg-subtle font-medium';
    $cell = 'whitespace-nowrap text-ink';
    $borderedCell = 'border-l border-line first:border-l-0';
    $sizes = [
        'sm' => 'px-3 py-2 text-[13px]',
        'md' => 'px-4 py-3',
    ];
    $aligns = [
        'left' => 'text-left',
        'center' => 'text-center',
        'right' => 'text-right',
    ];

    $regionLabel = $label ?? $caption ?? 'Table';
    $cellClass = fn ($base, $column) => implode(' ', array_filter([
        $base,
        $sizes[$size] ?? $sizes['md'],
        $bordered ? $borderedCell : null,
        $aligns[$column['align'] ?? 'left'],
    ]));
@endphp

<div {{ $attributes->merge(['tabindex' => '0', 'role' => 'region', 'aria-label' => $regionLabel])->class([$wrapper, $rounded => ! $square]) }}>
    <table class="{{ $tableClass }}">
        @if ($caption)
            <caption class="{{ $captionClasses[$showCaption ? 'visible' : 'hidden'] }}">{{ $caption }}</caption>
        @endif
        <thead>
            <tr class="{{ $headRow }}{{ $bordered ? ' '.$borderedHeadRow : '' }}">
                @foreach ($columns as $column)
                    <th scope="col" class="{{ $cellClass($headCell, $column) }}">{{ $column['label'] }}</th>
                @endforeach
            </tr>
        </thead>
        <tbody>
            @foreach ($rows as $item)
                <tr class="{{ $row }}{{ $striped ? ' '.$stripedRow : '' }}">
                    @foreach ($columns as $column)
                        <td class="{{ $cellClass($cell, $column) }}">{{ $item[$column['key']] ?? '' }}</td>
                    @endforeach
                </tr>
            @endforeach
        </tbody>
        @if ($footer)
            <tfoot>
                <tr class="{{ $footRow }}">
                    @foreach ($columns as $column)
                        <td class="{{ $cellClass($cell, $column) }}">{{ $footer[$column['key']] ?? '' }}</td>
                    @endforeach
                </tr>
            </tfoot>
        @endif
    </table>
</div>
```

```blade
<x-markas.table
    caption="Daftar pesanan terbaru"
    label="Tabel pesanan"
    :columns="[
        ['key' => 'kode', 'label' => 'Pesanan'],
        ['key' => 'pelanggan', 'label' => 'Pelanggan'],
        ['key' => 'total', 'label' => 'Total', 'align' => 'right'],
    ]"
    :rows="$orders->map(fn ($o) => ['kode' => $o->code, 'pelanggan' => $o->customer, 'total' => $o->total_label])->all()"
/>
```

### React

Simpan di `src/components/markas/Table.tsx`. Tanpa dependensi tambahan.

```tsx
import type { ComponentProps, ReactNode } from "react";

const wrapper =
  "overflow-x-auto border border-line focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary";
const tableClass = "w-full border-collapse text-sm";
const captionClasses = {
  hidden: "sr-only",
  visible: "px-4 py-3 text-left font-medium text-ink",
};
const headRow = "bg-subtle text-left text-sm font-medium text-muted";
const rounded = "rounded-xl";
const borderedHeadRow = "border-b-2 border-line";
const headCell = "font-medium whitespace-nowrap";
const rowClass =
  "border-t border-line transition-colors duration-150 ease-out hover:bg-subtle/50 motion-reduce:transition-none";
const stripedRow = "even:bg-subtle/40";
const footRow = "border-t-2 border-line bg-subtle font-medium";
const cell = "whitespace-nowrap text-ink";
const borderedCell = "border-l border-line first:border-l-0";
const sizes = {
  sm: "px-3 py-2 text-[13px]",
  md: "px-4 py-3",
};
const aligns = {
  left: "text-left",
  center: "text-center",
  right: "text-right",
};
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type TableColumn = {
  key: string;
  label: ReactNode;
  align?: keyof typeof aligns;
};

export type TableRow = Record<string, string | number | null | undefined>;

export type TableProps = ComponentProps<"div"> & {
  columns: TableColumn[];
  rows: TableRow[];
  caption?: string;
  showCaption?: boolean;
  label?: string;
  /** Nama field berisi id unik per baris, dipakai sebagai key. Tanpa ini key memakai indeks. */
  rowKey?: string;
  /** Isi sel kustom. Kembalikan undefined untuk memakai nilai teks bawaan. */
  renderCell?: (row: TableRow, key: string) => ReactNode;
  /** Garis di setiap sel, seperti grid kotak. */
  bordered?: boolean;
  /** Sudut siku (tanpa radius), seperti tabel klasik. */
  square?: boolean;
  /** Latar selang-seling di baris genap. */
  striped?: boolean;
  size?: keyof typeof sizes;
  /** Satu baris tfoot opsional, nilai per key kolom (misalnya total). */
  footer?: TableRow;
};

export function Table({
  columns,
  rows,
  caption,
  showCaption = false,
  label,
  rowKey,
  renderCell,
  bordered = false,
  square = false,
  striped = false,
  size = "md",
  footer,
  className,
  ...props
}: TableProps) {
  const cellClass = (base: string, column: TableColumn) =>
    cx(
      base,
      sizes[size] ?? sizes.md,
      bordered && borderedCell,
      aligns[column.align ?? "left"],
    );

  return (
    <div
      tabIndex={0}
      role="region"
      aria-label={label ?? caption ?? "Table"}
      className={cx(wrapper, !square && rounded, className)}
      {...props}
    >
      <table className={tableClass}>
        {caption && (
          <caption
            className={captionClasses[showCaption ? "visible" : "hidden"]}
          >
            {caption}
          </caption>
        )}
        <thead>
          <tr className={cx(headRow, bordered && borderedHeadRow)}>
            {columns.map((column) => (
              <th
                key={column.key}
                scope="col"
                className={cellClass(headCell, column)}
              >
                {column.label}
              </th>
            ))}
          </tr>
        </thead>
        <tbody>
          {rows.map((row, index) => (
            <tr
              key={rowKey === undefined ? index : String(row[rowKey])}
              className={cx(rowClass, striped && stripedRow)}
            >
              {columns.map((column) => (
                <td key={column.key} className={cellClass(cell, column)}>
                  {renderCell?.(row, column.key) ?? row[column.key] ?? ""}
                </td>
              ))}
            </tr>
          ))}
        </tbody>
        {footer && (
          <tfoot>
            <tr className={footRow}>
              {columns.map((column) => (
                <td key={column.key} className={cellClass(cell, column)}>
                  {footer[column.key] ?? ""}
                </td>
              ))}
            </tr>
          </tfoot>
        )}
      </table>
    </div>
  );
}
```

```tsx
import { Table } from "@/components/markas/Table";

<Table
  caption="Daftar pesanan terbaru"
  label="Tabel pesanan"
  columns={[
    { key: "kode", label: "Pesanan" },
    { key: "status", label: "Status" },
    { key: "total", label: "Total", align: "right" },
  ]}
  rows={orders}
  renderCell={(row, key) =>
    key === "status" ? <Badge>{row.status}</Badge> : undefined
  }
/>;
```

### Vue

Simpan di `src/components/markas/Table.vue`. Tanpa dependensi tambahan.

```vue
<script setup lang="ts">
export type TableColumn = {
  key: string;
  label: string;
  align?: "left" | "center" | "right";
};
export type TableRow = Record<string, string | number | null | undefined>;

const props = withDefaults(
  defineProps<{
    columns: TableColumn[];
    rows: TableRow[];
    caption?: string;
    showCaption?: boolean;
    label?: string;
    /** Nama field berisi id unik per baris, dipakai sebagai key. Tanpa ini key memakai indeks. */
    rowKey?: string;
    /** Garis di setiap sel, seperti grid kotak. */
    bordered?: boolean;
    /** Sudut siku (tanpa radius), seperti tabel klasik. */
    square?: boolean;
    /** Latar selang-seling di baris genap. */
    striped?: boolean;
    size?: "sm" | "md";
    /** Satu baris tfoot opsional, nilai per key kolom (misalnya total). */
    footer?: TableRow;
  }>(),
  {
    bordered: false,
    square: false,
    striped: false,
    size: "md",
    footer: undefined,
    caption: undefined,
    showCaption: false,
    label: undefined,
    rowKey: undefined,
  },
);

defineSlots<Record<`cell-${string}`, (props: { row: TableRow }) => unknown>>();

const wrapper =
  "overflow-x-auto border border-line focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary";
const tableClass = "w-full border-collapse text-sm";
const captionClasses = {
  hidden: "sr-only",
  visible: "px-4 py-3 text-left font-medium text-ink",
};
const headRow = "bg-subtle text-left text-sm font-medium text-muted";
const rounded = "rounded-xl";
const borderedHeadRow = "border-b-2 border-line";
const headCell = "font-medium whitespace-nowrap";
const rowClass =
  "border-t border-line transition-colors duration-150 ease-out hover:bg-subtle/50 motion-reduce:transition-none";
const stripedRow = "even:bg-subtle/40";
const footRow = "border-t-2 border-line bg-subtle font-medium";
const cell = "whitespace-nowrap text-ink";
const borderedCell = "border-l border-line first:border-l-0";
const sizes = {
  sm: "px-3 py-2 text-[13px]",
  md: "px-4 py-3",
};
const aligns = {
  left: "text-left",
  center: "text-center",
  right: "text-right",
};

const cellClass = (base: string, column: TableColumn) => [
  base,
  sizes[props.size] ?? sizes.md,
  props.bordered && borderedCell,
  aligns[column.align ?? "left"],
];
</script>

<template>
  <div
    tabindex="0"
    role="region"
    :aria-label="props.label ?? props.caption ?? 'Table'"
    :class="[wrapper, !props.square && rounded]"
  >
    <table :class="tableClass">
      <caption
        v-if="props.caption"
        :class="captionClasses[props.showCaption ? 'visible' : 'hidden']"
      >
        {{
          props.caption
        }}
      </caption>
      <thead>
        <tr :class="[headRow, props.bordered && borderedHeadRow]">
          <th
            v-for="column in props.columns"
            :key="column.key"
            scope="col"
            :class="cellClass(headCell, column)"
          >
            {{ column.label }}
          </th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="(row, index) in props.rows"
          :key="props.rowKey === undefined ? index : String(row[props.rowKey])"
          :class="[rowClass, props.striped && stripedRow]"
        >
          <td
            v-for="column in props.columns"
            :key="column.key"
            :class="cellClass(cell, column)"
          >
            <slot :name="`cell-${column.key}`" :row="row">{{
              row[column.key] ?? ""
            }}</slot>
          </td>
        </tr>
      </tbody>
      <tfoot v-if="props.footer">
        <tr :class="footRow">
          <td
            v-for="column in props.columns"
            :key="column.key"
            :class="cellClass(cell, column)"
          >
            {{ props.footer[column.key] ?? "" }}
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>
```

```vue
<template>
  <Table
    caption="Daftar pesanan terbaru"
    label="Tabel pesanan"
    :columns="[
      { key: 'kode', label: 'Pesanan' },
      { key: 'status', label: 'Status' },
      { key: 'total', label: 'Total', align: 'right' },
    ]"
    :rows="orders"
  >
    <template #cell-status="{ row }">
      <Badge>{{ row.status }}</Badge>
    </template>
  </Table>
</template>
```

### Svelte

Simpan di `src/lib/components/markas/Table.svelte`. Tanpa dependensi tambahan.

```svelte
<script lang="ts" module>
  export type TableColumn = {
    key: string;
    label: string;
    align?: "left" | "center" | "right";
  };

  export type TableRow = Record<string, string | number | null | undefined>;
</script>

<script lang="ts">
  import type { Snippet } from "svelte";
  import type { HTMLAttributes } from "svelte/elements";

  const wrapper =
    "overflow-x-auto border border-line focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary";
  const tableClass = "w-full border-collapse text-sm";
  const captionClasses = {
    hidden: "sr-only",
    visible: "px-4 py-3 text-left font-medium text-ink",
  };
  const headRow = "bg-subtle text-left text-sm font-medium text-muted";
  const rounded = "rounded-xl";
  const borderedHeadRow = "border-b-2 border-line";
  const headCell = "font-medium whitespace-nowrap";
  const rowClass =
    "border-t border-line transition-colors duration-150 ease-out hover:bg-subtle/50 motion-reduce:transition-none";
  const stripedRow = "even:bg-subtle/40";
  const footRow = "border-t-2 border-line bg-subtle font-medium";
  const cell = "whitespace-nowrap text-ink";
  const borderedCell = "border-l border-line first:border-l-0";
  const sizes = {
    sm: "px-3 py-2 text-[13px]",
    md: "px-4 py-3",
  };
  const aligns = {
    left: "text-left",
    center: "text-center",
    right: "text-right",
  };

  type Props = HTMLAttributes<HTMLDivElement> & {
    columns: TableColumn[];
    rows: TableRow[];
    caption?: string;
    showCaption?: boolean;
    label?: string;
    /** Field holding a unique id per row, used as the key. Without it the index is used. */
    rowKey?: string;
    /** Custom cell content per column key. Other columns show the plain value. */
    cells?: Record<string, Snippet<[TableRow]>>;
    /** Lines around every cell, like a grid. */
    bordered?: boolean;
    /** Square corners (no radius), like a classic table. */
    square?: boolean;
    /** Alternating background on even rows. */
    striped?: boolean;
    size?: keyof typeof sizes;
    /** One optional tfoot row, values per column key (for example a total). */
    footer?: TableRow;
  };

  let {
    columns,
    rows,
    caption,
    showCaption = false,
    label,
    rowKey,
    cells = {},
    bordered = false,
    square = false,
    striped = false,
    size = "md",
    footer,
    class: className,
    ...rest
  }: Props = $props();

  const cellClass = (base: string, column: TableColumn) => [
    base,
    sizes[size] ?? sizes.md,
    bordered && borderedCell,
    aligns[column.align ?? "left"],
  ];
</script>

<!-- svelte-ignore a11y_no_noninteractive_tabindex -->
<div
  tabindex="0"
  role="region"
  aria-label={label ?? caption ?? "Table"}
  class={[wrapper, !square && rounded, className]}
  {...rest}
>
  <table class={tableClass}>
    {#if caption}
      <caption class={captionClasses[showCaption ? "visible" : "hidden"]}
        >{caption}</caption
      >
    {/if}
    <thead>
      <tr class={[headRow, bordered && borderedHeadRow]}>
        {#each columns as column (column.key)}
          <th scope="col" class={cellClass(headCell, column)}>{column.label}</th
          >
        {/each}
      </tr>
    </thead>
    <tbody>
      {#each rows as row, index (rowKey === undefined ? index : String(row[rowKey]))}
        <tr class={[rowClass, striped && stripedRow]}>
          {#each columns as column (column.key)}
            {@const custom = cells[column.key]}
            <td class={cellClass(cell, column)}
              >{#if custom}{@render custom(row)}{:else}{row[column.key] ??
                  ""}{/if}</td
            >
          {/each}
        </tr>
      {/each}
    </tbody>
    {#if footer}
      <tfoot>
        <tr class={footRow}>
          {#each columns as column (column.key)}
            <td class={cellClass(cell, column)}>{footer[column.key] ?? ""}</td>
          {/each}
        </tr>
      </tfoot>
    {/if}
  </table>
</div>
```

```svelte
<script lang="ts">
  import Badge from "$lib/components/markas/Badge.svelte";
  import Table, { type TableRow } from "$lib/components/markas/Table.svelte";

  const columns = [
    { key: "code", label: "Order" },
    { key: "status", label: "Status" },
    { key: "total", label: "Total", align: "right" as const },
  ];
</script>

{#snippet status(row: TableRow)}
  <Badge>{row.status}</Badge>
{/snippet}

<Table caption="Recent orders" {columns} rows={orders} cells={{ status }} />
```

`cells` memetakan key kolom ke snippet yang menerima baris. Kolom lain menampilkan nilai teks.

### Astro

Simpan di `src/components/markas/Table.astro`. Tanpa dependensi tambahan.

```astro
---
import type { HTMLAttributes } from "astro/types";

export type TableColumn = {
  key: string;
  label: string;
  align?: "left" | "center" | "right";
};

export type TableRow = Record<string, string | number | null | undefined>;

const wrapper =
  "overflow-x-auto border border-line focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary";
const tableClass = "w-full border-collapse text-sm";
const captionClasses = {
  hidden: "sr-only",
  visible: "px-4 py-3 text-left font-medium text-ink",
};
const headRow = "bg-subtle text-left text-sm font-medium text-muted";
const rounded = "rounded-xl";
const borderedHeadRow = "border-b-2 border-line";
const headCell = "font-medium whitespace-nowrap";
const rowClass =
  "border-t border-line transition-colors duration-150 ease-out hover:bg-subtle/50 motion-reduce:transition-none";
const stripedRow = "even:bg-subtle/40";
const footRow = "border-t-2 border-line bg-subtle font-medium";
const cell = "whitespace-nowrap text-ink";
const borderedCell = "border-l border-line first:border-l-0";
const sizes = {
  sm: "px-3 py-2 text-[13px]",
  md: "px-4 py-3",
};
const aligns = {
  left: "text-left",
  center: "text-center",
  right: "text-right",
};

type Props = HTMLAttributes<"div"> & {
  columns: TableColumn[];
  rows: TableRow[];
  caption?: string;
  showCaption?: boolean;
  label?: string;
  /** Lines around every cell, like a grid. */
  bordered?: boolean;
  /** Square corners (no radius), like a classic table. */
  square?: boolean;
  /** Alternating background on even rows. */
  striped?: boolean;
  size?: keyof typeof sizes;
  /** One optional tfoot row, values per column key (for example a total). */
  footer?: TableRow;
};

const {
  columns,
  rows,
  caption,
  showCaption = false,
  label,
  bordered = false,
  square = false,
  striped = false,
  size = "md",
  footer,
  class: className,
  ...rest
} = Astro.props;

const cellClass = (base: string, column: TableColumn) => [
  base,
  sizes[size] ?? sizes.md,
  bordered && borderedCell,
  aligns[column.align ?? "left"],
];

// A function child, (row, key) => markup, renders custom cells. Empty output falls back to the value.
const hasRenderer = Astro.slots.has("default");
const body = await Promise.all(
  rows.map((row) =>
    Promise.all(
      columns.map(async (column) => ({
        column,
        html: hasRenderer
          ? (await Astro.slots.render("default", [row, column.key])).trim()
          : "",
        text: row[column.key] ?? "",
      })),
    ),
  ),
);
---

<div
  tabindex="0"
  role="region"
  aria-label={label ?? caption ?? "Table"}
  class:list={[wrapper, !square && rounded, className]}
  {...rest}
>
  <table class={tableClass}>
    {
      caption && (
        <caption class={captionClasses[showCaption ? "visible" : "hidden"]}>
          {caption}
        </caption>
      )
    }
    <thead>
      <tr class:list={[headRow, bordered && borderedHeadRow]}>
        {
          columns.map((column) => (
            <th scope="col" class:list={cellClass(headCell, column)}>
              {column.label}
            </th>
          ))
        }
      </tr>
    </thead>
    <tbody>
      {
        body.map((cells) => (
          <tr class:list={[rowClass, striped && stripedRow]}>
            {cells.map(({ column, html, text }) =>
              html ? (
                <td class:list={cellClass(cell, column)} set:html={html} />
              ) : (
                <td class:list={cellClass(cell, column)}>{text}</td>
              ),
            )}
          </tr>
        ))
      }
    </tbody>
    {
      footer && (
        <tfoot>
          <tr class={footRow}>
            {columns.map((column) => (
              <td class:list={cellClass(cell, column)}>
                {footer[column.key] ?? ""}
              </td>
            ))}
          </tr>
        </tfoot>
      )
    }
  </table>
</div>
```

```astro
---
import Badge from "@/components/markas/Badge.astro";
import Table, { type TableRow } from "@/components/markas/Table.astro";
---

<Table caption="Recent orders" columns={columns} rows={orders}>
  {
    (row: TableRow, key: string) =>
      key === "status" && <Badge>{row.status}</Badge>
  }
</Table>
```

Function child dijalankan untuk setiap sel. Kembalikan kosong untuk memakai nilai teks.

### Angular

Simpan di `src/app/markas/table.component.ts`. Tanpa dependensi tambahan.

```ts
import { NgTemplateOutlet } from "@angular/common";
import {
  booleanAttribute,
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
  type TemplateRef,
} from "@angular/core";

const wrapper =
  "overflow-x-auto border border-line focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary";
const tableClass = "w-full border-collapse text-sm";
const captionClasses = {
  hidden: "sr-only",
  visible: "px-4 py-3 text-left font-medium text-ink",
};
const headRow = "bg-subtle text-left text-sm font-medium text-muted";
const rounded = "rounded-xl";
const borderedHeadRow = "border-b-2 border-line";
const headCell = "font-medium whitespace-nowrap";
const rowClass =
  "border-t border-line transition-colors duration-150 ease-out hover:bg-subtle/50 motion-reduce:transition-none";
const stripedRow = "even:bg-subtle/40";
const footRow = "border-t-2 border-line bg-subtle font-medium";
const cell = "whitespace-nowrap text-ink";
const borderedCell = "border-l border-line first:border-l-0";
const sizes = {
  sm: "px-3 py-2 text-[13px]",
  md: "px-4 py-3",
};
const aligns = {
  left: "text-left",
  center: "text-center",
  right: "text-right",
};

export type TableColumn = {
  key: string;
  label: string;
  align?: keyof typeof aligns;
};

export type TableRow = Record<string, string | number | null | undefined>;

export type TableCellContext = { $implicit: TableRow };

const join = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

/**
 * Attribute selector on the native wrapper div: the user's class and attributes stay on the
 * scrollable region. Custom cells: `[cells]="{ status: statusCell }"` with
 * `<ng-template #statusCell let-row>`.
 */
@Component({
  selector: "div[markas-table]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [NgTemplateOutlet],
  host: {
    tabindex: "0",
    role: "region",
    "[attr.aria-label]": "label() ?? caption() ?? 'Table'",
    "[class]": "wrapperClass()",
  },
  template: `
    <table [class]="tableClass">
      @if (caption()) {
        <caption [class]="captionClass()">
          {{
            caption()
          }}
        </caption>
      }
      <thead>
        <tr [class]="headRowClass()">
          @for (column of columns(); track column.key) {
            <th scope="col" [class]="cellClass(headCell, column)">
              {{ column.label }}
            </th>
          }
        </tr>
      </thead>
      <tbody>
        @for (row of rows(); track trackRow($index, row)) {
          <tr [class]="bodyRowClass()">
            @for (column of columns(); track column.key) {
              <td [class]="cellClass(cell, column)">
                @if (cells()[column.key]; as custom) {
                  <ng-container
                    [ngTemplateOutlet]="custom"
                    [ngTemplateOutletContext]="{ $implicit: row }"
                  />
                } @else {
                  {{ row[column.key] ?? "" }}
                }
              </td>
            }
          </tr>
        }
      </tbody>
      @if (footer(); as foot) {
        <tfoot>
          <tr [class]="footRow">
            @for (column of columns(); track column.key) {
              <td [class]="cellClass(cell, column)">
                {{ foot[column.key] ?? "" }}
              </td>
            }
          </tr>
        </tfoot>
      }
    </table>
  `,
})
export class Table {
  readonly columns = input.required<TableColumn[]>();
  readonly rows = input.required<TableRow[]>();
  readonly caption = input<string>();
  readonly showCaption = input(false, { transform: booleanAttribute });
  readonly label = input<string>();
  /** Field holding a unique id per row, used to track rows. Without it the index is used. */
  readonly rowKey = input<string>();
  /** Custom cell templates per column key. Other columns show the plain value. */
  readonly cells = input<Record<string, TemplateRef<TableCellContext>>>({});
  /** Lines around every cell, like a grid. */
  readonly bordered = input(false, { transform: booleanAttribute });
  /** Square corners (no radius), like a classic table. */
  readonly square = input(false, { transform: booleanAttribute });
  /** Alternating background on even rows. */
  readonly striped = input(false, { transform: booleanAttribute });
  readonly size = input<keyof typeof sizes>("md");
  /** One optional tfoot row, values per column key (for example a total). */
  readonly footer = input<TableRow>();

  protected readonly tableClass = tableClass;
  protected readonly headCell = headCell;
  protected readonly cell = cell;
  protected readonly footRow = footRow;
  protected readonly wrapperClass = computed(() =>
    join(wrapper, !this.square() && rounded),
  );
  protected readonly captionClass = computed(
    () => captionClasses[this.showCaption() ? "visible" : "hidden"],
  );
  protected readonly headRowClass = computed(() =>
    join(headRow, this.bordered() && borderedHeadRow),
  );
  protected readonly bodyRowClass = computed(() =>
    join(rowClass, this.striped() && stripedRow),
  );

  protected cellClass(base: string, column: TableColumn) {
    return join(
      base,
      sizes[this.size()] ?? sizes.md,
      this.bordered() && borderedCell,
      aligns[column.align ?? "left"],
    );
  }

  protected trackRow(index: number, row: TableRow) {
    const key = this.rowKey();
    return key === undefined ? index : String(row[key]);
  }
}
```

```ts
import { Component } from "@angular/core";
import { Badge } from "./markas/badge.component";
import { Table, type TableColumn } from "./markas/table.component";

@Component({
  selector: "app-orders",
  imports: [Badge, Table],
  template: `
    <div
      markas-table
      caption="Recent orders"
      [columns]="columns"
      [rows]="orders"
      [cells]="{ status: statusCell }"
    ></div>
    <ng-template #statusCell let-row>
      <span markas-badge>{{ row.status }}</span>
    </ng-template>
  `,
})
export class Orders {
  columns: TableColumn[] = [
    { key: "code", label: "Order" },
    { key: "status", label: "Status" },
    { key: "total", label: "Total", align: "right" },
  ];
  orders = [{ code: "INV-1024", status: "Paid", total: "$250.00" }];
}
```

`cells` memetakan key kolom ke `ng-template` yang menerima baris sebagai `let-row`.

## Props

| Nama | Tipe | Default | Keterangan |
| --- | --- | --- | --- |
| `columns` | `{ key: string, label: string, align?: 'left' \| 'center' \| 'right' }[]` | [] | Daftar kolom. key menunjuk properti di setiap baris. |
| `rows` | `Record<string, string \| number>[]` | [] | Data baris. Di Blade berupa array PHP. |
| `caption` | `string` | - | Judul tabel di elemen caption. Disembunyikan secara visual kecuali show-caption aktif. |
| `showCaption` | `boolean` | false | Tampilkan caption di atas tabel. |
| `label` | `string` | caption or Table | aria-label untuk region pembungkus yang bisa digulir. Tanpa label dipakai caption, lalu "Table". Isi misalnya "Tabel pesanan" untuk situs berbahasa Indonesia. |
| `bordered` | `boolean` | false | Garis di sekeliling setiap sel, termasuk header, agar tampak seperti grid kotak klasik. Header mendapat garis bawah yang lebih tegas. |
| `square` | `boolean` | false | Sudut siku tanpa radius, seperti tabel spreadsheet klasik. |
| `striped` | `boolean` | false | Beri latar lembut pada baris genap. |
| `size` | `'sm' \| 'md'` | md | Padding sel. sm memakai padding lebih rapat dan teks 13px untuk data padat. |
| `footer` | `Record<string, string \| number>` | - | Satu baris footer opsional di tfoot, berisi nilai per key kolom, misalnya total. Key yang tidak ada menjadi sel kosong. |
| `rowKey` | `string` | - | Nama field berisi id unik per baris (misalnya id), dipakai sebagai key React dan Vue agar baris tidak tertukar saat data diurutkan atau dihapus. Tanpa ini key memakai indeks. Blade tidak memakainya. |
| `renderCell / slot cell-<key> / cells` | `(row, key) => ReactNode · slot { row } · Record<string, Snippet \| TemplateRef>` | - | Isi sel kustom. React memakai renderCell, Vue named slot cell-<key>, Svelte dan Angular peta cells berisi snippet atau ng-template per key kolom, Astro function child (row, key) yang mengembalikan markup atau kosong. Blade hanya teks. |

## Aksesibilitas

- Pembungkus memakai role region, aria-label, dan tabindex 0 agar tabel yang terpotong bisa difokus lalu digulir dengan panah.
- Header kolom memakai th dengan scope col.
- Caption tetap dibacakan screen reader walau disembunyikan secara visual.
- Baris footer dirender di dalam tfoot sehingga screen reader membacakannya sebagai footer tabel.

## Catatan

- Blade hanya menampilkan nilai teks yang di-escape. Untuk sel berisi komponen lain, tulis tabel langsung memakai class yang sama, atau siapkan string di controller.
- `renderCell` yang mengembalikan `undefined` memakai nilai teks bawaan, jadi cukup tangani kolom yang perlu diubah.
- Tambahkan `show-caption` (Blade, Vue) atau `showCaption` (React) untuk menampilkan caption di atas tabel.
- Sel tidak memotong baris (`whitespace-nowrap`). Di layar sempit tabel digulir horizontal di dalam pembungkusnya.
- Isi `rowKey` (React dan Vue, misalnya `rowKey="id"`) dengan nama field berisi id unik per baris saat baris bisa diurutkan, disaring, atau dihapus. Tanpa `rowKey`, key memakai indeks baris. Blade tidak membutuhkannya.
- Nama region diambil dari `label`, lalu `caption`, lalu "Table" (default bahasa Inggris). Isi `label` atau `caption` dalam Bahasa Indonesia, misalnya `label="Tabel pesanan"`.
- Tambahkan `bordered` untuk grid kotak dengan garis di setiap sel, `striped` untuk latar baris selang-seling, dan `size="sm"` untuk padding lebih rapat dengan teks 13px di tabel padat.
- Kirim baris total lewat `footer`, objek (array PHP di Blade) dengan key per kolom, misalnya `:footer="['item' => 'Total', 'value' => '$4,578.00']"` di Blade atau `footer={{ item: "Total", value: "$4,578.00" }}` di React. Hasilnya satu baris di `tfoot` dengan perataan sama seperti kolomnya.
