Skip to content

Commit 90d131c

Browse files
fix: defer scrollToIndex until grid is ready (#9223) (#9231)
Co-authored-by: Ugur Saglam <106508695+ugur-vaadin@users.noreply.github.com>
1 parent e3e47fd commit 90d131c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
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
@@ -483,6 +483,10 @@ export const DataProviderMixin = (superClass) =>
483483
* @param indexes {...number} Row indexes to scroll to
484484
*/
485485
scrollToIndex(...indexes) {
486+
if (!this.__virtualizer || !this.clientHeight || !this._columnTree) {
487+
this.__pendingScrollToIndexes = indexes;
488+
return;
489+
}
486490
// Synchronous data provider may cause changes to the cache on scroll without
487491
// ending up in a loading state. Try scrolling to the index until the target
488492
// index stabilizes.
@@ -491,7 +495,7 @@ export const DataProviderMixin = (superClass) =>
491495
this._scrollToFlatIndex(targetIndex);
492496
}
493497

494-
if (this._dataProviderController.isLoading() || !this.clientHeight || !this._columnTree) {
498+
if (this._dataProviderController.isLoading()) {
495499
this.__pendingScrollToIndexes = indexes;
496500
}
497501
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,24 @@ describe('scroll to index', () => {
577577
expect(getFirstVisibleItem(grid).index).to.equal(50);
578578
});
579579
});
580+
581+
describe('before grid is attached', () => {
582+
let grid;
583+
584+
beforeEach(async () => {
585+
const container = fixtureSync('<div></div>');
586+
grid = document.createElement('vaadin-grid');
587+
const column = document.createElement('vaadin-grid-column');
588+
grid.appendChild(column);
589+
grid.items = Array.from({ length: 100 }, (_, index) => `Item ${index}`);
590+
grid.scrollToIndex(50);
591+
container.appendChild(grid);
592+
await oneEvent(grid, 'animationend');
593+
await nextFrame();
594+
});
595+
596+
it('should scroll to index after items are rendered', () => {
597+
expect(getFirstVisibleItem(grid).index).to.equal(50);
598+
});
599+
});
580600
});

0 commit comments

Comments
 (0)