How to Add Phase at Runtime using Context-menu in React Diagram?
This article explains how to add a phase at runtime using the context menu in React Diagram. In Syncfusion® React Diagram, you can dynamically add phases to a diagram at runtime using a context menu. This feature enhances user interaction by allowing users to insert phases directly into the swimlane.
To add a phase using the context menu, you can use the addPhases method. In the addPhases method, you pass the selected swimlane and new phase data as arguments.
In the following code, it demonstrates how to add a phase using the context menu:
function contextMenuClick(args) {
diagram = diagramInstance;
if ((args.item.id === 'InsertPhaseBefore' || args.item.id === 'InsertPhaseAfter') &&
diagram.selectedItems.nodes[0].isPhase) {
// The currently selected phase
let node = diagram.selectedItems.nodes[0];
let swimlane = diagram.getObject(diagram.selectedItems.nodes[0].parentId);
let shape = swimlane.shape;
let existingPhase = shape.phases[node.columnIndex];
// Create a new phase object to be added
let newPhase = {
id: randomId(),
offset: existingPhase.offset,
header: {
style: existingPhase.header.style,
},
style: existingPhase.style,
};
//To add phase before existing phase
if (args.item.id === 'InsertPhaseBefore') {
newPhase.offset = newPhase.offset - newPhase.offset / 2;
diagram.addPhases(swimlane, [newPhase]);
} else {
//To add phase after existing phase
newPhase.offset = newPhase.offset + newPhase.offset / 2;
if (newPhase.offset >= swimlane.width) {
newPhase.offset = existingPhase.offset - 50;
}
diagram.addPhases(swimlane, [newPhase]);
}
diagram.clearSelection();
}
}
Refer to the working sample for additional details and implementation:sample
Conclusion
We hope you enjoyed learning about how to add phase at runtime using context-menu in 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, BoldDesk Support, or feedback portal. We are always happy to assist you!