Skip to content

Commit e5cd161

Browse files
committed
fix(initial depth): ensures branches do not fully expand on first click
1 parent 747726e commit e5cd161

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/Tree/index.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -399,16 +399,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
399399
);
400400

401401
const rootNode = tree(
402-
hierarchy(this.state.data[0], d => {
403-
if (initialDepth !== undefined && isInitialRenderForDataset) {
404-
// If `initialDepth` is defined, a node's depth determines
405-
// whether we append `children` on the first render.
406-
return d.__rd3t.depth >= initialDepth ? null : d.children;
407-
} else {
408-
// Node's `collapsed` property determines appending of `children` for subsequent renders.
409-
return d.__rd3t.collapsed ? null : d.children;
410-
}
411-
})
402+
hierarchy(this.state.data[0], d => (d.__rd3t.collapsed ? null : d.children))
412403
);
413404
let nodes = rootNode.descendants();
414405
const links = rootNode.links();
@@ -498,7 +489,6 @@ class Tree extends React.Component<TreeProps, TreeState> {
498489
transform={`translate(${translate.x},${translate.y}) scale(${scale})`}
499490
>
500491
{links.map((linkData, i) => {
501-
// console.log(linkData);
502492
return (
503493
<Link
504494
key={'link-' + i}
@@ -516,7 +506,6 @@ class Tree extends React.Component<TreeProps, TreeState> {
516506
})}
517507

518508
{nodes.map(({ data, x, y, parent, ...rest }, i) => {
519-
// console.log({ data, x, y, parent, ...rest });
520509
return (
521510
<Node
522511
key={'node-' + i}

src/Tree/tests/index.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ describe('<Tree />', () => {
245245
const renderedComponent = shallow(<Tree data={mockTree_D1N2_D2N2} initialDepth={0} />);
246246
expect(renderedComponent.find(Node).length).toBe(1);
247247
});
248+
249+
it('increases tree depth by no more than 1 level when a node is expanded after initialising to `initialDepth`', () => {
250+
const renderedComponent = mount(<Tree data={mockTree_D1N2_D2N2} initialDepth={0} />);
251+
expect(renderedComponent.find(Node).length).toBe(1);
252+
renderedComponent
253+
.find(Node)
254+
.first()
255+
.find('circle')
256+
.simulate('click');
257+
expect(renderedComponent.find(Node).length).toBe(3);
258+
});
248259
});
249260

250261
describe('zoomable', () => {

0 commit comments

Comments
 (0)