-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample-fixed-sized-list.tsx
69 lines (61 loc) · 1.69 KB
/
example-fixed-sized-list.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
import React, { Ref } from "react";
import AutoSizer from "react-virtualized-auto-sizer";
import "./App.css";
import {
ChildrenProps,
SortableFixedSizeList,
SortableVariableSizeList,
} from "./src";
const data: any[] = [];
const n = 1000;
for (var i = 0; i < n; i++) data.push(i.toString());
interface State {
data: any[];
}
function move<T>(array: T[], from: number, to: number) {
array.splice(to, 0, array.splice(from, 1)[0]);
}
class App extends React.Component<{}, State> {
constructor(props: any) {
super(props);
this.state = {
data: data,
};
}
render() {
return (
<div style={{ width: "100%", height: "100%" }}>
<AutoSizer>
{({ height, width }: { height: number; width: number }) => (
<SortableFixedSizeList
height={height}
width={width}
itemCount={n}
itemSize={30}
itemData={this.state.data}
onSortOrderChanged={({ originalIndex, newIndex }) => {
move(this.state.data, originalIndex, newIndex);
this.setState({
data: this.state.data.slice(0),
});
}}
>
{React.forwardRef(
(
{ data, index, style, onSortMouseDown }: ChildrenProps,
ref: Ref<any>
) => (
<div ref={ref} style={style}>
<button onMouseDown={onSortMouseDown}>drag handle</button>
{data[index]}
</div>
)
)}
</SortableFixedSizeList>
)}
</AutoSizer>
</div>
);
}
}
export default App;