1
0

ColGroup.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 } from './interface';
  8. export interface ColGroupProps {
  9. columns?: ColumnProps[];
  10. prefixCls?: string;
  11. className?: string;
  12. style?: React.CSSProperties;
  13. components?: Record<string, any>;
  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. // eslint-disable-next-line @typescript-eslint/no-shadow
  30. const ColGroup = get(components, 'colgroup.wrapper', 'colgroup');
  31. const Col = get(components, 'colgroup.col', 'col');
  32. const cols = flattenColumns(columns).map((column: ColumnProps, idx: number) => {
  33. const colStyle: { width?: number | string; minWidth?: number | string } = {};
  34. /**
  35. * table width
  36. */
  37. if (column.width) {
  38. colStyle.width = column.width;
  39. colStyle.minWidth = colStyle.width;
  40. }
  41. return (
  42. <Col
  43. className={classnames(`${prefixCls}-col`, column.className)}
  44. key={column.key || column.dataIndex || idx}
  45. style={colStyle}
  46. />
  47. );
  48. });
  49. const groupCls = classnames(`${prefixCls}-colgroup`, className);
  50. return (
  51. <ColGroup className={groupCls} style={style}>
  52. {cols}
  53. </ColGroup>
  54. );
  55. }
  56. }