From 8b95cd712f53c710823e4d1936feb3a5bd3eb41f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20M=C3=B6ller?= Date: Fri, 22 Oct 2021 12:09:01 +0200 Subject: [PATCH] feat(noderenderer): render nodes recursively --- example/src/Basic/index.tsx | 7 +++--- src/container/NodeRenderer/index.tsx | 37 +++++++++++++++++++--------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/example/src/Basic/index.tsx b/example/src/Basic/index.tsx index aa854864..3b0cf32e 100644 --- a/example/src/Basic/index.tsx +++ b/example/src/Basic/index.tsx @@ -32,7 +32,7 @@ const initialNodes: Node[] = [ }, { id: '4a', - data: { label: 'Node 4a', isNested: true }, + data: { label: 'Node 4a' }, position: { x: 400, y: 400 }, className: 'light', parentNode: '4', @@ -47,14 +47,14 @@ const initialNodes: Node[] = [ }, { id: '4b1', - data: { label: 'Node 4b1', isNested: true }, + data: { label: 'Node 4b1' }, position: { x: 450, y: 450 }, className: 'light', parentNode: '4b', }, { id: '4b2', - data: { label: 'Node 4b2', isNested: true }, + data: { label: 'Node 4b2' }, position: { x: 550, y: 550 }, className: 'light', parentNode: '4b', @@ -135,6 +135,7 @@ const BasicFlow = () => { defaultZoom={1.5} minZoom={0.2} maxZoom={4} + onlyRenderVisibleElements={false} > diff --git a/src/container/NodeRenderer/index.tsx b/src/container/NodeRenderer/index.tsx index d78552ae..72bae44a 100644 --- a/src/container/NodeRenderer/index.tsx +++ b/src/container/NodeRenderer/index.tsx @@ -31,7 +31,7 @@ const selector = (s: ReactFlowState) => ({ }); interface NodesProps extends NodeRendererProps { - nodes?: Node[]; + nodes: Node[]; isDraggable?: boolean; resizeObserver: ResizeObserver | null; scale: number; @@ -41,10 +41,12 @@ interface NodesProps extends NodeRendererProps { nodesConnectable: boolean; elementsSelectable: boolean; recursionDepth: number; + parentId?: string; } function Nodes({ - nodes = [], + nodes, + parentId, isDraggable, resizeObserver, scale, @@ -56,7 +58,12 @@ function Nodes({ recursionDepth, ...props }: NodesProps): any { - return nodes.map((node) => { + const rootNodes = useMemo( + () => (parentId ? nodes.filter((n) => n.parentNode === parentId) : nodes.filter((n) => !n.parentNode)), + [nodes, parentId] + ); + + return rootNodes.map((node) => { const nodeType = node.type || 'default'; if (!props.nodeTypes[nodeType]) { @@ -75,18 +82,11 @@ function Nodes({ node.height !== null && typeof node.width !== 'undefined' && typeof node.height !== 'undefined'; - let childRect; const childNodes = nodes.filter((n) => n.parentNode === node.id && !n.isHidden); - // if (childNodes.length) { - // console.log(node.id, childNodes, getRectOfNodes(childNodes)); - // } - - // console.log(childNodes); - if (childNodes.length) { - childRect = getRectOfNodes(childNodes); + const childRect = getRectOfNodes(childNodes); node.position = node.isDragging ? node.position : { x: Math.round(childRect.x) - 10, y: Math.round(childRect.y) - 10 }; @@ -130,10 +130,23 @@ function Nodes({ isDraggable={isNodeDraggable} isSelectable={isSelectable} isConnectable={isConnectable} - resizeObserver={resizeObserver} + resizeObserver={childNodes.length ? resizeObserver : null} dragHandle={node.dragHandle} zIndex={3 + recursionDepth} /> + ); });