Skip to content

Commit 7a16d1e

Browse files
committed
ghost item should have a key
1 parent b77ba5a commit 7a16d1e

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

examples/animate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function genItem() {
1616
}
1717

1818
const originDataSource: Item[] = [];
19-
for (let i = 0; i < 9; i += 1) {
19+
for (let i = 0; i < 20; i += 1) {
2020
originDataSource.push(genItem());
2121
}
2222

src/List.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getNodeHeight,
77
getRangeIndex,
88
getItemTop,
9+
GHOST_ITEM_KEY,
910
} from './util';
1011

1112
type RenderFunc<T> = (item: T) => React.ReactNode;
@@ -136,7 +137,12 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
136137
originStartItemTop += this.itemElementHeights[key] || 0;
137138
}
138139

139-
console.log('Length changed. Origin top:', originItemTops, this.state.startIndex);
140+
console.log(
141+
'Length changed. Origin top:',
142+
originItemTops,
143+
this.state.startIndex,
144+
this.itemElementHeights,
145+
);
140146

141147
// Loop to get the adjusted item top
142148
const { scrollHeight, clientHeight } = this.listRef.current;
@@ -151,7 +157,7 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
151157
visibleCount,
152158
);
153159

154-
const locatedItemTop = getItemTop({
160+
let locatedItemTop = getItemTop({
155161
itemIndex,
156162
itemOffsetPtg,
157163
itemElementHeights: this.itemElementHeights,
@@ -165,7 +171,7 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
165171
for (let index = itemIndex; index >= startIndex; index -= 1) {
166172
const key = this.getItemKey(index);
167173
itemTops[key] = locatedItemTop;
168-
originStartItemTop -= this.itemElementHeights[key] || 0;
174+
locatedItemTop -= this.itemElementHeights[key] || 0;
169175
}
170176

171177
console.log('=>', scrollTop, itemTops);
@@ -207,7 +213,16 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
207213

208214
public getItemKey = (index: number, props?: ListProps<T>) => {
209215
const { dataSource, itemKey } = props || this.props;
216+
217+
// Return ghost key as latest index item
218+
if (index === dataSource.length) {
219+
return GHOST_ITEM_KEY;
220+
}
221+
210222
const item = dataSource[index];
223+
if (!item) {
224+
console.error('Not find index item. Please report this since it is a bug.');
225+
}
211226
return item && itemKey ? item[itemKey] : index;
212227
};
213228

src/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
22

3+
/**
4+
* Our algorithm have additional one ghost item
5+
* whose index as `dataSource.length` to simplify the calculation
6+
*/
7+
export const GHOST_ITEM_KEY = '__rc_ghost_item__';
8+
39
interface LocationItemResult {
410
/** Located item index */
511
index: number;

0 commit comments

Comments
 (0)