How to Convert Connectors to Connector While Joining in React Diagram?
The Syncfusion® React Diagram allows you to merge two separate connectors into a single connector as you drag and join nodes. This can be achieved by handling the sourcePointChange
and targetPointChange
events, which allow you to dynamically update the source and target points of the connectors.
In this article, we will walk through the steps to handle the source and target points to convert two connectors into a single connector when one connector end is dragged and attached to the port of another connector.
The sourcePointChange
and targetPointChange
events are triggered when a connector’s source or target point is moved. These events provide an opportunity to update the source and target IDs of the connector dynamically. By handling these events, you can merge two connectors into one by removing the old connectors and updating the diagram with the new connection.
sourcePointChange={
(args) => {
if (args.state == 'Completed') {
var sourceConnector = diagramInstance.getObject(args.connector.sourceID);
var sourceNode = diagramInstance.getObject((sourceConnector).sourceID);
args.connector.sourceID = (sourceNode).id;
diagramInstance.dataBind();
diagramInstance.remove(sourceConnector);
diagramInstance.clearSelection();
}
}
}
targetPointChange={
(args) => {
if (args.state == 'Completed') {
var targetConnector = diagramInstance.getObject(args.connector.targetID);
var targetNode = diagramInstance.getObject((targetConnector).targetID);
args.connector.targetID = (targetNode).id;
diagramInstance.dataBind();
diagramInstance.remove(targetConnector);
diagramInstance.clearSelection();
}
}
}
You can find a working example of this implementation on StackBlitz.
Conclusion
I hope you enjoyed learning how to convert two connectors into single connector while dragging and joining using React Diagram.You can refer to our React Diagram feature tour page to know 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, Direct-Trac, or feedback portal. We are always happy to assist you!