Alex Curtis created react-treebeard a data driven tree view for React. I needed to enable users to filter the tree's nodes based on input text. To accomplish this I transform the tree's data using 2 functions when the filter changes and rerender the view:
filterTree
Given a tree return a new tree keeping only the nodes that meet one of the following conditions:- Has matching text
- Has a parent with matching text
- Has a child with matching text
expandNodesWithMatchingDescendants
- Given a tree mark all the nodes with matching descendants as toggled
Here are the aformentioned functions.