From b764875dec528f84117c6e274f3e41b131c82273 Mon Sep 17 00:00:00 2001 From: moklick Date: Wed, 7 May 2025 09:39:30 +0200 Subject: [PATCH] fix(react): call onNodesChange for uncontrolled flows that use updateNode #5062 --- examples/react/src/examples/Basic/index.tsx | 9 +++-- .../src/components/BatchProvider/index.tsx | 38 +++++++++---------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/examples/react/src/examples/Basic/index.tsx b/examples/react/src/examples/Basic/index.tsx index 5754fd62..168bcff6 100644 --- a/examples/react/src/examples/Basic/index.tsx +++ b/examples/react/src/examples/Basic/index.tsx @@ -11,6 +11,7 @@ import { useReactFlow, Panel, OnNodeDrag, + FitViewOptions, } from '@xyflow/react'; const onNodeDrag: OnNodeDrag = (_, node: Node, nodes: Node[]) => console.log('drag', node, nodes); @@ -54,6 +55,9 @@ const initialEdges: Edge[] = [ ]; const defaultEdgeOptions = {}; +const fitViewOptions: FitViewOptions = { + padding: { top: '100px', left: '0%', right: '10%', bottom: 0.1 }, +}; const BasicFlow = () => { const { @@ -135,6 +139,7 @@ const BasicFlow = () => { { minZoom={0.2} maxZoom={4} fitView - fitViewOptions={{ - padding: { top: '100px', left: '0%', right: '10%', bottom: 0.1 }, - }} + fitViewOptions={fitViewOptions} defaultEdgeOptions={defaultEdgeOptions} selectNodesOnDrag={false} elevateEdgesOnSelect diff --git a/packages/react/src/components/BatchProvider/index.tsx b/packages/react/src/components/BatchProvider/index.tsx index f41b79a8..92bb4370 100644 --- a/packages/react/src/components/BatchProvider/index.tsx +++ b/packages/react/src/components/BatchProvider/index.tsx @@ -40,28 +40,28 @@ export function BatchProvider[]; + if (hasDefaultNodes) { setNodes(next); - } else { - // When a controlled flow is used we need to collect the changes - const changes = getElementsDiffChanges({ - items: next, - lookup: nodeLookup, - }) as NodeChange[]; + } - // We only want to fire onNodesChange if there are changes to the nodes - if (changes.length > 0) { - onNodesChange?.(changes); - } else if (fitViewQueued) { - // If there are no changes to the nodes, we still need to call setNodes - // to trigger a re-render and fitView. - window.requestAnimationFrame(() => { - const { fitViewQueued, nodes, setNodes } = store.getState(); - if (fitViewQueued) { - setNodes(nodes); - } - }); - } + // We only want to fire onNodesChange if there are changes to the nodes + if (changes.length > 0) { + onNodesChange?.(changes); + } else if (fitViewQueued) { + // If there are no changes to the nodes, we still need to call setNodes + // to trigger a re-render and fitView. + window.requestAnimationFrame(() => { + const { fitViewQueued, nodes, setNodes } = store.getState(); + if (fitViewQueued) { + setNodes(nodes); + } + }); } }, []);