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 and DenisVershkov committed Dec 25, 2023
1 parent 56aa587 commit f736e78
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 @@ -346,6 +346,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 @@ -363,7 +380,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 @@ -398,6 +415,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 @@ -458,7 +476,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 @@ -554,6 +572,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 f736e78

Please sign in to comment.