@@ -127,16 +127,18 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
127
127
// Re-calculate the scroll position align with the current visible item position
128
128
if ( prevProps . dataSource . length !== dataSource . length && height ) {
129
129
// We will record all the visible item top for next loop match check
130
- const itemTops : { [ key : string ] : number } = { } ;
130
+ const originItemTops : { [ key : string ] : number } = { } ;
131
131
const { startIndex : originStartIndex , itemIndex : originItemIndex } = this . state ;
132
132
let originStartItemTop = this . state . startItemTop ;
133
133
for ( let index = originStartIndex ; index <= originItemIndex ; index += 1 ) {
134
- const key = this . getItemKey ( index ) ;
135
- itemTops [ key ] = originStartItemTop ;
134
+ const key = this . getItemKey ( index , prevProps ) ;
135
+ originItemTops [ key ] = originStartItemTop ;
136
136
originStartItemTop += this . itemElementHeights [ key ] || 0 ;
137
137
}
138
138
139
- console . log ( 'Length changed. Origin top:' , itemTops , this . itemElementHeights ) ;
139
+ console . log ( 'Length changed. Origin top:' , originItemTops , this . state . startIndex ) ;
140
+
141
+ // Loop to get the adjusted item top
140
142
const { scrollHeight, clientHeight } = this . listRef . current ;
141
143
const maxScrollTop = scrollHeight - clientHeight ;
142
144
for ( let scrollTop = 0 ; scrollTop <= maxScrollTop ; scrollTop += 1 ) {
@@ -149,18 +151,24 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
149
151
visibleCount ,
150
152
) ;
151
153
152
- // const startItemTop = getStartItemTop({
153
- // itemIndex,
154
- // startIndex,
155
- // itemOffsetPtg,
156
- // itemElementHeights: this.itemElementHeights,
157
- // scrollTop: this.listRef.current.scrollTop,
158
- // scrollPtg,
159
- // clientHeight: this.listRef.current.clientHeight,
160
- // getItemKey: this.getItemKey,
161
- // });
162
-
163
- // console.log('=>', scrollTop, startIndex, startItemTop);
154
+ const locatedItemTop = getItemTop ( {
155
+ itemIndex,
156
+ itemOffsetPtg,
157
+ itemElementHeights : this . itemElementHeights ,
158
+ scrollTop : this . listRef . current . scrollTop ,
159
+ scrollPtg,
160
+ clientHeight : this . listRef . current . clientHeight ,
161
+ getItemKey : this . getItemKey ,
162
+ } ) ;
163
+
164
+ const itemTops : { [ key : string ] : number } = { } ;
165
+ for ( let index = itemIndex ; index >= startIndex ; index -= 1 ) {
166
+ const key = this . getItemKey ( index ) ;
167
+ itemTops [ key ] = locatedItemTop ;
168
+ originStartItemTop -= this . itemElementHeights [ key ] || 0 ;
169
+ }
170
+
171
+ console . log ( '=>' , scrollTop , itemTops ) ;
164
172
}
165
173
}
166
174
}
@@ -197,8 +205,8 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
197
205
} ) ;
198
206
} ;
199
207
200
- public getItemKey = ( index : number ) => {
201
- const { dataSource, itemKey } = this . props ;
208
+ public getItemKey = ( index : number , props ?: ListProps < T > ) => {
209
+ const { dataSource, itemKey } = props || this . props ;
202
210
const item = dataSource [ index ] ;
203
211
return item && itemKey ? item [ itemKey ] : index ;
204
212
} ;
0 commit comments