Skip to content

Commit c9efa26

Browse files
fix: recalculate column widths on initial item load with same data provider (#9859) (#9868)
Co-authored-by: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com>
1 parent dd243e0 commit c9efa26

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/grid/src/vaadin-grid-column-auto-width-mixin.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ export const ColumnAutoWidthMixin = (superClass) =>
5959
}
6060

6161
/** @private */
62-
__flatSizeChangedAutoWidth() {
62+
__flatSizeChangedAutoWidth(flatSize) {
6363
// Flat size changed, recalculate column widths if pending (asynchronously, to allow grid to render row elements first)
64-
requestAnimationFrame(() => this.__tryToRecalculateColumnWidthsIfPending());
64+
requestAnimationFrame(() => {
65+
if (!!flatSize && !this.__hasHadRenderedRowsForColumnWidthCalculation) {
66+
this.recalculateColumnWidths();
67+
} else {
68+
this.__tryToRecalculateColumnWidthsIfPending();
69+
}
70+
});
6571
}
6672

6773
/**

packages/grid/test/column-auto-width.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,26 @@ describe('column auto-width', () => {
353353
expect(parseFloat(lastColumn.width)).not.to.be.lessThan(getCellIntrinsicWidth(frozenToEndHeaderCell));
354354
});
355355

356+
it('should recalculate column widths on initial item load without data provider change', async () => {
357+
let items = [];
358+
grid.dataProvider = (_, cb) => cb(items, items.length);
359+
await nextFrame();
360+
361+
// Should recalculate once on initial load of items
362+
spy.resetHistory();
363+
items = [testItems[0], testItems[1]];
364+
grid.size = items.length;
365+
await nextFrame();
366+
expect(spy.callCount).to.equal(1);
367+
368+
// Should not recalculate on further update of items
369+
spy.resetHistory();
370+
items = [...items, testItems[2]];
371+
grid.size = items.length;
372+
await nextFrame();
373+
expect(spy.called).to.be.false;
374+
});
375+
356376
describe('focusButtonMode column', () => {
357377
beforeEach(async () => {
358378
const column = document.createElement('vaadin-grid-column');

0 commit comments

Comments
 (0)