Skip to content

Commit c4de58c

Browse files
fix: add dialog and notification children to host element (#4547)
* fix: add dialog and notification children to host element * refactor: cleanup and override correct methods Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent 551e553 commit c4de58c

6 files changed

Lines changed: 135 additions & 125 deletions

File tree

‎vaadin-dialog-flow-parent/vaadin-dialog-flow/src/main/java/com/vaadin/flow/component/dialog/Dialog.java‎

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616
package com.vaadin.flow.component.dialog;
1717

1818
import java.io.Serializable;
19+
import java.util.Collection;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122
import java.util.Objects;
2223
import java.util.stream.Collectors;
23-
import java.util.stream.Stream;
24-
import java.util.stream.Stream.Builder;
2524

2625
import com.vaadin.flow.component.AttachEvent;
2726
import com.vaadin.flow.component.Component;
2827
import com.vaadin.flow.component.ComponentEvent;
2928
import com.vaadin.flow.component.ComponentEventListener;
30-
import com.vaadin.flow.component.ComponentUtil;
3129
import com.vaadin.flow.component.DetachEvent;
3230
import com.vaadin.flow.component.DomEvent;
3331
import com.vaadin.flow.component.EventData;
@@ -87,7 +85,6 @@ public class Dialog extends Component implements HasComponents, HasSize,
8785

8886
private static final String OVERLAY_LOCATOR_JS = "this.$.overlay";
8987

90-
private Element container;
9188
private boolean autoAddedToTheUi;
9289
private int onCloseConfigured;
9390
private String width;
@@ -105,13 +102,6 @@ public class Dialog extends Component implements HasComponents, HasSize,
105102
* Creates an empty dialog.
106103
*/
107104
public Dialog() {
108-
container = new Element("div");
109-
this.container.addAttachListener(event -> {
110-
this.container.executeJs(
111-
"Vaadin.FlowComponentHost.patchVirtualContainer(this)");
112-
});
113-
getElement().appendVirtualChild(container);
114-
115105
// Needs to be updated on each
116106
// attach, as element depends on node id which is subject to change if
117107
// the dialog is transferred to another UI, e.g. due to
@@ -354,37 +344,12 @@ public Dialog(Component... components) {
354344
* the components to add
355345
*/
356346
@Override
357-
public void add(Component... components) {
358-
Objects.requireNonNull(components, "Components should not be null");
359-
for (Component component : components) {
360-
Objects.requireNonNull(component,
361-
"Component to add cannot be null");
362-
container.appendChild(component.getElement());
363-
}
347+
public void add(Collection<Component> components) {
348+
HasComponents.super.add(components);
364349

365350
updateVirtualChildNodeIds();
366351
}
367352

368-
@Override
369-
public void remove(Component... components) {
370-
Objects.requireNonNull(components, "Components should not be null");
371-
for (Component component : components) {
372-
Objects.requireNonNull(component,
373-
"Component to remove cannot be null");
374-
if (container.equals(component.getElement().getParent())) {
375-
container.removeChild(component.getElement());
376-
} else {
377-
throw new IllegalArgumentException("The given component ("
378-
+ component + ") is not a child of this component");
379-
}
380-
}
381-
}
382-
383-
@Override
384-
public void removeAll() {
385-
container.removeAllChildren();
386-
}
387-
388353
/**
389354
* Adds the given component into this dialog at the given index.
390355
* <p>
@@ -400,14 +365,7 @@ public void removeAll() {
400365
*/
401366
@Override
402367
public void addComponentAtIndex(int index, Component component) {
403-
Objects.requireNonNull(component, "Component should not be null");
404-
if (index < 0) {
405-
throw new IllegalArgumentException(
406-
"Cannot add a component with a negative index");
407-
}
408-
// The case when the index is bigger than the children count is handled
409-
// inside the method below
410-
container.insertChild(index, component.getElement());
368+
HasComponents.super.addComponentAtIndex(index, component);
411369

412370
updateVirtualChildNodeIds();
413371
}
@@ -861,14 +819,6 @@ private void setModality(boolean modal) {
861819
}
862820
}
863821

864-
@Override
865-
public Stream<Component> getChildren() {
866-
Builder<Component> childComponents = Stream.builder();
867-
container.getChildren().forEach(childElement -> ComponentUtil
868-
.findComponents(childElement, childComponents::add));
869-
return childComponents.build();
870-
}
871-
872822
/**
873823
* Add a lister for event fired by the {@code opened-changed} events.
874824
*
@@ -911,7 +861,7 @@ public Registration addDetachListener(
911861
private Map<Element, Registration> childDetachListenerMap = new HashMap<>();
912862
private ElementDetachListener childDetachListener = e -> {
913863
var child = e.getSource();
914-
var childDetachedFromContainer = !container.getChildren().anyMatch(
864+
var childDetachedFromContainer = !getElement().getChildren().anyMatch(
915865
containerChild -> Objects.equals(child, containerChild));
916866

917867
if (childDetachedFromContainer) {
@@ -935,15 +885,15 @@ public Registration addDetachListener(
935885
*/
936886
private void updateVirtualChildNodeIds() {
937887
// Add detach listeners (child may be removed with removeFromParent())
938-
container.getChildren().forEach(child -> {
888+
getElement().getChildren().forEach(child -> {
939889
if (!childDetachListenerMap.containsKey(child)) {
940890
childDetachListenerMap.put(child,
941891
child.addDetachListener(childDetachListener));
942892
}
943893
});
944894

945895
this.getElement().setPropertyList("virtualChildNodeIds",
946-
container.getChildren()
896+
getElement().getChildren()
947897
.map(element -> element.getNode().getId())
948898
.collect(Collectors.toList()));
949899

@@ -1009,12 +959,14 @@ private void initHeaderFooterRenderer() {
1009959
}
1010960

1011961
private void setDimension(String dimension, String value) {
1012-
getElement().executeJs("requestAnimationFrame(e => "
1013-
+ OVERLAY_LOCATOR_JS + ".$.overlay.style[$0]=$1)", dimension,
1014-
value);
962+
getElement().executeJs(OVERLAY_LOCATOR_JS + ".$.overlay.style[$0]=$1",
963+
dimension, value);
1015964
}
1016965

1017966
private void attachComponentRenderer() {
967+
this.getElement().executeJs(
968+
"Vaadin.FlowComponentHost.patchVirtualContainer(this)");
969+
1018970
String appId = UI.getCurrent().getInternals().getAppId();
1019971

1020972
getElement().executeJs(

‎vaadin-dialog-flow-parent/vaadin-dialog-flow/src/test/java/com/vaadin/flow/component/dialog/DialogChildrenTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.vaadin.flow.component.dialog;
1717

1818
import java.util.Arrays;
19+
import java.util.List;
1920
import java.util.stream.Collectors;
2021

2122
import org.junit.After;
@@ -72,6 +73,23 @@ public void addMany_virtualNodeIdsInSync() {
7273
assertVirtualChildren(child, child2);
7374
}
7475

76+
@Test
77+
public void addCollection_virtualNodeIdsInSync() {
78+
var child = new Div();
79+
var child2 = new Div();
80+
dialog.add(List.of(child, child2));
81+
assertVirtualChildren(child, child2);
82+
}
83+
84+
@Test
85+
public void addComponentAsFirst_virtualNodeIdsInSync() {
86+
var child = new Div();
87+
var child2 = new Div();
88+
dialog.add(child);
89+
dialog.addComponentAsFirst(child2);
90+
assertVirtualChildren(child2, child);
91+
}
92+
7593
@Test
7694
public void remove_virtualNodeIdsInSync() {
7795
var child = new Div();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2000-2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.notification.tests;
17+
18+
import com.vaadin.flow.component.Text;
19+
import com.vaadin.flow.component.html.Div;
20+
import com.vaadin.flow.component.html.NativeButton;
21+
import com.vaadin.flow.component.notification.Notification;
22+
import com.vaadin.flow.router.Route;
23+
24+
@Route("vaadin-notification/nested")
25+
public class NestedNotificationPage extends Div {
26+
27+
public NestedNotificationPage() {
28+
var parent = new Notification();
29+
30+
var child = new Notification();
31+
child.setText("Child text");
32+
33+
parent.add(child);
34+
parent.add(new NativeButton("Open child", e -> child.open()));
35+
36+
var openParentButton = new NativeButton("Open parent",
37+
e -> parent.open());
38+
openParentButton.setId("open");
39+
add(openParentButton);
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2000-2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.notification.tests;
17+
18+
import com.vaadin.flow.testutil.TestPath;
19+
import com.vaadin.tests.AbstractComponentIT;
20+
21+
import org.junit.Test;
22+
import org.openqa.selenium.By;
23+
24+
@TestPath("vaadin-notification/nested")
25+
public class NestedNotificationIT extends AbstractComponentIT {
26+
27+
@Test
28+
public void openNotification_shouldNotThrow() {
29+
open();
30+
31+
findElement(By.id("open")).click();
32+
33+
checkLogsForErrors();
34+
}
35+
36+
}

0 commit comments

Comments
 (0)