Files
DefinitelyTyped/types/react-beautiful-dnd/react-beautiful-dnd-tests.tsx
2018-02-16 15:10:50 -06:00

99 lines
2.5 KiB
TypeScript

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 = (isDragging: boolean, draggableStyle: 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 (
<DragDropContext onDragEnd={this.onDragEnd}>
<Droppable droppableId="droppable">
{(provided, snapshot) => (
<div ref={provided.innerRef} style={getListStyle(snapshot.isDraggingOver)}>
{this.state.items.map((item, index) => (
<Draggable key={item.id} draggableId={item.id} index={index}>
{(provided, snapshot) => (
<div>
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
style={getItemStyle(snapshot.isDragging, provided.draggableProps.style)}
>
{item.content}
</div>
{provided.placeholder}
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));