Skip to content

Commit

Permalink
feat(Table): use <colgroup/> to set cells width (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogonkov authored Dec 7, 2023
1 parent 8851ac8 commit c33b330
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ export class Table<I extends TableDataItem = Record<string, string>> extends Rea
);
}

private renderColgroup() {
const {columns} = this.props;
const {columnsStyles} = this.state;

if (!columnsStyles.length) {
return null;
}

return (
<colgroup>
{columnsStyles.map(({width}, index) => (
<col style={{width}} key={columns[index].id} />
))}
</colgroup>
);
}

private renderHead() {
const {columns, edgePadding, wordWrap} = this.props;
const {columnsStyles} = this.state;
Expand All @@ -351,7 +368,7 @@ export class Table<I extends TableDataItem = Record<string, string>> extends Rea
<th
key={id}
ref={this.state.columnHeaderRefs[index]}
style={columnsStyles[index]}
style={this.getCellStyles(columnsStyles[index])}
className={b(
'cell',
{
Expand Down Expand Up @@ -386,6 +403,7 @@ export class Table<I extends TableDataItem = Record<string, string>> extends Rea
private renderTable() {
return (
<table ref={this.tableRef} className={b('table')}>
{this.renderColgroup()}
{this.renderHead()}
{this.renderBody()}
</table>
Expand Down Expand Up @@ -445,7 +463,7 @@ export class Table<I extends TableDataItem = Record<string, string>> extends Rea
return (
<td
key={id}
style={columnsStyles[colIndex]}
style={this.getCellStyles(columnsStyles[colIndex])}
className={b(
'cell',
{
Expand Down Expand Up @@ -535,6 +553,13 @@ export class Table<I extends TableDataItem = Record<string, string>> extends Rea
return style;
}

private getCellStyles({
width: _width,
...styles
}: React.CSSProperties): React.CSSProperties | undefined {
return Object.keys(styles).length ? styles : undefined;
}

private handleScrollContainerMouseenter = () => {
this.setState({activeScrollElement: 'scrollContainer'});
};
Expand Down

0 comments on commit c33b330

Please sign in to comment.