From 0f1fec441e7f1aa1967bcaceefb77282d6e07c01 Mon Sep 17 00:00:00 2001 From: 3keyroman <46850604+3keyroman@users.noreply.github.com> Date: Wed, 4 Sep 2024 16:02:15 +0200 Subject: [PATCH] Fix react flow custom nodes positioning (#795) --- src/components/FlowChart/index.tsx | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/components/FlowChart/index.tsx b/src/components/FlowChart/index.tsx index 6a5f22f3..15f5d5d3 100644 --- a/src/components/FlowChart/index.tsx +++ b/src/components/FlowChart/index.tsx @@ -158,20 +158,21 @@ const getLayoutedElements = (nodes: CustomNode[], edges: Edge[], direction = 'TB dagre.layout(dagreGraph); - nodes.forEach((node: CustomNode) => { + const updatedNodes = nodes.map((node: CustomNode) => { const nodeWithPosition = dagreGraph.node(node.id); - node.targetPosition = isHorizontal ? Position.Left : Position.Top; - node.sourcePosition = isHorizontal ? Position.Right : Position.Bottom; const currentNodeHeight = node.data?.description ? nodeHeight + 35 : nodeHeight; - node.position = { - x: nodeWithPosition.x - nodeWidth / 2, - y: nodeWithPosition.y - currentNodeHeight / 2, + return { + ...node, + targetPosition: isHorizontal ? Position.Left : Position.Top, + sourcePosition: isHorizontal ? Position.Right : Position.Bottom, + position: { + x: nodeWithPosition.x - nodeWidth / 2, + y: nodeWithPosition.y - currentNodeHeight / 2, + }, }; - - return node; }); - return { nodes, edges }; + return { nodes: updatedNodes, edges }; } }; const FlowChartContent = ({