Ordo UI
组件Data Display

Table

@mox-hub/ui 组件 · Data Display · canonical

table@mox-hub/uiData Displaycanonicalsrc/components/table.tsx
变体演示table
名称状态
示例行运行中

Props API

Prop类型默认值说明
className*string——
containerClassName*string——
size*TableSize"dense"—

源码

packages/ui/src/components/table.tsx
"use client"

import * as React from "react"

import { cn } from "../lib/utils"

/** 行高两档(F 类 F7.2 / 宪法 §4.2):dense(默认,td py-2)/ comfortable(td py-2.5,高触达场景);同一表只选一档。表头固定 h-control-lg(F7.3) */
type TableSize = "dense" | "comfortable"

const TableSizeContext = React.createContext<TableSize>("dense")

function Table({
  className,
  containerClassName,
  size = "dense",
  ...props
}: React.ComponentProps<"table"> & {
  size?: TableSize
  /**
   * 外层滚动容器类名。DataTable 等已经自带滚动根时传 `overflow-visible`,
   * 让 `thead` 的 sticky 滚动祖先指向真正的滚动容器,而不是这层等高的内层 div。
   */
  containerClassName?: string
}) {
  return (
    <TableSizeContext.Provider value={size}>
      <div
        data-slot="table-container"
        className={cn("relative w-full overflow-x-auto overscroll-x-contain", containerClassName)}
      >
        <table
          data-slot="table"
          data-size={size}
          className={cn("w-full caption-bottom text-sm", className)}
          {...props}
        />
      </div>
    </TableSizeContext.Provider>
  )
}

function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
  return (
    <thead
      data-slot="table-header"
      className={cn("[&_tr]:border-b [&_tr]:border-border", className)}
      {...props}
    />
  )
}

function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
  return (
    <tbody
      data-slot="table-body"
      className={cn("[&_tr:last-child]:border-0", className)}
      {...props}
    />
  )
}

function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
  return (
    <tfoot
      data-slot="table-footer"
      className={cn(
        "border-t border-border bg-muted/50 font-medium [&>tr]:last:border-b-0",
        className
      )}
      {...props}
    />
  )
}

function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
  return (
    <tr
      data-slot="table-row"
      className={cn(
        "border-b border-border transition-colors duration-fast hover:bg-muted has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted",
        className
      )}
      {...props}
    />
  )
}

function TableHead({ className, ...props }: React.ComponentProps<"th">) {
  return (
    <th
      data-slot="table-head"
      className={cn(
        "h-control-lg px-2 text-start align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pe-0",
        className
      )}
      {...props}
    />
  )
}

function TableCell({ className, ...props }: React.ComponentProps<"td">) {
  const size = React.useContext(TableSizeContext)
  return (
    <td
      data-slot="table-cell"
      className={cn(
        "px-2 py-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pe-0",
        size === "comfortable" && "py-2.5",
        className
      )}
      {...props}
    />
  )
}

function TableCaption({
  className,
  ...props
}: React.ComponentProps<"caption">) {
  return (
    <caption
      data-slot="table-caption"
      className={cn("mt-4 text-sm text-muted-foreground", className)}
      {...props}
    />
  )
}

export {
  Table,
  TableHeader,
  TableBody,
  TableFooter,
  TableHead,
  TableRow,
  TableCell,
  TableCaption,
}

可访问性与 RTL

  • 键盘:交互件保留:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。
  • RTL:间距、对齐与方向性图标使用逻辑属性 (ms-* /text-start /rtl:-scale-x-100);通过 ThemeProvider 或I18nProvider 的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。
  • 文案:优先用组件labels /translate /translator props,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。
  • 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。

用法说明

基础表格,由 Table、Header、Body、Row、Head、Cell 组合;排序筛选等交互用 data-table。