I'm attempting to implement a simple drag and drop piece of functionality in my React app.
Currently, the functionality works great on desktop, using these three functions:
const [dragId, setDragId] = useState()
const [array, setArray] = useState(jobsArray)
const handleDragOver = (ev) => { ev.preventDefault() }
const handleDrag = (ev) => {
console.log(ev.currentTarget.id);
setDragId(ev.currentTarget.id)
}
const handleDrop = (ev) => {
// Find the items that I want to arrange
const foundItem = array.find(job => job.title === dragId)
const dragItem = array.find(job => job.title === ev.currentTarget.id)
// Create a copy of the array, with the element removed
const filteredArray = array.filter(job => foundItem.title !== job.title)
// Find the element position of the item that I want to push up
const elementPosition = array.indexOf(dragItem)
// Insert the item into the correct element
const newArray = [...filteredArray.slice(0, elementPosition), foundItem, ...filteredArray.slice(elementPosition)]
console.log(newArray);
// Update the original array to be the new array
setArray(newArray)
}
I'm then applying these onto a 'draggable' div, like so:
<div className="dashboard-list width-100 flex-center" draggable onDragOver={onDragOver} onDragStart={onDragStart} onDrop={onDrop} onTouchStart={onDragStart} onTouchEnd={onDrop} onTouchMove={onDragOver} id={title}>
{ content }
</div>
As you can see, I've added the same functions onto the appropriate touch events (I think) - but it's not working.
What am I doing wrong?