Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove node in react-sortable-tree

I'm starting to use React, and I want to add a react-tree component in my project. I am trying to remove a node in the function removeNode():

removeNode(e){
    this.setState({
           treeData: removeNodeAtPath({
               treeData: this.state.treeData,
               path: ????,
               getNodeKey: ({node: TreeNode, treeIndex: number}) => {
                   console.log(number);
                   return number;
               },
               ignoreCollapsed: false,
           })
    })
}

how can I get the path node in my case? My whole file App.js:

export default class TreeVera extends Component {
constructor(props) {
    super(props);
    this.updateTreeData = this.updateTreeData.bind(this);
    this.removeNode = this.removeNode.bind(this);
    this.state = {
        treeData: [{
            title: (<div>
                    <input />
                    <button onClick={this.removeNode}>remove</button>
                </div>),
        }]
    };
}
removeNode(e){
    this.setState({
           treeData: removeNodeAtPath({
               treeData: this.state.treeData,
               path: ????,
               getNodeKey: ({node: TreeNode, treeIndex: number}) => {
                   console.log(number);
                   return number;
               },
               ignoreCollapsed: false,
           })
    })
}
updateTreeData(treeData) {
    this.setState({ treeData });
}
render() {
    return (
        <div style={{ height: 600 }}>
            <SortableTree
                treeData={this.state.treeData}
                onChange={this.updateTreeData}
            />
        </div>
    );
}

component on GitHub: https://github.com/fritz-c/react-sortable-tree

like image 637
Vladimir Avatar asked Feb 10 '17 19:02

Vladimir


1 Answers

I solved this problem:

export default class Tree extends Component {

constructor(props) {
    super(props);
    this.updateTreeData = this.updateTreeData.bind(this);
    this.addNode = this.addNode.bind(this);
    this.removeNode = this.removeNode.bind(this);
    this.state = {
        treeData: [{
            title: '',
        }]
    };
}

getChildContext() {
            return { muiTheme: getMuiTheme(baseTheme) };
        }
addNode(rowInfo){
    let NEW_NODE = {title: ''};
    let {node, treeIndex, path} = rowInfo;
    path.pop();
    let parentNode = getNodeAtPath({
        treeData: this.state.treeData,
        path : path,
        getNodeKey: ({ treeIndex }) =>  treeIndex,
        ignoreCollapsed : true
    });
    let getNodeKey = ({ node: object, treeIndex: number }) => {
        return number;
    };
    let parentKey = getNodeKey(parentNode);
    if(parentKey == -1) {
        parentKey = null;
    }
    let newTree = addNodeUnderParent({
            treeData: this.state.treeData,
            newNode: NEW_NODE,
            expandParent: true,
            parentKey: parentKey,
            getNodeKey: ({ treeIndex }) =>  treeIndex
     });
     this.setState({treeData: newTree.treeData});
}

removeNode(rowInfo) {
let {node, treeIndex, path} = rowInfo;
this.setState({ treeData : removeNodeAtPath({
                   treeData: this.state.treeData,
                   path: path,   // You can use path from here
                   getNodeKey: ({node: TreeNode, treeIndex: number}) => {
                       // console.log(number);
                       return number;
                   },
                   ignoreCollapsed: false,
                })
    })
}

updateTreeData(treeData) {
    this.setState({ treeData });
}

render() {
    return (
        <div style={{ height: 600 }}>
            <SortableTree
                     treeData={this.state.treeData}
                     onChange={this.updateTreeData}
                     generateNodeProps={rowInfo => ({
                         buttons: [
                                  <div style={divStyle}>
                                  <TextField
                                      hintText=""
                                      multiLine={true}
                                      rows={1}
                                      rowsMax={4}
                                   /><br />
                                   <button label='Delete'
                                           onClick={(event) => this.removeNode(rowInfo)}>Remove</button>
                                   <button label='Add'
                                           onClick={(event) => this.addNode(rowInfo)}>Add</button>
                                   </div>,
                                ],
                         style: {
                                  height: '50px',
                                }
                      })}


            />
        </div>
    );
}

}

like image 193
Vladimir Avatar answered Oct 26 '22 18:10

Vladimir