Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Adding new nodes #147

Open
einatnielsen opened this issue Oct 18, 2020 · 2 comments
Open

Support Adding new nodes #147

einatnielsen opened this issue Oct 18, 2020 · 2 comments

Comments

@einatnielsen
Copy link

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
Copy link
Author

@diogofcunha

@martinshaw
Copy link

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,
        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants