Skip to content

Commit

Permalink
fix: wait for defined columns before measuring auto-width (#7064)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored Jan 11, 2024
1 parent e4f1628 commit e6d8660
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/grid/src/vaadin-grid-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,16 @@ export const GridMixin = (superClass) =>
return;
}
const cols = this._getColumns().filter((col) => !col.hidden && col.autoWidth);
this._recalculateColumnWidths(cols);

const undefinedCols = cols.filter((col) => !customElements.get(col.localName));
if (undefinedCols.length) {
// Some of the columns are not defined yet, wait for them to be defined before measuring
Promise.all(undefinedCols.map((col) => customElements.whenDefined(col.localName))).then(() => {
this._recalculateColumnWidths(cols);
});
} else {
this._recalculateColumnWidths(cols);
}
}

/** @private */
Expand Down
30 changes: 30 additions & 0 deletions packages/grid/test/column-auto-width.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,36 @@ describe('column auto-width', () => {
expect(headerCell.getBoundingClientRect().width).to.be.closeTo(headerCellWidth, 5);
});

it('should have correct column widths for lazily defined columns', async () => {
const tagName = 'vaadin-custom-column';
const newColumn = document.createElement(tagName);
newColumn.autoWidth = true;
newColumn.flexGrow = 0;
newColumn.path = 'a';

// Replace 'a' column with a new one
grid.removeChild(columns[0]);
grid.insertBefore(newColumn, columns[1]);
columns = grid.querySelectorAll(`${tagName}, vaadin-grid-column`);
grid.items = testItems;

// Define the custom column element after a delay
await new Promise((resolve) => {
setTimeout(() => {
customElements.define(
tagName,
class extends customElements.get('vaadin-grid-column') {
static is = tagName;
},
);
resolve();
}, 100);
});

await recalculateWidths();
expectColumnWidthsToBeOk(columns);
});

describe('focusButtonMode column', () => {
beforeEach(async () => {
const column = document.createElement('vaadin-grid-column');
Expand Down

0 comments on commit e6d8660

Please sign in to comment.