react-arborist icon indicating copy to clipboard operation
react-arborist copied to clipboard

Dragging Nodes Between Trees

Open mafin1799 opened this issue 2 years ago • 9 comments

how to implement copying of a node when dragging from one tree to another?

mafin1799 avatar Nov 22 '23 15:11 mafin1799

I've never tried this, but this would be a good demo to attempt. Thanks for the idea.

jameskerr avatar Dec 12 '23 17:12 jameskerr

that is, the existing functionality is enough for this?)

mafin1799 avatar Dec 14 '23 08:12 mafin1799

This is not possible today. Unfortunately. I few changes will need to be made to the drag and drop functionality.

jameskerr avatar Dec 14 '23 17:12 jameskerr

Can I +1 dragging between trees please? I've got everything else working as a full controlled component (so moving, editing, deleting) apart from this - I took a look at the source code but was put off by "the most complex, tricky function in the whole repo." :) At the moment I can see it does do a drop event onto another tree, but the index and parent are not being set correctly.

theearlofsandwich avatar Feb 20 '24 15:02 theearlofsandwich

My use case is similar but slightly different. I want to be able to drop nodes of tree into another react component. is there anyway to set type and item properties of useDrag which must be use internally in app. or do i need to use draghandle?

atmnksd avatar Nov 12 '24 16:11 atmnksd

I created this PR using which this is now possible https://github.com/brimdata/react-arborist/pull/282

atmnksd avatar Nov 12 '24 19:11 atmnksd

I created this PR using which this is now possible #282

Hi atmnk, are you able to provide a short example of how to use this, please? I've managed to patch Arborist with your code and build, but not sure how to use what you've done. Does this add the node data into the Tree onMove?

theearlofsandwich avatar Nov 13 '24 17:11 theearlofsandwich

you need to use useDrop Hook in the target List as it is being done in this code. This should have been originally possible as well. I just made it more flexible where accept determined the key 'NODE' read more about react dnd useDrag and useDrop hook

const onDrop = useCallback((props: any, monitor: any) => {
    if (monitor.getItemType() == 'NODE') {
     modify target tree data here
  }, [your dependencies])
const [{ isDropOver, canDrop }, drop] = useDrop({
    accept: ['NODE'],
    drop: onDrop,
    collect: (monitor) => ({
      isDropOver: monitor.isOver(),
      canDrop: monitor.getItemType()==monitor.canDrop(),
    }),
  });
  const isActive = isDropOver && canDrop
  let backgroundColor = 'rounded-xl '
  if (isActive) {
    backgroundColor = 'rounded-xl bg-green-400'
  } else if (canDrop) {
    backgroundColor = 'rounded-xl bg-yellow-400'
  }```
 

atmnksd avatar Nov 14 '24 05:11 atmnksd

Hi all, I've created a PR that enables cross tree dragging and dropping of nodes. Works for both controlled and uncontrolled trees and demo included in the showcase.

https://github.com/brimdata/react-arborist/pull/306

theearlofsandwich avatar Apr 28 '25 09:04 theearlofsandwich