Skip to content

Commit e79090d

Browse files
committed
Refactors style props to simplify API (#11)
1 parent 8cbe737 commit e79090d

File tree

4 files changed

+62
-53
lines changed

4 files changed

+62
-53
lines changed

src/Node/index.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,66 +65,62 @@ export default class Node extends React.Component {
6565
}
6666

6767
render() {
68-
const { nodeData } = this.props;
68+
const { nodeData, styles } = this.props;
69+
const nodeStyle = nodeData._children ?
70+
{ ...this.state.initialStyle, ...styles.node } :
71+
{ ...this.state.initialStyle, ...styles.leafNode };
6972

7073
return (
7174
<g
7275
id={nodeData.id}
7376
ref={(n) => { this.node = n; }}
74-
style={this.state.initialStyle}
77+
style={nodeStyle}
7578
className={nodeData._children ? 'nodeBase' : 'leafNodeBase'}
7679
transform={this.state.transform}
7780
onClick={this.handleClick}
7881
>
7982
<text
80-
className="primaryLabelBase"
83+
className="nodeNameBase"
8184
textAnchor={this.props.textAnchor}
82-
style={this.props.primaryLabelStyle}
85+
style={styles.name}
8386
x="10"
8487
y="-10"
8588
dy=".35em"
8689
>
87-
{this.props.primaryLabel}
90+
{this.props.name}
8891
</text>
8992

9093
<circle
9194
r={this.props.circleRadius}
92-
style={nodeData._children ? this.props.circleStyle : this.props.leafCircleStyle}
95+
style={nodeData._children ? styles.circle : styles.leafCircle}
9396
/>
9497

9598
<text
96-
className="secondaryLabelsBase"
99+
className="nodeAttributesBase"
97100
y="0"
98101
textAnchor={this.props.textAnchor}
99-
style={this.props.secondaryLabelsStyle}
102+
style={styles.attributes}
100103
>
101-
{this.props.secondaryLabels && Object.keys(this.props.secondaryLabels).map((labelKey) =>
102-
<tspan x="10" dy="1.2em" key={uuid.v4()}>
103-
{labelKey}: {this.props.secondaryLabels[labelKey]}
104-
</tspan>
105-
)}
104+
{
105+
this.props.attributes &&
106+
Object.keys(this.props.attributes).map((labelKey) =>
107+
<tspan x="10" dy="1.2em" key={uuid.v4()}>
108+
{labelKey}: {this.props.attributes[labelKey]}
109+
</tspan>
110+
)
111+
}
106112
</text>
107113
</g>
108114
);
109115
}
110116
}
111117

112118
Node.defaultProps = {
113-
depthFactor: undefined,
114119
circleRadius: 10,
115-
circleStyle: {
116-
stroke: '#000',
117-
strokeWidth: 2,
118-
fill: 'grey',
119-
},
120-
leafCircleStyle: {
121-
stroke: '#000',
122-
strokeWidth: 2,
123-
fill: 'transparent',
124-
},
120+
textAnchor: 'start',
121+
attributes: undefined,
125122
};
126123

