Skip to content

Commit f182f33

Browse files
feature: expose node information to node related callbacks (#385)
* feat: pass node data to all event handlers * docs: update jsdoc block for Graph component * feat: in sandbox, display node position in node events * docs: add missing param in onRightClickNode docs
1 parent ca84242 commit f182f33

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

sandbox/Sandbox.jsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export default class Sandbox extends React.Component {
6666

6767
onClickGraph = () => toast("Clicked the graph");
6868

69-
onClickNode = id => {
70-
toast(`Clicked node ${id}`);
69+
onClickNode = (id, node) => {
70+
toast(`Clicked node ${id} in position (${node.x}, ${node.y})`);
7171
// NOTE: below sample implementation for focusAnimation when clicking on node
7272
// this.setState({
7373
// data: {
@@ -77,11 +77,13 @@ export default class Sandbox extends React.Component {
7777
// });
7878
};
7979

80-
onDoubleClickNode = id => toast(`Double clicked node ${id}`);
80+
onDoubleClickNode = (id, node) => {
81+
toast(`Double clicked node ${id} in position (${node.x}, ${node.y})`);
82+
};
8183

82-
onRightClickNode = (event, id) => {
84+
onRightClickNode = (event, id, node) => {
8385
event.preventDefault();
84-
toast(`Right clicked node ${id}`);
86+
toast(`Right clicked node ${id} in position (${node.x}, ${node.y})`);
8587
};
8688

8789
onClickLink = (source, target) => toast(`Clicked link between ${source} and ${target}`);
@@ -91,9 +93,13 @@ export default class Sandbox extends React.Component {
9193
toast(`Right clicked link between ${source} and ${target}`);
9294
};
9395

94-
onMouseOverNode = id => console.info(`Do something when mouse is over node (${id})`);
96+
onMouseOverNode = (id, node) => {
97+
console.info(`Do something when mouse is over node ${id} in position (${node.x}, ${node.y})`);
98+
};
9599

96-
onMouseOutNode = id => console.info(`Do something when mouse is out of node (${id})`);
100+
onMouseOutNode = (id, node) => {
101+
console.info(`Do something when mouse is out of node ${id} in position (${node.x}, ${node.y})`);
102+
};
97103

98104
onMouseOverLink = (source, target) =>
99105
console.info(`Do something when mouse is over link between ${source} and ${target}`);

src/components/graph/Graph.jsx

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,24 @@ import { merge, debounce, throwErr } from "../../utils";
6262
* window.alert('Clicked the graph background');
6363
* };
6464
*
65-
* const onClickNode = function(nodeId) {
66-
* window.alert('Clicked node ${nodeId}');
65+
* const onClickNode = function(nodeId, node) {
66+
* window.alert('Clicked node ${nodeId} in position (${node.x}, ${node.y})');
6767
* };
6868
*
69-
* const onDoubleClickNode = function(nodeId) {
70-
* window.alert('Double clicked node ${nodeId}');
69+
* const onDoubleClickNode = function(nodeId, node) {
70+
* window.alert('Double clicked node ${nodeId} in position (${node.x}, ${node.y})');
7171
* };
7272
*
73-
* const onRightClickNode = function(event, nodeId) {
74-
* window.alert('Right clicked node ${nodeId}');
73+
* const onRightClickNode = function(event, nodeId, node) {
74+
* window.alert('Right clicked node ${nodeId} in position (${node.x}, ${node.y})');
7575
* };
7676
*
77-
* const onMouseOverNode = function(nodeId) {
78-
* window.alert(`Mouse over node ${nodeId}`);
77+
* const onMouseOverNode = function(nodeId, node) {
78+
* window.alert(`Mouse over node ${nodeId} in position (${node.x}, ${node.y})`);
7979
* };
8080
*
81-
* const onMouseOutNode = function(nodeId) {
82-
* window.alert(`Mouse out node ${nodeId}`);
81+
* const onMouseOutNode = function(nodeId, node) {
82+
* window.alert(`Mouse out node ${nodeId} in position (${node.x}, ${node.y})`);
8383
* };
8484
*
8585
* const onClickLink = function(source, target) {
@@ -347,6 +347,8 @@ export default class Graph extends React.Component {
347347
* @returns {undefined}
348348
*/
349349
onClickNode = clickedNodeId => {
350+
const clickedNode = this.state.nodes[clickedNodeId];
351+
350352
if (this.state.config.collapsible) {
351353
const leafConnections = getTargetLeafConnections(clickedNodeId, this.state.links, this.state.config);
352354
const links = toggleLinksMatrixConnections(this.state.links, leafConnections, this.state.config);
@@ -367,7 +369,7 @@ export default class Graph extends React.Component {
367369
d3Links,
368370
},
369371
() => {
370-
this.props.onClickNode && this.props.onClickNode(clickedNodeId);
372+
this.props.onClickNode && this.props.onClickNode(clickedNodeId, clickedNode);
371373

372374
if (isExpanding) {
373375
this._graphNodeDragConfig();
@@ -377,16 +379,27 @@ export default class Graph extends React.Component {
377379
} else {
378380
if (!this.nodeClickTimer) {
379381
this.nodeClickTimer = setTimeout(() => {
380-
this.props.onClickNode && this.props.onClickNode(clickedNodeId);
382+
this.props.onClickNode && this.props.onClickNode(clickedNodeId, clickedNode);
381383
this.nodeClickTimer = null;
382384
}, CONST.TTL_DOUBLE_CLICK_IN_MS);
383385
} else {
384-
this.props.onDoubleClickNode && this.props.onDoubleClickNode(clickedNodeId);
386+
this.props.onDoubleClickNode && this.props.onDoubleClickNode(clickedNodeId, clickedNode);
385387
this.nodeClickTimer = clearTimeout(this.nodeClickTimer);
386388
}
387389
}
388390
};
389391

392+
/**
393+
* Handles right click event on a node.
394+
* @param {Object} event - Right click event.
395+
* @param {string} id - id of the node that participates in the event.
396+
* @returns {undefined}
397+
*/
398+
onRightClickNode = (event, id) => {
399+
const clickedNode = this.state.nodes[id];
400+
this.props.onRightClickNode && this.props.onRightClickNode(event, id, clickedNode);
401+
};
402+
390403
/**
391404
* Handles mouse over node event.
392405
* @param {string} id - id of the node that participates in the event.
@@ -397,7 +410,8 @@ export default class Graph extends React.Component {
397410
return;
398411
}
399412

400-
this.props.onMouseOverNode && this.props.onMouseOverNode(id);
413+
const clickedNode = this.state.nodes[id];
414+
this.props.onMouseOverNode && this.props.onMouseOverNode(id, clickedNode);
401415

402416
this.state.config.nodeHighlightBehavior && this._setNodeHighlightedValue(id, true);
403417
};
@@ -412,7 +426,8 @@ export default class Graph extends React.Component {
412426
return;
413427
}
414428

415-
this.props.onMouseOutNode && this.props.onMouseOutNode(id);
429+
const clickedNode = this.state.nodes[id];
430+
this.props.onMouseOutNode && this.props.onMouseOutNode(id, clickedNode);
416431

417432
this.state.config.nodeHighlightBehavior && this._setNodeHighlightedValue(id, false);
418433
};
@@ -623,7 +638,7 @@ export default class Graph extends React.Component {
623638
{
624639
onClickNode: this.onClickNode,
625640
onDoubleClickNode: this.onDoubleClickNode,
626-
onRightClickNode: this.props.onRightClickNode,
641+
onRightClickNode: this.onRightClickNode,
627642
onMouseOverNode: this.onMouseOverNode,
628643
onMouseOut: this.onMouseOutNode,
629644
},

0 commit comments

Comments
 (0)