Skip to content

Commit fec58a2

Browse files
committed
support render pure if all item is visible
1 parent d06ed5c commit fec58a2

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

examples/animate.tsx

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

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

src/Filler.tsx

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,40 @@ interface FillerProps {
44
/** Virtual filler height. Should be `count * itemMinHeight` */
55
height: number;
66
/** Set offset of visible items. Should be the top of start item position */
7-
offset: number;
7+
offset?: number;
88

99
children: React.ReactNode;
1010
}
1111

1212
/**
1313
* Fill component to provided the scroll content real height.
1414
*/
15-
const Filler: React.FC<FillerProps> = ({ height, offset, children }): React.ReactElement => (
16-
<div style={{ height, position: 'relative', overflow: 'hidden' }}>
17-
<div
18-
style={{
19-
marginTop: offset,
20-
position: 'absolute',
21-
left: 0,
22-
right: 0,
23-
top: 0,
24-
display: 'flex',
25-
flexDirection: 'column',
26-
}}
27-
>
28-
{children}
15+
const Filler: React.FC<FillerProps> = ({ height, offset, children }): React.ReactElement => {
16+
let outerStyle: React.CSSProperties = {};
17+
18+
let innerStyle: React.CSSProperties = {
19+
display: 'flex',
20+
flexDirection: 'column',
21+
};
22+
23+
if (offset !== undefined) {
24+
outerStyle = { height, position: 'relative', overflow: 'hidden' };
25+
26+
innerStyle = {
27+
...innerStyle,
28+
marginTop: offset,
29+
position: 'absolute',
30+
left: 0,
31+
right: 0,
32+
top: 0,
33+
};
34+
}
35+
36+
return (
37+
<div style={outerStyle}>
38+
<div style={innerStyle}>{children}</div>
2939
</div>
30-
</div>
31-
);
40+
);
41+
};
3242

3343
export default Filler;

src/List.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
150150
*/
151151
public renderChildren = (list: T[], startIndex: number, renderFunc: RenderFunc<T>) =>
152152
// We should measure rendered item height
153-
list.map((item, index) => {
153+
list.map((item, index) => {
154154
const node = renderFunc(item) as React.ReactElement;
155155
const eleIndex = startIndex + index;
156156
const eleKey = this.getItemKey(eleIndex);
@@ -162,8 +162,7 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
162162
this.itemElements[eleKey] = ele;
163163
},
164164
});
165-
})
166-
;
165+
});
167166

168167
public render() {
169168
const {
@@ -177,11 +176,18 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
177176
...restProps
178177
} = this.props;
179178

180-
// Render pure list if not set height
181-
if (height === undefined) {
179+
const mergedStyle = {
180+
...style,
181+
height,
182+
overflowY: 'auto',
183+
overflowAnchor: 'none',
184+
};
185+
186+
// Render pure list if not set height or height is enough for all items
187+
if (height === undefined || dataSource.length * itemHeight <= height) {
182188
return (
183-
<Component style={style} {...restProps}>
184-
{this.renderChildren(dataSource, 0, children)}
189+
<Component style={mergedStyle} {...restProps}>
190+
<Filler height={height}>{this.renderChildren(dataSource, 0, children)}</Filler>
185191
</Component>
186192
);
187193
}
@@ -190,17 +196,7 @@ class List<T> extends React.Component<ListProps<T>, ListState> {
190196
const contentHeight = dataSource.length * itemHeight;
191197

192198
return (
193-
<Component
194-
style={{
195-
...style,
196-
height,
197-
overflowY: 'auto',
198-
overflowAnchor: 'none',
199-
}}
200-
{...restProps}
201-
onScroll={this.onScroll}
202-
ref={this.listRef}
203-
>
199+
<Component style={mergedStyle} {...restProps} onScroll={this.onScroll} ref={this.listRef}>
204200
<Filler height={contentHeight} offset={status === 'MEASURE_DONE' ? startItemTop : 0}>
205201
{this.renderChildren(dataSource.slice(startIndex, endIndex + 1), startIndex, children)}
206202
</Filler>

src/util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export function getScrollPercentage(element: HTMLElement | null) {
3232
}
3333

3434
const { scrollTop, scrollHeight, clientHeight } = element;
35+
if (scrollHeight <= clientHeight) {
36+
return 0;
37+
}
38+
3539
const scrollTopPtg = scrollTop / (scrollHeight - clientHeight);
3640
return scrollTopPtg;
3741
}

0 commit comments

Comments
 (0)