Skip to content

Commit 9fc7a87

Browse files
SpyZzeyamanabiy
authored andcommitted
fix: dynamically added columns ignore minWidth (#4288)
1 parent 3fd53ea commit 9fc7a87

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/table/__tests__/columns-width.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,53 @@ test('should use the fallback value for columns without explicit width', () => {
137137
]);
138138
});
139139

140+
test('should respect minWidth when dynamically adding columns via visibleColumns', () => {
141+
const columns: TableProps.ColumnDefinition<Item>[] = [
142+
{ id: 'id', header: '', cell: item => item.id, width: 100 },
143+
{ id: 'small-width', header: '', cell: () => '-', width: 80, minWidth: 150 },
144+
{ id: 'width-only', header: '', cell: () => '-', width: 180 },
145+
{ id: 'minWidth-larger', header: '', cell: () => '-', width: 180, minWidth: 200 },
146+
{ id: 'no-width-no-minWidth', header: '', cell: () => '-' },
147+
{ id: 'no-width-minWidth-large', header: '', cell: () => '-', minWidth: 200 },
148+
{ id: 'no-width-minWidth-small', header: '', cell: () => '-', minWidth: 80 },
149+
{ id: 'width-larger-than-minWidth', header: '', cell: () => '-', width: 200, minWidth: 100 },
150+
];
151+
const { wrapper, rerender } = renderTable(
152+
<Table columnDefinitions={columns} visibleColumns={['id']} items={defaultItems} resizableColumns={true} />
153+
);
154+
expect(wrapper.findColumnHeaders().map(column => column.getElement().style.width)).toEqual(['100px']);
155+
156+
// Dynamically add columns with various width/minWidth configurations
157+
rerender(
158+
<Table
159+
columnDefinitions={columns}
160+
visibleColumns={[
161+
'id',
162+
'small-width',
163+
'width-only',
164+
'minWidth-larger',
165+
'no-width-no-minWidth',
166+
'no-width-minWidth-large',
167+
'no-width-minWidth-small',
168+
'width-larger-than-minWidth',
169+
]}
170+
items={defaultItems}
171+
resizableColumns={true}
172+
/>
173+
);
174+
175+
expect(wrapper.findColumnHeaders().map(column => column.getElement().style.width)).toEqual([
176+
'100px', // original column unchanged
177+
'150px', // width=80, minWidth=150 -> use minWidth because 150 > 80
178+
'180px', // width=180, no minWidth -> minWidth defaults to width, use 180
179+
'200px', // width=180, minWidth=200 -> use minWidth because 200 > 180
180+
'120px', // no width, no minWidth -> width defaults to DEFAULT (120), minWidth defaults to width
181+
'200px', // no width, minWidth=200 -> width defaults to DEFAULT (120), minWidth=200 -> use 200
182+
'120px', // no width, minWidth=80 -> width defaults to DEFAULT (120), minWidth=80 -> use 120
183+
'200px', // width=200, minWidth=100 -> use width because 200 > 100
184+
]);
185+
});
186+
140187
describe('reading widths from the DOM', () => {
141188
const originalBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
142189
beforeEach(() => {

src/table/use-column-widths.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ export function ColumnWidthsProvider({ visibleColumns, resizableColumns, contain
168168
const column = visibleColumns[index];
169169
if (!columnWidths?.get(column.id) && lastVisible.indexOf(column.id) === -1) {
170170
updated = true;
171-
newColumnWidths.set(column.id, (column.width as number) || DEFAULT_COLUMN_WIDTH);
171+
const width = (column.width as number) || DEFAULT_COLUMN_WIDTH;
172+
const minWidth = (column.minWidth as number) || width;
173+
newColumnWidths.set(column.id, Math.max(width, minWidth));
172174
}
173175
}
174176
if (updated) {

0 commit comments

Comments
 (0)