✨ Add an example flow to demo custom minimap nodes.
This commit is contained in:
@@ -5,6 +5,7 @@ import Basic from '../examples/Basic';
|
|||||||
import Backgrounds from '../examples/Backgrounds';
|
import Backgrounds from '../examples/Backgrounds';
|
||||||
import ControlledUncontrolled from '../examples/ControlledUncontrolled';
|
import ControlledUncontrolled from '../examples/ControlledUncontrolled';
|
||||||
import CustomConnectionLine from '../examples/CustomConnectionLine';
|
import CustomConnectionLine from '../examples/CustomConnectionLine';
|
||||||
|
import CustomMiniMapNode from '../examples/CustomMiniMapNode';
|
||||||
import CustomNode from '../examples/CustomNode';
|
import CustomNode from '../examples/CustomNode';
|
||||||
import DefaultNodes from '../examples/DefaultNodes';
|
import DefaultNodes from '../examples/DefaultNodes';
|
||||||
import DragHandle from '../examples/DragHandle';
|
import DragHandle from '../examples/DragHandle';
|
||||||
@@ -77,6 +78,11 @@ const routes: IRoute[] = [
|
|||||||
path: '/custom-connectionline',
|
path: '/custom-connectionline',
|
||||||
component: CustomConnectionLine,
|
component: CustomConnectionLine,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Custom Minimap Node',
|
||||||
|
path: '/custom-minimap-node',
|
||||||
|
component: CustomMiniMapNode,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'Custom Node',
|
name: 'Custom Node',
|
||||||
path: '/custom-node',
|
path: '/custom-node',
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import { MouseEvent, CSSProperties, useCallback } from 'react';
|
||||||
|
|
||||||
|
import ReactFlow, {
|
||||||
|
addEdge,
|
||||||
|
Background,
|
||||||
|
BackgroundVariant,
|
||||||
|
Connection,
|
||||||
|
Controls,
|
||||||
|
Edge,
|
||||||
|
MiniMap,
|
||||||
|
Node,
|
||||||
|
ReactFlowInstance,
|
||||||
|
useEdgesState,
|
||||||
|
useNodesState,
|
||||||
|
} from 'reactflow';
|
||||||
|
|
||||||
|
const onInit = (reactFlowInstance: ReactFlowInstance) => console.log('flow loaded:', reactFlowInstance);
|
||||||
|
const onNodeClick = (_: MouseEvent, node: Node) => console.log('click', node);
|
||||||
|
const onNodeDragStop = (_: MouseEvent, node: Node) => console.log('drag stop', node);
|
||||||
|
|
||||||
|
const buttonStyle: CSSProperties = {
|
||||||
|
position: 'absolute',
|
||||||
|
left: 10,
|
||||||
|
top: 10,
|
||||||
|
zIndex: 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
const CustomMiniMapNodeFlow = () => {
|
||||||
|
const [nodes, setNodes, onNodesChange] = useNodesState([]);
|
||||||
|
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
|
||||||
|
|
||||||
|
const onConnect = useCallback((params: Connection | Edge) => setEdges((els) => addEdge(params, els)), [setEdges]);
|
||||||
|
const addRandomNode = () => {
|
||||||
|
const nodeId = (nodes.length + 1).toString();
|
||||||
|
const newNode: Node = {
|
||||||
|
id: nodeId,
|
||||||
|
data: { label: `Node: ${nodeId}` },
|
||||||
|
position: {
|
||||||
|
x: Math.random() * window.innerWidth,
|
||||||
|
y: Math.random() * window.innerHeight,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
setNodes((nds) => nds.concat(newNode));
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ReactFlow
|
||||||
|
nodes={nodes}
|
||||||
|
edges={edges}
|
||||||
|
onInit={onInit}
|
||||||
|
onNodesChange={onNodesChange}
|
||||||
|
onEdgesChange={onEdgesChange}
|
||||||
|
onNodeClick={onNodeClick}
|
||||||
|
onConnect={(p) => onConnect(p)}
|
||||||
|
onNodeDragStop={onNodeDragStop}
|
||||||
|
onlyRenderVisibleElements={false}
|
||||||
|
>
|
||||||
|
<Controls />
|
||||||
|
<Background variant={BackgroundVariant.Lines} />
|
||||||
|
<MiniMap nodeComponent={CustomMiniMapNode} />
|
||||||
|
|
||||||
|
<button type="button" onClick={addRandomNode} style={buttonStyle}>
|
||||||
|
add node
|
||||||
|
</button>
|
||||||
|
</ReactFlow>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const CustomMiniMapNode = ({ x, y, width, height }) => (
|
||||||
|
<circle cx={x} cy={y} r={Math.max(width, height)} />
|
||||||
|
)
|
||||||
|
|
||||||
|
export default CustomMiniMapNodeFlow;
|
||||||
Reference in New Issue
Block a user