refactor(nodes,edges): data structure

This commit is contained in:
moklick
2019-07-25 16:33:19 +02:00
parent 064f7d9c75
commit 7de43f7843
14 changed files with 197 additions and 169 deletions
+26 -21
View File
@@ -1,14 +1,14 @@
import React, { PureComponent } from 'react';
import Graph from '../src';
import Graph, { isEdge } from '../src';
// import Graph from '../dist/ReactGraph';
const SpecialNode = ({ data, styles }) => (
const SpecialNode = ({ data, onChange, styles }) => (
<div
style={{ background: '#FFCC00', padding: 10, borderRadius: 30, ...styles }}
>
<div>I am <strong>special</strong>!<br />{data.label}</div>
<select onChange={(e) => data.onChange(e.target.value, data)}>
<select onChange={(e) => onChange(e.target.value, data)}>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
@@ -22,30 +22,35 @@ class App extends PureComponent {
const onChange = (id, d) => {
this.setState(prevState => (
{ elements: prevState.elements.map(e => ({
...e,
data: {
...e.data,
label: '6' === e.data.id ? `option ${id} selected` : e.data.label
{elements: prevState.elements.map(e => {
if (isEdge(e)) {
return e;
}
}))
}
return {
...e,
data: {
...e.data,
label: '6' === e.id ? `option ${id} selected` : e.data.label
}
};
})}
));
}
this.state = {
elements: [
{ data: { id: '1', label: 'Tests', type: 'input' }, position: { x: 50, y: 50 } },
{ data: { id: '2', label: 'This is a node This is a node This is a node This is a node' }, position: { x: 100, y: 100 } },
{ data: { id: '3', label: 'This is a node' }, position: { x: 100, y: 200 }, style: { background: '#222', color: '#fff' } },
{ data: { id: '4', label: 'nody nodes', type: 'output' }, position: { x: 50, y: 300 } },
{ data: { id: '5', label: 'Another node', type: 'default' }, position: { x: 400, y: 300 } },
{ data: { id: '6', label: 'no option selected', type: 'special', onChange }, position: { x: 400, y: 400 } },
{ data: { source: '1', target: '2', type: 'bezier', animated: true, style: { stroke: 'orange' } } },
{ data: { source: '2', target: '3' } },
{ data: { source: '3', target: '4' } },
{ data: { source: '3', target: '5' } },
{ data: { source: '5', target: '6' } }
{ id: '1', type: 'input', data: { label: 'Tests' }, position: { x: 50, y: 50 } },
{ id: '2', data: { label: 'This is a node This is a node This is a node This is a node' }, position: { x: 100, y: 100 } },
{ id: '3', data: { label: 'This is a node' }, position: { x: 100, y: 200 }, style: { background: '#222', color: '#fff' } },
{ id: '4', type: 'output', data: { label: 'nody nodes' }, position: { x: 50, y: 300 } },
{ id: '5', type: 'default', data: { label: 'Another node'}, position: { x: 400, y: 300 } },
{ id: '6', type: 'special', onChange, data: { label: 'no option selected' }, position: { x: 400, y: 400 } },
{ source: '1', target: '2', type: 'bezier', animated: true, style: { stroke: 'orange' } },
{ source: '2', target: '3' },
{ source: '3', target: '4' },
{ source: '3', target: '5' },
{ source: '5', target: '6' }
]
};
}