Entitree Flex Tree
This example shows how you can integrate entitree-flex with React Flow to create tree layouts that can have sibling nodes and support nodes with different dimensions.
import React, { useCallback } from 'react';
import {
Background,
ReactFlow,
addEdge,
ConnectionLineType,
Panel,
useNodesState,
useEdgesState,
} from '@xyflow/react';
import '@xyflow/react/dist/style.css';
import CustomNode from './CustomNode';
import { initialTree, treeRootId } from './initialElements';
import { layoutElements } from './layout-elements';
const nodeTypes = {
custom: CustomNode,
};
const { nodes: layoutedNodes, edges: layoutedEdges } = layoutElements(
initialTree,
treeRootId,
'TB',
);
const LayoutFlow = () => {
const [nodes, setNodes, onNodesChange] = useNodesState(layoutedNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(layoutedEdges);
const onConnect = useCallback(
(params) =>
setEdges((eds) =>
addEdge(
{ ...params, type: ConnectionLineType.SmoothStep, animated: true },
eds,
),
),
[],
);
const onLayout = useCallback(
(direction) => {
const { nodes: layoutedNodes, edges: layoutedEdges } = layoutElements(
initialTree,
treeRootId,
direction,
);
setNodes([...layoutedNodes]);
setEdges([...layoutedEdges]);
},
[nodes, edges],
);
return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
connectionLineType={ConnectionLineType.SmoothStep}
fitView
nodeTypes={nodeTypes}
style={{ backgroundColor: "#F7F9FB" }}
>
<Panel position="top-right">
<button onClick={() => onLayout('TB')}>vertical layout</button>
<button onClick={() => onLayout('LR')}>horizontal layout</button>
</Panel>
<Background />
</ReactFlow>
);
};
export default LayoutFlow;