Is there a way to hide a rootNode in the tree? #34
-
| Hey 👋 I've been using a  My  I've tried using CSS to hide the rootNode, but it does take up the  | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
| Hi @Shriram-Balaji. That's pretty possible with  Here you can see the example. I basically changed the  Changed Node component codeconst Node: FC<VariableSizeNodeComponentProps<ExtendedData>> = ({
  height,
  data: {id, isLeaf, name, nestingLevel},
  isOpen,
  resize,
  style,
  toggle,
  treeData: itemSize,
}) => {
  const canOpen = height <= itemSize;
  const halfSize = itemSize / 2;
  const toggleNodeSize = useCallback(
    () => resize(canOpen ? height + halfSize : height - halfSize, true),
    [height, resize],
  );
  useEffect(() => {
    // Applying resize to root node if it's height is not zero
    if (id === '0' && height !== 0) {
      resize(0, true);
    }
  }, [height]);
  return (
    <div
      style={{
        ...style,
        alignItems: 'center',
        background: canOpen ? undefined : '#ddd',
        display: 'flex',
        marginLeft: nestingLevel * 30 + (isLeaf ? 48 : 0),
        // Added overflowing to avoid collapsed root node to be visible
        overflow: 'hidden',
      }}
    >
      {!isLeaf && (
        <div>
          <button type="button" onClick={toggle} style={defaultButtonStyle}>
            {isOpen ? '-' : '+'}
          </button>
        </div>
      )}
      <div style={defaultGapStyle}>{name}</div>
      <div>
        <button type="button" onClick={toggleNodeSize} style={defaultGapStyle}>
          {canOpen ? 'Open' : 'Close'}
        </button>
      </div>
    </div>
  );
};By the way, could you tell me why you want to hide the root node? Probably, you just want to have a multiple nodes at the root? You can easily achieve it by changing the  Changed treeWalker codefunction* treeWalker(
  refresh: boolean,
): Generator<ExtendedData | string | symbol, void, boolean> {
  // Just fill the stack with any elements you want to be at the root
  const stack: StackElement[] = rootNode.children
    .map((node) => ({
      nestingLevel: 0,
      node,
    }))
    // reverse should be done because we use i-- in the for loop below
    .reverse();
  while (stack.length !== 0) {
    const {node, nestingLevel} = stack.pop()!;
    const id = node.id.toString();
    const isOpened = yield refresh
      ? {
          defaultHeight: itemSize,
          id,
          isLeaf: node.children.length === 0,
          isOpenByDefault: true,
          name: node.name,
          nestingLevel,
        }
      : id;
    if (node.children.length !== 0 && isOpened) {
      for (let i = node.children.length - 1; i >= 0; i--) {
        stack.push({
          nestingLevel: nestingLevel + 1,
          node: node.children[i],
        });
      }
    }
  }
} | 
Beta Was this translation helpful? Give feedback.

Hi @Shriram-Balaji. That's pretty possible with
resize(0, true), you just need to addoverflow: hiddento your Node host element and callresizeon eachheightchange (becausetoggleresets the height; btw, that looks like a bug).Here you can see the example. I basically changed the
Nodecomponent.Changed Node component code