primefaces / primeng

The Most Complete Angular UI Component Library
https://primeng.org
Other
10.32k stars 4.56k forks source link

Tree Component is setting parent of children to undefined on expanding a node. #14652

Open mconner opened 8 months ago

mconner commented 8 months ago

Describe the bug

I need a TreeNode structure where the nodes know their parents so that I can add nodes programmatically, find their path (from root), etc. Therefore, I'm populating the parent when I build the node structure. However, on expand of a node in the tree -- even just through the UI -- it is setting the value of the parent, sometimes to undefined.

Environment

Windows 10 for dev dev envirnonment, but it reproduces in Stackblitz.

Reproducer

https://stackblitz.com/edit/8tfcxu-mizzw9?file=src%2Fapp%2Fdemo%2Ftree-single-demo.html

Angular version

17.0.8

PrimeNG version

17.3.2

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

v20.10.0

Browser(s)

Edge: 120.0.2210.144, Chrome Version 120.0.6099.225

Steps to reproduce the behavior

The Reproducer is a copy of The PrimeNG Tree documentation single selection, with some modifications:

To reproduce: As repeated in the text of the reproducer: Steps to reproduce:

  1. Open the developer console in the Browser
  2. Expand the first node "Key: 0"
  3. You will see in the developer console that the children of Key: 0 are reporting that their parent is being set to undefined
  4. Also, if you un-expand and then expand again, all at once (i.e.: after the expand) the nodes are first properly set, and then set to undefined again.

Also, adding a breakpoint before expand, the children have a parent (key: 0)

image

Afterward (setting a breakpoint on collapse), they've lost their parent:

image

Setting a breakpoint in the MyTreeNode set parent(value), we see:

set parent (main.js:400)
ngOnInit (vendor.js:220767)
callHookInternal (vendor.js:69487)

this appears to be happening in UITreeNode.ngOnInit:

  ngOnInit() {
    this.node.parent = this.parentNode;
    if (this.parentNode) {
      this.setAllNodesTabIndexes();
      this.tree.syncNodeOption(this.node, this.tree.value, 'parent', this.tree.getNodeWithKey(this.parentNode.key, this.tree.value));
    }
  }

Though I don't see why this.parentNode would be unset at this point, as UITreeNode's inputs should have been set:

ngOnInit() Initialize the directive or component after Angular first displays the data-bound properties and sets the directive or component's input properties.

Expected behavior

The parent of a TreeNode should not be set to undefined (or null) if it actually has a parent.

zachsteffens commented 4 months ago

I'm having the same issue. Any timeline for resolution?