Articles in this section

How to restrict the node to drop on to another node in React Diagram?

This article explains how to restrict a node from being dropped onto another node in React Diagram. In the React Diagram, you can restrict a node from being dropped onto another node by using the positionChange and drop events. To perform the drag-and-drop functionality of nodes in the diagram, enable the AllowDrop constraints for the nodes. When each node renders, the node defaults callback method gets triggered. In the node defaults method, set the node constraints to AllowDrop. Thus, the constraints for every node are set to AllowDrop.

Please refer to the following code example for how to set node constraints in the NodeDefaults method.

   getNodeDefaults={(obj) => {
     //Sets the default values of a node
     obj.width = 100;
     obj.height = 100;
     obj.shape = { type: 'Basic', shape: 'Ellipse' };
     obj.style = { fill: '#37909A', strokeColor: '#024249' };
     obj.constraints =
       NodeConstraints.Default | NodeConstraints.AllowDrop;
     obj.annotations = [
       {
         content: obj.text,
         margin: { left: 10, right: 10 },
         style: {
           color: 'white',
           fill: 'none',
           strokeColor: 'none',
           bold: true,
         },
       },
     ];
     return obj;
   }}

When you drag the node, the positionChange event gets triggered. When you start to drag the node positionChange event gets triggered for Start state. In that state, store the node offsetX and offsetY value. Please refer to the following code example for how to store the node offset values.

positionChange={(args) => {
 if (args.state === 'Start') {
   offsetX = args.newValue.offsetX;
   offsetY = args.newValue.offsetY;
 }
}}

By using the drop event, you can drag and drop a certain node to a different node in the diagram. When we drag the node and the mouse hovers over another node, a highlighter is shown. After the node gets dropped, the drop event is triggered. In that event, retrieve the dropped node from the element arguments and reset the node offset values to the old offset that you have stored in the position change event. Please refer to the following code example and sample.

  drop={(args) => {
     if (args.element instanceof Node) {
       args.element.offsetX = offsetX;
       args.element.offsetY = offsetY;
       diagramInstance.dataBind();
     }
   }}

Refer to the working sample for additional details and implementation: Sample

Conclusion

We hope you enjoyed learning about how to restrict the node to dropped on another node in React Diagram.

You can refer to our React Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our React Diagram example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied