HeadTable.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* eslint-disable max-len */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { get, noop } from 'lodash-es';
  5. import classnames from 'classnames';
  6. import ColGroup from './ColGroup';
  7. import TableHeader from './TableHeader';
  8. import { Fixed, TableComponents, Scroll, BodyScrollEvent, ColumnProps } from './interface';
  9. export interface HeadTableProps {
  10. [x: string]: any;
  11. anyColumnFixed?: boolean;
  12. columns?: ColumnProps[];
  13. components?: TableComponents;
  14. dataSource?: Record<string, any>[];
  15. fixed?: Fixed;
  16. handleBodyScroll?: React.EventHandler<BodyScrollEvent>;
  17. prefixCls?: string;
  18. forwardedRef?: React.MutableRefObject<HTMLDivElement> | ((instance: any) => void);
  19. scroll?: Scroll;
  20. selectedRowKeysSet: Set<any>;
  21. showHeader?: boolean;
  22. onDidUpdate?: (ref: React.MutableRefObject<any>) => void;
  23. }
  24. /**
  25. * When there are fixed columns, the header is rendered as a separate Table
  26. */
  27. class HeadTable extends React.PureComponent<HeadTableProps> {
  28. static propTypes = {
  29. anyColumnFixed: PropTypes.bool,
  30. columns: PropTypes.array,
  31. components: PropTypes.object,
  32. dataSource: PropTypes.array,
  33. fixed: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
  34. handleBodyScroll: PropTypes.func,
  35. prefixCls: PropTypes.string,
  36. forwardedRef: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
  37. scroll: PropTypes.object,
  38. selectedRowKeysSet: PropTypes.instanceOf(Set).isRequired, // Useful when update is selected
  39. showHeader: PropTypes.bool,
  40. onDidUpdate: PropTypes.func,
  41. };
  42. static defaultProps = {
  43. handleBodyScroll: noop,
  44. };
  45. constructor(props: HeadTableProps = { selectedRowKeysSet: new Set() }) {
  46. super(props);
  47. }
  48. render() {
  49. const {
  50. scroll,
  51. prefixCls,
  52. fixed,
  53. forwardedRef,
  54. handleBodyScroll,
  55. columns,
  56. components,
  57. onDidUpdate,
  58. showHeader,
  59. anyColumnFixed,
  60. } = this.props;
  61. if (!showHeader) {
  62. return null;
  63. }
  64. const Table = get(components, 'header.outer', 'table');
  65. const x = get(scroll, 'x');
  66. const headStyle = {};
  67. const tableStyle: { width?: number | string } = {};
  68. if (x && !fixed) {
  69. tableStyle.width = x;
  70. }
  71. const colgroup = <ColGroup columns={columns} prefixCls={prefixCls} />;
  72. const tableHeader = (
  73. <TableHeader {...this.props} columns={columns} components={components} onDidUpdate={onDidUpdate} />
  74. );
  75. return (
  76. <div
  77. key="headTable"
  78. style={headStyle}
  79. className={`${prefixCls}-header`}
  80. ref={forwardedRef}
  81. onScroll={handleBodyScroll}
  82. >
  83. <Table
  84. style={tableStyle}
  85. className={classnames(prefixCls, {
  86. [`${prefixCls}-fixed`]: anyColumnFixed,
  87. })}
  88. >
  89. {colgroup}
  90. {tableHeader}
  91. </Table>
  92. </div>
  93. );
  94. }
  95. }
  96. export default React.forwardRef<HTMLDivElement, HeadTableProps>((props, ref) => <HeadTable {...props} forwardedRef={ref} />);