react-virtualized-tree icon indicating copy to clipboard operation
react-virtualized-tree copied to clipboard

Support Adding new nodes

Open einatnielsen opened this issue 3 years ago • 2 comments

I see that UPDATE_TYPE includes ADD option to add a new node, but how is it supported in the TreeView? I see there is support for DELETE and UPDATE, but I dont see any implementation for ADD, is that true?

einatnielsen avatar Oct 18 '20 13:10 einatnielsen

@diogofcunha

einatnielsen avatar Oct 18 '20 13:10 einatnielsen

Same, I love using the package, but it would be really useful if I could add children to the tree without changing the nodes prop value provided.

I have tried this, but it doesn't work :/

      performNodeUpdate(
          [
              {
                  "id": 564827411,
                  "name": "Controller.php",
                  "children": [],
                  "state": {
                      "selected": false,
                      "expanded": false,
                      "favorite": false,
                      "deletable": false,
                  }
              }
          ]
      );
    const performNodeUpdate = (newChildren: VirtualizedTreeNodeType[] = []) => {
        /**
         * Manipulating implementation of updateNode selector from https://github.com/diogofcunha/react-virtualized-tree/blob/master/src/selectors/nodes.js#L77
         * Also see: https://github.com/diogofcunha/react-virtualized-tree/blob/master/src/renderers/Deletable.js
         *           https://github.com/diogofcunha/react-virtualized-tree/blob/master/index.d.ts#L141
         *           https://github.com/diogofcunha/react-virtualized-tree/blob/master/src/renderers/Favorite.js
         *
         * Original implementation:
         *
         *     props.nodeProps.onChange({
         *         ...updateNode(props.nodeProps.node, {expanded: !isExpanded}),
         *         index: props.nodeProps.index,
         *     });
         *
         */
         props.nodeProps.onChange({
            index: props.nodeProps.index,
            node: {
                ...props.nodeProps.node,
                children: [
                    ...props.nodeProps.node.children,
                    newChildren,
                ],
                state: {
                    ...props.nodeProps.node.state,
                    expanded: hasChildren ? !isExpanded : false,
                    selected: true,
                },
            },
            type: UPDATE_TYPE.UPDATE,
        });
    }

martinshaw avatar Oct 12 '22 15:10 martinshaw