64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
import React, { useState } from 'react';
|
|
|
|
import ReactFlow, { removeElements, addEdge, isNode, Background } from 'react-flow-renderer';
|
|
|
|
const onNodeDragStop = (evt, node) => console.log('drag stop', node);
|
|
const onLoad = (reactFlowInstance) => console.log('flow loaded:', reactFlowInstance);
|
|
const onElementClick = (evt, element) => console.log('click', element);
|
|
|
|
const initialElements = [
|
|
{ id: '1', type: 'input', data: { label: 'Node 1' }, position: { x: 250, y: 5 } },
|
|
{ id: '2', data: { label: 'Node 2' }, position: { x: 100, y: 100 } },
|
|
{ id: '3', data: { label: 'Node 3' }, position: { x: 400, y: 100 } },
|
|
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 } },
|
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
|
{ id: 'e1-3', source: '1', target: '3' },
|
|
];
|
|
|
|
const BasicFlow = () => {
|
|
const [elements, setElements] = useState(initialElements);
|
|
const onElementsRemove = (elementsToRemove) => setElements((els) => removeElements(elementsToRemove, els));
|
|
const onConnect = (params) => setElements((els) => addEdge(params, els));
|
|
|
|
const updatePos = () => {
|
|
setElements((elms) => {
|
|
return elms.map((el) => {
|
|
if (isNode(el)) {
|
|
return {
|
|
...el,
|
|
position: {
|
|
x: Math.random() * 400,
|
|
y: Math.random() * 400,
|
|
},
|
|
};
|
|
}
|
|
|
|
return el;
|
|
});
|
|
});
|
|
};
|
|
|
|
return (
|
|
<ReactFlow
|
|
elements={elements}
|
|
onLoad={onLoad}
|
|
onElementClick={onElementClick}
|
|
onElementsRemove={onElementsRemove}
|
|
onConnect={onConnect}
|
|
onNodeDragStop={onNodeDragStop}
|
|
className="react-flow-basic-example"
|
|
defaultZoom={1.5}
|
|
minZoom={0.2}
|
|
maxZoom={4}
|
|
>
|
|
<Background variant="lines" />
|
|
|
|
<button onClick={updatePos} style={{ position: 'absolute', right: 10, top: 30, zIndex: 4 }}>
|
|
change pos
|
|
</button>
|
|
</ReactFlow>
|
|
);
|
|
};
|
|
|
|
export default BasicFlow;
|