Table

Semantic table element for presenting data.

use radix_yew_themes::{
    Table, TableBody, TableCell, TableColumnHeaderCell, TableHeader, TableRow, TableRowHeaderCell,
};
use yew::prelude::*;

#[function_component]
pub fn TableExample() -> Html {
    html! {
        <Table>
            <TableHeader>
                <TableRow>
                    <TableColumnHeaderCell>{"Full name"}</TableColumnHeaderCell>
                    <TableColumnHeaderCell>{"Email"}</TableColumnHeaderCell>
                    <TableColumnHeaderCell>{"Group"}</TableColumnHeaderCell>
                </TableRow>
            </TableHeader>

            <TableBody>
                <TableRow>
                    <TableRowHeaderCell>{"Danilo Sousa"}</TableRowHeaderCell>
                    <TableCell>{"danilo@example.com"}</TableCell>
                    <TableCell>{"Developer"}</TableCell>
                </TableRow>

                <TableRow>
                    <TableRowHeaderCell>{"Zahra Ambessa"}</TableRowHeaderCell>
                    <TableCell>{"zahra@example.com"}</TableCell>
                    <TableCell>{"Admin"}</TableCell>
                </TableRow>

                <TableRow>
                    <TableRowHeaderCell>{"Jasper Eriksson"}</TableRowHeaderCell>
                    <TableCell>{"jasper@example.com"}</TableCell>
                    <TableCell>{"Developer"}</TableCell>
                </TableRow>
            </TableBody>
        </Table>
    }
}

API Reference

Root

Groups the Header and Body parts. This component is based on the table element and supports common margin props.

PropTypeDefault
sizeResponsive<1..3>1
variantTableVariantTableVariant::Ghost
layoutOption<Responsive<TableLayout>>-

Contains the column headings for the table, based on the thead element.

Body

Displays the table data. This component is based on the tbody element.

Row

A row of table cells. Based on the tr element.

PropTypeDefault
alignOption<Responsive<TableRowAlign>>-

Cell

A basic table cell. This component is based on the td element, but uses justify instead of align to control how horizontal space is distributed within the table cell.

PropTypeDefault
justifyOption<Responsive<TableCellJustify>>-
widthOption<Responsive<String>>-
min_widthOption<Responsive<String>>-
max_widthOption<Responsive<String>>-
pOption<Responsive<0..9>>-
pxOption<Responsive<0..9>>-
pyOption<Responsive<0..9>>-
ptOption<Responsive<0..9>>-
prOption<Responsive<0..9>>-
pbOption<Responsive<0..9>>-
plOption<Responsive<0..9>>-

ColumnHeaderCell

The header of a table column. Based on the th element and provides the same props interface as the Cell part.

PropTypeDefault
justifyOption<Responsive<TableCellJustify>>-
widthOption<Responsive<String>>-
min_widthOption<Responsive<String>>-
max_widthOption<Responsive<String>>-
pOption<Responsive<0..9>>-
pxOption<Responsive<0..9>>-
pyOption<Responsive<0..9>>-
ptOption<Responsive<0..9>>-
prOption<Responsive<0..9>>-
pbOption<Responsive<0..9>>-
plOption<Responsive<0..9>>-

RowHeaderCell

The header of a table row. Based on the th element and provides the same props interface as the Cell part.

PropTypeDefault
justifyOption<Responsive<TableCellJustify>>-
widthOption<Responsive<String>>-
min_widthOption<Responsive<String>>-
max_widthOption<Responsive<String>>-
pOption<Responsive<0..9>>-
pxOption<Responsive<0..9>>-
pyOption<Responsive<0..9>>-
ptOption<Responsive<0..9>>-
prOption<Responsive<0..9>>-
pbOption<Responsive<0..9>>-
plOption<Responsive<0..9>>-

Examples

Size

Use the size prop to control how large the text and padding of the table cells should be.

use radix_yew_themes::{
    Flex, FlexDirection, Table, TableBody, TableCell, TableColumnHeaderCell, TableHeader, TableRow,
    TableRowHeaderCell,
};
use yew::prelude::*;

