Skip to content

Commit 93c7c62

Browse files
committed
style: minor code style adjustments + comments (followup to #381)
1 parent 9ddea93 commit 93c7c62

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/Tree/index.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,29 +385,28 @@ class Tree extends React.Component<TreeProps, TreeState> {
385385

386386
/**
387387
* Takes a hierarchy point node and centers the node on the screen
388-
* if the dimensions parameter is passed to the tree.
388+
* if the dimensions parameter is passed to `Tree`.
389389
*
390390
* This code is adapted from Rob Schmuecker's centerNode method.
391-
* Link: http://www.robschmuecker.com/d3-js-drag-and-drop-zoomable-tree/
392-
*
393-
* @param hierarchyPointNode
391+
* Link: http://bl.ocks.org/robschmuecker/7880033
394392
*/
395393
centerNode = (hierarchyPointNode: HierarchyPointNode<TreeNodeDatum>) => {
396-
// if the dimensions are given
397-
if (this.props.dimensions) {
394+
const { dimensions, orientation, zoom } = this.props;
395+
if (dimensions) {
398396
const g = select(`.${this.gInstanceRef}`);
399397
const svg = select(`.${this.svgInstanceRef}`);
400398
const scale = this.state.d3.scale;
401399

402-
let x, y;
400+
let x: number;
401+
let y: number;
403402
// if the orientation is horizontal, calculate the variables inverted (x->y, y->x)
404-
if (this.props.orientation === 'horizontal') {
405-
y = -hierarchyPointNode.x * scale + this.props.dimensions.width / 2;
406-
x = -hierarchyPointNode.y * scale + this.props.dimensions.height / 2;
403+
if (orientation === 'horizontal') {
404+
y = -hierarchyPointNode.x * scale + dimensions.width / 2;
405+
x = -hierarchyPointNode.y * scale + dimensions.height / 2;
407406
} else {
408407
// else, calculate the variables normally (x->x, y->y)
409-
x = -hierarchyPointNode.x * scale + this.props.dimensions.width / 2;
410-
y = -hierarchyPointNode.y * scale + this.props.dimensions.height / 2;
408+
x = -hierarchyPointNode.x * scale + dimensions.width / 2;
409+
y = -hierarchyPointNode.y * scale + dimensions.height / 2;
411410
}
412411
//@ts-ignore
413412
g.transition()
@@ -416,7 +415,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
416415
// Sets the viewport to the new center so that it does not jump back to original
417416
// coordinates when dragged/zoomed
418417
//@ts-ignore
419-
svg.call(d3zoom().transform, zoomIdentity.translate(x, y).scale(this.props.zoom));
418+
svg.call(d3zoom().transform, zoomIdentity.translate(x, y).scale(zoom));
420419
}
421420
};
422421

@@ -500,7 +499,6 @@ class Tree extends React.Component<TreeProps, TreeState> {
500499
orientation,
501500
pathFunc,
502501
transitionDuration,
503-
zoomable,
504502
nodeSize,
505503
depthFactor,
506504
initialDepth,

src/Tree/types.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ export interface TreeProps {
122122
*/
123123
translate?: Point;
124124

125+
/**
126+
* Enables the centering of nodes on click by providing the dimensions of the tree container,
127+
* e.g. via {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect | `getBoundingClientRect()`}.
128+
*
129+
* If dimensions are given: node will center on click. If not, node will not center on click.
130+
*/
131+
dimensions?: {
132+
width: number;
133+
height: number;
134+
};
135+
125136
/**
126137
* The draw function (or `d`) used to render `path`/`link` elements. Accepts a predefined
127138
* `PathFunctionOption` or a user-defined `PathFunction`.
@@ -285,14 +296,4 @@ export interface TreeProps {
285296
* {@link Tree.defaultProps.hasInteractiveNodes | Default value}
286297
*/
287298
hasInteractiveNodes?: boolean;
288-
289-
/**
290-
* Gives the user the ability to pass dimensions of the tree container to center a node on click
291-
*
292-
* If dimensions are given: node will center on click. If not, node will not center on click.
293-
*/
294-
dimensions?: {
295-
height: number;
296-
width: number;
297-
};
298299
}

0 commit comments

Comments
 (0)