Skip to content

Commit 6235ba6

Browse files
committed
Fix: TreeNode#activate() now correctly expands the node as intended.
Fix: Refactored TreeRoot#clear() and removeNode() to follow the TreeNode implementation for consistent sub-node handling. Fix: Skip collapse() in removeNode if sub-nodes are empty to support expanded states during UI refreshes/redrawing.
1 parent b963cff commit 6235ba6

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

domino-ui/src/main/java/org/dominokit/domino/ui/tree/TreeNode.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,18 @@ public abstract class TreeNode<V, N extends TreeNode<V, N, S>, S>
122122
* node.
123123
*/
124124
private void onActivation() {
125-
if (isParent()) {
125+
onActivation(true);
126+
}
127+
128+
/**
129+
* Handles the logic when a tree node is "activated" (clicked or otherwise triggered), toggling
130+
* expand/collapse if it's a parent node and then calling {@link #activateNode()} to highlight the
131+
* node.
132+
*
133+
* @param toggle indicates whether the node’s state should be toggled
134+
*/
135+
private void onActivation(boolean toggle) {
136+
if (toggle && isParent()) {
126137
toggle();
127138
}
128139
activateNode();
@@ -345,19 +356,15 @@ public N appendChild(N... treeItems) {
345356
}
346357

347358
/**
348-
* Removes a specified child node from this node. If no children remain after removal, the node is
349-
* collapsed.
359+
* Removes a specified child node from the node's internal list of sub-nodes.
360+
*
361+
* <p>Note: This does not remove the element from the DOM. To detach the item visually, call
362+
* {@link TreeItem#remove()} on the item itself.
350363
*
351364
* @param item the child node to remove
352365
*/
353366
public void removeNode(N item) {
354-
if (subNodes.contains(item)) {
355-
subNodes.remove(item);
356-
}
357-
358-
if (subNodes.isEmpty()) {
359-
collapse();
360-
}
367+
subNodes.remove(item);
361368
}
362369

363370
/**
@@ -451,18 +458,10 @@ public RootNode<V, N, S> getRootNode() {
451458
return parent.getRootNode();
452459
}
453460

454-
/**
455-
* Retrieves this node's optional parent. If the parent is a {@code TreeNode}, returns a non-empty
456-
* {@link Optional}; otherwise, it may be empty if this node is top-level.
457-
*
458-
* @return an {@link Optional} containing the parent node if it exists
459-
*/
461+
/** {@inheritDoc} */
462+
@Override
460463
public Optional<IsParentNode<V, N, S>> getParent() {
461-
if (parent instanceof TreeNode) {
462-
return Optional.of(parent);
463-
} else {
464-
return Optional.empty();
465-
}
464+
return Optional.ofNullable(parent);
466465
}
467466

468467
/**
@@ -552,7 +551,7 @@ protected void doActivate(boolean activateParent) {
552551
*/
553552
public N activate() {
554553
this.show(true);
555-
onActivation();
554+
onActivation(false);
556555
return (N) this;
557556
}
558557

domino-ui/src/main/java/org/dominokit/domino/ui/tree/TreeRoot.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public RootNode<V, N, S> getRootNode() {
238238
*
239239
* @return An empty optional.
240240
*/
241+
@Override
241242
public Optional<IsParentNode<V, N, S>> getParent() {
242243
return Optional.empty();
243244
}
@@ -614,15 +615,15 @@ public List<V> getActivePathValues() {
614615
}
615616

616617
/**
617-
* Removes the specified tree item from this tree. This method removes the tree item from the list
618-
* of sub-items and calls the {@link org.dominokit.domino.ui.tree.TreeItem#remove()} method on the
619-
* item to detach it from the DOM.
618+
* Removes the specified item from the tree's internal list of sub-items.
619+
*
620+
* <p>Note: This does not remove the element from the DOM. To detach the item visually, call
621+
* {@link TreeItem#remove()} on the item itself.
620622
*
621623
* @param item The tree item to be removed.
622624
*/
623625
public void removeNode(N item) {
624626
subNodes.remove(item);
625-
item.remove();
626627
}
627628

628629
/**
@@ -631,7 +632,7 @@ public void removeNode(N item) {
631632
* @return This `Tree` instance for method chaining.
632633
*/
633634
public C clear() {
634-
subNodes.forEach(TreeNode::remove);
635+
new ArrayList<>(subNodes).forEach(TreeNode::remove);
635636
return (C) this;
636637
}
637638

0 commit comments

Comments
 (0)