Elementos
Table
Table organiza algumas linhas que o app escreve como filhos (faturas, um resumo, uma comparação) e dá aos leitores de tela uma tabela: a tabela em si, grupos de linhas, linhas, cabeçalhos de coluna e células, cada um com seu papel. Suas partes são
TableHeader, TableBody e TableFooter (grupos de linhas), TableRow (suas células lado a lado), TableHead (a
célula de cabeçalho de uma coluna), TableCell e TableCaption. A aparência é do app: nenhuma parte tem cores, bordas, preenchimentos ou
larguras próprias, e cada uma aceita class e style do app. Para muitas linhas, ordenação, seleção e colunas redimensionáveis, a tabela é DataTable.
import { mount, Text, View } from "nsh";
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "nsh/base";
import { size, Stage, ui } from "./palette";
// A few rows the app writes out: the parts give screen readers a table, the
// classes and the palette give it its look.
const invoices = [
{ id: "INV001", status: "Paid", method: "Card", amount: 250 },
{ id: "INV002", status: "Pending", method: "Transfer", amount: 150 },
{ id: "INV003", status: "Unpaid", method: "Card", amount: 350 },
{ id: "INV004", status: "Paid", method: "Cash", amount: 450 },
];
const widths = ["w-24", "flex-1", "w-24", "w-24"];
function Cell(props: { i: number; head?: boolean; end?: boolean; children: string }) {
const style = { fontSize: size.label, color: props.head ? ui().muted : ui().text, fontWeight: props.head ? ("500" as const) : ("400" as const) };
const Part = props.head ? TableHead : TableCell;
return (
<Part class={`${widths[props.i]} px-3 py-2.5`}>
<Text style={{ ...style, textAlign: props.end ? "right" : "left" }}>{props.children}</Text>
</Part>
);
}
function App() {
const line = { borderBottomWidth: 1, borderColor: ui().line };
const total = invoices.reduce((sum, i) => sum + i.amount, 0);
return (
<Stage width={520}>
<View style={{ ...ui().card, borderRadius: 12, overflow: "hidden" }}>
<Table accessibilityLabel="Recent invoices">
<TableHeader>
<TableRow style={line}>
{["Invoice", "Status", "Method", "Amount"].map((title, i) => (
<Cell i={i} head end={i === 3}>
{title}
</Cell>
))}
</TableRow>
</TableHeader>
<TableBody>
{invoices.map((inv) => (
<TableRow style={line}>
<Cell i={0}>{inv.id}</Cell>
<Cell i={1}>{inv.status}</Cell>
<Cell i={2}>{inv.method}</Cell>
<Cell i={3} end>{`$${inv.amount.toFixed(2)}`}</Cell>
</TableRow>
))}
</TableBody>
<TableFooter>
<TableRow>
<Cell i={0} head>Total</Cell>
<Cell i={1}>{""}</Cell>
<Cell i={2}>{""}</Cell>
<Cell i={3} end>{`$${total.toFixed(2)}`}</Cell>
</TableRow>
</TableFooter>
<TableCaption class="px-3 py-2">
<Text style={{ fontSize: size.detail, color: ui().muted }}>A list of your recent invoices.</Text>
</TableCaption>
</Table>
</View>
</Stage>
);
}
mount(App);use nsh_base::{table, table_body, table_cell, table_head, table_header, table_row, theme};
use nsh_core::prelude::*;
fn invoices() -> impl IntoElement {
let cell = |label: &'static str| table_cell().w(px(120.0)).px_3().py_2().child(text(label));
table()
.accessibility_label("Recent invoices")
.child(
table_header().child(
table_row()
.border_b_1()
.border_color(move || theme().border)
.child(table_head().w(px(120.0)).px_3().py_2().child(text("Invoice")))
.child(table_head().w(px(120.0)).px_3().py_2().child(text("Amount"))),
),
)
.child(table_body().child(table_row().child(cell("INV001")).child(cell("$250.00"))))
}import { mount, Text } from "nsh";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "nsh/base";
function App() {
return (
<Table accessibilityLabel="Recent invoices">
<TableHeader>
<TableRow class="border-b border-border">
<TableHead class="w-32 px-3 py-2"><Text>Invoice</Text></TableHead>
<TableHead class="w-32 px-3 py-2"><Text>Amount</Text></TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell class="w-32 px-3 py-2"><Text>INV001</Text></TableCell>
<TableCell class="w-32 px-3 py-2"><Text>$250.00</Text></TableCell>
</TableRow>
</TableBody>
</Table>
);
}
mount(App);Table e suas partes vêm de nsh/base, o crate nsh-base em Rust, em que table(),
table_header(), table_body(), table_footer(), table_row(), table_head(), table_cell() e
table_caption() as criam, com os métodos de estilo tipados e .child(..). Uma linha dispõe suas células lado a lado; o
app define as larguras das células, para que as colunas fiquem alinhadas.
#Props
| Prop | Rust | Tipo | Padrão | Descrição |
|---|---|---|---|---|
accessibilityLabel |
.accessibility_label(..) |
string |
nenhum | Somente em Table: como os leitores de tela chamam a tabela. |
children |
.child(..) |
JSX.Element |
nenhum | O que a parte contém: grupos e uma legenda em um Table, linhas em um grupo, células em uma linha, conteúdo em uma célula. |
class, style |
métodos de estilo | string, StyleProp<ViewStyle> |
nenhum | A aparência e o layout da parte. |
#Partes
| Parte | Dentro de | Papel para leitores de tela |
|---|---|---|
Table |
tabela | |
TableHeader |
Table |
grupo de linhas: as linhas de cabeçalhos de coluna |
TableBody |
Table |
grupo de linhas: as linhas de dados |
TableFooter |
Table |
grupo de linhas: totais, uma linha de resumo |
TableRow |
um grupo, ou Table |
linha; suas células lado a lado |
TableHead |
um TableRow do cabeçalho |
cabeçalho de coluna |
TableCell |
TableRow |
célula |
TableCaption |
Table |
conteúdo que o app exibe sobre a tabela |
Cada plataforma repassa os papéis ao seu leitor de tela: papéis de tabela, linha, célula e cabeçalho de coluna no Linux (AT-SPI), na web (ARIA), no Android e no backend GPUI; grades e grupos no macOS e no Windows; cabeçalhos como cabeçalhos no iOS.
#Veja também
- DataTable: uma tabela com qualquer número de linhas e colunas, com seleção, ordenação e colunas redimensionáveis e móveis.
- Acessibilidade: os valores de
accessibilityRole,table,row,cell,columnheadererowgroup, entre outros.