Merge pull request #5132 from xyflow/fix/fit-view-no-on-nodes-change
Fix/fit view no on nodes change
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@xyflow/react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix fitView not working when onNodesChange is not defined.
|
||||||
@@ -42,13 +42,16 @@ export function BatchProvider<NodeType extends Node = Node, EdgeType extends Edg
|
|||||||
|
|
||||||
if (hasDefaultNodes) {
|
if (hasDefaultNodes) {
|
||||||
setNodes(next);
|
setNodes(next);
|
||||||
} else if (onNodesChange) {
|
} else {
|
||||||
|
// When a controlled flow is used we need to collect the changes
|
||||||
const changes = getElementsDiffChanges({
|
const changes = getElementsDiffChanges({
|
||||||
items: next,
|
items: next,
|
||||||
lookup: nodeLookup,
|
lookup: nodeLookup,
|
||||||
}) as NodeChange<NodeType>[];
|
}) as NodeChange<NodeType>[];
|
||||||
|
|
||||||
|
// We only want to fire onNodesChange if there are changes to the nodes
|
||||||
if (changes.length > 0) {
|
if (changes.length > 0) {
|
||||||
onNodesChange(changes);
|
onNodesChange?.(changes);
|
||||||
} else if (fitViewQueued) {
|
} else if (fitViewQueued) {
|
||||||
// If there are no changes to the nodes, we still need to call setNodes
|
// If there are no changes to the nodes, we still need to call setNodes
|
||||||
// to trigger a re-render and fitView.
|
// to trigger a re-render and fitView.
|
||||||
@@ -61,6 +64,7 @@ export function BatchProvider<NodeType extends Node = Node, EdgeType extends Edg
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const nodeQueue = useQueue<NodeType>(nodeQueueHandler);
|
const nodeQueue = useQueue<NodeType>(nodeQueueHandler);
|
||||||
|
|
||||||
const edgeQueueHandler = useCallback((queueItems: QueueItem<EdgeType>[]) => {
|
const edgeQueueHandler = useCallback((queueItems: QueueItem<EdgeType>[]) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user