127-
/* eslint-disable */
128124
Node.propTypes = {
129125
nodeData: PropTypes.object.isRequired,
130126
orientation: PropTypes.oneOf([
@@ -133,14 +129,9 @@ Node.propTypes = {
133129
]).isRequired,
134130
transitionDuration: PropTypes.number.isRequired,
135131
onClick: PropTypes.func.isRequired,
136-
depthFactor: PropTypes.number,
137-
primaryLabel: PropTypes.string,
138-
primaryLabelStyle: PropTypes.object,
139-
secondaryLabels: PropTypes.object,
140-
secondaryLabelsStyle: PropTypes.object,
132+
name: PropTypes.string.isRequired,
133+
attributes: PropTypes.object,
141134
textAnchor: PropTypes.string,
142135
circleRadius: PropTypes.number,
143-
circleStyle: PropTypes.object,
144-
leafCircleStyle: PropTypes.object,
136+
styles: PropTypes.object.isRequired,
145137
};
146-
/* eslint-enable */

src/Node/style.css

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
.nodeBase {
22
cursor: pointer;
3+
fill: #777;
4+
stroke: #000;
5+
stroke-width: 2;
36
}
47

58
.leafNodeBase {
69
cursor: pointer;
10+
fill: transparent;
11+
stroke: #000;
12+
stroke-width: 2;
713
}
814

9-
.primaryLabelBase {
10-
fill: #000;
15+
.nodeNameBase {
16+
stroke: #000;
17+
stroke-width: 1;
1118
}
1219

13-
.secondaryLabelsBase {
14-
fill: #777;
20+
.nodeAttributesBase {
21+
stroke: #777;
22+
stroke-width: 1;
1523
font-size: smaller;
16-
}
24+
}

src/Node/tests/index.test.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ describe('<Node />', () => {
1818

1919
const mockProps = {
2020
nodeData,
21+
name: nodeData.name,
2122
orientation: 'horizontal',
2223
transitionDuration: 500,
2324
onClick: () => {},
25+
styles: {},
2426
};
2527

2628

@@ -56,26 +58,25 @@ describe('<Node />', () => {
5658

5759

5860
it('applies correct <circle> style prop if `nodeData._children` is defined', () => {
59-
const leafCircleStyle = { fill: 'blue' };
60-
const circleStyle = { fill: 'green' };
61+
const leafCircle = { fill: 'blue' };
62+
const circle = { fill: 'green' };
63+
const styles = { circle, leafCircle };
6164
const noChildrenComponent = shallow(
6265
<Node
6366
{...mockProps}
64-
leafCircleStyle={leafCircleStyle}
65-
circleStyle={circleStyle}
67+
styles={styles}
6668
/>
6769
);
6870
const withChildrenComponent = shallow(
6971
<Node
7072
{...mockProps}
7173
nodeData={{ ...nodeData, _children: [] }}
72-
leafCircleStyle={leafCircleStyle}
73-
circleStyle={circleStyle}
74+
styles={styles}
7475
/>
7576
);
7677

77-
expect(noChildrenComponent.find('circle').prop('style')).toBe(leafCircleStyle);
78-
expect(withChildrenComponent.find('circle').prop('style')).toBe(circleStyle);
78+
expect(noChildrenComponent.find('circle').prop('style')).toBe(leafCircle);
79+
expect(withChildrenComponent.find('circle').prop('style')).toBe(circle);
7980
});
8081

8182

@@ -122,17 +123,17 @@ describe('<Node />', () => {
122123
});
123124

124125

125-
it('maps each `props.secondaryLabels` to a <tspan> element', () => {
126+
it('maps each `props.attributes` to a <tspan> element', () => {
126127
const fixture = { keyA: 'valA', keyB: 'valB' };
127128
const renderedComponent = shallow(
128129
<Node
129130
{...mockProps}
130-
secondaryLabels={fixture}
131+
attributes={fixture}
131132
/>
132133
);
133134
const textNode = renderedComponent
134135
.find('text')
135-
.findWhere((n) => n.prop('className') === 'secondaryLabelsBase');
136+
.findWhere((n) => n.prop('className') === 'nodeAttributesBase');
136137

137138
expect(textNode.findWhere((n) =>
138139
n.text() === `keyA: ${fixture.keyA}`).length

src/Tree/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ export default class Tree extends React.Component {
230230
orientation,
231231
translate,
232232
pathFunc,
233-
depthFactor,
234233
transitionDuration,
235234
zoomable,
235+
styles,
236236
} = this.props;
237237

238238
return (
@@ -247,13 +247,13 @@ export default class Tree extends React.Component {
247247
<Node
248248
key={nodeData.id}
249249
orientation={orientation}
250-
depthFactor={depthFactor}
251250
transitionDuration={transitionDuration}
252251
textAnchor="start"
253252
nodeData={nodeData}
254-
primaryLabel={nodeData.name}
255-
secondaryLabels={nodeData.attributes}
253+
name={nodeData.name}
254+
attributes={nodeData.attributes}
256255
onClick={this.handleNodeToggle}
256+
styles={styles.nodes}
257257
/>
258258
)}
259259

@@ -264,6 +264,7 @@ export default class Tree extends React.Component {
264264
pathFunc={pathFunc}
265265
linkData={linkData}
266266
transitionDuration={transitionDuration}
267+
styles={styles.links}
267268
/>
268269
)}
269270
</TransitionGroup>
@@ -283,6 +284,10 @@ Tree.defaultProps = {
283284
initialDepth: undefined,
284285
zoomable: true,
285286
scaleExtent: { min: 0.1, max: 1 },
287+
styles: {
288+
nodes: {},
289+
links: {},
290+
},
286291
};
287292

288293
Tree.propTypes = {
@@ -308,4 +313,8 @@ Tree.propTypes = {
308313
min: PropTypes.number,
309314
max: PropTypes.number,
310315
}),
316+
styles: PropTypes.shape({
317+
nodes: PropTypes.object,
318+
links: PropTypes.object,
319+
}),
311320
};

0 commit comments

Comments
 (0)