Skip to content

Commit 73a37a8

Browse files
committed
fix: dynamically added columns ignore minWidth
1 parent 12d64a9 commit 73a37a8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,36 @@ 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+
];
147+
const { wrapper, rerender } = renderTable(
148+
<Table columnDefinitions={columns} visibleColumns={['id']} items={defaultItems} resizableColumns={true} />
149+
);
150+
expect(wrapper.findColumnHeaders().map(column => column.getElement().style.width)).toEqual(['100px']);
151+
152+
// Dynamically add columns with various width/minWidth configurations
153+
rerender(
154+
<Table
155+
columnDefinitions={columns}
156+
visibleColumns={['id', 'small-width', 'width-only', 'minWidth-larger']}
157+
items={defaultItems}
158+
resizableColumns={true}
159+
/>
160+
);
161+
162+
expect(wrapper.findColumnHeaders().map(column => column.getElement().style.width)).toEqual([
163+
'100px', // original column unchanged
164+
'150px', // use minWidth because 150 > 80
165+
'180px', // sue width (minWidth defaults to width)
166+
'200px', // use minWidth because 200 > 180
167+
]);
168+
});
169+
140170
describe('reading widths from the DOM', () => {
141171
const originalBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
142172
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) || 0;
172+
const minWidth = (column.minWidth as number) || width || DEFAULT_COLUMN_WIDTH;
173+
newColumnWidths.set(column.id, Math.max(width || DEFAULT_COLUMN_WIDTH, minWidth));
172174
}
173175
}
174176
if (updated) {

0 commit comments

Comments
 (0)