#[function_component]
pub fn TableSizeExample() -> Html {
    html! {
        <Flex direction={FlexDirection::Column} gap=5 max_width="350px">
            <Table size=1>
                <TableHeader>
                    <TableRow>
                        <TableColumnHeaderCell>{"Full name"}</TableColumnHeaderCell>
                        <TableColumnHeaderCell>{"Email"}</TableColumnHeaderCell>
                    </TableRow>
                </TableHeader>

                <TableBody>
                    <TableRow>
                        <TableRowHeaderCell>{"Danilo Sousa"}</TableRowHeaderCell>
                        <TableCell>{"danilo@example.com"}</TableCell>
                    </TableRow>
                    <TableRow>
                        <TableRowHeaderCell>{"Zahra Ambessa"}</TableRowHeaderCell>
                        <TableCell>{"zahra@example.com"}</TableCell>
                    </TableRow>
                </TableBody>
            </Table>

            <Table size=2>
                <TableHeader>
                    <TableRow>
                        <TableColumnHeaderCell>{"Full name"}</TableColumnHeaderCell>
                        <TableColumnHeaderCell>{"Email"}</TableColumnHeaderCell>
                    </TableRow>
                </TableHeader>

                <TableBody>
                    <TableRow>
                        <TableRowHeaderCell>{"Danilo Sousa"}</TableRowHeaderCell>
                        <TableCell>{"danilo@example.com"}</TableCell>
                    </TableRow>
                    <TableRow>
                        <TableRowHeaderCell>{"Zahra Ambessa"}</TableRowHeaderCell>
                        <TableCell>{"zahra@example.com"}</TableCell>
                    </TableRow>
                </TableBody>
            </Table>

            <Table size=3>
                <TableHeader>
                    <TableRow>
                        <TableColumnHeaderCell>{"Full name"}</TableColumnHeaderCell>
                        <TableColumnHeaderCell>{"Email"}</TableColumnHeaderCell>
                    </TableRow>
                </TableHeader>

                <TableBody>
                    <TableRow>
                        <TableRowHeaderCell>{"Danilo Sousa"}</TableRowHeaderCell>
                        <TableCell>{"danilo@example.com"}</TableCell>
                    </TableRow>
                    <TableRow>
                        <TableRowHeaderCell>{"Zahra Ambessa"}</TableRowHeaderCell>
                        <TableCell>{"zahra@example.com"}</TableCell>
                    </TableRow>
                </TableBody>
            </Table>
        </Flex>
    }
}

With a Backplate

Use TableVariant::Surface to add a visually enclosed backplate to the table.

use radix_yew_themes::{
    Table, TableBody, TableCell, TableColumnHeaderCell, TableHeader, TableRow, TableRowHeaderCell,
    TableVariant,
};
use yew::prelude::*;

#[function_component]
pub fn TableWithABackplateExample() -> Html {
    html! {
        <Table variant={TableVariant::Surface}>
            <TableHeader>
                <TableRow>
                    <TableColumnHeaderCell>{"Full name"}</TableColumnHeaderCell>
                    <TableColumnHeaderCell>{"Email"}</TableColumnHeaderCell>
                    <TableColumnHeaderCell>{"Group"}</TableColumnHeaderCell>
                </TableRow>
            </TableHeader>

            <TableBody>
                <TableRow>
                    <TableRowHeaderCell>{"Danilo Sousa"}</TableRowHeaderCell>
                    <TableCell>{"danilo@example.com"}</TableCell>
                    <TableCell>{"Developer"}</TableCell>
                </TableRow>

                <TableRow>
                    <TableRowHeaderCell>{"Zahra Ambessa"}</TableRowHeaderCell>
                    <TableCell>{"zahra@example.com"}</TableCell>
                    <TableCell>{"Admin"}</TableCell>
                </TableRow>

                <TableRow>
                    <TableRowHeaderCell>{"Jasper Eriksson"}</TableRowHeaderCell>
                    <TableCell>{"jasper@example.com"}</TableCell>
                    <TableCell>{"Developer"}</TableCell>
                </TableRow>
            </TableBody>
        </Table>
    }
}

See Also