import { PureComponent } from 'react'; import classnames from 'classnames'; import Row from './Row'; import ColGroup from './ColGroup'; import { IGridScrollDelta, GridRowClassNameType, IGridRowClickHandler, IGridExpandation, IGridInnerFixedType, } from './types'; import { IGridInnerColumn, IGridProps } from './Grid'; import isNil from '../utils/isNil'; export interface IGridBodyProps { size: IGridProps['size']; prefix: string; columns: Array>; rowKey: string; rowClassName?: GridRowClassNameType; fixed?: IGridInnerFixedType; /** * true if Grid has any fixed columns */ hasFixedColumn: boolean; scroll: IGridScrollDelta; fixedColumnsBodyRowsHeight: Array; fixedColumnsBodyExpandRowsHeight: Array; expandRowKeys: boolean[]; mouseOverRowIndex: number; expandRender: IGridExpandation['expandRender']; rowProps?: (data: Data, index: number) => any; datasets: ReadonlyArray; components?: { row?: React.ComponentType; }; onRowClick: IGridRowClickHandler; onRowMouseEnter: (index: number) => void; disableHoverHighlight: boolean; } class Body extends PureComponent> { getRows() { const { prefix, datasets, columns, rowKey, rowClassName, onRowClick, onRowMouseEnter, mouseOverRowIndex, fixed, hasFixedColumn, scroll, expandRowKeys, expandRender, fixedColumnsBodyRowsHeight, fixedColumnsBodyExpandRowsHeight, components, rowProps, disableHoverHighlight, } = this.props; const row: React.ReactNode[] = []; (datasets || []).forEach((data, index) => { row.push( ); if (expandRender && expandRowKeys.length > 0) { const height = fixed && fixedColumnsBodyExpandRowsHeight[index] ? fixedColumnsBodyExpandRowsHeight[index] : undefined; const trProps = { key: `${index}-expand`, className: `${prefix}-grid-tr__expanded`, style: { display: expandRowKeys[index] ? '' : 'none', height }, }; if (fixed !== 'right') { row.push( {expandRender(data)} ); } else { row.push( ); } } }); return row; } onBodyMouseLeave = () => { const { onRowMouseEnter, hasFixedColumn } = this.props; hasFixedColumn && onRowMouseEnter(-1); }; renderTbody() { const { prefix, columns } = this.props; const tbodyClass = classnames(`${prefix}-grid-tbody`, { [`${prefix}-grid-tbody-span`]: columns.some( item => !!(item.colSpan || item.rowSpan) ), }); return ( {this.getRows()} ); } render() { const { scroll, fixed, prefix, columns, size } = this.props; const bodyStyle: React.CSSProperties = {}; if (!fixed && !isNil(scroll.x)) { bodyStyle.width = scroll.x; } return scroll.y ? ( {this.renderTbody()}
) : ( this.renderTbody() ); } } export default Body;