Skip to content

Commit 5f8fad5

Browse files
committed
Merge branch 'feature/svg-shape-none'
2 parents 904c131 + 6c7f2ed commit 5f8fad5

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

.eslintrc.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ module.exports = {
4141
"import/no-named-as-default": 0,
4242
"import/no-unresolved": 2,
4343
"import/prefer-default-export": 0,
44-
"indent": [
45-
2,
46-
2,
47-
{
48-
"SwitchCase": 1
49-
}
50-
],
44+
// "indent": [
45+
// 2,
46+
// 2,
47+
// {
48+
// "SwitchCase": 1
49+
// }
50+
// ],
5151
"jsx-a11y/aria-props": 2,
5252
"jsx-a11y/heading-has-content": 0,
5353
"jsx-a11y/href-no-hash": 0,

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ const svgSquare = {
129129
<Tree data={myTreeData} nodeSvgShape={svgSquare}>
130130
```
131131

132+
To avoid rendering any node element, simply set `nodeSvgShape` to `{ shape: 'none' }`.
133+
132134
### Overridable `shapeProps`
133135
`shapeProps` is currently merged with `node.circle`/`leafNode.circle` (see [Styling](#styling)).
134136

src/Node/index.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ export default class Node extends React.Component {
7474
}
7575
}
7676

77+
renderNodeElement(nodeStyle) {
78+
const { circleRadius, nodeSvgShape } = this.props;
79+
/* TODO: DEPRECATE <circle /> */
80+
if (circleRadius) {
81+
return <circle r={circleRadius} style={nodeStyle.circle} />;
82+
}
83+
84+
return nodeSvgShape.shape === 'none'
85+
? null
86+
: React.createElement(nodeSvgShape.shape, {
87+
...nodeSvgShape.shapeProps,
88+
...nodeStyle.circle,
89+
});
90+
}
91+
7792
handleClick() {
7893
this.props.onClick(this.props.nodeData.id);
7994
}
@@ -96,14 +111,7 @@ export default class Node extends React.Component {
96111
}
97112

98113
render() {
99-
const {
100-
nodeData,
101-
nodeSvgShape,
102-
nodeSize,
103-
nodeLabelComponent,
104-
allowForeignObjects,
105-
styles,
106-
} = this.props;
114+
const { nodeData, nodeSize, nodeLabelComponent, allowForeignObjects, styles } = this.props;
107115
const nodeStyle = nodeData._children ? { ...styles.node } : { ...styles.leafNode };
108116
return (
109117
<g
@@ -118,15 +126,7 @@ export default class Node extends React.Component {
118126
onMouseOver={this.handleOnMouseOver}
119127
onMouseOut={this.handleOnMouseOut}
120128
>
121-
{/* TODO: DEPRECATE <circle /> */}
122-
{this.props.circleRadius ? (
123-
<circle r={this.props.circleRadius} style={nodeStyle.circle} />
124-
) : (
125-
React.createElement(nodeSvgShape.shape, {
126-
...nodeSvgShape.shapeProps,
127-
...nodeStyle.circle,
128-
})
129-
)}
129+
{this.renderNodeElement(nodeStyle)}
130130

131131
{allowForeignObjects && nodeLabelComponent ? (
132132
<ForeignObjectElement nodeData={nodeData} nodeSize={nodeSize} {...nodeLabelComponent} />

src/Node/tests/index.test.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,30 @@ describe('<Node />', () => {
209209
expect(nodeComponent.find('.nodeNameBase').prop('style')).toBe(fixture.node.name);
210210
});
211211

212+
describe('Node Element', () => {
213+
// TODO: DEPRECATE in v2
214+
it('renders legacy `<circle />` if `props.circleRadius` is defined', () => {
215+
const props = { ...mockProps, circleRadius: 99 };
216+
const renderedComponent = shallow(<Node {...props} />);
217+
218+
expect(renderedComponent.find('circle').length).toBe(1);
219+
expect(renderedComponent.find('circle').prop('r')).toBe(99);
220+
});
221+
222+
it('renders the appropriate SVG element if `props.nodeSvgShape` is defined', () => {
223+
const props = { ...mockProps, nodeSvgShape: { shape: 'rect', shapeProps: { y: 123 } } };
224+
const renderedComponent = shallow(<Node {...props} />);
225+
expect(renderedComponent.find('rect').length).toBe(1);
226+
expect(renderedComponent.find('rect').prop('y')).toBe(123);
227+
});
228+
229+
it('renders nothing if `nodeSvgShape.shape` is set to `none`', () => {
230+
const props = { ...mockProps, nodeSvgShape: { shape: 'none' } };
231+
const renderedComponent = shallow(<Node {...props} />);
232+
expect(renderedComponent.instance().renderNodeElement({})).toBe(null);
233+
});
234+
});
235+
212236
describe('Node Label', () => {
213237
it('renders a SvgTextElement by default', () => {
214238
const renderedComponent = shallow(<Node {...mockProps} />);
@@ -223,15 +247,6 @@ describe('<Node />', () => {
223247
});
224248
});
225249

226-
// TODO: DEPRECATE in v2
227-
it('falls back to legacy `<circle />` prop only if `circleRadius` is defined', () => {
228-
const props = { ...mockProps, circleRadius: 99 };
229-
const renderedComponent = shallow(<Node {...props} />);
230-
231-
expect(renderedComponent.find('circle').length).toBe(1);
232-
expect(renderedComponent.find('circle').prop('r')).toBe(99);
233-
});
234-
235250
// TODO: Find a way to meaningfully test `componentWillLeave`
236251

237252
// it('regresses to its parent coords when unmounting/leaving', () => {

0 commit comments

Comments
 (0)