refactor(examples): enhance update node example

This commit is contained in:
moklick
2020-12-10 12:30:29 +01:00
parent 36025b28b0
commit a64a1239a9
3 changed files with 33 additions and 23 deletions
+4 -10
View File
@@ -25,12 +25,9 @@ const BasicFlow = () => {
setElements((elms) => { setElements((elms) => {
return elms.map((el) => { return elms.map((el) => {
if (isNode(el)) { if (isNode(el)) {
return { el.position = {
...el, x: Math.random() * 400,
position: { y: Math.random() * 400,
x: Math.random() * 400,
y: Math.random() * 400,
},
}; };
} }
@@ -46,10 +43,7 @@ const BasicFlow = () => {
setElements((elms) => { setElements((elms) => {
return elms.map((el) => { return elms.map((el) => {
if (isNode(el)) { if (isNode(el)) {
return { el.className = el.className === 'light' ? 'dark' : 'light';
...el,
className: el.className === 'light' ? 'dark' : 'light',
};
} }
return el; return el;
@@ -8,20 +8,19 @@ const initialElements = [
{ id: 'e1-2', source: '1', target: '2' }, { id: 'e1-2', source: '1', target: '2' },
]; ];
const BasicFlow = () => { const UpdateNode = () => {
const [elements, setElements] = useState(initialElements); const [elements, setElements] = useState(initialElements);
const [nodeName, setNodeName] = useState('Node 1'); const [nodeName, setNodeName] = useState('Node 1');
const [nodeBg, setNodeBg] = useState('#eee');
useEffect(() => { useEffect(() => {
setElements((els) => setElements((els) =>
els.map((el) => { els.map((el) => {
if (el.id === '1') { if (el.id === '1') {
return { // it's important that you create a new object here in order to notify react flow about the change
...el, el.data = {
data: { ...el.data,
...el.data, label: nodeName,
label: nodeName,
},
}; };
} }
@@ -30,13 +29,30 @@ const BasicFlow = () => {
); );
}, [nodeName, setElements]); }, [nodeName, setElements]);
useEffect(() => {
setElements((els) =>
els.map((el) => {
if (el.id === '1') {
// it's important that you create a new object here in order to notify react flow about the change
el.style = { ...el.style, backgroundColor: nodeBg };
}
return el;
})
);
}, [nodeBg, setElements]);
return ( return (
<ReactFlow elements={elements} defaultZoom={1.5} minZoom={0.2} maxZoom={4}> <ReactFlow elements={elements} defaultZoom={1.5} minZoom={0.2} maxZoom={4}>
<div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4 }}> <div style={{ position: 'absolute', right: 10, top: 10, zIndex: 4, fontSize: 12 }}>
<label style={{ display: 'block' }}>label:</label>
<input value={nodeName} onChange={(evt) => setNodeName(evt.target.value)} /> <input value={nodeName} onChange={(evt) => setNodeName(evt.target.value)} />
<label style={{ display: 'block', marginTop: 10 }}>background:</label>
<input value={nodeBg} onChange={(evt) => setNodeBg(evt.target.value)} />
</div> </div>
</ReactFlow> </ReactFlow>
); );
}; };
export default BasicFlow; export default UpdateNode;
+4 -4
View File
@@ -16,7 +16,7 @@ import EdgeTypes from './EdgeTypes';
import CustomConnectionLine from './CustomConnectionLine'; import CustomConnectionLine from './CustomConnectionLine';
import NodeTypeChange from './NodeTypeChange'; import NodeTypeChange from './NodeTypeChange';
import UpdatableEdge from './UpdatableEdge'; import UpdatableEdge from './UpdatableEdge';
import UpdateLabel from './UpdateLabel'; import UpdateNode from './UpdateNode';
import './index.css'; import './index.css';
@@ -86,9 +86,9 @@ const routes = [
label: 'Updatable Edge', label: 'Updatable Edge',
}, },
{ {
path: '/update-label', path: '/update-node',
component: UpdateLabel, component: UpdateNode,
label: 'Update Label', label: 'Update Node',
}, },
]; ];