1
0

ColGroup.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classnames from 'classnames';
  4. import { get } from 'lodash';
  5. import { cssClasses } from '@douyinfe/semi-foundation/table/constants';
  6. import { flattenColumns } from '@douyinfe/semi-foundation/table/utils';
  7. import { ColumnProps, TableComponents } from './interface';
  8. export interface ColGroupProps {
  9. columns?: ColumnProps[];
  10. prefixCls?: string;
  11. className?: string;
  12. style?: React.CSSProperties;
  13. components?: TableComponents['body']
  14. }
  15. export default class ColGroup extends React.PureComponent<ColGroupProps> {
  16. static propTypes = {
  17. columns: PropTypes.array,
  18. prefixCls: PropTypes.string,
  19. className: PropTypes.string,
  20. style: PropTypes.object,
  21. components: PropTypes.object,
  22. };
  23. static defaultProps = {
  24. columns: [] as [],
  25. prefixCls: cssClasses.PREFIX,
  26. };
  27. render() {
  28. const { columns, className, style, prefixCls, components } = this.props;
  29. const ColGroup = get(components, 'colgroup.wrapper', 'colgroup');
  30. const Col = get(components, 'colgroup.col', 'col');
  31. const cols = flattenColumns(columns).map((column: ColumnProps, idx: number) => {
  32. const colStyle: { width?: number | string; minWidth?: number | string } = {};
  33. /**
  34. * table width
  35. */
  36. if (column.width) {
  37. colStyle.width = column.width;
  38. colStyle.minWidth = colStyle.width;
  39. }
  40. return (
  41. <Col
  42. className={classnames(`${prefixCls}-col`, column.className)}
  43. key={column.key || column.dataIndex || idx}
  44. style={colStyle}
  45. />
  46. );
  47. });
  48. const groupCls = classnames(`${prefixCls}-colgroup`, className);
  49. return (
  50. <ColGroup className={groupCls} style={style}>
  51. {cols}
  52. </ColGroup>
  53. );
  54. }
  55. }