/* eslint-disable max-len */ import React from 'react'; import PropTypes from 'prop-types'; import { get, noop } from 'lodash'; import classnames from 'classnames'; import ColGroup from './ColGroup'; import TableHeader from './TableHeader'; import { Fixed, TableComponents, Scroll, BodyScrollEvent, ColumnProps, OnHeaderRow, Sticky } from './interface'; export interface HeadTableProps { tableLayout?: 'fixed' | 'auto'; bodyHasScrollBar?: boolean; columns?: ColumnProps[]; components?: TableComponents; dataSource?: Record[]; fixed?: Fixed; handleBodyScroll?: React.EventHandler; prefixCls?: string; forwardedRef?: React.MutableRefObject | ((instance: any) => void); scroll?: Scroll; selectedRowKeysSet: Set; showHeader?: boolean; onDidUpdate?: (ref: React.MutableRefObject) => void; onHeaderRow?: OnHeaderRow; sticky?: Sticky } /** * When there are fixed columns, the header is rendered as a separate Table */ class HeadTable extends React.PureComponent { static propTypes = { tableLayout: PropTypes.string, bodyHasScrollBar: PropTypes.bool, columns: PropTypes.array, components: PropTypes.object, dataSource: PropTypes.array, fixed: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]), handleBodyScroll: PropTypes.func, prefixCls: PropTypes.string, forwardedRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func]), scroll: PropTypes.shape({ x: PropTypes.oneOfType([PropTypes.number, PropTypes.string, PropTypes.bool]), y: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), }), selectedRowKeysSet: PropTypes.instanceOf(Set).isRequired, // Useful when update is selected showHeader: PropTypes.bool, onDidUpdate: PropTypes.func, onHeaderRow: PropTypes.func, }; static defaultProps = { handleBodyScroll: noop, }; constructor(props: HeadTableProps = { selectedRowKeysSet: new Set() }) { super(props); } render() { const { scroll, prefixCls, fixed, forwardedRef, handleBodyScroll, columns, components, onDidUpdate, showHeader, tableLayout, bodyHasScrollBar, sticky } = this.props; if (!showHeader) { return null; } const Table = get(components, 'header.outer', 'table') as unknown as typeof React.Component; const x = get(scroll, 'x'); const headStyle: Partial = {}; const tableStyle: { width?: number | string } = {}; if (x && !fixed) { tableStyle.width = x; } if (bodyHasScrollBar) { headStyle.overflowY = 'scroll'; } const colgroup = ; const tableHeader = ( ); const headTableCls = classnames(`${prefixCls}-header`, { [`${prefixCls}-header-sticky`]: sticky, }); const stickyTop = get(sticky, 'top', 0); if (typeof stickyTop === 'number') { headStyle.top = stickyTop; } return (
{colgroup} {tableHeader}
); } } export default React.forwardRef((props, ref) => );