Skip to content

Commit

Permalink
fix: manual virtual index adjustments (#3843)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored and vaadin-bot committed May 13, 2022
1 parent 32ffd9d commit ecc3013
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/component-base/src/virtualizer-iron-list-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ export class IronListAdapter {

super._scrollHandler();

if (this._physicalCount !== 0) {
// After running super._scrollHandler, fix _virtualStart to workaround an iron-list issue.
// See https://github.com/vaadin/web-components/issues/1691
const reusables = this._getReusables(true);
this._virtualStart += reusables.indexes.length;
}

if (this.reorderElements) {
this.__scrollReorderDebouncer = Debouncer.debounce(
this.__scrollReorderDebouncer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
import { fixtureSync, nextFrame, nextRender } from '@vaadin/testing-helpers';
import Sinon from 'sinon';
import { Virtualizer } from '../src/virtualizer.js';

Expand All @@ -11,7 +11,7 @@ function canScroll(el, deltaY) {
return isScrollableElement && (canScrollAndScrollingDownwards || canScrollAndScrollingUpwards);
}

describe('scrolling mode', () => {
describe('virtualizer - wheel scrolling', () => {
const IGNORE_WHEEL_TIMEOUT = 500;
let wrapper;
let virtualizer;
Expand Down Expand Up @@ -192,3 +192,51 @@ describe('scrolling mode', () => {
expect(wrapper.scrollTop).to.equal(0);
});
});

describe('virtualizer - scrollbar scrolling', () => {
let virtualizer;
let scrollTarget;

beforeEach(() => {
scrollTarget = fixtureSync(`
<div style="height: 100px;">
<div></div>
</div>
`);
const scrollContainer = scrollTarget.firstElementChild;

virtualizer = new Virtualizer({
createElements: (count) => [...Array(count)].map(() => document.createElement('div')),
updateElement: (el, index) => {
el.textContent = `item ${index}`;
el.classList.add(`item`);
},
scrollTarget,
scrollContainer,
});
});

it('should have an item at the bottom of the viewport after scrolling up', async () => {
virtualizer.size = 100000;

// Scroll to end
virtualizer.scrollToIndex(100000);
await nextFrame();

// Scroll upwards in 1000px steps
for (let i = 0; i < 10; i++) {
await nextFrame();
scrollTarget.scrollTop -= 1000;
}

// There should be an item at the bottom of the viewport
await nextFrame();
const listRect = scrollTarget.getBoundingClientRect();
const lastVisibleElement = scrollTarget.getRootNode().elementFromPoint(listRect.left, listRect.bottom - 1);
expect([...lastVisibleElement.classList]).to.contain('item');
});

it('should not throw on flush if size is not set', () => {
expect(() => virtualizer.flush()).not.to.throw(Error);
});
});

0 comments on commit ecc3013

Please sign in to comment.