-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathdrag-handle.js
47 lines (39 loc) · 1.05 KB
/
drag-handle.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
import React, {Component} from 'react';
import {render} from 'react-dom';
import {
sortableContainer,
sortableElement,
sortableHandle,
} from 'react-sortable-hoc';
import arrayMove from 'array-move';
const DragHandle = sortableHandle(() => <span>::</span>);
const SortableItem = sortableElement(({value}) => (
<li>
<DragHandle />
{value}
</li>
));
const SortableContainer = sortableContainer(({children}) => {
return <ul>{children}</ul>;
});
class App extends Component {
state = {
items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'],
};
onSortEnd = ({oldIndex, newIndex}) => {
this.setState(({items}) => ({
items: arrayMove(items, oldIndex, newIndex),
}));
};
render() {
const {items} = this.state;
return (
<SortableContainer onSortEnd={this.onSortEnd} useDragHandle>
{items.map((value, index) => (
<SortableItem key={`item-${value}`} index={index} value={value} />
))}
</SortableContainer>
);
}
}
render(<App />, document.getElementById('root'));