-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
141 lines (128 loc) · 3.9 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { useState, useEffect, useRef } from 'react';
import VirtualList from 'zr-virtual-list';
// import VirtualList from '../src';
import RadioGroup from '../RadioGroup';
import './styles.less';
interface ListProps {}
interface Item {
id: string;
count: number;
}
const countList = [10, 20, 50, 100, 500, 3000];
const indexList = [undefined, 0, 6, 21, 112, 666, 2345];
const scrollList = [undefined, 0, 100, 1800, 8888, 22000];
const List: React.FC<ListProps> = () => {
const [data, setData] = useState<any[]>([]);
const scrollTop1 = useRef<number | undefined>();
const [visible, setVisible] = useState(true);
const [renderCount, setRenderCount] = useState(20);
const [defaultStartIndex, setDefaultStartIndex] = useState<
number | undefined
>(666);
const [defaultScrollTop, setDefaultScrollTop] = useState<number | undefined>(
0
);
useEffect(() => {
const arr = [];
for (let i = 0; i < 10000; i++) {
arr.push({ id: `id-${i}`, count: 0 });
}
setData(arr);
}, []);
useEffect(() => {
if (visible && scrollTop1.current) {
setDefaultScrollTop(scrollTop1.current);
}
}, [visible]);
useEffect(() => {
if (defaultScrollTop === undefined) {
scrollTop1.current = undefined;
}
}, [defaultScrollTop]);
const onScroll = (_scrollTop: number) => {
console.log(_scrollTop);
scrollTop1.current = _scrollTop;
};
const countHandler = (index: number, type: 'increment' | 'decrement') => {
setData((prev) => {
const newItem = {
...prev![index],
count:
type === 'increment'
? prev![index].count + 1
: prev![index].count - 1,
};
prev!.splice(index, 1, newItem);
return [...prev!];
});
};
const onStartIndexChange = (visibleItemIndex: number, startIndex: number) => {
// console.log(visibleItemIndex, startIndex);
};
const onVisibleChange = () => {
setVisible(!visible);
};
return (
<>
<div className="header">
<h2 className="title">zr-virtual-list example</h2>
<p>dataLength: {data.length}</p>
<RadioGroup
className="render_count"
name="renderCount"
value={renderCount}
setValue={setRenderCount}
dataList={countList}
/>
<RadioGroup
className="start_index"
name="defaultStartIndex"
value={defaultStartIndex}
setValue={setDefaultStartIndex}
dataList={indexList}
/>
<RadioGroup
className="scroll_top"
name="defaultScrollTop"
value={defaultScrollTop}
setValue={setDefaultScrollTop}
dataList={scrollList}
/>
<p>
<button className="btn-ctrl" onClick={onVisibleChange}>
{visible ? 'Hide List' : 'Show List'}
</button>
</p>
</div>
{visible && (
<VirtualList
itemKey="id"
className="scroll-container"
dataList={data}
renderCount={renderCount}
defaultScrollTop={defaultScrollTop}
defaultStartIndex={defaultStartIndex}
onScroll={onScroll}
onStartIndexChange={onStartIndexChange}
>
{(item: Item, index) => (
<div className={`item ${index % 2 === 0 ? 'item-2n' : ''}`}>
<p>
<button onClick={() => countHandler(index, 'decrement')}>
count--
</button>
<button onClick={() => countHandler(index, 'increment')}>
count++
</button>
</p>
<p>id: {item.id}</p>
<p>count: {item.count}</p>
</div>
)}
</VirtualList>
)}
</>
);
};
export default List;