Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collapse open nodes when opening new one #46

Closed
RohanDamani opened this issue Oct 27, 2017 · 7 comments
Closed

Collapse open nodes when opening new one #46

RohanDamani opened this issue Oct 27, 2017 · 7 comments

Comments

@RohanDamani
Copy link
Contributor

The ability to collapse all open nodes on the same depth as a new one is opened. Would be a great feature as the tree gets large with lots of traversing nodes.

@andrewhl
Copy link

I would love this feature as well.

@anilramavtar
Copy link

here is the work around solution to achieve this

<Tree data={phaseData} ref="myRef"
initialDepth={1}
orientation="vertical"
pathFunc="elbow"
translate={{ x: 575, y: 20 }}
styles={{ links: { stroke: '#89828D', strokeWidth: 1 } }}
nodeSize={{ x: 160, y: 50 }}
zoomable={false}
onClick={(nodeObj) => this.handleClick(nodeObj, true)}
/>

handleClick(nodeObj, cont) {
if(!cont || nodeObj._collapsed)
return;
const myRef = this.refs.myRef;
const parentObj = nodeObj.parent;
if(parentObj && myRef) {
const nodesToBeCollapsed = parentObj.children.filter(c=> c.id !== nodeObj.id);
nodesToBeCollapsed.map((ndObj,index) => {
if(!ndObj._collapsed)
myRef.handleNodeToggle(ndObj.id, false);
});

}

}

@feralislatr
Copy link

@anilramavtar
I tried using that and I'm getting too much recursion from clone.js

node_modules/react-d3-tree/node_modules/clone/clone.js:75

  72 | // recurse this function so we don't reset allParents and allChildren
  73 | function _clone(parent, depth) {
  74 |   // cloning null always returns null
> 75 |   if (parent === null)
  76 |     return null;
  77 | 
  78 |   if (depth === 0)

Any ideas off the top of your head?

@Dienert
Copy link

Dienert commented Aug 1, 2018

I corrected the code above and it's working now.

import clone from 'clone';

handleClick(nodeObj) {
    if (!nodeObj || nodeObj._collapsed)
        return;
    const myRef = this.refs.myRef;
    const parentObj = nodeObj.parent;
    if (parentObj && myRef) {
        const nodesToBeCollapsed = parentObj.children.filter(c => c.id !== nodeObj.id);
        nodesToBeCollapsed.map((ndObj, index) => {
            if (!ndObj._collapsed) {
                const data = clone(myRef.state.data);
                const nodeId = ndObj.id;
                const matches = myRef.findNodesById(nodeId, data, []);
                const targetNode = matches[0];
                myRef.collapseNode(targetNode);
                myRef.setState({ data });

            }
        });
    }
}

           <Tree
                ref="myRef"
                data={this.state.data}
                translate={this.state.translate}
                orientation={'horizontal'}
                zoomable={true}
                transitionDuration={0}
                initialDepth={1}
                separation={{siblings:0.5, nonSiblings:2}}
                onClick={(nodeObj) => this.handleClick(nodeObj)}
            />

@feralislatr
Copy link

cool, thanks a lot!

@bkrem bkrem removed this from the v1.11 milestone Aug 2, 2018
@bkrem
Copy link
Owner

bkrem commented Aug 2, 2018

Hey everyone,

I had some spare time today and @Dienert's revamp of the workaround reminded me to finally get this done.

So thank you to @Dienert and @anilramavtar for the workarounds first of all, but you should be able to simply use shouldCollapseNeighborNodes now as of v1.11.0 😄

(If there are issues with the implementation, please open a separate bug ticket so we can close out this feature request ✌️)

@bkrem bkrem closed this as completed Aug 2, 2018
@Dienert
Copy link

Dienert commented Aug 2, 2018

Yes, tested and it's working. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants