Git Product home page Git Product logo

Comments (12)

fritz-c avatar fritz-c commented on July 18, 2024

Can you try applying an onClick prop to your nodes using the generateNodeProps setting?
I haven't done anything like this myself, but you would probably maintain a list of the selected node(s) in your own component's state, update it using onClick or onMouseDown, and change the appearance by adding a className in that same generateNodeProps callback.

from react-sortable-tree.

fritz-c avatar fritz-c commented on July 18, 2024

Doing a bit of issue cleanup. Let me know if anything's changed.

from react-sortable-tree.

rhazegh avatar rhazegh commented on July 18, 2024

Using Version: 1.5.3

I am trying to do the same thing. Setting the className in the onClick function does not seem to add selected-node class to the DOM.

Am I doing it wrong?

export class MyComponent extends Component {
// ....
// ....

    nodeClicked = (event, rowInfo) => {
        if (event.target.className.includes('collapseButton') ||
            event.target.className.includes('expandButton')) {
            // ignore the event
        } else {
            const treeData = changeNodeAtPath({
                treeData: this.state.treeData,
                path: rowInfo.path,
                newNode: {...rowInfo.node, className: 'selected-node'}, // <--- here
                getNodeKey: ({ treeIndex }) => (treeIndex),
                ignoreCollapsed: true
            });
            this.setState({treeData, selectedNode: rowInfo.node});
        }
    };

    render() {
        let tree = (<div>Loading ...</div>);

        if(this.state.IsDataReady) {
            tree = <SortableTree
                treeData={this.state.treeData}
                canDrag={false}
                isVirtualized={false}
                onChange={treeData => {this.setState({ treeData })}}
                generateNodeProps={rowInfo => {
                    return {
                        onClick: event => this.nodeClicked(event, rowInfo)
                    }
                }}
            />
        }

        return (
            <div>
                {tree}
            </div>
        );
    }
}

export default MyComponent;

from react-sortable-tree.

fritz-c avatar fritz-c commented on July 18, 2024

@rhazegh I would recommend you use a more unique identifier than the treeIndex in getNodeKey. Something like getNodeKey: ({ node }) => node.id (be sure to assign it to the tree's getNodeKey prop). With a unique ID, you could store the selected id in your component's state, and when calling generateNodeProps you check if the last part of path matches the selected id, and return the style or className accordingly.

By the way, the issue in your code is that setting className directly to a node does not translate to className getting set on the DOM node. title, subtitle, expanded and children are the only attributes of nodes that get directly accessed by the code. The rest you have to pass on yourself, with callbacks, etc.

from react-sortable-tree.

rhazegh avatar rhazegh commented on July 18, 2024

@fritz-c Thank you. It works now πŸ˜ƒ

BTW, great component! Really like it πŸ‘

// ...

                generateNodeProps={rowInfo => {
                    let nodeProps =  { onClick: event => this.nodeClicked(event, rowInfo) };
                    if (this.state.selectedNodeId === rowInfo.node.id) {
                        nodeProps.className = 'selected-node';
                    }
                    return nodeProps;
                }}
// ...

from react-sortable-tree.

charue808 avatar charue808 commented on July 18, 2024

@rhazegh could you explain your code? I've been trying to get this to work as well and tried to follow along but ran into errors. Thank you!

from react-sortable-tree.

rhazegh avatar rhazegh commented on July 18, 2024

@charue808 friz-c has provided excellent explanations and my code is based on his suggestions. Which part of it do you need help with?

from react-sortable-tree.

charue808 avatar charue808 commented on July 18, 2024

@rhazegh Yeah, actually his explanation made some sense. I guess I don't understand how your code snippet works..since I got fatal errors with using "let" the way you did... but it makes sense since i think it is a callback.

from react-sortable-tree.

rhazegh avatar rhazegh commented on July 18, 2024

@charue808 let and const are used in ES6. You need to use babel to transpile to ES5. https://babeljs.io

from react-sortable-tree.

charue808 avatar charue808 commented on July 18, 2024

Correct, that's not the issue. So before your reply i got it to work, with lint errors but it worked.

However the issue is when I follow your example I can't render buttons. So i'm not sure if I need to include rendering buttons into the return of nodeProps...or something else.

from react-sortable-tree.

rhazegh avatar rhazegh commented on July 18, 2024

@charue808 I am afraid I don’t understand your problem.

I suggest you open a new issue and put your code with extra details in there. Someone might be able to help you.

from react-sortable-tree.

Letrab avatar Letrab commented on July 18, 2024

For future reference, the updated nodeClicked handler was missing...

Full example:

  nodeClicked = (event, rowInfo) => {
    if (event.target.className.includes('collapseButton') ||
        event.target.className.includes('expandButton')) {
        // ignore the event
    } else {
        this.setState({selectedNodeId: rowInfo.node.id});
    }
};

and

                generateNodeProps={rowInfo => {
                    let nodeProps =  { onClick: event => this.nodeClicked(event, rowInfo) };
                    if (this.state.selectedNodeId === rowInfo.node.id) {
                        nodeProps.className = 'selected-node';
                    }
                    return nodeProps;
                }}

from react-sortable-tree.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.