Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Table): use <colgroup/> to set cells width #831

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading