Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions src/table/__tests__/columns-width.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ test('should use the fallback value for columns without explicit width', () => {
]);
});

test('should respect minWidth for dynamically added columns without explicit width', () => {
const columns: TableProps.ColumnDefinition<Item>[] = [
{ id: 'id', header: '', cell: item => item.id, width: 100 },
{ id: 'name', header: '', cell: () => '-', minWidth: 250 },
{ id: 'description', header: '', cell: () => '-', minWidth: 300 },
];
const { wrapper, rerender } = renderTable(
<Table columnDefinitions={columns} visibleColumns={['id']} items={defaultItems} resizableColumns={true} />
);
expect(wrapper.findColumnHeaders().map(column => column.getElement().style.width)).toEqual(['100px']);
// Dynamically add columns that have minWidth but no explicit width
rerender(
<Table
columnDefinitions={columns}
visibleColumns={['id', 'name', 'description']}
items={defaultItems}
resizableColumns={true}
/>
);
// Bug #4236: Previously these would be '120px' (DEFAULT_COLUMN_WIDTH) instead of respecting minWidth
expect(wrapper.findColumnHeaders().map(column => column.getElement().style.width)).toEqual([
'100px',
'250px',
'300px',
]);
});

describe('reading widths from the DOM', () => {
const originalBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
beforeEach(() => {
Expand Down
5 changes: 4 additions & 1 deletion src/table/use-column-widths.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ export function ColumnWidthsProvider({ visibleColumns, resizableColumns, contain
const column = visibleColumns[index];
if (!columnWidths?.get(column.id) && lastVisible.indexOf(column.id) === -1) {
updated = true;
newColumnWidths.set(column.id, (column.width as number) || DEFAULT_COLUMN_WIDTH);
// Handle minWidth consistently with readWidths() for initial render
const width = (column.width as number) || 0;
const minWidth = (column.minWidth as number) || width || DEFAULT_COLUMN_WIDTH;
newColumnWidths.set(column.id, Math.max(width, minWidth));
}
}
if (updated) {
Expand Down
Loading