UNPKG

1.07 kBJavaScriptView Raw
1import React, {Component} from 'react';
2import {render} from 'react-dom';
3import {
4 sortableContainer,
5 sortableElement,
6 sortableHandle,
7} from 'react-sortable-hoc';
8import arrayMove from 'array-move';
9
10const DragHandle = sortableHandle(() => <span>::</span>);
11
12const SortableItem = sortableElement(({value}) => (
13 <li>
14 <DragHandle />
15 {value}
16 </li>
17));
18
19const SortableContainer = sortableContainer(({children}) => {
20 return <ul>{children}</ul>;
21});
22
23class App extends Component {
24 state = {
25 items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6'],
26 };
27
28 onSortEnd = ({oldIndex, newIndex}) => {
29 this.setState(({items}) => ({
30 items: arrayMove(items, oldIndex, newIndex),
31 }));
32 };
33
34 render() {
35 const {items} = this.state;
36
37 return (
38 <SortableContainer onSortEnd={this.onSortEnd} useDragHandle>
39 {items.map((value, index) => (
40 <SortableItem key={`item-${index}`} index={index} value={value} />
41 ))}
42 </SortableContainer>
43 );
44 }
45}
46
47render(<App />, document.getElementById('root'));
48
\No newline at end of file