Skip to content

Commit c9576ec

Browse files
committed
feat(VirtualList): consider as items of the same height by default
1 parent 30fd59d commit c9576ec

File tree

6 files changed

+115
-27
lines changed

6 files changed

+115
-27
lines changed

docs/virtual-list/demo/basic.md

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ const generateLi = (index = 'index') => {
3232
for (let i = 0; i < 1000; i++) {
3333
dataSource.push(generateLi(i));
3434
}
35-
console.log(dataSource)
36-
const getHeight = index => index % 3 === 0 ? 20 : 30;
3735

3836
const demo = (
3937
<div className={'virtual-box'}>

docs/virtual-list/demo/initial-index.md

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# 设置初始位置
22

3-
- order: 0
3+
- order: 1
44

55
使用 jumpIndex 设置初始位置
66

77
:::lang=en-us
88
# Basic
99

10-
- order: 0
10+
- order: 1
1111

1212
Use jumpIndex to set first item.
1313

@@ -21,11 +21,7 @@ import { VirtualList } from '@alifd/next';
2121
const dataSource = [];
2222

2323
function generateLi(index) {
24-
if (index % 3 === 0) {
25-
return (<li key={`key-${index}`} style={{lineHeight: '30px', background: '#5f83ff', color: '#fff'}}>key-{index}</li>);
26-
} else {
27-
return (<li key={`key-${index}`} style={{lineHeight: '20px'}}>key-{index}</li>);
28-
}
24+
return (<li key={`key-${index}`} style={{lineHeight: '20px'}}>key-{index}</li>);
2925
}
3026
function generateData(len) {
3127
for (let i = 0; i < len; i++) {
@@ -35,20 +31,9 @@ function generateData(len) {
3531

3632
class App extends React.Component {
3733
state = {
38-
initial: 20,
34+
initial: 50,
3935
dataSource: generateData(1000)
4036
}
41-
componentDidMount() {
42-
setTimeout(()=> {
43-
const instance = this.refs.virtual.getInstance();
44-
instance.scrollTo(50);
45-
}, 200);
46-
47-
}
48-
49-
getHeight(index) {
50-
return index % 3 === 0 ? 30 : 20;
51-
}
5237
onClick() {
5338
this.setState({
5439
initial: this.state.initial + 20
@@ -61,7 +46,7 @@ class App extends React.Component {
6146
<br/>
6247
<br/>
6348
<div className={'virtual-box'}>
64-
<VirtualList ref="virtual" jumpIndex={this.state.initial} itemSizeGetter={this.getHeight.bind(this)}>
49+
<VirtualList ref="virtual" jumpIndex={this.state.initial}>
6550
{dataSource}
6651
</VirtualList>
6752
</div>
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 不等高的item
2+
3+
- order: 2
4+
5+
使用 jumpIndex 设置初始位置, 并设置 itemSizeGetter
6+
7+
:::lang=en-us
8+
# Basic
9+
10+
- order: 2
11+
12+
Use jumpIndex and itemSizeGetter to set first item in visual area.
13+
14+
:::
15+
16+
---
17+
18+
````jsx
19+
import { VirtualList } from '@alifd/next';
20+
21+
const dataSource = [];
22+
23+
function generateLi(index) {
24+
if (index % 3 === 0) {
25+
return (<li key={`key-${index}`} style={{lineHeight: '30px', background: '#5f83ff', color: '#fff'}}>key-{index}</li>);
26+
} else {
27+
return (<li key={`key-${index}`} style={{lineHeight: '20px'}}>key-{index}</li>);
28+
}
29+
}
30+
function generateData(len) {
31+
for (let i = 0; i < len; i++) {
32+
dataSource.push(generateLi(i));
33+
}
34+
}
35+
36+
class App extends React.Component {
37+
state = {
38+
initial: 20,
39+
dataSource: generateData(1000)
40+
}
41+
42+
componentDidMount() {
43+
setTimeout(()=> {
44+
const instance = this.refs.virtual.getInstance();
45+
instance.scrollTo(50);
46+
}, 200);
47+
48+
}
49+
50+
getHeight(index) {
51+
return index % 3 === 0 ? 30 : 20;
52+
}
53+
54+
onClick() {
55+
this.setState({
56+
initial: this.state.initial + 20
57+
})
58+
}
59+
60+
render() {
61+
return (
62+
<div>
63+
<button onClick={this.onClick.bind(this)}>jump to {this.state.initial + 20}</button>
64+
<br/>
65+
<br/>
66+
<div className={'virtual-box'}>
67+
<VirtualList ref="virtual" jumpIndex={this.state.initial} itemSizeGetter={this.getHeight.bind(this)}>
68+
{dataSource}
69+
</VirtualList>
70+
</div>
71+
</div>
72+
);
73+
}
74+
}
75+
76+
ReactDOM.render(<App />, mountNode);
77+
````
78+
79+
````css
80+
.virtual-box {
81+
height: 200px;
82+
width: 200px;
83+
border: 1px solid #ddd;
84+
overflow: auto;
85+
}
86+
.virtual-box ul {
87+
padding: 0;
88+
margin: 0;
89+
list-style: none;
90+
}
91+
.virtual-box li {
92+
padding-left: 10px;
93+
border-bottom: 1px solid #333;
94+
}
95+
````

docs/virtual-list/index.en-us.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
| itemsRenderer | items parent dom,by default (items, ref) => <ul ref={ref}>{items}</ul><br><br>**signature**:<br>Function() => void | Function | (items, ref) => &lt;ul ref={ref}>{items}&lt;/ul> |
1919
| threshold | height of threshold | Number | 100 |
2020
| itemSizeGetter | get item's height<br><br>**signature**:<br>Function() => void | Function | - |
21-
| jumpIndex | set this to jump to the index you want, works only when you set itemSizeGetter | Number | 0 |
21+
| jumpIndex | the index you want to jump to, set itemSizeGetter if the height of items vary | Number | 0 |

docs/virtual-list/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
| itemsRenderer | 父渲染函数,默认为 (items, ref) => <ul ref={ref}>{items}</ul><br><br>**签名**:<br>Function() => void | Function | (items, ref) => &lt;ul ref={ref}>{items}&lt;/ul> |
1919
| threshold | 缓冲区高度 | Number | 100 |
2020
| itemSizeGetter | 获取item高度的函数<br><br>**签名**:<br>Function() => void | Function | - |
21-
| jumpIndex | 设置初始位置,需要设置 itemSizeGetter 才能生效 | Number | 0 |
21+
| jumpIndex | 设置跳转位置,需要设置 itemSizeGetter 才能生效, 不设置认为元素等高并取第一个元素高度作为默认高 | Number | 0 |

src/virtual-list/virtual-list.jsx

+13-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default class VirtualList extends Component {
4848
*/
4949
itemSizeGetter: PropTypes.func,
5050
/**
51-
* 设置跳转位置,需要设置 itemSizeGetter 才能生效
51+
* 设置跳转位置,需要设置 itemSizeGetter 才能生效, 不设置认为元素等高并取第一个元素高度作为默认高
5252
*/
5353
jumpIndex: PropTypes.number,
5454
className: PropTypes.string
@@ -153,7 +153,7 @@ export default class VirtualList extends Component {
153153
el = el.parentElement;
154154

155155
switch (window.getComputedStyle(el).overflowY) {
156-
case 'auto': case 'scroll': case 'overlay': return el;
156+
case 'auto': case 'scroll': case 'overlay': case 'visible': return el;
157157
}
158158

159159
return window;
@@ -338,14 +338,24 @@ export default class VirtualList extends Component {
338338

339339
getSizeOf(index) {
340340
const { cache } = this;
341-
const { itemSizeGetter } = this.props;
341+
const { itemSizeGetter, jumpIndex } = this.props;
342+
342343
// Try the cache.
343344
if (index in cache) {
344345
return cache[index];
345346
}
346347
if (itemSizeGetter) {
347348
return itemSizeGetter(index);
348349
}
350+
351+
const height = Object.values(this.cache).pop();
352+
if (!this.defaultItemHeight && jumpIndex > -1 && height) {
353+
this.defaultItemHeight = height;
354+
}
355+
356+
if (this.defaultItemHeight) {
357+
return this.defaultItemHeight;
358+
}
349359
}
350360

351361
constrain(from, size, {children, minSize}) {

0 commit comments

Comments
 (0)