-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathreact-virtualized.js
90 lines (74 loc) · 2.12 KB
/
react-virtualized.js
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
import React, {Component} from 'react';
import {render} from 'react-dom';
import {sortableContainer, sortableElement} from 'react-sortable-hoc';
import arrayMove from 'array-move';
import {List} from 'react-virtualized';
const SortableItem = sortableElement(({value}) => {
return <li>{value}</li>;
});
class VirtualList extends Component {
renderRow = ({index}) => {
const {items} = this.props;
const {value} = items[index];
return <SortableItem index={index} value={value} />;
};
getRowHeight = ({index}) => {
const {items} = this.props;
return items[index].height;
};
render() {
const {items, getRef} = this.props;
return (
<List
ref={getRef}
rowHeight={this.getRowHeight}
rowRenderer={this.renderRow}
rowCount={items.length}
width={400}
height={600}
/>
);
}
}
const SortableVirtualList = sortableContainer(VirtualList);
class App extends Component {
state = {
items: [
{value: 'Item 1', height: 89},
{value: 'Item 2', height: 59},
{value: 'Item 3', height: 130},
{value: 'Item 4', height: 59},
{value: 'Item 5', height: 200},
{value: 'Item 6', height: 150},
],
};
registerListRef = (listInstance) => {
this.List = listInstance;
};
onSortEnd = ({oldIndex, newIndex}) => {
if (oldIndex === newIndex) {
return;
}
const {items} = this.state;
this.setState({
items: arrayMove(items, oldIndex, newIndex),
});
// We need to inform React Virtualized that the items have changed heights
// This can either be done by imperatively calling the recomputeRowHeights and
// forceUpdate instance methods on the `List` ref, or by passing an additional prop
// to List that changes whenever the order changes to force it to re-render
this.List.recomputeRowHeights();
this.List.forceUpdate();
};
render() {
const {items} = this.state;
return (
<SortableVirtualList
getRef={this.registerListRef}
items={items}
onSortEnd={this.onSortEnd}
/>
);
}
}
render(<App />, document.getElementById('root'));