Skip to content

Commit b77ba5a

Browse files
committed
should use origin dataSource
1 parent 9304ab5 commit b77ba5a

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

examples/animate.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import './animate.less';
77

88
let uuid = 0;
99
function genItem() {
10-
uuid += 1;
11-
return {
10+
const item = {
1211
id: `key_${uuid}`,
1312
uuid,
1413
};
14+
uuid += 1;
15+
return item;
1516
}
1617

1718
const originDataSource: Item[] = [];

src/List.tsx

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,18 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
127127
// Re-calculate the scroll position align with the current visible item position
128128
if (prevProps.dataSource.length !== dataSource.length && height) {
129129
// 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 } = {};
131131
const { startIndex: originStartIndex, itemIndex: originItemIndex } = this.state;
132132
let originStartItemTop = this.state.startItemTop;
133133
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;
136136
originStartItemTop += this.itemElementHeights[key] || 0;
137137
}
138138

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
140142
const { scrollHeight, clientHeight } = this.listRef.current;
141143
const maxScrollTop = scrollHeight - clientHeight;
142144
for (let scrollTop = 0; scrollTop <= maxScrollTop; scrollTop += 1) {
@@ -149,18 +151,24 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
149151
visibleCount,
150152
);
151153

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);
164172
}
165173
}
166174
}
@@ -197,8 +205,8 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
197205
});
198206
};
199207

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;
202210
const item = dataSource[index];
203211
return item && itemKey ? item[itemKey] : index;
204212
};

0 commit comments

Comments
 (0)