import * as React from 'react'; import { PureComponent } from 'react'; import Td from './Td'; // 需要传入一个组件模板 export default class Body extends PureComponent { constructor(props) { super(props); const { datasets, isExpanded } = props; const expandItems = datasets.reduce((items, rowData, rowIndex) => { if (typeof isExpanded === 'function') { items[rowIndex] = isExpanded(rowData, rowIndex); } else { items[rowIndex] = false; } return items; }, {}); this.state = { expandItems, }; } handleExpand(rowIndex) { return () => { const expandItems = { ...this.state.expandItems }; expandItems[rowIndex] = !(expandItems[rowIndex] || 0); this.setState({ expandItems, }); }; } isExpanded(rowData, rowIndex) { return this.state.expandItems[rowIndex] || 0; } onRowClick(key) { const { selection } = this.props; if (selection.canRowSelect) { selection.onSelect( key, (selection.selectedRowKeys ?? []).indexOf(key) === -1 ); } } render() { const { datasets, columns, emptyLabel, rowKey, selection, getRowConf, expandRender, needExpand, } = this.props; const trs = []; const dataIterator = (rowData, rowIndex) => { const { canSelect = true, rowClass = '' } = getRowConf(rowData, rowIndex); const key = rowData[rowKey] || rowIndex; const tds = []; if (needExpand) { tds.push(
); } columns.forEach((item, columnIndex) => { // 位置信息 const pos = { row: rowIndex, column: columnIndex, }; let needSelect = false; if (selection.needSelect && columnIndex === 0) { needSelect = true; } tds.push( ); }); trs.push(
this.onRowClick(key)} > {tds}
); }; const expandedInterator = (rowData, rowIndex) => { const key = rowData[rowKey] || rowIndex; trs.push(
this.onRowClick(key)} >
{expandRender(rowData)}
); }; if (needExpand) { datasets.forEach((rowData, rowIndex) => { dataIterator(rowData, rowIndex); expandedInterator(rowData, rowIndex); }); } else { datasets.forEach((rowData, rowIndex) => { dataIterator(rowData, rowIndex); }); } return (
{datasets.length !== 0 ? ( trs ) : (
{emptyLabel}
)}
); } }