Skip to content

Commit

Permalink
fix: wrong item range when overscanCount option set to zero
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jun 5, 2021
1 parent 9c61c25 commit 9117692
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-coins-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-virtual": patch
---

fix: wrong item range when `overscanCount` option set to zero
13 changes: 9 additions & 4 deletions src/useVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default <
while (
vStart < msData.length &&
msData[vStart].start < (msData[vStart + 1]?.start || 0) &&
msData[vStart].start < scrollOffset
msData[vStart].start + msData[vStart].size < scrollOffset
)
vStart += 1;
} else {
Expand All @@ -148,20 +148,25 @@ export default <
vStop < msData.length &&
currStart < scrollOffset + outerRectRef.current[sizeKey]
) {
currStart += msData[vStop].size;
vStop += 1;
currStart += msData[vStop]?.size || 0;
}

const oStart = Math.max(vStart - overscanCount, 0);
const oStop = Math.min(vStop + overscanCount, msData.length) - 1;
const margin = msData[oStart].start;
const lastStart = Math[oStop < msData.length - 1 ? "max" : "min"](
msData[oStop].end + msData[oStop].size,
msData[msData.length - 1].start
);

return {
oStart,
oStop: Math.min(vStop + overscanCount, msData.length) - 1,
oStop,
vStart,
vStop: vStop - 1,
margin,
innerSize: msData[msData.length - 1].end - margin,
innerSize: lastStart - margin,
};
},
[overscanCount, sizeKey]
Expand Down

0 comments on commit 9117692

Please sign in to comment.