forked from weird94/react-typed-recyclelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
80 lines (71 loc) · 1.89 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
import React, { memo, useState } from 'react';
import List, { CellProps, CellDatas } from '../../src/index';
import useStoreState from '../../src/useStoreState';
import ReactDOM from 'react-dom';
const cellStyle: React.CSSProperties = {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column'
};
const Cell = memo((props: CellProps<{ name: string; index: number }>) => {
const [active, setActive] = useStoreState(false, 'active', props);
const { style, index, data } = props;
const bgColor = index % 2 === 0 ? 'white' : 'yellow';
return (
<div
style={{ ...cellStyle, ...style, backgroundColor: bgColor }}
onClick={() => setActive(!active)}
>
<div
style={{
color: active ? 'red' : 'black',
fontWeight: active ? 'bolder' : 'normal'
}}
>
[{data.index}] active: <span>{active + ''}</span>
</div>
</div>
);
});
const cellData: CellDatas<{ name: string; index: number }> = Array(1000)
.fill(1)
.map((_, i) => {
return {
height: 100,
data: { name: `name`, index: i },
Component: Cell,
uniqueKey: i + ''
};
});
const Demo = () => {
const [data, setData] = useState(cellData);
const [sortKey, setSortKey] = useState('0');
const reverse = () => {
data.reverse();
setData([...data]);
setSortKey(Date.now() + '');
};
return (
<>
<div
style={{
width: 80,
height: 50,
backgroundColor: 'yellow',
textAlign: 'center',
lineHeight: '50px',
position: 'absolute',
top: 20,
right: 20,
zIndex: 100
}}
onClick={reverse}
>
reverse
</div>
<List height={screen.height} width={screen.width} cellData={data} sortKey={sortKey} />
</>
);
};
ReactDOM.render(<Demo />, document.getElementById('root'));