Skip to content

Commit 2f4c014

Browse files
committed
move to more detail function
1 parent b3dd9ca commit 2f4c014

File tree

2 files changed

+69
-21
lines changed

2 files changed

+69
-21
lines changed

src/List.tsx

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as React from 'react';
22
import Filler from './Filler';
3-
import { getScrollPercentage, getNodeHeight, getRangeIndex } from './util';
3+
import {
4+
getElementScrollPercentage,
5+
getScrollPercentage,
6+
getNodeHeight,
7+
getRangeIndex,
8+
getStartItemTop,
9+
} from './util';
410

511
type RenderFunc<T> = (item: T) => React.ReactNode;
612

@@ -84,18 +90,29 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
8490
this.itemElementHeights[eleKey] = getNodeHeight(this.itemElements[eleKey]);
8591
}
8692

87-
// Calculate top visible item top offset
88-
const scrollPtg = getScrollPercentage(this.listRef.current);
89-
const locatedItemHeight = this.itemElementHeights[this.getItemKey(itemIndex)] || 0;
90-
const locatedItemTop = scrollPtg * this.listRef.current.clientHeight;
91-
const locatedItemOffset = itemOffsetPtg * locatedItemHeight;
92-
const locatedItemMergedTop =
93-
this.listRef.current.scrollTop + locatedItemTop - locatedItemOffset;
94-
95-
let startItemTop = locatedItemMergedTop;
96-
for (let index = itemIndex - 1; index >= startIndex; index -= 1) {
97-
startItemTop -= this.itemElementHeights[this.getItemKey(index)] || 0;
98-
}
93+
// // Calculate top visible item top offset
94+
// const scrollPtg = getScrollPercentage(this.listRef.current);
95+
// const locatedItemHeight = this.itemElementHeights[this.getItemKey(itemIndex)] || 0;
96+
// const locatedItemTop = scrollPtg * this.listRef.current.clientHeight;
97+
// const locatedItemOffset = itemOffsetPtg * locatedItemHeight;
98+
// const locatedItemMergedTop =
99+
// this.listRef.current.scrollTop + locatedItemTop - locatedItemOffset;
100+
101+
// let startItemTop = locatedItemMergedTop;
102+
// for (let index = itemIndex - 1; index >= startIndex; index -= 1) {
103+
// startItemTop -= this.itemElementHeights[this.getItemKey(index)] || 0;
104+
// }
105+
106+
const startItemTop = getStartItemTop({
107+
itemIndex,
108+
startIndex,
109+
itemOffsetPtg,
110+
itemElementHeights: this.itemElementHeights,
111+
scrollTop: this.listRef.current.scrollTop,
112+
scrollPtg: getElementScrollPercentage(this.listRef.current),
113+
clientHeight: this.listRef.current.clientHeight,
114+
getItemKey: this.getItemKey,
115+
});
99116

100117
this.setState({ status: 'MEASURE_DONE', startItemTop });
101118
}
@@ -119,7 +136,7 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
119136
return;
120137
}
121138

122-
const scrollPtg = getScrollPercentage(this.listRef.current);
139+
const scrollPtg = getElementScrollPercentage(this.listRef.current);
123140
const visibleCount = Math.ceil(height / itemHeight);
124141

125142
const { itemIndex, itemOffsetPtg, startIndex, endIndex } = getRangeIndex(

src/util.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function getLocationItem(scrollPtg: number, total: number): LocationItemResult {
2626
};
2727
}
2828

29-
function internalGetScrollPercentage({
29+
export function getScrollPercentage({
3030
scrollTop,
3131
scrollHeight,
3232
clientHeight,
@@ -43,12 +43,12 @@ function internalGetScrollPercentage({
4343
return scrollTopPtg;
4444
}
4545

46-
export function getScrollPercentage(element: HTMLElement | null) {
46+
export function getElementScrollPercentage(element: HTMLElement | null) {
4747
if (!element) {
4848
return 0;
4949
}
5050

51-
return internalGetScrollPercentage(element);
51+
return getScrollPercentage(element);
5252
}
5353

5454
/**
@@ -80,11 +80,42 @@ export function getRangeIndex(scrollPtg: number, itemCount: number, visibleCount
8080
};
8181
}
8282

83-
interface ItemTopConfig {
84-
clientHeight: number;
83+
interface ItemTopConfig<T> {
84+
itemIndex: number;
85+
itemElementHeights: { [key: string]: number };
86+
startIndex: number;
87+
itemOffsetPtg: number;
88+
8589
scrollTop: number;
8690
scrollPtg: number;
87-
itemOffsetPtg: number;
91+
clientHeight: number;
92+
93+
getItemKey: (index: number) => string;
8894
}
8995

90-
export function getStartItemTop(itemIndex: number, itemElementHeights: { [key: string]: number }) {}
96+
/**
97+
* Calculate virtual list start item top offset position.
98+
*/
99+
export function getStartItemTop({
100+
itemIndex,
101+
startIndex,
102+
itemOffsetPtg,
103+
itemElementHeights,
104+
scrollTop,
105+
scrollPtg,
106+
clientHeight,
107+
getItemKey,
108+
}: ItemTopConfig) {
109+
// Calculate top visible item top offset
110+
const locatedItemHeight = itemElementHeights[getItemKey(itemIndex)] || 0;
111+
const locatedItemTop = scrollPtg * clientHeight;
112+
const locatedItemOffset = itemOffsetPtg * locatedItemHeight;
113+
const locatedItemMergedTop = scrollTop + locatedItemTop - locatedItemOffset;
114+
115+
let startItemTop = locatedItemMergedTop;
116+
for (let index = itemIndex - 1; index >= startIndex; index -= 1) {
117+
startItemTop -= itemElementHeights[getItemKey(index)] || 0;
118+
}
119+
120+
return startItemTop;
121+
}

0 commit comments

Comments
 (0)