From 3969758af25702a56c9d14366aa35b208f1f82bd Mon Sep 17 00:00:00 2001 From: moklick Date: Fri, 21 Feb 2025 18:22:05 +0100 Subject: [PATCH 1/3] refactor(expandParent): use current value on drag #5039 --- packages/react/src/store/index.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/react/src/store/index.ts b/packages/react/src/store/index.ts index 2d0552c0..e3b5279a 100644 --- a/packages/react/src/store/index.ts +++ b/packages/react/src/store/index.ts @@ -51,7 +51,7 @@ const createStore = ({ * setNodes() is called exclusively in response to user actions: * - either when the `` prop is updated in the controlled ReactFlow setup, * - or when the user calls something like `reactFlowInstance.setNodes()` in an uncontrolled ReactFlow setup. - * + * * When this happens, we take the note objects passed by the user and extend them with fields * relevant for internal React Flow operations. */ @@ -154,16 +154,18 @@ const createStore = ({ const changes = []; for (const [id, dragItem] of nodeDragItems) { - const expandParent = !!(dragItem?.expandParent && dragItem?.parentId && dragItem?.position); + // we are using the nodelookup to be sure to use the current expandParent and parentId value + const node = get().nodeLookup.get(id); + const expandParent = !!(node?.expandParent && node?.parentId && dragItem?.position); const change: NodeChange = { id, type: 'position', position: expandParent ? { - x: Math.max(0, dragItem.position.x), - y: Math.max(0, dragItem.position.y), - } + x: Math.max(0, dragItem.position.x), + y: Math.max(0, dragItem.position.y), + } : dragItem.position, dragging, }; @@ -171,7 +173,7 @@ const createStore = ({ if (expandParent) { parentExpandChildren.push({ id, - parentId: dragItem.parentId!, + parentId: node.parentId!, rect: { ...dragItem.internals.positionAbsolute, width: dragItem.measured.width!, From 0292ad20109a3b2518dc686a82e100a0a6964fb8 Mon Sep 17 00:00:00 2001 From: moklick Date: Fri, 21 Feb 2025 18:22:54 +0100 Subject: [PATCH 2/3] chore(changeset): add --- .changeset/ten-waves-remain.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/ten-waves-remain.md diff --git a/.changeset/ten-waves-remain.md b/.changeset/ten-waves-remain.md new file mode 100644 index 00000000..9210e772 --- /dev/null +++ b/.changeset/ten-waves-remain.md @@ -0,0 +1,5 @@ +--- +'@xyflow/react': patch +--- + +Use current expandParent value on drag to be able to update it while dragging From 8dd2b4f9e2ece77f5ffc075aab68ebd7774f722b Mon Sep 17 00:00:00 2001 From: moklick Date: Tue, 25 Feb 2025 10:57:17 +0100 Subject: [PATCH 3/3] chore(updateNodePositions): cleanup --- packages/react/src/store/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/react/src/store/index.ts b/packages/react/src/store/index.ts index e3b5279a..19a885a2 100644 --- a/packages/react/src/store/index.ts +++ b/packages/react/src/store/index.ts @@ -152,10 +152,11 @@ const createStore = ({ updateNodePositions: (nodeDragItems, dragging = false) => { const parentExpandChildren: ParentExpandChild[] = []; const changes = []; + const { nodeLookup, triggerNodeChanges } = get(); for (const [id, dragItem] of nodeDragItems) { // we are using the nodelookup to be sure to use the current expandParent and parentId value - const node = get().nodeLookup.get(id); + const node = nodeLookup.get(id); const expandParent = !!(node?.expandParent && node?.parentId && dragItem?.position); const change: NodeChange = { @@ -170,10 +171,10 @@ const createStore = ({ dragging, }; - if (expandParent) { + if (expandParent && node.parentId) { parentExpandChildren.push({ id, - parentId: node.parentId!, + parentId: node.parentId, rect: { ...dragItem.internals.positionAbsolute, width: dragItem.measured.width!, @@ -186,12 +187,12 @@ const createStore = ({ } if (parentExpandChildren.length > 0) { - const { nodeLookup, parentLookup, nodeOrigin } = get(); + const { parentLookup, nodeOrigin } = get(); const parentExpandChanges = handleExpandParent(parentExpandChildren, nodeLookup, parentLookup, nodeOrigin); changes.push(...parentExpandChanges); } - get().triggerNodeChanges(changes); + triggerNodeChanges(changes); }, triggerNodeChanges: (changes) => { const { onNodesChange, setNodes, nodes, hasDefaultNodes, debug } = get();