React-sortable-tree: Limiting to 1 root node (maybe also limit number of children?)

Created on 21 Jul 2017  路  2Comments  路  Source: frontend-collective/react-sortable-tree

Hi, can I limit the number of root nodes for react-sortable-tree? Also, can I limit the number of direct children that nodes can have?

question

Most helpful comment

I suppose you could create a custom canDrop prop that examines the nextPath to see if the length is 0 (meaning you're targeting the top level / root of the tree), as well as if your treeData array is already the max length or not.
You could solve your children-limiting logic in that same method, checking the length of nextParent.children.

All 2 comments

I suppose you could create a custom canDrop prop that examines the nextPath to see if the length is 0 (meaning you're targeting the top level / root of the tree), as well as if your treeData array is already the max length or not.
You could solve your children-limiting logic in that same method, checking the length of nextParent.children.

Thanks, it's working - looks like this for anyone wondering (I instantiate a full tree before allowing editing):

<SortableTree
  {...otherProps}
  canDrop={({ nextParent,nextPath }) =>(!nextParent || !nextParent.noChildren) && nextPath.length>1 }
/>
Was this page helpful?
0 / 5 - 0 ratings