UNPKG

react-sortable-hoc

Version:

Set of higher-order components to turn any list into a sortable, touch-friendly, animated list

48 lines (39 loc) 1.07 kB
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'));