Skip to content

Commit e6d8660

Browse files
authored
fix: wait for defined columns before measuring auto-width (#7064)
1 parent e4f1628 commit e6d8660

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎packages/grid/src/vaadin-grid-mixin.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,16 @@ export const GridMixin = (superClass) =>
484484
return;
485485
}
486486
const cols = this._getColumns().filter((col) => !col.hidden && col.autoWidth);
487-
this._recalculateColumnWidths(cols);
487+
488+
const undefinedCols = cols.filter((col) => !customElements.get(col.localName));
489+
if (undefinedCols.length) {
490+
// Some of the columns are not defined yet, wait for them to be defined before measuring
491+
Promise.all(undefinedCols.map((col) => customElements.whenDefined(col.localName))).then(() => {
492+
this._recalculateColumnWidths(cols);
493+
});
494+
} else {
495+
this._recalculateColumnWidths(cols);
496+
}
488497
}
489498

490499
/** @private */

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,36 @@ describe('column auto-width', () => {
222222
expect(headerCell.getBoundingClientRect().width).to.be.closeTo(headerCellWidth, 5);
223223
});
224224

225+
it('should have correct column widths for lazily defined columns', async () => {
226+
const tagName = 'vaadin-custom-column';
227+
const newColumn = document.createElement(tagName);
228+
newColumn.autoWidth = true;
229+
newColumn.flexGrow = 0;
230+
newColumn.path = 'a';
231+
232+
// Replace 'a' column with a new one
233+
grid.removeChild(columns[0]);
234+
grid.insertBefore(newColumn, columns[1]);
235+
columns = grid.querySelectorAll(`${tagName}, vaadin-grid-column`);
236+
grid.items = testItems;
237+
238+
// Define the custom column element after a delay
239+
await new Promise((resolve) => {
240+
setTimeout(() => {
241+
customElements.define(
242+
tagName,
243+
class extends customElements.get('vaadin-grid-column') {
244+
static is = tagName;
245+
},
246+
);
247+
resolve();
248+
}, 100);
249+
});
250+
251+
await recalculateWidths();
252+
expectColumnWidthsToBeOk(columns);
253+
});
254+
225255
describe('focusButtonMode column', () => {
226256
beforeEach(async () => {
227257
const column = document.createElement('vaadin-grid-column');

0 commit comments

Comments
 (0)