Skip to content

Commit c13d136

Browse files
MathiasKochdanielcaldas
authored andcommitted
Add onClick handler to the canvas, for use in eg. unselecting nodes (#113)
* Add onClick handler to the canvas, for use in eg. unselecting nodes * Discard sandbox/rd3g.sandbox.bundle* changes * Update jsdoc to reflect that onClick event is an object, not a string * Uppercase Object in JSDoc * Add onClickGraph to README * Added onClickGraph documentation to JSDocs of Graph component
1 parent e2f5807 commit c13d136

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ const myConfig = {
6565
};
6666

6767
// graph event callbacks
68+
const onClickGraph = function() {
69+
window.alert(`Clicked the graph background`);
70+
};
71+
6872
const onClickNode = function(nodeId) {
6973
window.alert(`Clicked node ${nodeId}`);
7074
};
@@ -94,6 +98,7 @@ const onMouseOutLink = function(source, target) {
9498
data={data}
9599
config={myConfig}
96100
onClickNode={onClickNode}
101+
onClickGraph={onClickGraph}
97102
onClickLink={onClickLink}
98103
onMouseOverNode={onMouseOverNode}
99104
onMouseOutNode={onMouseOutNode}

sandbox/Sandbox.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export default class Sandbox extends React.Component {
5050
};
5151
}
5252

53+
onClickGraph = () => console.info(`Clicked the graph`);
54+
5355
onClickNode = id => !this.state.config.collapsible && window.alert(`Clicked node ${id}`);
5456

5557
onClickLink = (source, target) => window.alert(`Clicked link between ${source} and ${target}`);
@@ -294,6 +296,7 @@ export default class Sandbox extends React.Component {
294296
data,
295297
config: this.state.config,
296298
onClickNode: this.onClickNode,
299+
onClickGraph: this.onClickGraph,
297300
onClickLink: this.onClickLink,
298301
onMouseOverNode: this.onMouseOverNode,
299302
onMouseOutNode: this.onMouseOutNode,

src/components/graph/Graph.jsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ import utils from '../../utils';
4949
* };
5050
*
5151
* // graph event callbacks
52+
* const onClickGraph = function() {
53+
* window.alert('Clicked the graph background');
54+
* };
55+
*
5256
* const onClickNode = function(nodeId) {
5357
* window.alert('Clicked node ${nodeId}');
5458
* };
@@ -77,6 +81,7 @@ import utils from '../../utils';
7781
* id='graph-id' // id is mandatory, if no id is defined rd3g will throw an error
7882
* data={data}
7983
* config={myConfig}
84+
* onClickGraph={onClickGraph}
8085
* onClickNode={onClickNode}
8186
* onClickLink={onClickLink}
8287
* onMouseOverNode={onMouseOverNode}
@@ -378,6 +383,23 @@ export default class Graph extends React.Component {
378383
this.props.onClickNode && this.props.onClickNode(clickedNodeId);
379384
};
380385

386+
/**
387+
* Calls the callback passed to the component.
388+
* @param {Object} e - The event of onClick handler.
389+
* @returns {undefined}
390+
*/
391+
onClickGraph = e => {
392+
// Only trigger the graph onClickHandler, if not clicked a node or link.
393+
// toUpperCase() is added as a precaution, as the documentation says tagName should always
394+
// return in UPPERCASE, but chrome returns lowercase
395+
if (
396+
e.target.tagName.toUpperCase() === 'SVG' &&
397+
e.target.attributes.name.value === `svg-container-${this.state.id}`
398+
) {
399+
this.props.onClickGraph && this.props.onClickGraph();
400+
}
401+
};
402+
381403
render() {
382404
const { nodes, links } = graphRenderer.buildGraph(
383405
this.state.nodes,
@@ -406,7 +428,7 @@ export default class Graph extends React.Component {
406428

407429
return (
408430
<div id={`${this.state.id}-${CONST.GRAPH_WRAPPER_ID}`}>
409-
<svg style={svgStyle}>
431+
<svg name={`svg-container-${this.state.id}`} style={svgStyle} onClick={this.onClickGraph}>
410432
<g id={`${this.state.id}-${CONST.GRAPH_CONTAINER_ID}`}>
411433
{links}
412434
{nodes}

test/snapshot/graph/__snapshots__/graph.snapshot.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ exports[`Snapshot - Graph Component should match snapshot 1`] = `
55
id="graphId-graph-wrapper"
66
>
77
<svg
8+
name="svg-container-graphId"
9+
onClick={[Function]}
810
style={
911
Object {
1012
"height": 600,

0 commit comments

Comments
 (0)