refactor(elements): simplfy node and edge handling
This commit is contained in:
+2
-28
@@ -1,12 +1,10 @@
|
||||
import React, { PureComponent } from 'react';
|
||||
import isEqual from 'lodash.isequal';
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
const whyDidYouRender = require('@welldone-software/why-did-you-render');
|
||||
whyDidYouRender(React);
|
||||
}
|
||||
|
||||
import { parseElements, isNode, isEdge } from '../graph-utils';
|
||||
import GraphView from '../GraphView';
|
||||
import GlobalKeyHandler from '../GlobalKeyHandler';
|
||||
import { Provider } from '../GraphContext';
|
||||
@@ -28,42 +26,18 @@ class ReactGraph extends PureComponent {
|
||||
|
||||
this.nodeTypes = createNodeTypes(props.nodeTypes);
|
||||
this.edgeTypes = createEdgeTypes(props.edgeTypes);
|
||||
|
||||
this.state = {
|
||||
nodes: [],
|
||||
edges: []
|
||||
};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.updateElements(this.props.elements);
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
if (!isEqual(prevProps.elements, this.props.elements)) {
|
||||
this.updateElements(this.props.elements);
|
||||
}
|
||||
}
|
||||
|
||||
updateElements(elements) {
|
||||
const parsedElements = elements.map(parseElements);
|
||||
|
||||
this.setState({
|
||||
nodes: parsedElements.filter(isNode),
|
||||
edges: parsedElements.filter(isEdge),
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
style, onElementClick, children, onLoad,
|
||||
style, onElementClick, elements, children, onLoad,
|
||||
onMove, onChange, onElementsRemove, onConnect, onNodeDragStop,
|
||||
connectionLineType, connectionLineStyle
|
||||
} = this.props;
|
||||
|
||||
return (
|
||||
<div style={style} className="react-graph">
|
||||
<Provider nodes={this.state.nodes} edges={this.state.edges} onConnect={onConnect}>
|
||||
<Provider elements={elements} onConnect={onConnect}>
|
||||
<GraphView
|
||||
onLoad={onLoad}
|
||||
onMove={onMove}
|
||||
|
||||
Reference in New Issue
Block a user