import React from 'react' import { ColumnWithLooseAccessor } from 'react-table' import { useColumnWidths } from './providers/ColumnWidthsProvider' import { useScrollbarWidth } from './providers/ScrollbarWidthProvider' import { useTable } from './providers/TableProvider' type Props = { columns: Array> // 考虑并解决滚动条的宽度问题 considerScrollbar?: boolean } export default function Colgroup({ columns, considerScrollbar = false }: Props) { const { fixHead } = useTable() const { colWidths } = useColumnWidths() const { width: scrollbarWidth } = useScrollbarWidth() return ( {columns.map((column, idx) => { // fixHead 用真实宽度,否则用配置里的宽度 let width: number | string | undefined if (fixHead) { let _width = colWidths[column.id!] if (typeof _width === 'number') { if (considerScrollbar === true && idx === columns.length - 1) { // 最后一个 th 加上滚动条的宽度 _width += scrollbarWidth } width = _width + 'px' } else { width = _width } } else { width = column.width } return })} ) }