import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { DragDropContext, Draggable, Droppable, DropResult } from 'react-beautiful-dnd'; interface Item { id: string; content: string; } const getItems = (count: number): Item[] => { return Array .from({length: count}, (v, k) => k) .map(k => ({ id: `item-${k}`, content: `item ${k}` })); }; const reorder = (list: any[], startIndex: number, endIndex: number) => { const result = Array.from(list); const [removed] = result.splice(startIndex, 1); result.splice(endIndex, 0, removed); return result; }; const getItemStyle = (draggableStyle: any, isDragging: any) => ({ userSelect: 'none', background: isDragging ? 'lightgreen' : 'grey', ...draggableStyle }); const getListStyle = (isDraggingOver: any) => ({ background: isDraggingOver ? 'lightblue' : 'lightgrey', width: 250 }); interface AppState { items: Item[]; } class App extends React.Component<{}, AppState> { constructor(props: any) { super(props); this.state = { items: getItems(10) }; this.onDragEnd = this.onDragEnd.bind(this); } onDragEnd(result: DropResult) { if (!result.destination) { return; } const items = reorder( this.state.items, result.source.index, result.destination.index ); this.setState({items}); } render() { return ( {(provided, snapshot) => (
{this.state.items.map(item => ( {(provided, snapshot) => (
{item.content}
{provided.placeholder}
)}
))} {provided.placeholder}
)}
); } } ReactDOM.render(, document.getElementById('app'));