How to use generate node props
See original GitHub issueHi @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:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
@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
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