Skip to content

Commit dd0e208

Browse files
authored
feature/custom-classnames – added ability to add custom classes (#323)
1 parent 2257384 commit dd0e208

File tree

6 files changed

+43
-5
lines changed

6 files changed

+43
-5
lines changed

docs/components/Link.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ type: `func`
3131
type: `enum('horizontal'|'vertical')`
3232

3333

34+
### `pathClass`
35+
36+
type: `func`
37+
38+
3439
### `pathFunc` (required)
3540

3641
type: `union(enum|func)`

docs/components/Tree.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ type: `number`
1818
defaultValue: `undefined`
1919

2020

21+
### `className`
22+
23+
type: `string`
24+
defaultValue: `undefined`
25+
26+
2127
### `collapsible`
2228

2329
type: `bool`
@@ -112,6 +118,11 @@ type: `enum('horizontal'|'vertical')`
112118
defaultValue: `'horizontal'`
113119

114120

121+
### `pathClass`
122+
123+
type: `func`
124+
125+
115126
### `pathFunc`
116127

117128
type: `union(enum|func)`

lib/react-d3-tree.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react-d3-tree.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare module "react-d3-tree" {
1717
type ReactD3TreeTranslate = {x: number, y: number}
1818

1919
type ReactD3TreeProps = {
20+
className?: string,
2021
data: ReactD3TreeItem[] | ReactD3TreeItem,
2122
nodeSvgShape?: NodeSvgShape,
2223
nodeLabelComponent?: object,
@@ -30,6 +31,7 @@ declare module "react-d3-tree" {
3031
orientation?: "horizontal" | "vertical",
3132
translate?: Partial<ReactD3TreeTranslate>,
3233
pathFunc?: ("diagonal" | "elbow" | "straight" | "step") | ((...args: any[]) => any),
34+
pathClass?: ((...args: any[]) => any),
3335
transitionDuration?: number,
3436
depthFactor?: number,
3537
collapsible?: boolean,
@@ -60,7 +62,7 @@ declare module "react-d3-tree" {
6062
};
6163

6264
var Tree: React.ComponentClass<ReactD3TreeProps>;
63-
65+
6466
export {Tree, ReactD3TreeProps, ReactD3TreeItem, ReactD3TreeTranslate, NodeSvgShape};
6567
export default Tree;
66-
}
68+
}

src/Link/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ export default class Link extends React.PureComponent {
9898
return this.drawDiagonalPath(linkData, orientation);
9999
}
100100

101+
getClassNames() {
102+
const { linkData, pathClass } = this.props;
103+
const classNames = ['linkBase'];
104+
105+
if (typeof pathClass === 'function') {
106+
classNames.push(pathClass(linkData));
107+
}
108+
109+
return classNames.join(' ');
110+
}
111+
101112
handleOnClick = evt => {
102113
this.props.onClick(this.props.linkData.source, this.props.linkData.target, evt);
103114
};
@@ -118,7 +129,7 @@ export default class Link extends React.PureComponent {
118129
this.link = l;
119130
}}
120131
style={{ ...this.state.initialStyle, ...styles }}
121-
className="linkBase"
132+
className={this.getClassNames()}
122133
d={this.drawPath()}
123134
onClick={this.handleOnClick}
124135
onMouseOver={this.handleOnMouseOver}
@@ -132,11 +143,13 @@ export default class Link extends React.PureComponent {
132143

133144
Link.defaultProps = {
134145
styles: {},
146+
pathClass: undefined,
135147
};
136148

137149
Link.propTypes = {
138150
linkData: T.object.isRequired,
139151
orientation: T.oneOf(['horizontal', 'vertical']).isRequired,
152+
pathClass: T.func,
140153
pathFunc: T.oneOfType([T.oneOf(['diagonal', 'elbow', 'straight', 'step']), T.func]).isRequired,
141154
transitionDuration: T.number.isRequired,
142155
onClick: T.func.isRequired,

src/Tree/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,12 @@ class Tree extends React.Component {
485485
const { nodes, links } = this.generateTree();
486486
const { rd3tSvgClassName, rd3tGClassName } = this.state;
487487
const {
488+
className,
488489
nodeSvgShape,
489490
nodeLabelComponent,
490491
orientation,
491492
pathFunc,
493+
pathClass,
492494
transitionDuration,
493495
zoomable,
494496
textLayout,
@@ -504,7 +506,7 @@ class Tree extends React.Component {
504506
const subscriptions = { ...nodeSize, ...separation, depthFactor, initialDepth };
505507
return (
506508
<div className={`rd3t-tree-container ${zoomable ? 'rd3t-grabbable' : undefined}`}>
507-
<svg className={rd3tSvgClassName} width="100%" height="100%">
509+
<svg className={[rd3tSvgClassName, className].join(' ')} width="100%" height="100%">
508510
<NodeWrapper
509511
transitionDuration={transitionDuration}
510512
component="g"
@@ -516,6 +518,7 @@ class Tree extends React.Component {
516518
key={uuid.v4()}
517519
orientation={orientation}
518520
pathFunc={pathFunc}
521+
pathClass={pathClass}
519522
linkData={linkData}
520523
onClick={this.handleOnLinkClickCb}
521524
onMouseOver={this.handleOnLinkMouseOverCb}
@@ -554,6 +557,7 @@ class Tree extends React.Component {
554557
}
555558

556559
Tree.defaultProps = {
560+
className: undefined,
557561
nodeSvgShape: {
558562
shape: 'circle',
559563
shapeProps: {
@@ -570,6 +574,7 @@ Tree.defaultProps = {
570574
onUpdate: undefined,
571575
orientation: 'horizontal',
572576
translate: { x: 0, y: 0 },
577+
pathClass: undefined,
573578
pathFunc: 'diagonal',
574579
transitionDuration: 500,
575580
depthFactor: undefined,
@@ -594,6 +599,7 @@ Tree.defaultProps = {
594599
};
595600

596601
Tree.propTypes = {
602+
className: T.string,
597603
data: T.oneOfType([T.array, T.object]).isRequired,
598604
nodeSvgShape: T.shape({
599605
shape: T.string,
@@ -612,6 +618,7 @@ Tree.propTypes = {
612618
x: T.number,
613619
y: T.number,
614620
}),
621+
pathClass: T.func,
615622
pathFunc: T.oneOfType([T.oneOf(['diagonal', 'elbow', 'straight', 'step']), T.func]),
616623
transitionDuration: T.number,
617624
depthFactor: T.number,

0 commit comments

Comments
 (0)