Skip to content

Commit 3b39377

Browse files
committed
fix: ensures branches don't expand completely on first click (#47)
After the initial render with `initialDepth` set, clicking a node which contained multiple sublevels would expand the entire subtree. This was due to an unnecessary special case in the tree generation step, which meant not all possible child nodes could be collapsed when the tree was generated.
1 parent 4f8b610 commit 3b39377

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/Tree/index.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,7 @@ class Tree extends React.Component {
439439
.separation(
440440
(a, b) => (a.parent.id === b.parent.id ? separation.siblings : separation.nonSiblings),
441441
)
442-
.children(d => {
443-
if (initialDepth !== undefined && isInitialRenderForDataset) {
444-
// If `initialDepth` is defined, a node's depth determines
445-
// whether we append `children` on the first render.
446-
return d.depth >= initialDepth ? null : d._children;
447-
}
448-
// Node's `collapsed` property determines appending of `children` for subsequent renders.
449-
return d._collapsed ? null : d._children;
450-
});
442+
.children(d => (d._collapsed ? null : d._children));
451443

452444
const rootNode = this.state.data[0];
453445
let nodes = tree.nodes(rootNode);

src/Tree/tests/index.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@ describe('<Tree />', () => {
286286
const renderedComponent = shallow(<Tree data={mockTree_D1N2_D2N2} initialDepth={0} />);
287287
expect(renderedComponent.find(Node).length).toBe(1);
288288
});
289+
290+
it('increases tree depth by no more than 1 level when a node is expanded after initialising to `initialDepth`', () => {
291+
const renderedComponent = mount(<Tree data={mockTree_D1N2_D2N2} initialDepth={0} />);
292+
expect(renderedComponent.find(Node).length).toBe(1);
293+
renderedComponent
294+
.find(Node)
295+
.first()
296+
.find('circle')
297+
.simulate('click');
298+
expect(renderedComponent.find(Node).length).toBe(3);
299+
});
289300
});
290301

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

0 commit comments

Comments
 (0)