Skip to content

Commit 18987fa

Browse files
committed
support 0 index item
1 parent c1f9d8a commit 18987fa

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/List.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,35 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
152152

153153
let originCompareItemIndex = removedItemIndex - 1;
154154
if (originCompareItemIndex >= originItemIndex) originCompareItemIndex = originItemIndex;
155-
const compareItemKey = this.getItemKey(originCompareItemIndex, prevProps);
156155

156+
// Find compare item key & offset top
157+
let compareItemIndex: number;
158+
let compareItemKey: string;
157159
let originCompareItemTop = this.state.startItemTop;
158-
for (let index = originStartIndex; index <= originItemIndex; index += 1) {
159-
const key = this.getItemKey(index, prevProps);
160-
if (key === compareItemKey) {
161-
break;
160+
if (originCompareItemIndex < 0) {
161+
// If remove item is the first one, we have compare next one
162+
originCompareItemIndex = 0;
163+
compareItemIndex = 0;
164+
compareItemKey = this.getItemKey(compareItemIndex);
165+
} else {
166+
// If exist compare item
167+
compareItemKey = this.getItemKey(originCompareItemIndex, prevProps);
168+
169+
for (let index = originStartIndex; index <= originItemIndex; index += 1) {
170+
const key = this.getItemKey(index, prevProps);
171+
if (key === compareItemKey) {
172+
break;
173+
}
174+
175+
originCompareItemTop += this.itemElementHeights[key] || 0;
162176
}
163177

164-
originCompareItemTop += this.itemElementHeights[key] || 0;
178+
// Find current compare item index
179+
compareItemIndex = dataSource.findIndex(
180+
(_, index) => this.getItemKey(index) === compareItemKey,
181+
);
165182
}
166183

167-
// Find current compare item index
168-
const compareItemIndex = dataSource.findIndex(
169-
(_, index) => this.getItemKey(index) === compareItemKey,
170-
);
171-
172184
// Loop to generate compared item top and find best one
173185
const { scrollHeight, clientHeight } = this.listRef.current;
174186
const maxScrollTop = scrollHeight - clientHeight;

0 commit comments

Comments
 (0)