-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathreact-infinite.js
54 lines (47 loc) · 1.31 KB
/
react-infinite.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
import React, {Component} from 'react';
import {render} from 'react-dom';
import {sortableContainer, sortableElement} from 'react-sortable-hoc';
import arrayMove from 'array-move';
import Infinite from 'react-infinite';
const SortableItem = sortableElement(({height, value}) => {
return <li style={{height}}>{value}</li>;
});
const SortableInfiniteList = sortableContainer(({items}) => {
return (
<Infinite
containerHeight={600}
elementHeight={items.map(({height}) => height)}
>
{items.map(({value, height}, index) => (
<SortableItem
key={`item-${value}`}
index={index}
value={value}
height={height}
/>
))}
</Infinite>
);
});
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},
],
};
onSortEnd = ({oldIndex, newIndex}) => {
this.setState(({items}) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
};
render() {
const {items} = this.state;
return <SortableInfiniteList items={items} onSortEnd={this.onSortEnd} />;
}
}
render(<App />, document.getElementById('root'));