index.tsx 998 Bytes
import { Table, type TableProps } from 'antd';
import { useFillHeight } from './tools/useFillHeight';

/**
 * 自动铺满父容器剩余高度的表格。
 *
 * 原理:用 ResizeObserver 测量外层容器高度,扣除表头与分页高度后,
 * 作为 antd Table 的 scroll.y —— 表体精确填满、表头吸顶、表体内部纵向滚动。
 * 与 antd 内部 DOM 结构解耦,版本无关。
 *
 * 用法:放在具有确定高度的 flex 容器内(如外层 .uj-table-fill 的父级)。
 * 例:
 *   <div style={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
 *     <FillTable columns={cols} dataSource={data} rowKey="id" />
 *   </div>
 */
export function FillTable<T extends object = Record<string, unknown>>(props: TableProps<T>) {
  const { wrapRef, bodyHeight } = useFillHeight();

  return (
    <div ref={wrapRef} className="uj-table-fill">
      <Table {...props} scroll={{ ...props.scroll, y: bodyHeight ?? props.scroll?.y }} />
    </div>
  );
}