1
1
import * as React from 'react' ;
2
2
import Filler from './Filler' ;
3
- import { getLocationItem , getScrollPercentage , getNodeHeight } from './util' ;
3
+ import { getScrollPercentage , getNodeHeight , getRangeIndex } from './util' ;
4
4
5
5
type RenderFunc < T > = ( item : T ) => React . ReactNode ;
6
6
@@ -17,7 +17,6 @@ interface ListState {
17
17
status : 'NONE' | 'MEASURE_START' | 'MEASURE_DONE' ;
18
18
19
19
scrollTop : number | null ;
20
- scrollPtg : number ;
21
20
itemIndex : number ;
22
21
itemOffsetPtg : number ;
23
22
startIndex : number ;
@@ -49,7 +48,6 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
49
48
state : ListState = {
50
49
status : 'NONE' ,
51
50
scrollTop : null ,
52
- scrollPtg : 0 ,
53
51
itemIndex : 0 ,
54
52
itemOffsetPtg : 0 ,
55
53
startIndex : 0 ,
@@ -76,8 +74,8 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
76
74
* Phase 5: Trigger re-render to use correct position
77
75
*/
78
76
public componentDidUpdate ( prevProps : ListProps < T > ) {
79
- const { status, scrollPtg , startIndex, endIndex, itemIndex, itemOffsetPtg } = this . state ;
80
- const { dataSource, itemKey } = this . props ;
77
+ const { status, startIndex, endIndex, itemIndex, itemOffsetPtg } = this . state ;
78
+ const { dataSource } = this . props ;
81
79
82
80
if ( status === 'MEASURE_START' ) {
83
81
// Record here since measure item height will get warning in `render`
@@ -87,6 +85,7 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
87
85
}
88
86
89
87
// Calculate top visible item top offset
88
+ const scrollPtg = getScrollPercentage ( this . listRef . current ) ;
90
89
const locatedItemHeight = this . itemElementHeights [ this . getItemKey ( itemIndex ) ] || 0 ;
91
90
const locatedItemTop = scrollPtg * this . listRef . current . clientHeight ;
92
91
const locatedItemOffset = itemOffsetPtg * locatedItemHeight ;
@@ -121,21 +120,21 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
121
120
}
122
121
123
122
const scrollPtg = getScrollPercentage ( this . listRef . current ) ;
124
-
125
- const { index, offsetPtg } = getLocationItem ( scrollPtg , dataSource . length ) ;
126
123
const visibleCount = Math . ceil ( height / itemHeight ) ;
127
124
128
- const beforeCount = Math . ceil ( scrollPtg * visibleCount ) ;
129
- const afterCount = Math . ceil ( ( 1 - scrollPtg ) * visibleCount ) ;
125
+ const { itemIndex, itemOffsetPtg, startIndex, endIndex } = getRangeIndex (
126
+ scrollPtg ,
127
+ dataSource . length ,
128
+ visibleCount ,
129
+ ) ;
130
130
131
131
this . setState ( {
132
132
status : 'MEASURE_START' ,
133
133
scrollTop,
134
- scrollPtg,
135
- itemIndex : index ,
136
- itemOffsetPtg : offsetPtg ,
137
- startIndex : Math . max ( 0 , index - beforeCount ) ,
138
- endIndex : Math . min ( dataSource . length - 1 , index + afterCount ) ,
134
+ itemIndex,
135
+ itemOffsetPtg,
136
+ startIndex,
137
+ endIndex,
139
138
} ) ;
140
139
} ;
141
140
0 commit comments