# Table

Display structured data in a condensed, searchable format.

```tsx
import { Avatar, Badge, Button, Table } from '@fluxui-dev/react';
import { EllipsisHorizontalIcon } from '@fluxui-dev/react/icons';
import { useState } from 'react';

// orders: Order::orderBy($sortBy, $sortDirection)->paginate(5), passed as a page prop
const [sortBy, setSortBy] = useState('date');
const [sortDirection, setSortDirection] = useState('desc');

const sort = (column) => {
    if (sortBy === column) {
        setSortDirection(sortDirection === 'asc' ? 'desc' : 'asc');
    } else {
        setSortBy(column);
        setSortDirection('asc');
    }
};

<Table paginate={orders}>
    <Table.Columns>
        <Table.Column>Customer</Table.Column>
        <Table.Column sortable sorted={sortBy === 'date'} direction={sortDirection} onClick={() => sort('date')}>Date</Table.Column>
        <Table.Column sortable sorted={sortBy === 'status'} direction={sortDirection} onClick={() => sort('status')}>Status</Table.Column>
        <Table.Column sortable sorted={sortBy === 'amount'} direction={sortDirection} onClick={() => sort('amount')}>Amount</Table.Column>
    </Table.Columns>

    <Table.Rows>
        {orders.data.map((order) => (
            <Table.Row key={order.id}>
                <Table.Cell className="flex items-center gap-3">
                    <Avatar size="xs" src={order.customer_avatar} />

                    {order.customer}
                </Table.Cell>

                <Table.Cell className="whitespace-nowrap">{order.date}</Table.Cell>

                <Table.Cell className="py-0">
                    <Badge size="sm" color={order.status_color}>{order.status}</Badge>
                </Table.Cell>

                <Table.Cell variant="strong">{order.amount}</Table.Cell>

                <Table.Cell className="py-0">
                    <Button variant="ghost" size="sm" icon={EllipsisHorizontalIcon} aria-label="Actions" />
                </Table.Cell>
            </Table.Row>
        ))}
    </Table.Rows>
</Table>
```

## Simple

The primary table example above is a full-featured table with sorting, pagination, etc. Here's a clean example of a simple data table that you can use as a simpler starting point.

```tsx
<Table>
    <Table.Columns>
        <Table.Column>Customer</Table.Column>
        <Table.Column>Date</Table.Column>
        <Table.Column>Status</Table.Column>
        <Table.Column>Amount</Table.Column>
    </Table.Columns>

    <Table.Rows>
        <Table.Row>
            <Table.Cell>Lindsey Aminoff</Table.Cell>
            <Table.Cell>Jul 29, 10:45 AM</Table.Cell>
            <Table.Cell className="py-0"><Badge color="green" size="sm">Paid</Badge></Table.Cell>
            <Table.Cell variant="strong">$49.00</Table.Cell>
        </Table.Row>

        <Table.Row>
            <Table.Cell>Hanna Lubin</Table.Cell>
            <Table.Cell>Jul 28, 2:15 PM</Table.Cell>
            <Table.Cell className="py-0"><Badge color="green" size="sm">Paid</Badge></Table.Cell>
            <Table.Cell variant="strong">$312.00</Table.Cell>
        </Table.Row>

        <Table.Row>
            <Table.Cell>Kianna Bushevi</Table.Cell>
            <Table.Cell>Jul 30, 4:05 PM</Table.Cell>
            <Table.Cell className="py-0"><Badge color="zinc" size="sm">Refunded</Badge></Table.Cell>
            <Table.Cell variant="strong">$132.00</Table.Cell>
        </Table.Row>

        <Table.Row>
            <Table.Cell>Gustavo Geidt</Table.Cell>
            <Table.Cell>Jul 27, 9:30 AM</Table.Cell>
            <Table.Cell className="py-0"><Badge color="green" size="sm">Paid</Badge></Table.Cell>
            <Table.Cell variant="strong">$31.00</Table.Cell>
        </Table.Row>
    </Table.Rows>
</Table>
```

## Full-bleed

When placing a table inside a card, use the `bleed` prop to extend its dividers through the card's horizontal padding while keeping the first and last columns aligned with the card content. The gutter automatically adapts to the card's size.

```tsx
import { Button, Card, Heading, Table, Text } from '@fluxui-dev/react';
import { PlusIcon } from '@fluxui-dev/react/icons';

<Card>
    <div className="flex items-center justify-between gap-4">
        <div>
            <Heading>Recent customers</Heading>
            <Text className="mt-1">Your latest customer activity.</Text>
        </div>

        <Button size="sm" icon={PlusIcon}>Add customer</Button>
    </div>

    <Table bleed containerClassName="mt-6">
        <Table.Columns>
            <Table.Column>Customer</Table.Column>
            <Table.Column>Date</Table.Column>
            <Table.Column>Status</Table.Column>
            <Table.Column align="end">Amount</Table.Column>
        </Table.Columns>

        <Table.Rows>
            {orders.map((order) => (
                <Table.Row key={order.id}>
                    <Table.Cell variant="strong">{order.customer}</Table.Cell>
                    <Table.Cell>{order.date}</Table.Cell>
                    <Table.Cell>{order.status}</Table.Cell>
                    <Table.Cell align="end" variant="strong">{order.amount}</Table.Cell>
                </Table.Row>
            ))}
        </Table.Rows>
    </Table>
</Card>
```

