1616package com .vaadin .flow .component .dialog ;
1717
1818import java .io .Serializable ;
19+ import java .util .Collection ;
1920import java .util .HashMap ;
2021import java .util .Map ;
2122import java .util .Objects ;
2223import java .util .stream .Collectors ;
23- import java .util .stream .Stream ;
24- import java .util .stream .Stream .Builder ;
2524
2625import com .vaadin .flow .component .AttachEvent ;
2726import com .vaadin .flow .component .Component ;
2827import com .vaadin .flow .component .ComponentEvent ;
2928import com .vaadin .flow .component .ComponentEventListener ;
30- import com .vaadin .flow .component .ComponentUtil ;
3129import com .vaadin .flow .component .DetachEvent ;
3230import com .vaadin .flow .component .DomEvent ;
3331import 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 (
0 commit comments