question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Drag stop listeners aren't working if multiple nodes are moved

See original GitHub issue

Issue

If more than one node is selected with the “click to multi select” feature (https://github.com/wbkd/react-flow/issues/609, https://github.com/wbkd/react-flow/pull/663), then there seems to be no handler that will work, if more than one node is dragged.

Reproducing the issue

  • Open the overview example
  • Enable or set multiSelectionKeyCode (default is Meta)
  • Meta + Click two nodes
  • Drag to change both node positions

Observation

onNodeDragStop(event, node) will fire, but only contain a single node instead of a list of all nodes.

image

onSelectionDragStop(event, nodes) doesn’t fire. Based on this comment it seems to be the expected behavior, and “selection” is the blue box, but not multiple elements that are selected per click.

Potential solutions

It might be that I’m missing something here. So, helping me out, could be a solution 😃 If that’s not the case:

  • onSelectionDragStop could fire with a list of moved nodes regardless if the multiselection was done with multiSelectionKeyCode or with a rectangle
  • onNodeDragStop could be called with a list of nodes instead of a single node, if more than one node was moved

Info

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
elbakerinocommented, Oct 30, 2021

Another workaround, using the internal state of nodes, so far seems to work with single and multi selection drag movement.

const store = useStore()
const onNodeDragStop: (event: ReactMouseEvent, node: Node) => void = React.useCallback((e, node) => {
    // get currently selected elements
    const selectedElems = store.getState().selectedElements
    // get those as nodes to be able to access `__rf`
    const nodes = store.getState().nodes.filter(n => selectedElems?.findIndex(se => se.id === n.id) !== -1)
    const updatedNodes = []
    nodes?.forEach(elem => {
        // do your updating stuff
        updatedNodes.push({
            id: elem.id,
            type: elem.type,
            // @ts-ignore
            position: elem.__rf && elem.__rf.position ? elem.__rf.position : elem.position,
        })
    })
    updateNodesMoved(updatedNodes)
}, [updateNodesMoved, store])
1reaction
moklickcommented, May 17, 2022

We are working on a next release #2152. We dropped react-draggable and rewrote the dragging logic. We are now passing a third parameter nodes to onNodeDragStart, onNodeDrag and onNodeDragStop that includes all selected nodes. You can check out the next version with npm install react-flow-renderer@next.

@Zelenya7 the new version should solve your issue, otherwise you can filter your local nodes array const selectedNodes = nodes.filter(n => n.selected). The nodes receive selected as an attribute.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event listener on parent node is triggering on all children ...
I solved it, it wasn't working because I was setting the current element as this.elem = e.target instead of this.elem = e.currentTarget ....
Read more >
Drag'n'Drop with mouse events - The Modern JavaScript Tutorial
Drag 'n'Drop is a great interface solution. Taking something and dragging and dropping it is a clear and simple way to do many...
Read more >
HTML Drag and Drop API - MDN Web Docs
HTML drag-and-drop uses the DOM event model and drag events inherited from mouse events . A typical drag operation begins when a user...
Read more >
Drag Overlay - dnd-kit – Documentation
If your item needs to move from one container to another while dragging, ... render the <DragOverlay> component, drop animations will not work....
Read more >
Mind map - Miro Help Center
All nodes are automatically aligned but you can select and drag the ... (for Mac) if you'd like to move the node instead...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found