162 lines
8.7 KiB
TypeScript
162 lines
8.7 KiB
TypeScript
/**
|
||
* 表格:antd `Table` 的唯一切面(列定义 + 数据源)。
|
||
*
|
||
* 症状(2026-11 第三轮重写的对象):前两轮只把单元格**里面的**控件换成了 antd,
|
||
* 表格本身仍是自绘 `<table>`——表头吸顶、单元格描边、行悬停/选中底色
|
||
* 全靠 <Table> 上那一串 `[&_th]:…[&_td]:…` 子选择器手工维持,
|
||
* 横向滚动和高度上限又外包给 `<TableBox>` 手搓一个 overflow div。
|
||
* 于是它永远只有 antd 表格的七分像:表头没有排序/分组的能力、
|
||
* 列宽靠内容随机浮动、空态得由页面自己在表外补一个 <Empty>、
|
||
* 选中行要用 `[data-chosen]` 加一段 arbitrary variant 才能生效。
|
||
* 用户说「所有界面全部换掉」,表格是全站最密的地方,也是最露馅的地方。
|
||
* 做法:页面改成 antd 的标准用法——`columns` + `dataSource`。
|
||
* 四件原来手搓的事全部交还给 antd:
|
||
* · 密度 → `size`(默认 small,站内表格一律紧凑档)
|
||
* · 表头吸顶 + 高度上限 → `scroll.y`(由 maxHeight 翻译,false 就整页滚)
|
||
* · 窄屏横向滚动 → `scroll.x`(由 minWidth 翻译,0 表示不设下限、吃满容器)
|
||
* · 空态 → antd 自己的空态(需要专属文案时用 empty 覆写 locale.emptyText)
|
||
* 整行热区走 `onPick`(antd 的 onRow),选中行走 `chosen` 判定,
|
||
* 底色直接复用 antd 自己的 `.ant-table-row-selected`——它的颜色来自
|
||
* Table.rowSelectedBg token,不需要再写一条 arbitrary variant。
|
||
* 为什么删 TableBox / WideTable:滚动容器、吸顶表头、空态都是 Table 的内建能力,
|
||
* 在外面再套一层 div 等于把两套尺子请回来;列宽请写在 column.width 上
|
||
* (antd 在 scroll.y 下用的是 fixed 布局,没有 width 的列才拿剩余宽度)。
|
||
*/
|
||
import type { ReactNode } from 'react'
|
||
import { Grid as AntGrid, Table as AntTable, type TableProps } from 'antd'
|
||
import { cn } from '../../lib/cn'
|
||
|
||
/**
|
||
* 表格窄档开关:<992(antd/tailwind 的 `lg`)返回 true。
|
||
*
|
||
* 症状(2026-11 第 30 轮 · NEX 阶段 8.5 响应式收口):站内六张表的列宽预算是按
|
||
* 1440 那把尺子算的(全宽表容器 1118),窄屏容器只剩 356 / 462 / 502,
|
||
* 定宽列合计 400–902 直接把长文列挤成 0 宽(tableaudit「横向溢出 546」),
|
||
* 或者整张表拖一条横向滚动条才能看完。
|
||
* 做法:页面按档位给两套 columns——宽档(≥992)保持原来那份一字不动,
|
||
* 窄档把次要列并进主列的第二、三行(数据换位置,不失踪),长文列才有活路。
|
||
* 为什么是 992:侧栏 224 + 正文左右 48 之后,992 视口的表容器实测 670,
|
||
* 是「全站那份定宽预算放得下」的最窄一档;992 以下实测只有 502 / 462 / 356,
|
||
* 而 768 那一档比 576 还窄(侧栏没折叠,正文被压到 462),
|
||
* 所以三档窄屏共用同一套精简列,不再按 576/768 细分。
|
||
* 为什么用 Grid.useBreakpoint 而不是 CSS `max-lg:`:换的是**列定义**(少几列、
|
||
* 单元格合成几行),不是显隐两个 DOM;两套 columns 只有一份在渲染,
|
||
* 表头与单元格的辅助技术朗读才不会读到藏起来的列。
|
||
*/
|
||
export function useNarrowTable(): boolean {
|
||
return AntGrid.useBreakpoint().lg === false
|
||
}
|
||
|
||
/** 列定义:直接借用 antd 的类型,页面不需要(也不准)自己 import antd */
|
||
export type Column<T> = NonNullable<TableProps<T>['columns']>[number]
|
||
export type Columns<T> = NonNullable<TableProps<T>['columns']>
|
||
|
||
/**
|
||
* 长文本列:允许折行而不是把整张表顶宽(旧 cellWrap)。
|
||
* 用法是 column.className——antd 会把它落到该列的每个单元格上。
|
||
*
|
||
* 但它只解决「不顶宽表格」,不解决「不撑高行」:一句 90 字的质检结论
|
||
* 在 464px 的列里仍要折 7 行,整张表一屏只剩 4 行可读。封顶交给 <Clamp>。
|
||
*/
|
||
export const cellWrap = 'max-w-[340px] whitespace-normal leading-normal wrap-anywhere'
|
||
|
||
/**
|
||
* 单元格里读不完的长文案:最多折两行,剩下的收进原生 title。
|
||
*
|
||
* 症状(第 27 轮全站表格体检):审核留痕「说明」464px 被一条质检长文顶到 255px 行高,
|
||
* 调度清单「说明」只剩 112px 时同一句话折成 8 行(行高 168px)——
|
||
* 列宽和文案长度是两头堵,只调列宽永远调不完。
|
||
* 做法:列宽先按「长文列 ≥ 200px」分配,文案本身用本组件封顶两行,
|
||
* 完整内容挂原生 title,悬停看全。
|
||
* 为什么不用 antd 的 column.ellipsis:它只支持单行,长说明折成一行等于没显示;
|
||
* 也不为这一点事再包一层 Tooltip(R8:页面不 import antd,包组件要给全站用才值)。
|
||
*/
|
||
export function Clamp({ children, title, lines = 2, className }: {
|
||
children?: ReactNode
|
||
/** 悬停看全文:传拼接好的纯文本(组件猜不到调用方拼了什么) */
|
||
title?: string
|
||
/** 封顶行数:1 = 单行省略(标题类),2 = 两行省略(说明/错误类) */
|
||
lines?: 1 | 2
|
||
className?: string
|
||
}) {
|
||
if (children === null || children === undefined || children === '') return null
|
||
return (
|
||
<div title={title}
|
||
className={cn('whitespace-normal leading-normal wrap-anywhere',
|
||
lines === 1 ? 'line-clamp-1' : 'line-clamp-2', className)}>
|
||
{children}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
type TableExtra<T> = {
|
||
/** 列定义(antd Columns) */
|
||
columns: Columns<T>
|
||
/** 数据源 */
|
||
dataSource: readonly T[] | undefined
|
||
/** 行主键:字段名或取值函数,默认取 id */
|
||
rowKey?: TableProps<T>['rowKey']
|
||
/** 横向滚动下限(px)。0 = 不设下限,表格吃满容器;默认 720 沿用旧站窄屏兜底 */
|
||
minWidth?: number | false
|
||
/** 内容区高度上限(px)。false = 不内滚、不吸顶,跟随页面滚动 */
|
||
maxHeight?: number | false
|
||
/** 整行是热区(账号列表、模型列表这类「点一行看详情」):指针形状说明可点 */
|
||
onPick?: (row: T, index: number) => void
|
||
/** 当前选中的行:命中就用 antd 的选中行底色(排在 hover 之后,两层状态都看得见) */
|
||
chosen?: (row: T, index: number) => boolean
|
||
/** 密度:站内表格一律 sm;只有一张表例外(帮助页的属性表)也保持 sm,档位留着是为了别再写 size */
|
||
size?: 'sm' | 'md'
|
||
/** 空态文案(默认交还 antd 的「暂无数据」) */
|
||
empty?: ReactNode
|
||
className?: string
|
||
/** 行级样式追加(拖拽落点、临时高亮这类「只有页面知道」的状态):与内部选中态叠加 */
|
||
rowClassName?: TableProps<T>['rowClassName']
|
||
/** 行级事件追加:拖拽排序(draggable/onDrop)走这里,整行热区仍由 onPick 负责 */
|
||
onRow?: TableProps<T>['onRow']
|
||
/** 分页:站内表格默认全量渲染 + 容器内滚,需要时再传 antd 的分页配置 */
|
||
pagination?: TableProps<T>['pagination']
|
||
loading?: TableProps<T>['loading']
|
||
expandable?: TableProps<T>['expandable']
|
||
footer?: TableProps<T>['footer']
|
||
summary?: TableProps<T>['summary']
|
||
rowSelection?: TableProps<T>['rowSelection']
|
||
onChange?: TableProps<T>['onChange']
|
||
}
|
||
|
||
export function Table<T extends object>({
|
||
columns, dataSource, rowKey = 'id', minWidth = 720, maxHeight = 520,
|
||
onPick, chosen, size = 'sm', empty, className, rowClassName, onRow, ...rest
|
||
}: TableExtra<T>) {
|
||
const x = minWidth === false ? undefined : minWidth || undefined
|
||
const y = maxHeight === false ? undefined : maxHeight || undefined
|
||
return (
|
||
<div className={cn('nex-table min-w-0', className)}>
|
||
<AntTable<T>
|
||
columns={columns}
|
||
dataSource={dataSource as T[]}
|
||
rowKey={rowKey as never}
|
||
size={size === 'sm' ? 'small' : 'middle'}
|
||
pagination={rest.pagination === undefined ? false : rest.pagination}
|
||
scroll={{ x, y }}
|
||
locale={empty === undefined ? undefined : { emptyText: empty }}
|
||
rowClassName={(row, index) => cn(
|
||
onPick && 'cursor-pointer',
|
||
chosen?.(row, index ?? 0) && 'ant-table-row-selected',
|
||
typeof rowClassName === 'function' ? rowClassName(row, index, 0) : rowClassName
|
||
)}
|
||
onRow={(row, index) => {
|
||
const extra = onRow?.(row, index) || {}
|
||
if (!onPick) return extra
|
||
return {
|
||
...extra,
|
||
onClick: (e) => { extra.onClick?.(e); onPick(row, index ?? 0) },
|
||
onKeyDown: (e) => { if (e.key === 'Enter') onPick(row, index ?? 0) },
|
||
tabIndex: 0
|
||
} as typeof extra
|
||
}}
|
||
{...rest}
|
||
/>
|
||
</div>
|
||
)
|
||
}
|