Skip to content

Commit b0a02d0

Browse files
authored
refactor: cache column group widths in auto-width calculation (#9180)
1 parent 1f98d2a commit b0a02d0

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const ColumnAutoWidthMixin = (superClass) =>
100100

101101
const columnWidth = Math.max(
102102
this.__getIntrinsicWidth(col),
103-
this.__getDistributedWidth((col.assignedSlot || col).parentElement, col),
103+
this.__getDistributedWidth(this.__getParentColumnGroup(col), col),
104104
);
105105

106106
// We're processing a regular grid-column and not a grid-column-group
@@ -153,13 +153,38 @@ export const ColumnAutoWidthMixin = (superClass) =>
153153
const lvi = this._lastVisibleIndex;
154154
this.__viewportRowsCache = this._getRenderedRows().filter((row) => row.index >= fvi && row.index <= lvi);
155155

156-
// Pre-cache the intrinsic width of each column
156+
// Get columns with autoWidth
157157
const cols = this.__getAutoWidthColumns();
158-
this.__calculateAndCacheIntrinsicWidths(cols);
158+
159+
// Get all ancestor groups of the columns
160+
const ancestorColumnGroups = new Set();
161+
for (const col of cols) {
162+
let parent = this.__getParentColumnGroup(col);
163+
while (parent && !ancestorColumnGroups.has(parent)) {
164+
ancestorColumnGroups.add(parent);
165+
parent = this.__getParentColumnGroup(parent);
166+
}
167+
}
168+
169+
// Pre-cache the intrinsic width of each column and ancestor group
170+
this.__calculateAndCacheIntrinsicWidths([...cols, ...ancestorColumnGroups]);
159171

160172
cols.forEach((col) => {
161173
col.width = `${this.__getDistributedWidth(col)}px`;
162174
});
175+
176+
// Clear the column-width cache to avoid a memory leak
177+
this.__intrinsicWidthCache.clear();
178+
}
179+
180+
/**
181+
* Returns the parent column group of the given column.
182+
*
183+
* @private
184+
*/
185+
__getParentColumnGroup(col) {
186+
const parent = (col.assignedSlot || col).parentElement;
187+
return parent && parent !== this ? parent : null;
163188
}
164189

165190
/**

0 commit comments

Comments
 (0)