Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@eric-khoury
Last active May 8, 2018 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric-khoury/300ed3846f996de8ef5e53a4ae495dd4 to your computer and use it in GitHub Desktop.
Save eric-khoury/300ed3846f996de8ef5e53a4ae495dd4 to your computer and use it in GitHub Desktop.
React drag n drop example
state = {
items: ['DRAG ME 1', 'DRAG ME 2', 'DRAG ME 3'],
draggingId: undefined,
droppedId: undefined
}
dragStart = (e) => {
let id = e.target.getAttribute("data-id")
this.setState({ draggingId: id })
}
dragOver = (e) => {
e.preventDefault()
}
onDrop = (e) => {
var id = parseInt(this.state.draggingId) + 1
this.setState({ droppedId: id })
}
render() {
return (
<div className="App">
<ul className="columns">
{
this.state.items.map((item, i) => {
const draggingId = (i == this.state.draggingId) ? "dragging" : "";
return (
<li className={draggingId}
data-id={i}
key={i}
draggable="true"
onDragStart={this.dragStart}
onDragEnd={this.dragEnd}>
{item}
</li>
)
})
}
</ul>
<ul className="columns">
<li
className="drop-here"
onDrop={this.onDrop}
draggable="true"
onDragOver={this.dragOver}
>
DROP HERE
<br />
<span>Has item: { this.state.droppedId }</span>
</li>
</ul>
</div>
);
}
// CSS
//
// .columns {
// display: flex;
// list-style: none;
// padding: 0;
//}
//.columns li {
// padding: 30px;
// margin: 10px;
// background-color: white;
// border: 4px solid #000;
// transition: 0.2s;
// color: #000;
// font-size: 20px;
// width: 10%;
// height: 100%;
// text-align: center;
//}
//
//.columns li.drop-here {
// background: orange;
//}
//
//.columns .isEnabled, .columns .dragging {
// background-color: #6ad66a;
//}
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment