forked from bkrem/react-d3-tree
/
index.tsx
163 lines (142 loc) · 4.63 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, { SyntheticEvent } from 'react';
import { linkHorizontal, linkVertical } from 'd3-shape';
import { DagLink, DagNode } from 'd3-dag';
import { select } from 'd3-selection';
import {
Orientation,
PathFunctionOption,
PathFunction,
TreeNodeDatum,
PathClassFunction,
} from '../types/common';
type LinkEventHandler = (
source: DagNode<TreeNodeDatum>,
target: DagNode<TreeNodeDatum>,
evt: SyntheticEvent
) => void;
interface LinkProps {
linkData: DagLink<TreeNodeDatum>;
orientation: Orientation;
pathFunc: PathFunctionOption | PathFunction;
pathClassFunc?: PathClassFunction;
enableLegacyTransitions: boolean;
transitionDuration: number;
onClick: LinkEventHandler;
onMouseOver: LinkEventHandler;
onMouseOut: LinkEventHandler;
}
type LinkState = {
initialStyle: { opacity: number };
};
export default class Link extends React.PureComponent<LinkProps, LinkState> {
private linkRef: SVGPathElement = null;
state = {
initialStyle: {
opacity: 0,
},
};
componentDidMount() {
this.applyOpacity(1, this.props.transitionDuration);
}
componentWillLeave(done) {
this.applyOpacity(0, this.props.transitionDuration, done);
}
applyOpacity(
opacity: number,
transitionDuration: LinkProps['transitionDuration'],
done = () => {}
) {
if (this.props.enableLegacyTransitions) {
select(this.linkRef)
// @ts-ignore
.transition()
.duration(transitionDuration)
.style('opacity', opacity)
.on('end', done);
} else {
select(this.linkRef).style('opacity', opacity);
done();
}
}
drawStepPath(linkData: LinkProps['linkData'], orientation: LinkProps['orientation']) {
const { source, target } = linkData;
const deltaY = target.y - source.y;
return orientation === 'horizontal'
? `M${source.y},${source.x} H${source.y + deltaY / 2} V${target.x} H${target.y}`
: `M${source.x},${source.y} V${source.y + deltaY / 2} H${target.x} V${target.y}`;
}
drawDiagonalPath(linkData: LinkProps['linkData'], orientation: LinkProps['orientation']) {
const { source, target } = linkData;
return orientation === 'horizontal'
? linkHorizontal()({
source: [source.y, source.x],
target: [target.y, target.x],
})
: linkVertical()({
source: [source.x, source.y],
target: [target.x, target.y],
});
}
drawStraightPath(linkData: LinkProps['linkData'], orientation: LinkProps['orientation']) {
const { source, target } = linkData;
return orientation === 'horizontal'
? `M${source.y},${source.x}L${target.y},${target.x}`
: `M${source.x},${source.y}L${target.x},${target.y}`;
}
drawElbowPath(linkData: LinkProps['linkData'], orientation: LinkProps['orientation']) {
return orientation === 'horizontal'
? `M${linkData.source.y},${linkData.source.x}V${linkData.target.x}H${linkData.target.y}`
: `M${linkData.source.x},${linkData.source.y}V${linkData.target.y}H${linkData.target.x}`;
}
drawPath() {
const { linkData, orientation, pathFunc } = this.props;
if (typeof pathFunc === 'function') {
return pathFunc(linkData, orientation);
}
if (pathFunc === 'elbow') {
return this.drawElbowPath(linkData, orientation);
}
if (pathFunc === 'straight') {
return this.drawStraightPath(linkData, orientation);
}
if (pathFunc === 'step') {
return this.drawStepPath(linkData, orientation);
}
return this.drawDiagonalPath(linkData, orientation);
}
getClassNames() {
const { linkData, orientation, pathClassFunc } = this.props;
const classNames = ['rd3dag-link'];
if (typeof pathClassFunc === 'function') {
classNames.push(pathClassFunc(linkData, orientation));
}
return classNames.join(' ').trim();
}
handleOnClick = evt => {
this.props.onClick(this.props.linkData.source, this.props.linkData.target, evt);
};
handleOnMouseOver = evt => {
this.props.onMouseOver(this.props.linkData.source, this.props.linkData.target, evt);
};
handleOnMouseOut = evt => {
this.props.onMouseOut(this.props.linkData.source, this.props.linkData.target, evt);
};
render() {
const { linkData } = this.props;
return (
<path
ref={l => {
this.linkRef = l;
}}
style={{ ...this.state.initialStyle }}
className={this.getClassNames()}
d={this.drawPath()}
onClick={this.handleOnClick}
onMouseOver={this.handleOnMouseOver}
onMouseOut={this.handleOnMouseOut}
data-source-id={linkData.source.data.__rd3dag.id}
data-target-id={linkData.target.data.__rd3dag.id}
/>
);
}
}