Skip to content

Commit f3d0fc2

Browse files
committed
fix: fix scroll offset when scrollSize is samller than containerSize
1 parent 899753e commit f3d0fc2

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

packages/infinitegrid/src/InfiniteGrid.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ class InfiniteGrid<Options extends InfiniteGridOptions = InfiniteGridOptions> ex
916916
const infinite = this.infinite;
917917
const scrollManager = this.scrollManager;
918918
const scrollPos = scrollManager.getRelativeScrollPos()!;
919+
const orgScrollPos = scrollManager.getScrollPos()!;
919920
const prevScrollSize = infinite.getScrollSize();
920921
const prevContainerSize = infinite.getSize();
921922
const prevVisibleArea = infinite.getVisibleArea(scrollPos, direction);
@@ -957,16 +958,23 @@ class InfiniteGrid<Options extends InfiniteGridOptions = InfiniteGridOptions> ex
957958
let offset = nextPos - prevPos;
958959

959960
// If reversed, scroll size (case where container size is reduced)
961+
const nextScrollSize = infinite.getScrollSize();
962+
const nextContainerSize = infinite.getSize();
963+
960964
if (offset < 0) {
961-
const nextScrollSize = infinite.getScrollSize();
962-
const nextContainerSize = infinite.getSize();
963965
const endOffset = Math.max(scrollPos - Math.max(0, prevScrollSize - prevContainerSize), 0);
964966
const nextScollPos
965967
= Math.min(scrollPos, Math.max(0, nextScrollSize - nextContainerSize))
966968
+ endOffset;
967969

968970
// The scroll size is restored to the extent that it has been reduced.
969971
offset += scrollPos - nextScollPos;
972+
} else if (offset > 0) {
973+
// If it is smaller than the scroll size when in the forward direction, the offset is 0.
974+
const maxScrollPos = Math.max(0, nextScrollSize - nextContainerSize);
975+
const nextScrollPos = orgScrollPos + offset;
976+
977+
offset = Math.max(0, Math.min(maxScrollPos, nextScrollPos) - orgScrollPos);
970978
}
971979

972980
this.scrollManager.scrollBy(offset);

packages/infinitegrid/test/unit/Infinite.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Infinite } from "../../src/Infinite";
22
import { cleanup } from "./utils/utils";
33
import * as sinon from "sinon";
44

5-
describe.only("test Infinite", () => {
5+
describe("test Infinite", () => {
66
let infinite: Infinite | null;
77

88
afterEach(() => {

packages/infinitegrid/test/unit/InfiniteGrid.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,48 @@ describe("test InfiniteGrid", () => {
509509

510510
expect(elements.map((el) => el.innerHTML)).to.be.deep.equals(["0", "1", "2", "3", "4", "5", "6", "7", "8"]);
511511
});
512+
it("should check if the offset is entered correctly when prepend", async () => {
513+
// Given
514+
// sufficient height so that scroll size does not occur
515+
const igScrollContainer = ig!.getScrollContainerElement();
516+
517+
igScrollContainer.style.height = "1000px";
518+
ig!.isReachEnd = true;
519+
ig!.syncItems([3, 4, 5].map((child) => {
520+
return {
521+
groupKey: Math.floor(child / 3),
522+
key: child,
523+
html: `<div style="height: 150px;">${child}</div>`,
524+
};
525+
}));
526+
ig!.renderItems({
527+
useResize: true,
528+
});
529+
530+
await waitEvent(ig!, "renderComplete");
531+
532+
const spy = sinon.spy();
533+
534+
535+
// When
536+
ig!.syncItems([0, 1, 2, 3, 4, 5].map((child) => {
537+
return {
538+
groupKey: Math.floor(child / 3),
539+
key: child,
540+
html: `<div style="height: 150px;">${child}</div>`,
541+
};
542+
}));
543+
ig!.renderItems();
544+
545+
// the requestPrepend event is fired because scrollPos is 0.
546+
ig!.once("requestPrepend", spy);
547+
await waitEvent(ig!, "renderComplete");
548+
549+
// Then
550+
expect(ig!.getStartCursor()).to.be.equals(0);
551+
expect(ig!.getEndCursor()).to.be.equals(1);
552+
expect(spy.called).to.be.true;
553+
});
512554
});
513555
describe("test contentError event", () => {
514556
it("should check if contentError event occurs with error image", async () => {

0 commit comments

Comments
 (0)