Skip to content

Commit 9867190

Browse files
authored
fix: defer scrollToIndex until grid is ready (#9223) (#9244)
1 parent ed4bc1f commit 9867190

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/grid/src/vaadin-grid-data-provider-mixin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,12 @@ export const DataProviderMixin = (superClass) =>
500500
}
501501

502502
scrollToIndex(index) {
503+
if (!this.__virtualizer || !this.clientHeight || !this._columnTree) {
504+
this.__pendingScrollToIndex = index;
505+
return;
506+
}
503507
super.scrollToIndex(index);
504-
if (!isNaN(index) && (this._cache.isLoading() || !this.clientHeight)) {
508+
if (!isNaN(index) && this._cache.isLoading()) {
505509
this.__pendingScrollToIndex = index;
506510
}
507511
}

packages/grid/test/scroll-to-index.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, listenOnce, nextFrame } from '@vaadin/testing-helpers';
2+
import { fixtureSync, listenOnce, nextFrame, oneEvent } from '@vaadin/testing-helpers';
33
import '@vaadin/polymer-legacy-adapter/template-renderer.js';
44
import '../vaadin-grid.js';
55
import '../vaadin-grid-tree-column.js';
@@ -281,4 +281,24 @@ describe('scroll to index', () => {
281281
grid.scrollToIndex(14);
282282
});
283283
});
284+
285+
describe('before grid is attached', () => {
286+
let grid;
287+
288+
beforeEach(async () => {
289+
const container = fixtureSync('<div></div>');
290+
grid = document.createElement('vaadin-grid');
291+
const column = document.createElement('vaadin-grid-column');
292+
grid.appendChild(column);
293+
grid.items = Array.from({ length: 100 }, (_, index) => `Item ${index}`);
294+
grid.scrollToIndex(50);
295+
container.appendChild(grid);
296+
await oneEvent(grid, 'animationend');
297+
await nextFrame();
298+
});
299+
300+
it('should scroll to index after items are rendered', () => {
301+
expect(getFirstVisibleItem(grid).index).to.equal(50);
302+
});
303+
});
284304
});

0 commit comments

Comments
 (0)