Update src/App.js
Browse files- src/App.js +124 -31
src/App.js
CHANGED
|
@@ -1,49 +1,142 @@
|
|
| 1 |
-
import React, { useCallback } from 'react';
|
| 2 |
-
import ReactFlow, {
|
| 3 |
-
ReactFlowProvider,
|
| 4 |
-
useNodesState,
|
| 5 |
-
useEdgesState,
|
| 6 |
-
useReactFlow,
|
| 7 |
-
} from 'reactflow';
|
| 8 |
-
|
| 9 |
-
import { initialNodes, initialEdges } from './nodes-edges.js';
|
| 10 |
import 'reactflow/dist/style.css';
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
};
|
| 15 |
|
| 16 |
-
const
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
const [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
const layouted = getLayoutedElements(nodes, edges);
|
| 23 |
|
| 24 |
-
|
| 25 |
-
setEdges([...layouted.edges]);
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return (
|
| 33 |
<ReactFlow
|
| 34 |
nodes={nodes}
|
| 35 |
edges={edges}
|
| 36 |
onNodesChange={onNodesChange}
|
| 37 |
onEdgesChange={onEdgesChange}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
fitView
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
);
|
| 41 |
};
|
| 42 |
|
| 43 |
-
export default
|
| 44 |
-
return (
|
| 45 |
-
<ReactFlowProvider>
|
| 46 |
-
<LayoutFlow />
|
| 47 |
-
</ReactFlowProvider>
|
| 48 |
-
);
|
| 49 |
-
}
|
|
|
|
| 1 |
+
import React, { useState, useEffect, useCallback } from 'react';
|
| 2 |
+
import ReactFlow, { useNodesState, useEdgesState, addEdge, MiniMap, Controls } from 'reactflow';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import 'reactflow/dist/style.css';
|
| 4 |
|
| 5 |
+
import ColorSelectorNode from './ColorSelectorNode';
|
| 6 |
+
|
| 7 |
+
import './index.css';
|
| 8 |
+
|
| 9 |
+
const initBgColor = '#1A192B';
|
| 10 |
+
|
| 11 |
+
const connectionLineStyle = { stroke: '#fff' };
|
| 12 |
+
const snapGrid = [20, 20];
|
| 13 |
+
const nodeTypes = {
|
| 14 |
+
selectorNode: ColorSelectorNode,
|
| 15 |
};
|
| 16 |
|
| 17 |
+
const defaultViewport = { x: 0, y: 0, zoom: 1.5 };
|
| 18 |
+
|
| 19 |
+
const CustomNodeFlow = () => {
|
| 20 |
+
const [nodes, setNodes, onNodesChange] = useNodesState([]);
|
| 21 |
+
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
|
| 22 |
+
const [bgColor, setBgColor] = useState(initBgColor);
|
| 23 |
+
|
| 24 |
+
useEffect(() => {
|
| 25 |
+
const onChange = (event) => {
|
| 26 |
+
setNodes((nds) =>
|
| 27 |
+
nds.map((node) => {
|
| 28 |
+
if (node.id !== '2') {
|
| 29 |
+
return node;
|
| 30 |
+
}
|
| 31 |
|
| 32 |
+
const color = event.target.value;
|
|
|
|
| 33 |
|
| 34 |
+
setBgColor(color);
|
|
|
|
| 35 |
|
| 36 |
+
return {
|
| 37 |
+
...node,
|
| 38 |
+
data: {
|
| 39 |
+
...node.data,
|
| 40 |
+
color,
|
| 41 |
+
},
|
| 42 |
+
};
|
| 43 |
+
})
|
| 44 |
+
);
|
| 45 |
+
};
|
| 46 |
|
| 47 |
+
setNodes([
|
| 48 |
+
{
|
| 49 |
+
id: '1',
|
| 50 |
+
type: 'input',
|
| 51 |
+
data: { label: 'An input node' },
|
| 52 |
+
position: { x: 0, y: 50 },
|
| 53 |
+
sourcePosition: 'right',
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
id: '2',
|
| 57 |
+
type: 'selectorNode',
|
| 58 |
+
data: { onChange: onChange, color: initBgColor },
|
| 59 |
+
style: { border: '1px solid #777', padding: 10 },
|
| 60 |
+
position: { x: 300, y: 50 },
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
id: '3',
|
| 64 |
+
type: 'output',
|
| 65 |
+
data: { label: 'Output A' },
|
| 66 |
+
position: { x: 650, y: 25 },
|
| 67 |
+
targetPosition: 'left',
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
id: '4',
|
| 71 |
+
type: 'output',
|
| 72 |
+
data: { label: 'Output B' },
|
| 73 |
+
position: { x: 650, y: 100 },
|
| 74 |
+
targetPosition: 'left',
|
| 75 |
+
},
|
| 76 |
+
]);
|
| 77 |
+
|
| 78 |
+
setEdges([
|
| 79 |
+
{
|
| 80 |
+
id: 'e1-2',
|
| 81 |
+
source: '1',
|
| 82 |
+
target: '2',
|
| 83 |
+
animated: true,
|
| 84 |
+
style: { stroke: '#fff' },
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
id: 'e2a-3',
|
| 88 |
+
source: '2',
|
| 89 |
+
target: '3',
|
| 90 |
+
sourceHandle: 'a',
|
| 91 |
+
animated: true,
|
| 92 |
+
style: { stroke: '#fff' },
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
id: 'e2b-4',
|
| 96 |
+
source: '2',
|
| 97 |
+
target: '4',
|
| 98 |
+
sourceHandle: 'b',
|
| 99 |
+
animated: true,
|
| 100 |
+
style: { stroke: '#fff' },
|
| 101 |
+
},
|
| 102 |
+
]);
|
| 103 |
+
}, []);
|
| 104 |
+
|
| 105 |
+
const onConnect = useCallback(
|
| 106 |
+
(params) =>
|
| 107 |
+
setEdges((eds) => addEdge({ ...params, animated: true, style: { stroke: '#fff' } }, eds)),
|
| 108 |
+
[]
|
| 109 |
+
);
|
| 110 |
return (
|
| 111 |
<ReactFlow
|
| 112 |
nodes={nodes}
|
| 113 |
edges={edges}
|
| 114 |
onNodesChange={onNodesChange}
|
| 115 |
onEdgesChange={onEdgesChange}
|
| 116 |
+
onConnect={onConnect}
|
| 117 |
+
style={{ background: bgColor }}
|
| 118 |
+
nodeTypes={nodeTypes}
|
| 119 |
+
connectionLineStyle={connectionLineStyle}
|
| 120 |
+
snapToGrid={true}
|
| 121 |
+
snapGrid={snapGrid}
|
| 122 |
+
defaultViewport={defaultViewport}
|
| 123 |
fitView
|
| 124 |
+
attributionPosition="bottom-left"
|
| 125 |
+
>
|
| 126 |
+
<MiniMap
|
| 127 |
+
nodeStrokeColor={(n) => {
|
| 128 |
+
if (n.type === 'input') return '#0041d0';
|
| 129 |
+
if (n.type === 'selectorNode') return bgColor;
|
| 130 |
+
if (n.type === 'output') return '#ff0072';
|
| 131 |
+
}}
|
| 132 |
+
nodeColor={(n) => {
|
| 133 |
+
if (n.type === 'selectorNode') return bgColor;
|
| 134 |
+
return '#fff';
|
| 135 |
+
}}
|
| 136 |
+
/>
|
| 137 |
+
<Controls />
|
| 138 |
+
</ReactFlow>
|
| 139 |
);
|
| 140 |
};
|
| 141 |
|
| 142 |
+
export default CustomNodeFlow;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|