Skip to content

Commit dcaf0d7

Browse files
authored
Graph forces must be reconfigured if some d3 config is updated (#117)
1 parent 8ec7204 commit dcaf0d7

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/components/graph/Graph.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ export default class Graph extends React.Component {
312312
);
313313
const state = graphElementsUpdated ? graphHelper.initializeGraphState(nextProps, this.state) : this.state;
314314
const newConfig = nextProps.config || {};
315-
const configUpdated =
316-
newConfig && !utils.isObjectEmpty(newConfig) && !utils.isDeepEqual(newConfig, this.state.config);
315+
const { configUpdated, d3ConfigUpdated } = graphHelper.checkForGraphConfigChanges(nextProps, this.state);
317316
const config = configUpdated ? utils.merge(DEFAULT_CONFIG, newConfig) : this.state.config;
318317

319318
// in order to properly update graph data we need to pause eventual d3 ongoing animations
@@ -325,6 +324,7 @@ export default class Graph extends React.Component {
325324
...state,
326325
config,
327326
configUpdated,
327+
d3ConfigUpdated,
328328
newGraphElements,
329329
transform
330330
});
@@ -334,10 +334,10 @@ export default class Graph extends React.Component {
334334
// if the property staticGraph was activated we want to stop possible ongoing simulation
335335
this.state.config.staticGraph && this.pauseSimulation();
336336

337-
if (!this.state.config.staticGraph && this.state.newGraphElements) {
337+
if (!this.state.config.staticGraph && (this.state.newGraphElements || this.state.d3ConfigUpdated)) {
338338
this._graphForcesConfig();
339339
this.restartSimulation();
340-
this.setState({ newGraphElements: false });
340+
this.setState({ newGraphElements: false, d3ConfigUpdated: false });
341341
}
342342

343343
if (this.state.configUpdated) {

src/components/graph/graph.helper.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,23 @@ function checkForGraphElementsChanges(nextProps, currentState) {
392392
return { graphElementsUpdated, newGraphElements };
393393
}
394394

395+
/**
396+
* Logic to check for changes in graph config.
397+
* @param {Object} nextProps - nextProps that graph will receive.
398+
* @param {Object} currentState - the current state of the graph.
399+
* @returns {Object.<string, boolean>} returns object containing update check flags:
400+
* - configUpdated - global flag that indicates if any property was updated.
401+
* - d3ConfigUpdated - specific flag that indicates changes in d3 configurations.
402+
*/
403+
function checkForGraphConfigChanges(nextProps, currentState) {
404+
const newConfig = nextProps.config || {};
405+
const configUpdated =
406+
newConfig && !utils.isObjectEmpty(newConfig) && !utils.isDeepEqual(newConfig, currentState.config);
407+
const d3ConfigUpdated = newConfig && newConfig.d3 && !utils.isDeepEqual(newConfig.d3, currentState.config.d3);
408+
409+
return { configUpdated, d3ConfigUpdated };
410+
}
411+
395412
/**
396413
* Encapsulates common procedures to initialize graph.
397414
* @param {Object} props - Graph component props, object that holds data, id and config.
@@ -579,9 +596,9 @@ function getNodeCardinality(nodeId, linksMatrix) {
579596
}
580597

581598
export {
582-
NODE_PROPS_WHITELIST,
583599
buildLinkProps,
584600
buildNodeProps,
601+
checkForGraphConfigChanges,
585602
checkForGraphElementsChanges,
586603
disconnectLeafNodeConnections,
587604
getLeafNodeConnections,

0 commit comments

Comments
 (0)