Skip to content

Commit

Permalink
fix #999 Tree selection of subitems does not work when tree was not c…
Browse files Browse the repository at this point in the history
…ollapsed before
  • Loading branch information
vegegoku committed Jan 28, 2025
1 parent 2e3315a commit 42da68a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,12 @@ public Card appendChild(PrefixAddOn<?> prefix) {

@Override
public PostfixElement getPostfixElement() {
return PostfixElement.of(header.get().getMainHeader().element());
return header.get().getPostfixElement();
}

@Override
public PrefixElement getPrefixElement() {
return PrefixElement.of(header.get().getMainHeader().element());
return header.get().getPrefixElement();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@ public void setActiveNode(TreeItem<V> activeItem, boolean silent) {
this.activeNode.deactivate();
}
this.activeNode = activeItem;
this.activeNode.activate();
getParent().ifPresent(itemParent -> itemParent.setActiveNode(this, true));
this.activeNode.doActivate();
if (getParent().isPresent()) {
getParent().get().setActiveNode(this, true);
} else {
getRootNode().setActiveNode(this, true);
}
if (!silent) {
triggerSelectionListeners(activeItem, getSelection());
getRootNode().onActiveNodeChanged(activeItem, getSelection(), silent);
Expand Down
53 changes: 32 additions & 21 deletions domino-ui/src/main/java/org/dominokit/domino/ui/tree/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,22 @@ public abstract class TreeNode<V, N extends TreeNode<V, N, S>, S>
evt -> {
if (ToggleTarget.ANY.equals(this.toggleTarget)) {
evt.stopPropagation();
if (isParent()) {
toggle();
}
activateNode();
onActivation();
}
};

private void onActivation() {
if (isParent()) {
toggle();
}
activateNode();
}

private final EventListener iconListener =
evt -> {
if (ToggleTarget.ICON.equals(this.toggleTarget)) {
evt.stopPropagation();
if (isParent()) {
toggle();
}
activateNode();
onActivation();
}
};

Expand Down Expand Up @@ -433,19 +434,12 @@ public List<N> getSubNodes() {
return subNodes;
}

/**
* Expands (shows and activates) this tree item. This method expands the item and activates it.
*/
public void expandAndActivate() {
this.show(true).activate(true);
}

/**
* Activates (selects) this tree item without activating its parent items. This method returns
* void.
*/
public void activate() {
activate(false);
public void doActivate() {
doActivate(false);
}

/**
Expand All @@ -454,14 +448,30 @@ public void activate() {
*
* @param activateParent True to activate parent items, false to activate only this item.
*/
public void activate(boolean activateParent) {
protected void doActivate(boolean activateParent) {
addCss(dui_active);
if (activateParent) {
getParent().ifPresent(parent -> parent.setActiveNode((N) this));
}
updateIcon(isCollapsed());
}

public N activate() {
this.show(true);
onActivation();
return (N) this;
}

/**
* @return same instance
* @deprecated use {@link #activate()}
*/
@Deprecated
public N select() {
activate();
return (N) this;
}

/**
* Returns the currently active (selected) tree item within the tree structure.
*
Expand Down Expand Up @@ -638,12 +648,13 @@ public N expandNode(boolean expandParent) {
* @return This tree item with the node shown.
*/
public N show(boolean expandParent) {
if (isParent()) {
super.expand();
}
if (expandParent) {
getParent().ifPresent(itemParent -> itemParent.expandNode(true));
}

if (isParent()) {
super.expand();
}
return (N) this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void setActiveNode(N node, boolean silent) {
}

this.activeNode = node;
this.activeNode.activate();
this.activeNode.doActivate();
if (!silent) {
triggerSelectionListeners(node, getSelection());
this.activeNode.triggerSelectionListeners(node, getSelection());
Expand Down

0 comments on commit 42da68a

Please sign in to comment.