Skip to content

Commit 86b06d2

Browse files
committed
fix: Recalcuate the display range after scroll back to the view
1 parent db7a9ce commit 86b06d2

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/List.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,29 +450,31 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
450450
const { isVirtual } = this.state;
451451
const { height, itemHeight, data } = this.props;
452452
const { index, align = 'auto' } = arg0;
453-
const itemCount = Math.ceil(height / itemHeight);
453+
const visibleCount = Math.ceil(height / itemHeight);
454454
const item = data[index];
455455
if (item) {
456456
const { clientHeight } = this.listRef.current;
457457

458458
if (isVirtual) {
459459
// Calculate related data
460-
const { itemIndex, itemOffsetPtg, startIndex, endIndex } = this.state;
460+
const { itemIndex, itemOffsetPtg } = this.state;
461+
const { scrollTop } = this.listRef.current;
462+
const scrollPtg = getElementScrollPercentage(this.listRef.current);
461463

462464
const relativeLocatedItemTop = getItemRelativeTop({
463465
itemIndex,
464466
itemOffsetPtg,
465467
itemElementHeights: this.itemElementHeights,
466-
scrollPtg: getElementScrollPercentage(this.listRef.current),
468+
scrollPtg,
467469
clientHeight,
468470
getItemKey: this.getIndexKey,
469471
});
470472

471473
// We will force render related items to collect height for re-location
472474
this.setState(
473475
{
474-
startIndex: Math.max(0, index - itemCount),
475-
endIndex: Math.min(data.length - 1, index + itemCount),
476+
startIndex: Math.max(0, index - visibleCount),
477+
endIndex: Math.min(data.length - 1, index + visibleCount),
476478
},
477479
() => {
478480
this.collectItemHeights();
@@ -485,7 +487,7 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
485487
let shouldChange = true;
486488

487489
// Check if exist in the visible range
488-
if (Math.abs(itemIndex - index) < itemCount) {
490+
if (Math.abs(itemIndex - index) < visibleCount) {
489491
let itemTop = relativeLocatedItemTop;
490492
if (index < itemIndex) {
491493
for (let i = index; i < itemIndex; i += 1) {
@@ -506,7 +508,17 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
506508
// Out of range will fall back to position align
507509
mergedAlgin = index < itemIndex ? 'top' : 'bottom';
508510
} else {
511+
const {
512+
itemIndex: nextIndex,
513+
itemOffsetPtg: newOffsetPtg,
514+
startIndex,
515+
endIndex,
516+
} = getRangeIndex(scrollPtg, data.length, visibleCount);
517+
509518
this.setState({
519+
scrollTop,
520+
itemIndex: nextIndex,
521+
itemOffsetPtg: newOffsetPtg,
510522
startIndex,
511523
endIndex,
512524
});

0 commit comments

Comments
 (0)