-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathcollections.js
56 lines (47 loc) · 1.4 KB
/
collections.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
import React, {Component} from 'react';
import {render} from 'react-dom';
import {sortableContainer, sortableElement} from 'react-sortable-hoc';
import arrayMove from 'array-move';
const SortableItem = sortableElement(({value}) => <li>{value}</li>);
const SortableContainer = sortableContainer(({children}) => {
return <div>{children}</div>;
});
class App extends Component {
state = {
collections: [[0, 1, 2], [0, 1, 2, 3, 4], [0, 1, 2]],
};
onSortEnd = ({oldIndex, newIndex, collection}) => {
this.setState(({collections}) => {
const newCollections = [...collections];
newCollections[collection] = arrayMove(
collections[collection],
oldIndex,
newIndex,
);
return {collections: newCollections};
});
};
render() {
const {collections} = this.state;
return (
<SortableContainer onSortEnd={this.onSortEnd}>
{collections.map((items, index) => (
<React.Fragment key={index}>
<strong>LIST {index}</strong>
<ul>
{items.map((item, i) => (
<SortableItem
key={item}
value={`Item ${item}`}
index={i}
collection={index}
/>
))}
</ul>
</React.Fragment>
))}
</SortableContainer>
);
}
}
render(<App />, document.getElementById('root'));