Skip to content

Commit

Permalink
feat(noderenderer): render nodes recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
chrtze committed Oct 22, 2021
1 parent c1d5c35 commit 8b95cd7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
7 changes: 4 additions & 3 deletions example/src/Basic/index.tsx
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -135,6 +135,7 @@ const BasicFlow = () => {
defaultZoom={1.5}
minZoom={0.2}
maxZoom={4}
onlyRenderVisibleElements={false}
>
<MiniMap />
<Controls />
Expand Down
37 changes: 25 additions & 12 deletions src/container/NodeRenderer/index.tsx
Expand Up @@ -31,7 +31,7 @@ const selector = (s: ReactFlowState) => ({
});

interface NodesProps extends NodeRendererProps {
nodes?: Node[];
nodes: Node[];
isDraggable?: boolean;
resizeObserver: ResizeObserver | null;
scale: number;
Expand All @@ -41,10 +41,12 @@ interface NodesProps extends NodeRendererProps {
nodesConnectable: boolean;
elementsSelectable: boolean;
recursionDepth: number;
parentId?: string;
}

function Nodes({
nodes = [],
nodes,
parentId,
isDraggable,
resizeObserver,
scale,
Expand All @@ -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]) {
Expand All @@ -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 };
Expand Down Expand Up @@ -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}
/>
<MemoizedNodes
nodes={nodes}
parentId={node.id}
snapToGrid={snapToGrid}
snapGrid={snapGrid}
nodesDraggable={nodesDraggable}
nodesConnectable={nodesConnectable}
resizeObserver={resizeObserver}
elementsSelectable={elementsSelectable}
scale={scale}
recursionDepth={recursionDepth + 1}
{...props}
/>
</Fragment>
);
});
Expand Down

0 comments on commit 8b95cd7

Please sign in to comment.