### Custom gutters

Flux cards provide the gutter automatically, including `size="sm"` cards. For a custom container, set `--flux-bleed` to match its horizontal padding.

```tsx
<div className="p-4 [--flux-bleed:1rem]">
    <Table bleed>
        {/* ... */}
    </Table>
</div>
```

## Pagination

Allow users to navigate through different pages of data by passing in any Laravel paginator to the `paginate` prop.

```tsx
// orders: Order::paginate(5), passed as a page prop
<Table paginate={orders}>
    {/* ... */}
</Table>
```

### Pagination options

Pass a `Pagination` element to `paginate` to set its own props, such as `scrollTo` to scroll the page when a pagination button is clicked.

```tsx
import { Pagination } from '@fluxui-dev/react';

<Table paginate={<Pagination paginator={orders} scrollTo />} />
```

Use `onPageChange` to page through data in the browser instead of following the paginator's links.

```tsx
<Table paginate={<Pagination paginator={paginator} onPageChange={setPage} />} />
```

## Sortable

Allow users to sort rows by specific columns using a combination of the `sortable`, `sorted`, and `direction` props, and handle the click with `onClick`.

```tsx
<Table>
    <Table.Columns>
        <Table.Column>Customer</Table.Column>
        <Table.Column sortable sorted direction="desc">Date</Table.Column>
        <Table.Column sortable>Amount</Table.Column>
    </Table.Columns>

    {/* ... */}
</Table>
```

## Sticky header

Keep the header visible during vertical scrolling by adding the `sticky` prop to the `Table.Columns` component, and give it a background color.

```tsx
{/* Set the height of the table container... */}
<Table containerClassName="max-h-80">
    <Table.Columns sticky className="bg-white dark:bg-zinc-900">
        {/* ... */}
    </Table.Columns>

    {/* ... */}
</Table>
```

## Sticky columns

Keep important info visible during horizontal scrolling by adding the `sticky` prop to `Table.Column` and `Table.Cell` components, and give them a background color.

```tsx
<Table containerClassName="max-h-80">
    <Table.Columns sticky className="bg-white dark:bg-zinc-900">
        <Table.Column sticky className="bg-white dark:bg-zinc-900">ID</Table.Column>

        {/* ... */}
    </Table.Columns>

    <Table.Rows>
        {orders.map((order) => (
            <Table.Row key={order.id}>
                <Table.Cell sticky className="bg-white dark:bg-zinc-900">{order.id}</Table.Cell>

                {/* ... */}
            </Table.Row>
        ))}
    </Table.Rows>
</Table>
```

## Reference

### Table

**Prop:**

- `bleed` - Extends table dividers through the horizontal padding of a parent card while keeping the first and last columns aligned with the card content.
- `paginate` - A Laravel paginator, as Inertia receives it, to show pagination below the table. Also accepts a `Pagination` element to set its props, such as `scrollTo` or `onPageChange`.
- `containerClassName` - Additional CSS classes applied to the container. Useful for setting height constraints like `max-h-80`.
- `header` - Content displayed above the table, inside the container.
- `footer` - Content displayed below the table and above the pagination, inside the container.
- `className` - Additional CSS classes applied to the `table` element.

**CSS Variable:**

- `--flux-bleed` - Horizontal distance a bleeding table extends through its parent container. Defaults to `1.5rem`; set this to match custom parent padding. Flux cards provide it automatically.

**Attribute:**

- `data-flux-table` - Applied to the root element for styling and identification.

### Table.Columns

**Prop:**

- `sticky` - When present, makes the header row sticky when scrolling.
- `children` - The table columns.

### Table.Column

**Prop:**

- `align` - Alignment of the column content. Options: `start` (default), `center`, `end`.
- `sortable` - Enables sorting functionality for the column.
- `sorted` - Indicates this column is currently being sorted.
- `direction` - Sort direction when column is sorted. Options: `asc`, `desc`.
- `onClick` - Callback fired when the column header is clicked, such as to change the sort.
- `sticky` - When present, makes the column sticky when scrolling.

### Table.Sortable

**Prop:**

- `sorted` - Indicates the rows are sorted by this button's column.
- `direction` - Sort direction when sorted. Options: `asc`, `desc`.
- `render` - Element to render instead of the `button` tag, such as a router link to the sorted page, with the button's props merged in. Also accepts a function that receives the props and returns an element.

### Table.Rows

**Prop:**

- `children` - The table rows.

### Table.Row

**Prop:**

- `children` - The table cells for this row.
- `sticky` - When present, makes the row sticky when scrolling.

### Table.Cell

**Prop:**

- `align` - Alignment of the cell content. Options: `start` (default), `center`, `end`.
- `variant` - Visual style of the cell. Options: `default`, `strong`.
- `sticky` - When present, makes the cell sticky when scrolling.
