1
1
import * as React from 'react' ;
2
2
import Filler from './Filler' ;
3
- import { getScrollPercentage , getNodeHeight , getRangeIndex } from './util' ;
3
+ import {
4
+ getElementScrollPercentage ,
5
+ getScrollPercentage ,
6
+ getNodeHeight ,
7
+ getRangeIndex ,
8
+ getStartItemTop ,
9
+ } from './util' ;
4
10
5
11
type RenderFunc < T > = ( item : T ) => React . ReactNode ;
6
12
@@ -84,18 +90,29 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
84
90
this . itemElementHeights [ eleKey ] = getNodeHeight ( this . itemElements [ eleKey ] ) ;
85
91
}
86
92
87
- // Calculate top visible item top offset
88
- const scrollPtg = getScrollPercentage ( this . listRef . current ) ;
89
- const locatedItemHeight = this . itemElementHeights [ this . getItemKey ( itemIndex ) ] || 0 ;
90
- const locatedItemTop = scrollPtg * this . listRef . current . clientHeight ;
91
- const locatedItemOffset = itemOffsetPtg * locatedItemHeight ;
92
- const locatedItemMergedTop =
93
- this . listRef . current . scrollTop + locatedItemTop - locatedItemOffset ;
94
-
95
- let startItemTop = locatedItemMergedTop ;
96
- for ( let index = itemIndex - 1 ; index >= startIndex ; index -= 1 ) {
97
- startItemTop -= this . itemElementHeights [ this . getItemKey ( index ) ] || 0 ;
98
- }
93
+ // // Calculate top visible item top offset
94
+ // const scrollPtg = getScrollPercentage(this.listRef.current);
95
+ // const locatedItemHeight = this.itemElementHeights[this.getItemKey(itemIndex)] || 0;
96
+ // const locatedItemTop = scrollPtg * this.listRef.current.clientHeight;
97
+ // const locatedItemOffset = itemOffsetPtg * locatedItemHeight;
98
+ // const locatedItemMergedTop =
99
+ // this.listRef.current.scrollTop + locatedItemTop - locatedItemOffset;
100
+
101
+ // let startItemTop = locatedItemMergedTop;
102
+ // for (let index = itemIndex - 1; index >= startIndex; index -= 1) {
103
+ // startItemTop -= this.itemElementHeights[this.getItemKey(index)] || 0;
104
+ // }
105
+
106
+ const startItemTop = getStartItemTop ( {
107
+ itemIndex,
108
+ startIndex,
109
+ itemOffsetPtg,
110
+ itemElementHeights : this . itemElementHeights ,
111
+ scrollTop : this . listRef . current . scrollTop ,
112
+ scrollPtg : getElementScrollPercentage ( this . listRef . current ) ,
113
+ clientHeight : this . listRef . current . clientHeight ,
114
+ getItemKey : this . getItemKey ,
115
+ } ) ;
99
116
100
117
this . setState ( { status : 'MEASURE_DONE' , startItemTop } ) ;
101
118
}
@@ -119,7 +136,7 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
119
136
return ;
120
137
}
121
138
122
- const scrollPtg = getScrollPercentage ( this . listRef . current ) ;
139
+ const scrollPtg = getElementScrollPercentage ( this . listRef . current ) ;
123
140
const visibleCount = Math . ceil ( height / itemHeight ) ;
124
141
125
142
const { itemIndex, itemOffsetPtg, startIndex, endIndex } = getRangeIndex (
0 commit comments