question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use generate node props

See original GitHub issue

Hi @fritz-c I’m really liking your project but I keep running into questions with generateNodeProps. I was trying to highlight node onClick event to work. and with the help of the issues I was able to but now I can’t add in buttons so I’m not sure how these things work together is there something Im missing? here is the component below with highlight node working based on examples in the issues.

<SortableTree
      treeData={this.state.treeData}
      onChange={treeData => this.setState({ treeData })}
       getNodeKey={({ node }) => node._id}
       generateNodeProps={rowInfo => {
       let nodeProps =  { onClick: event => this.nodeClicked(event, rowInfo) };
           if (this.state.selectedNodeId === rowInfo.node._id) {
                  nodeProps.className = 'selected-node';
           }
       return nodeProps;
        }}
        onMoveNode={args => this.handleMovedNodes(args)}
        onVisibilityToggle={args => this.handleToggleVisiblity(args)}
 /> 

Any help would be appreciated!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
charue808commented, Mar 30, 2018

Hello, so I was able to figure out how to get this to work and will close it out. In case other noobs have this issue.

Here is the code from my original question:

<SortableTree treeData={this.state.treeData} onChange={treeData => this.setState({ treeData })} getNodeKey={({ node }) => node._id} generateNodeProps={rowInfo => { let nodeProps = { onClick: event => this.nodeClicked(event, rowInfo) }; if (this.state.selectedNodeId === rowInfo.node._id) { nodeProps.className = ‘selected-node’; } return nodeProps; }} onMoveNode={args => this.handleMovedNodes(args)} onVisibilityToggle={args => this.handleToggleVisiblity(args)} />

I hypothesised that since nodeProps object is being return, maybe as a callback?, that I needed to have addtional elements included in nodeProps.

<SortableTree treeData={this.state.treeData} onChange={treeData => this.setState({ treeData })} getNodeKey={({ node }) => node._id} generateNodeProps={rowInfo => { let nodeProps = { onClick: event => this.nodeClicked(event, rowInfo), buttons: [ <button className=“btn btn-danger” key={rowInfo.node._id} onClick={() => console.log(‘clicked’, rowInfo.node)} > Remove </button>, ], }; if (this.state.selectedNodeId === rowInfo.node._id) { nodeProps.className = ‘selected-node’; } return nodeProps; }} onMoveNode={args => this.handleMovedNodes(args)} onVisibilityToggle={args => this.handleToggleVisiblity(args)} />

By adding buttons key to the nodeProps object it now renders the button in all the nodes and I would also venture to guess could use this to render other types elements.

0reactions
charue808commented, Aug 27, 2018

@huiman Hey, So my preliminary code that to get buttons to render on the nodes and be able to highlight it is in the comment

 <SortableTree
              treeData={this.state.treeData}
              onChange={treeData => this.setState({ treeData })}
              getNodeKey={({ node }) => node._id}
              generateNodeProps={rowInfo => {
                let nodeProps =  { onClick: event => this.nodeClicked(event, rowInfo),
                   Buttons: [
                      <button
                          className="btn btn-danger"
                          key={rowInfo.node._id}
                          onClick={() => console.log('clicked', rowInfo.node)}
                     >
                       Remove 
                     </button>
                    ],
                  };
                   if (this.state.selectedNodeId === rowInfo.node._id) {
                       nodeProps.className = 'selected-feature-node';
                   }
                return nodeProps;
}}

Since the return is the nodeProps, you can place any buttons within that object. That’s probably a really hack way to do it thought… OR you could do

 <SortableTree
              treeData={this.state.treeData}
              onChange={treeData => this.setState({ treeData })}
              getNodeKey={({ node }) => node._id}
              generateNodeProps={(rowInfo) => {
                    return {
                  {/* Place whatever onClick function or  buttons to render here*/}
               }

}}`

```
That would also work but it's probably more involved in the onClick function you'll call to make the node highlighted.

Hope that helps.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Generate node prop StoryBook - CodeSandbox
Generate node prop StoryBook. 0. Embed Fork Create Sandbox ... Forked FromTree to tree dragging StoryBook; Environmentcreate-react-app. Files. example.js.
Read more >
How to add button in a specific node of react Sortable Tree
You can use generateNodeProps to add buttons to rendered elements. There is a dedicated buttons prop (I checked in versions 2.5.2 and 2.2.0 ......
Read more >
react-generate-props - npm
Generate default props based on your React component's PropTypes. ... Start using react-generate-props in your project by running `npm i ...
Read more >
Custom Nodes Guide - React Flow
You can add a new node type to React Flow by adding it to the nodeTypes prop. It's important that the nodeTypes are...
Read more >
Components and Props - React
This function is a valid React component because it accepts a single “props” (which stands for properties) object argument with data and returns...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found