import React, { ReactNode, TableHTMLAttributes, useRef } from 'react' import { ColumnWithLooseAccessor, Row, TableBodyProps } from 'react-table' import classnames from 'classnames' import { concat } from '../utils' import { useScrollBar } from './hooks' import Colgroup from './Colgroup' import MeasureRow from './rows/Measure' import Placeholder from './rows/Placeholder' import { useScrollbarWidth } from './providers/ScrollbarWidthProvider' import { useTable } from './providers/TableProvider' import { TableProps } from './types' import { useColumnWidths } from './providers/ColumnWidthsProvider' import { useHorizontalScroll } from './providers/HorizontalScrollProvider' import { useRefs } from '../utils/hooks' type Props = { // 列配置信息 columns: Array> // 要渲染的 rows rows: Array> records: R[] prepareRow: (row: Row) => void bodyProps: TableBodyProps empty: ReactNode tableHtmlProps?: TableHTMLAttributes getRowClassName: TableProps['getRowClassName'] cellClassName?: string onRowClick: TableProps['onRowClick'] } export default function TBody(props: Props) { const { columns, rows, prepareRow, empty, records, bodyProps, tableHtmlProps, getRowClassName, cellClassName, onRowClick } = props const { prefix: tablePrefix, fixHead } = useTable() const { colWidths } = useColumnWidths() const { registerBodyDom } = useHorizontalScroll() const bodyPrefix = concat(tablePrefix, 'body') const bodyRef = useRef(null) const bodyRefs = useRefs(bodyRef, registerBodyDom) useScrollBar(bodyRef, useScrollbarWidth().setWidth) function renderMeasureRow() { if (rows.length > 0) { return ( key={-1} columns={columns} /> ) } } function renderBodyRow() { return rows.map((row, rowIndex) => { prepareRow(row) return ( // eslint-disable-next-line react/jsx-key onRowClick && onRowClick(records[rowIndex], rowIndex) } > {row.cells.map((cell, index) => cell.render('Cell', { key: cell.column.id, colWidths, className: cellClassName, index }))} ) }) } function renderPlaceholderRow() { return records.length === 0 && } if (fixHead) { return (
{renderMeasureRow()} {renderBodyRow()} {renderPlaceholderRow()}
) } return ( {renderBodyRow()} {renderPlaceholderRow()} ) }