Calculate Roots And Seeds If Root Is Not Given
Summary: The component will show nothing if root element if not provided. In my case, I just want to reuse it without root specified due to elements are separated ## Explanation for seed and root `flatElements` is the representation of component tree expressed in an array form. It is derived from an object `props.elements` where id (key) is corresponding to its information about a component (value). This includes children ids of a component. `flatElements` allows visual indentation to be easily done at render process; we can use level directly as the number of spaces we need to make. `FlatElements` can be evaluated to tree by: - the lowest number is the root of a tree (in this case, it is 1) - a node with `i+1` level is a child of a closest previous node with level `i` `seed` is the function trying to derive elements in object form to array form. First, it is given that there is one id to be a root node. However, This component will not be rendered if a root node id is not given, while a root node id can be null or undefined, which doesn't make sense to me. In addition, if I want to reuse this component, this component should render without root node id given. That's the reason why there's this diff. The implementation is as followed: 1. Set every node to be root (level 1) 2. Iterate through elements 2.1. Remove nodes that are children of elements because they cannot be roots Assumed that there is no cycle, in the end we get all root nodes. Then, we do `seed` for all root nodes to get all trees in `FlatElements` structure Even though to cover my feature the removal of ids are not needed because we know that all elements will be roots of themselves, I want to cover the case that a user wants tree-like (or forest-like) hierarchy view without given root id. (Disclaimer: this is written from what I have observed while investigating this. I am not a person who have initially designed this, so apologize in advance in this is not correct) Reviewed By: mweststrate Differential Revision: D21214897 fbshipit-source-id: 7657d0ea8607e07e97be968ef927f17c2bc8990e
This commit is contained in:
committed by
Facebook GitHub Bot
parent
d7172df082
commit
9962b1c687
@@ -514,6 +514,13 @@ export class Elements extends PureComponent<ElementsProps, ElementsState> {
|
||||
|
||||
if (props.root != null) {
|
||||
seed(props.root, 1);
|
||||
} else {
|
||||
const virtualRoots: Set<string> = new Set();
|
||||
Object.keys(props.elements).forEach((id) => virtualRoots.add(id));
|
||||
Object.values(props.elements).forEach((element) =>
|
||||
element.children.forEach((id) => virtualRoots.delete(id)),
|
||||
);
|
||||
virtualRoots.forEach((id) => seed(id, 1));
|
||||
}
|
||||
|
||||
return {flatElements, flatKeys, maxDepth};
|
||||
|
||||
Reference in New Issue
Block a user