元素

Table

Table 排布应用作为子元素写出的少量行(发票、汇总、对比),并为屏幕阅读器提供表格语义:表格本身、行组、行、列标题和单元格,各有其角色。它的部件有 TableHeader、TableBody 和 TableFooter(行组)、TableRow(单元格横向排列)、TableHead(列的标题单元格)、TableCell 和 TableCaption。外观由应用决定:各部件没有自己的颜色、边框、内边距或宽度,都接受应用的 class 和 style。行数很多、需要排序、选择和可调整宽度的列时,使用 DataTable。

el-table.tsx
el-table.tsx运行中
import { mount, Text, View } from "nsh";
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "nsh/base";
import { Stage, size, 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::ui::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"))))
}

Table 及其部件来自 nsh/base,在 Rust 中是 nsh-base crate,由 table()、table_header()、table_body()、table_footer()、table_row()、table_head()、table_cell() 和 table_caption() 创建,配合类型化的样式方法和 .child(..)。行把单元格横向排列;单元格的宽度由应用设置,使各列对齐。

#属性

属性Rust类型默认值说明
accessibilityLabel.accessibility_label(..)string无仅 Table:屏幕阅读器对表格的称呼。
children.child(..)JSX.Element无部件包含的内容:Table 中的行组和标题说明,行组中的行,行中的单元格,单元格中的内容。
class、style样式方法string、StyleProp<ViewStyle>无部件的外观和布局。

#部件

部件位于屏幕阅读器中的角色
Table表格
TableHeaderTable行组:列标题所在的行
TableBodyTable行组:数据行
TableFooterTable行组:合计、汇总行
TableRow行组或 Table行;单元格横向排列
TableHead表头中的 TableRow列标题
TableCellTableRow单元格
TableCaptionTable应用显示的有关表格的内容

各平台把这些角色交给自己的屏幕阅读器:Linux(AT-SPI)、Web(ARIA)、Android 和 GPUI 后端使用表格、行、单元格和列标题角色;macOS 和 Windows 使用网格和分组;iOS 把列标题标为标题。

#另请参阅

  • DataTable:行列数量不限的表格,支持选择、排序以及可调整宽度和移动的列。
  • 无障碍:accessibilityRole 的取值,其中包括 table、row、cell、columnheader 和 rowgroup。