Skip to content

Commit b9c1e63

Browse files
m0rtynbkrem
authored andcommitted
add func for stairlike links rendering (#250)
* add func for stairlike links rendering * Flip the ternary for fix orientation of links * rename type of path function from Stair to Step * fix typo
1 parent 365001e commit b9c1e63

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Link/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ export default class Link extends React.PureComponent {
3232
}
3333
}
3434

35+
drawStepPath(linkData, orientation) {
36+
const { source, target } = linkData;
37+
const deltaY = target.y - source.y;
38+
39+
return orientation === 'horizontal'
40+
? `M${source.y},${source.x} H${source.y + deltaY / 2} V${target.x} H${target.y}`
41+
: `M${source.x},${source.y} V${source.y + deltaY / 2} H${target.x} V${target.y}`;
42+
}
43+
3544
drawDiagonalPath(linkData, orientation) {
3645
const diagonal = svg
3746
.diagonal()
@@ -82,6 +91,10 @@ export default class Link extends React.PureComponent {
8291
return this.drawStraightPath(linkData, orientation);
8392
}
8493

94+
if (pathFunc === 'step') {
95+
return this.drawStepPath(linkData, orientation);
96+
}
97+
8598
return this.drawDiagonalPath(linkData, orientation);
8699
}
87100

@@ -124,7 +137,7 @@ Link.defaultProps = {
124137
Link.propTypes = {
125138
linkData: T.object.isRequired,
126139
orientation: T.oneOf(['horizontal', 'vertical']).isRequired,
127-
pathFunc: T.oneOfType([T.oneOf(['diagonal', 'elbow', 'straight']), T.func]).isRequired,
140+
pathFunc: T.oneOfType([T.oneOf(['diagonal', 'elbow', 'straight', 'step']), T.func]).isRequired,
128141
transitionDuration: T.number.isRequired,
129142
onClick: T.func.isRequired,
130143
onMouseOver: T.func.isRequired,

src/Link/tests/index.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('<Link />', () => {
3636
jest.spyOn(Link.prototype, 'drawDiagonalPath');
3737
jest.spyOn(Link.prototype, 'drawElbowPath');
3838
jest.spyOn(Link.prototype, 'drawStraightPath');
39+
jest.spyOn(Link.prototype, 'drawStepPath');
3940
jest.spyOn(Link.prototype, 'applyOpacity');
4041
jest.spyOn(pathFuncs, 'testPathFunc');
4142

@@ -70,13 +71,15 @@ describe('<Link />', () => {
7071
const diagonalComponent = shallow(<Link {...mockProps} />);
7172
const elbowComponent = shallow(<Link {...mockProps} pathFunc="elbow" />);
7273
const straightComponent = shallow(<Link {...mockProps} pathFunc="straight" />);
74+
const stepComponent = shallow(<Link {...mockProps} pathFunc="step" />);
7375
shallow(<Link {...mockProps} pathFunc={pathFuncs.testPathFunc} />);
7476

7577
expect(diagonalComponent.instance().drawDiagonalPath).toHaveBeenCalled();
7678
expect(elbowComponent.instance().drawElbowPath).toHaveBeenCalled();
7779
expect(straightComponent.instance().drawStraightPath).toHaveBeenCalled();
80+
expect(stepComponent.instance().drawStepPath).toHaveBeenCalled();
7881
expect(pathFuncs.testPathFunc).toHaveBeenCalled();
79-
expect(Link.prototype.drawPath).toHaveBeenCalledTimes(4);
82+
expect(Link.prototype.drawPath).toHaveBeenCalledTimes(5);
8083
});
8184

8285
it('returns an appropriate elbowPath according to `props.orientation`', () => {
@@ -111,6 +114,18 @@ describe('<Link />', () => {
111114
);
112115
});
113116

117+
it('return an appropriate stepPath according to `props.orientation`', () => {
118+
const { source, target } = linkData;
119+
const deltaY = target.y - source.y;
120+
121+
expect(Link.prototype.drawStepPath(linkData, 'horizontal')).toBe(
122+
`M${source.y},${source.x} H${source.y + deltaY / 2} V${target.x} H${target.y}`,
123+
);
124+
expect(Link.prototype.drawStepPath(linkData, 'vertical')).toBe(
125+
`M${source.x},${source.y} V${source.y + deltaY / 2} H${target.x} V${target.y}`,
126+
);
127+
});
128+
114129
it('fades in once it has been mounted', () => {
115130
const fixture = 1;
116131
const renderedComponent = mount(<Link {...mockProps} />);

0 commit comments

Comments
 (0)