Skip to content

Commit ff54d60

Browse files
authored
feat: Modal dialog also server-side modal (#2608)
1 parent d5d6399 commit ff54d60

12 files changed

Lines changed: 438 additions & 11 deletions

File tree

  • vaadin-confirm-dialog-flow-parent
  • vaadin-context-menu-flow-parent/vaadin-context-menu-flow/src/main/java/com/vaadin/flow/component/contextmenu
  • vaadin-crud-flow-parent/vaadin-crud-flow-integration-tests/src/test/java/com/vaadin/flow/component/crud/test
  • vaadin-dialog-flow-parent
  • vaadin-login-flow-parent/vaadin-login-flow/src/main/java/com/vaadin/flow/component/login
  • vaadin-notification-flow-parent/vaadin-notification-flow/src/main/java/com/vaadin/flow/component/notification

‎vaadin-confirm-dialog-flow-parent/vaadin-confirm-dialog-flow-integration-tests/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090
<groupId>com.vaadin</groupId>
9191
<artifactId>flow-polymer-template</artifactId>
9292
</dependency>
93+
<dependency>
94+
<groupId>com.vaadin</groupId>
95+
<artifactId>flow-html-components-testbench</artifactId>
96+
<version>${flow.version}</version>
97+
<scope>test</scope>
98+
</dependency>
9399
</dependencies>
94100
<build>
95101
<plugins>

‎vaadin-confirm-dialog-flow-parent/vaadin-confirm-dialog-flow-integration-tests/src/main/java/com/vaadin/flow/component/confirmdialog/examples/BasicUseView.java‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
import com.vaadin.flow.component.button.Button;
44
import com.vaadin.flow.component.confirmdialog.ConfirmDialog;
55
import com.vaadin.flow.component.html.Div;
6+
import com.vaadin.flow.component.html.Hr;
7+
import com.vaadin.flow.component.html.NativeButton;
68
import com.vaadin.flow.router.Route;
79

810
@Route(value = "vaadin-confirm-dialog/basicuse")
911
public class BasicUseView extends Div {
1012

13+
private Log log = new Log();
14+
1115
boolean expanded = true;
1216

1317
public BasicUseView() {
@@ -16,6 +20,31 @@ public BasicUseView() {
1620
confirmDialog.setText("Here is my text");
1721
Button showDialogButton = new Button("Show dialog",
1822
e -> confirmDialog.open());
19-
add(confirmDialog, showDialogButton);
23+
showDialogButton.setId("open-dialog");
24+
25+
NativeButton logButton = new NativeButton("Log",
26+
event -> log.log("Clicked"));
27+
logButton.setId("log");
28+
29+
add(confirmDialog, showDialogButton, logButton, new Hr(), log);
30+
}
31+
32+
public static class Log extends Div {
33+
34+
public static final String LOG_ID = "log-output";
35+
36+
private int logCount;
37+
38+
public Log() {
39+
setId(LOG_ID);
40+
}
41+
42+
public void log(String msg) {
43+
Div div = new Div();
44+
div.addClassName("log");
45+
logCount++;
46+
div.setText(logCount + ". " + msg);
47+
add(div);
48+
}
2049
}
2150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.vaadin.flow.component.confirmdialog.examples;
2+
3+
import com.vaadin.flow.component.button.Button;
4+
import com.vaadin.flow.component.confirmdialog.ConfirmDialog;
5+
import com.vaadin.flow.component.html.Div;
6+
import com.vaadin.flow.component.html.Hr;
7+
import com.vaadin.flow.router.Route;
8+
9+
@Route(value = "vaadin-confirm-dialog/modality")
10+
public class ModalityPage extends Div {
11+
12+
private Log log = new Log();
13+
14+
public ModalityPage() {
15+
ConfirmDialog confirmDialog = new ConfirmDialog();
16+
confirmDialog.setHeader("My header");
17+
confirmDialog.setText("Here is my text");
18+
Button showDialogButton = new Button("Show dialog",
19+
e -> confirmDialog.open());
20+
showDialogButton.setId("open-dialog");
21+
22+
Button logButton = new Button("Log", event -> log.log("Clicked"));
23+
logButton.setId("log");
24+
25+
add(showDialogButton, logButton, new Hr(), log);
26+
}
27+
28+
public static class Log extends Div {
29+
30+
public static final String LOG_ID = "log-output";
31+
32+
private int logCount;
33+
34+
public Log() {
35+
setId(LOG_ID);
36+
}
37+
38+
public void log(String msg) {
39+
Div div = new Div();
40+
div.addClassName("log");
41+
logCount++;
42+
div.setText(logCount + ". " + msg);
43+
add(div);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.vaadin.flow.component.confirmdialog.test;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import com.vaadin.flow.component.button.testbench.ButtonElement;
8+
import com.vaadin.flow.component.confirmdialog.testbench.ConfirmDialogElement;
9+
import com.vaadin.flow.component.html.testbench.DivElement;
10+
import com.vaadin.flow.testutil.TestPath;
11+
import com.vaadin.testbench.TestBenchElement;
12+
import com.vaadin.tests.AbstractComponentIT;
13+
import com.vaadin.tests.AbstractParallelTest;
14+
15+
import static com.vaadin.flow.component.confirmdialog.examples.BasicUseView.Log.LOG_ID;
16+
17+
@TestPath("vaadin-confirm-dialog/modality")
18+
public class ModalityIT extends AbstractComponentIT {
19+
20+
@Before
21+
public void init() {
22+
open();
23+
Assert.assertEquals("No log messages should exist on open", 0,
24+
$(DivElement.class).id(LOG_ID).$("div").all().size());
25+
}
26+
27+
@Test
28+
public void openConfirmDialog_removeBackdrop_logClickNotAccepted() {
29+
$(ButtonElement.class).id("open-dialog").click();
30+
final DivElement backdrop = $(TestBenchElement.class).id("overlay")
31+
.$(DivElement.class).id("backdrop");
32+
33+
executeScript("arguments[0].remove()", backdrop);
34+
35+
Assert.assertFalse("Backdrop was not removed from dom",
36+
$(TestBenchElement.class).id("overlay").$(DivElement.class)
37+
.attributeContains("id", "backdrop").exists());
38+
39+
$(ButtonElement.class).id("log").click();
40+
41+
Assert.assertTrue("Dialog should not have closed",
42+
$(ConfirmDialogElement.class).exists());
43+
Assert.assertEquals("Click on button should not generate a log message",
44+
0, $(DivElement.class).id(LOG_ID).$("div").all().size());
45+
46+
$(ConfirmDialogElement.class).first().getConfirmButton().click();
47+
48+
Assert.assertFalse("Dialog should have closed",
49+
$(ConfirmDialogElement.class).exists());
50+
51+
$(ButtonElement.class).id("log").click();
52+
53+
Assert.assertEquals("Click should have resulted in a log message", 1,
54+
$(DivElement.class).id(LOG_ID).$("div").all().size());
55+
}
56+
57+
}

‎vaadin-confirm-dialog-flow-parent/vaadin-confirm-dialog-flow/src/main/java/com/vaadin/flow/component/confirmdialog/ConfirmDialog.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ public void setOpened(boolean opened) {
568568
if (opened) {
569569
ensureAttached();
570570
}
571+
if (isAttached()) {
572+
getUI().ifPresent(ui -> ui.setChildComponentModal(this, opened));
573+
}
571574
getElement().setProperty("opened", opened);
572575
}
573576

@@ -587,10 +590,11 @@ private void ensureAttached() {
587590
UI ui = getCurrentUI();
588591
ui.beforeClientResponse(ui, context -> {
589592
if (getElement().getNode().getParent() == null) {
590-
ui.add(this);
593+
ui.addToModalComponent(this);
591594
autoAddedToTheUi = true;
592595
updateWidth();
593596
updateHeight();
597+
ui.setChildComponentModal(this, true);
594598
}
595599
});
596600
}

‎vaadin-context-menu-flow-parent/vaadin-context-menu-flow/src/main/java/com/vaadin/flow/component/contextmenu/ContextMenuBase.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ private void addContextMenuToUi() {
412412
if (getElement().getNode().getParent() == null) {
413413
UI ui = getCurrentUI();
414414
ui.beforeClientResponse(ui, context -> {
415-
ui.add(this);
415+
ui.addToModalComponent(this);
416416
autoAddedToTheUi = true;
417417
});
418418
}

‎vaadin-crud-flow-parent/vaadin-crud-flow-integration-tests/src/test/java/com/vaadin/flow/component/crud/test/CompositeIT.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.vaadin.flow.component.crud.testbench.CrudElement;
55
import com.vaadin.flow.component.dialog.testbench.DialogElement;
66
import com.vaadin.flow.component.textfield.testbench.TextFieldElement;
7+
import com.vaadin.testbench.TestBenchElement;
8+
79
import org.junit.Assert;
810
import org.junit.Test;
911

@@ -27,11 +29,8 @@ public void compositeTouchesDirtyState() {
2729
$(TextFieldElement.class).attribute("editor-role", "language-field")
2830
.first().setValue("English");
2931

30-
$("vaadin-crud-dialog-overlay").first().$(ButtonElement.class)
31-
.attribute("editor-role", "language-confirm").first().click();
32-
33-
$(DialogElement.class).attribute("editor-role", "composite-dialog")
34-
.first().callFunction("set", "opened", false);
32+
$(TestBenchElement.class).id("overlay").$(ButtonElement.class).first()
33+
.click();
3534

3635
Assert.assertTrue(
3736
$(CrudElement.class).first().getEditorSaveButton().isEnabled());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2000-2022 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.dialog.tests;
17+
18+
import com.vaadin.flow.component.dialog.Dialog;
19+
import com.vaadin.flow.component.html.Div;
20+
import com.vaadin.flow.component.html.Hr;
21+
import com.vaadin.flow.component.html.NativeButton;
22+
import com.vaadin.flow.router.Route;
23+
24+
/**
25+
* Page created for testing purposes. Not suitable for demos.
26+
*
27+
* @author Vaadin Ltd.
28+
*
29+
*/
30+
@Route("vaadin-dialog/dialog-modality")
31+
public class ModalityDialogsPage extends Div {
32+
33+
private Log log = new Log();
34+
35+
public ModalityDialogsPage() {
36+
Dialog modalDialog = createModalDialog();
37+
38+
Dialog nonModalDialog = setupNonModalDialog();
39+
40+
NativeButton modalButton = new NativeButton("Open modal dialog",
41+
event -> modalDialog.open());
42+
modalButton.setId("open-modal-dialog");
43+
44+
NativeButton button = new NativeButton("Open non modal dialog",
45+
event -> nonModalDialog.open());
46+
button.setId("open-dialog");
47+
48+
NativeButton logButton = new NativeButton("Log",
49+
event -> log.log("Clicked"));
50+
logButton.setId("log");
51+
52+
final NativeButton showModal = new NativeButton("Show Hidden Modal",
53+
e -> modalDialog.setVisible(true));
54+
showModal.setId("show");
55+
add(modalButton, showModal, button, logButton, new Hr(), log);
56+
}
57+
58+
private Dialog setupNonModalDialog() {
59+
final Dialog nonModalDialog = createNonModalDialog();
60+
nonModalDialog
61+
.add(new NativeButton("close", e -> nonModalDialog.close()));
62+
return nonModalDialog;
63+
}
64+
65+
private Dialog createModalDialog() {
66+
Dialog modalDialog = new Dialog();
67+
modalDialog.setCloseOnOutsideClick(false);
68+
final NativeButton modalClose = new NativeButton("close",
69+
e -> modalDialog.close());
70+
modalClose.setId("close");
71+
final NativeButton hide = new NativeButton("hide",
72+
e -> modalDialog.setVisible(false));
73+
hide.setId("hide");
74+
modalDialog.add(modalClose, hide);
75+
76+
Dialog nonModalDialog = createNonModalDialog();
77+
78+
final NativeButton closeSub = new NativeButton("close",
79+
e -> nonModalDialog.close());
80+
closeSub.setId("close-sub");
81+
final NativeButton logSub = new NativeButton("log",
82+
e -> log.log("sub-click"));
83+
logSub.setId("log-sub");
84+
nonModalDialog.add(closeSub, logSub);
85+
86+
final NativeButton openSubDialog = new NativeButton("open dialog",
87+
e -> nonModalDialog.open());
88+
openSubDialog.setId("open-sub");
89+
modalDialog.add(openSubDialog);
90+
91+
return modalDialog;
92+
}
93+
94+
private Dialog createNonModalDialog() {
95+
Dialog nonModalDialog = new Dialog();
96+
97+
nonModalDialog.setCloseOnOutsideClick(false);
98+
nonModalDialog.setModal(false);
99+
100+
return nonModalDialog;
101+
}
102+
103+
public static class Log extends Div {
104+
105+
public static final String LOG_ID = "log-output";
106+
107+
private int logCount;
108+
109+
public Log() {
110+
setId(LOG_ID);
111+
}
112+
113+
public void log(String msg) {
114+
Div div = new Div();
115+
div.addClassName("log");
116+
logCount++;
117+
div.setText(logCount + ". " + msg);
118+
add(div);
119+
}
120+
}
121+
122+
}

0 commit comments

Comments
 (0)