Skip to content

Commit b314d3d

Browse files
authored
fix: add popover to UI modal component again (#7639)
* fix: add popover to UI modal component again * satisfy sonar
1 parent 3043f96 commit b314d3d

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎vaadin-popover-flow-parent/vaadin-popover-flow/src/main/java/com/vaadin/flow/component/popover/Popover.java‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -707,11 +707,9 @@ public void setTarget(Component target) {
707707

708708
// Target's JavaScript needs to be executed on each attach,
709709
// because Flow creates a new client-side element
710-
if (target.getUI().isPresent()) {
711-
onTargetAttach();
712-
}
710+
target.getUI().ifPresent(this::onTargetAttach);
713711
targetAttachRegistration = target
714-
.addAttachListener(e -> onTargetAttach());
712+
.addAttachListener(e -> onTargetAttach(e.getUI()));
715713
targetDetachRegistration = target.addDetachListener(e -> {
716714
removeFromUiIfAutoAdded();
717715
});
@@ -720,16 +718,29 @@ public void setTarget(Component target) {
720718
private void removeFromUiIfAutoAdded() {
721719
if (autoAddedToTheUi) {
722720
autoAddedToTheUi = false;
723-
getElement().removeFromParent();
721+
// Remove the popover from the UI
722+
// When using HasComponents.removeAll on the popover's parent, the
723+
// parent's children are cleared before sending detach events to the
724+
// children and clearing the parent reference from the children. If
725+
// the popover and its target are both children of the same parent,
726+
// then running Element.removeFromParent in the target's detach
727+
// listener will fail, as the popover still has a parent reference,
728+
// but the parent does not have it as a child anymore. Thus, check
729+
// beforehand if the parent still has the popover as a child.
730+
Optional<Component> maybeParent = getParent();
731+
if (maybeParent.isPresent() && maybeParent.get().getChildren()
732+
.anyMatch(child -> child == this)) {
733+
getElement().removeFromParent();
734+
}
724735
}
725736
}
726737

727-
private void onTargetAttach() {
738+
private void onTargetAttach(UI ui) {
728739
if (target != null) {
729740
if (getElement().getNode().getParent() == null) {
730741
// Remove the popover from its current state tree
731742
getElement().removeFromTree(false);
732-
target.getElement().getParent().appendChild(getElement());
743+
ui.addToModalComponent(this);
733744
autoAddedToTheUi = true;
734745
}
735746
getElement().executeJs("this.target = $0", target.getElement());

‎vaadin-popover-flow-parent/vaadin-popover-flow/src/test/java/com/vaadin/flow/component/popover/PopoverAutoAddTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public void setup() {
3939
session = Mockito.mock(VaadinSession.class);
4040
Mockito.when(session.hasLock()).thenReturn(true);
4141
ui.getInternals().setSession(session);
42+
VaadinSession.setCurrent(session);
43+
Mockito.when(session.getErrorHandler()).thenReturn(event -> {
44+
throw new RuntimeException(event.getThrowable());
45+
});
4246
}
4347

4448
@After
@@ -85,6 +89,19 @@ public void setTarget_detachTarget_autoRemoved() {
8589
Assert.assertNull(popover.getElement().getParent());
8690
}
8791

92+
@Test
93+
public void setTarget_removeAll_noException() {
94+
Div target = new Div();
95+
Popover popover = new Popover();
96+
popover.setTarget(target);
97+
ui.add(target);
98+
99+
ui.removeAll();
100+
101+
Assert.assertNull(popover.getElement().getParent());
102+
Assert.assertEquals(0, ui.getChildren().count());
103+
}
104+
88105
@Test
89106
public void setTarget_changeTarget_notAutoRemoved() {
90107
Popover popover = new Popover();

0 commit comments

Comments
 (0)