@@ -82,21 +82,20 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
82
82
if ( status === 'MEASURE_START' ) {
83
83
// Record here since measure item height will get warning in `render`
84
84
for ( let index = startIndex ; index <= endIndex ; index += 1 ) {
85
- const item = dataSource [ index ] ;
86
- const eleKey = itemKey ? item [ itemKey ] : index ;
87
- this . itemElementHeights [ index ] = getNodeHeight ( this . itemElements [ eleKey ] ) ;
85
+ const eleKey = this . getItemKey ( index ) ;
86
+ this . itemElementHeights [ eleKey ] = getNodeHeight ( this . itemElements [ eleKey ] ) ;
88
87
}
89
88
90
89
// Calculate top visible item top offset
91
- const locatedItemHeight = this . getItemHeight ( itemIndex ) ;
90
+ const locatedItemHeight = this . itemElementHeights [ this . getItemKey ( itemIndex ) ] || 0 ;
92
91
const locatedItemTop = scrollPtg * this . listRef . current . clientHeight ;
93
92
const locatedItemOffset = itemOffsetPtg * locatedItemHeight ;
94
93
const locatedItemMergedTop =
95
94
this . listRef . current . scrollTop + locatedItemTop - locatedItemOffset ;
96
95
97
96
let startItemTop = locatedItemMergedTop ;
98
97
for ( let index = itemIndex - 1 ; index >= startIndex ; index -= 1 ) {
99
- startItemTop -= this . getItemHeight ( index ) ;
98
+ startItemTop -= this . itemElementHeights [ this . getItemKey ( index ) ] || 0 ;
100
99
}
101
100
102
101
this . setState ( { status : 'MEASURE_DONE' , startItemTop } ) ;
@@ -108,8 +107,6 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
108
107
}
109
108
}
110
109
111
- public getItemHeight = ( index : number ) => this . itemElementHeights [ index ] || 0 ;
112
-
113
110
/**
114
111
* Phase 2: Trigger render since we should re-calculate current position.
115
112
*/
@@ -142,16 +139,21 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
142
139
} ) ;
143
140
} ;
144
141
142
+ public getItemKey = ( index : number ) => {
143
+ const { dataSource, itemKey } = this . props ;
144
+ const item = dataSource [ index ] ;
145
+ return item && itemKey ? item [ itemKey ] : index ;
146
+ } ;
147
+
145
148
/**
146
149
* Phase 4: Render item and get all the visible items height
147
150
*/
148
- public renderChildren = ( list : T [ ] , startIndex : number , renderFunc : RenderFunc < T > ) => {
149
- const { itemKey } = this . props ;
151
+ public renderChildren = ( list : T [ ] , startIndex : number , renderFunc : RenderFunc < T > ) =>
150
152
// We should measure rendered item height
151
- return list . map ( ( item , index ) => {
153
+ list . map ( ( item , index ) => {
152
154
const node = renderFunc ( item ) as React . ReactElement ;
153
155
const eleIndex = startIndex + index ;
154
- const eleKey = itemKey ? item [ itemKey ] : eleIndex ;
156
+ const eleKey = this . getItemKey ( eleIndex ) ;
155
157
156
158
// Pass `key` and `ref` for internal measure
157
159
return React . cloneElement ( node , {
@@ -160,8 +162,8 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
160
162
this . itemElements [ eleKey ] = ele ;
161
163
} ,
162
164
} ) ;
163
- } ) ;
164
- } ;
165
+ } )
166
+ ;
165
167
166
168
public render ( ) {
167
169
const {
0 commit comments