Last active
October 13, 2015 06:48
-
-
Save sweir27/4ea941dd717da69527d6 to your computer and use it in GitHub Desktop.
React drag/drop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dragStart: function(e) { | |
this.setState({dragMouseStart: e.clientY, draggingElementId: e.currentTarget.getAttribute('data-slug')}) | |
e.dataTransfer.effectAllowed = 'move'; | |
}, | |
dragOver: function(e) { | |
e.preventDefault(); | |
var over = e.currentTarget; // element we're hovering over | |
var draggingId = this.state.draggingElementId; | |
var toElId = over.getAttribute('data-slug'); | |
var movingUp = (this.state.dragMouseStart > e.clientY); | |
if (draggingId != toElId) { | |
orderCopy = this.state.artworksOrder | |
var toIndex = orderCopy.indexOf(toElId) | |
var fromIndex = orderCopy.indexOf(draggingId) | |
if (movingUp) { | |
orderCopy.splice(toIndex, 0, orderCopy.splice(fromIndex, 1)[0]) | |
} else { | |
orderCopy.splice(fromIndex, 0, orderCopy.splice(toIndex, 1)[0]) | |
} | |
this.setState({artworksOrder: orderCopy}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment