How to establish connection between two nodes using user handle in React diagram
This article describes how to establish connections between nodes using user handles. To create user handles, define and add them to the userHandles collection of the selectedItems property. The name property of userHandles is used to define the name of the user handle, which can then be used at runtime for identification and customization. The pathData property is used to define the path data of userhandle.
let handles = [
{
name: 'Draw',
pathData:
'M3.9730001,0 L8.9730001,5.0000007 3.9730001,10.000001 3.9730001,7.0090005 0,7.0090005 0,2.9910006 3.9730001,2.9910006 z',
tooltip: { content: 'Draw' },
visible: true,
offset: 0.5,
side: 'Right',
margin: { top: 0, bottom: 0, left: 0, right: 0 },
},
];
<DiagramComponent
id="diagram"
ref={(diagram) => (diagramInstance = diagram)}
width={'100%'}
height={'645px'}
nodes={nodes}
selectedItems={{
userHandles: handles,
}}
>
</DiagramComponent>
We have utilized the ‘getCustomTool’ method of the diagram. The getCustomTool method allows you to define and return a tool based on your custom logic. The tool could be a drawing tool, a selection tool, or any interactive tool that extends the diagram’s capabilities. The method typically accepts a parameter, which can be a string representing the tool name or any custom identifier that helps the method determine which tool to return.Refer the below code to establish the connection using the getCustomTool.
This method is designed to configure the diagram tool for drawing a connector from a selected node when the ‘Draw’ action is triggered. It prepares the tool to draw an orthogonal connector, starting from the selected node, and ensures the diagram updates accordingly.
<DiagramComponent
id="diagram"
ref={(diagram) => (diagramInstance = diagram)}
width={'100%'}
height={'645px'}
nodes={nodes}
getCustomTool={(action) => {
if (action == 'Draw') {
diagramInstance.tool = DiagramTools.DrawOnce;
diagramInstance.drawingObject.shape = {};
diagramInstance.drawingObject.type = diagramInstance
.drawingObject.type
? diagramInstance.drawingObject.type
: 'Orthogonal';
diagramInstance.drawingObject.sourceID =
diagramInstance.selectedItems.nodes[0].id;
diagramInstance.dataBind();
}
}}
>
</DiagramComponent>
We have utilized the selectionChange event of the diagram to customize the visibility of the user handles based on the selected items. Specifically, we apply the user handle constraints when a node is selected, enabling the user handles. Conversely, when a connector is selected, we remove the user handle constraint to hide the user handles.
<DiagramComponent
id="diagram"
ref={(diagram) => (diagramInstance = diagram)}
width={'100%'}
height={'645px'}
selectionChange={(args) => {
if (args.state === 'Changed') {
if (
args.newValue.length > 0 &&
args.newValue[0] instanceof Node
) {
diagramInstance.selectedItems = {
constraints:
SelectorConstraints.All | SelectorConstraints.UserHandle,
userHandles: handles,
};
} else {
diagramInstance.selectedItems = {
constraints:
SelectorConstraints.All & ~SelectorConstraints.UserHandle,
};
}
}
}}
>
</DiagramComponent>