Skip to content

Commit c5c107c

Browse files
authored
feat: add removeAll functionality for dialog header and footers (#3405)
* feat: add removeAll functionality for dialog header and footers Add removeAll functionality and the corresponding integration test for dialog header and footers. * refactor: fix two typos in Dialog Fix two typos in Dialog: one in JavaDoc, and one in an exception message * refactor: remove unnecessary terminal collect operation Remove unnecessary terminal collect operation. ForEach can already be called for Stream. * fix: revert refactor commit Revert refactor commit * fix: add integration tests for remove all footer/header content buttons Add missing integration tests for remove all footer/header content buttons for Dialog * fix: add missing integration tests for dialog header/footer remove all buttons Add missing integration tests for Dialog header/footer remove all buttons
1 parent eeac346 commit c5c107c

4 files changed

Lines changed: 84 additions & 3 deletions

File tree

‎vaadin-dialog-flow-parent/vaadin-dialog-flow-integration-tests/src/main/java/com/vaadin/flow/component/dialog/tests/DialogHeaderFooterPage.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public DialogHeaderFooterPage() {
4444
e -> dialog.getHeader().remove(headerContent));
4545
removeHeaderContent.setId("remove-header-content-button");
4646

47+
NativeButton removeAllHeaderContents = new NativeButton(
48+
"remove all header contents",
49+
e -> dialog.getHeader().removeAll());
50+
removeAllHeaderContents.setId("remove-all-header-contents-button");
51+
4752
NativeButton addSecondHeaderContent = new NativeButton(
4853
"add second header content",
4954
e -> dialog.getHeader().add(new Span(ANOTHER_HEADER_CONTENT)));
@@ -59,14 +64,20 @@ public DialogHeaderFooterPage() {
5964
e -> dialog.getFooter().remove(footerContent));
6065
removeFooterContent.setId("remove-footer-content-button");
6166

67+
NativeButton removeAllFooterContents = new NativeButton(
68+
"remove all footer contents",
69+
e -> dialog.getFooter().removeAll());
70+
removeAllFooterContents.setId("remove-all-footer-contents-button");
71+
6272
NativeButton addSecondFooterContent = new NativeButton(
6373
"add second footer content",
6474
e -> dialog.getFooter().add(new Span(ANOTHER_FOOTER_CONTENT)));
6575
addSecondFooterContent.setId("add-second-footer-content-button");
6676

6777
Div buttonsContainer = new Div(openDialog, attachDialog, addHeaderTitle,
6878
removeHeaderTitle, addHeaderContent, removeHeaderContent,
69-
addSecondHeaderContent, addFooterContent, removeFooterContent,
79+
removeAllHeaderContents, addSecondHeaderContent,
80+
addFooterContent, removeFooterContent, removeAllFooterContents,
7081
addSecondFooterContent);
7182

7283
NativeButton moveButtons = new NativeButton("move buttons",

‎vaadin-dialog-flow-parent/vaadin-dialog-flow-integration-tests/src/test/java/com/vaadin/flow/component/dialog/tests/DialogHeaderFooterIT.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ public class DialogHeaderFooterIT extends AbstractComponentIT {
2222
private static final String ADD_HEADER_CONTENT_BUTTON = "add-header-content-button";
2323
private static final String ADD_SECOND_HEADER_CONTENT_BUTTON = "add-second-header-content-button";
2424
private static final String REMOVE_HEADER_CONTENT_BUTTON = "remove-header-content-button";
25+
private static final String REMOVE_ALL_HEADER_CONTENTS_BUTTON = "remove-all-header-contents-button";
2526
private static final String ADD_FOOTER_CONTENT_BUTTON = "add-footer-content-button";
2627
private static final String ADD_SECOND_FOOTER_CONTENT_BUTTON = "add-second-footer-content-button";
2728
private static final String REMOVE_FOOTER_CONTENT_BUTTON = "remove-footer-content-button";
29+
private static final String REMOVE_ALL_FOOTER_CONTENTS_BUTTON = "remove-all-footer-contents-button";
2830

2931
@Before
3032
public void init() {
@@ -123,6 +125,19 @@ public void openedDialogWithHeaderContent_removeContent_noContentIsRendered() {
123125
assertDialogNotContains(DialogHeaderFooterPage.HEADER_CONTENT);
124126
}
125127

128+
@Test
129+
public void openedDialogWithHeaderContents_removeAllContents_noContentIsRendered() {
130+
clickButton(ADD_HEADER_CONTENT_BUTTON);
131+
clickButton(ADD_SECOND_HEADER_CONTENT_BUTTON);
132+
clickButton(OPEN_DIALOG_BUTTON);
133+
verifyDialogOpened();
134+
clickButton(MOVE_BUTTONS_BUTTON);
135+
clickButton(REMOVE_ALL_HEADER_CONTENTS_BUTTON);
136+
137+
assertDialogNotContains(DialogHeaderFooterPage.HEADER_CONTENT);
138+
assertDialogNotContains(DialogHeaderFooterPage.ANOTHER_HEADER_CONTENT);
139+
}
140+
126141
@Test
127142
public void closedDialog_footerRendererIsSet_contentIsRendered() {
128143
clickButton(ADD_FOOTER_CONTENT_BUTTON);
@@ -186,6 +201,19 @@ public void openedDialogWithFooterContent_removeContent_noContentIsRendered() {
186201
assertDialogNotContains(DialogHeaderFooterPage.FOOTER_CONTENT);
187202
}
188203

204+
@Test
205+
public void openedDialogWithFooterContents_removeAllContents_noContentIsRendered() {
206+
clickButton(ADD_FOOTER_CONTENT_BUTTON);
207+
clickButton(ADD_SECOND_FOOTER_CONTENT_BUTTON);
208+
clickButton(OPEN_DIALOG_BUTTON);
209+
verifyDialogOpened();
210+
clickButton(MOVE_BUTTONS_BUTTON);
211+
clickButton(REMOVE_ALL_FOOTER_CONTENTS_BUTTON);
212+
213+
assertDialogNotContains(DialogHeaderFooterPage.FOOTER_CONTENT);
214+
assertDialogNotContains(DialogHeaderFooterPage.ANOTHER_FOOTER_CONTENT);
215+
}
216+
189217
private void clickButton(String id) {
190218
findElement(By.id(id)).click();
191219
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ private DialogHeader(Dialog dialog) {
589589
}
590590

591591
/**
592-
* Class for adding and removing components to the header part of a dialog.
592+
* Class for adding and removing components to the footer part of a dialog.
593593
*/
594594
final public static class DialogFooter extends DialogHeaderFooter {
595595
private DialogFooter(Dialog dialog) {
@@ -661,6 +661,16 @@ public void remove(Component... components) {
661661
}
662662
}
663663

664+
/**
665+
* Removes all components from the container.
666+
*/
667+
public void removeAll() {
668+
root.removeAllChildren();
669+
dialog.getElement()
670+
.executeJs("this." + rendererFunction + " = null;");
671+
setRendererCreated(false);
672+
}
673+
664674
/**
665675
* Method called to create the renderer function using
666676
* {@link #rendererFunction} as the property name.
@@ -723,7 +733,7 @@ private UI getCurrentUI() {
723733
if (ui == null) {
724734
throw new IllegalStateException("UI instance is not available. "
725735
+ "It means that you are calling this method "
726-
+ "out of a normal workflow where it's always implicitely set. "
736+
+ "out of a normal workflow where it's always implicitly set. "
727737
+ "That may happen if you call the method from the custom thread without "
728738
+ "'UI::access' or from tests without proper initialization.");
729739
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,38 @@ public void elementRemovedFromHeaderOrFooter_elementShouldNotHaveDialogAsParent(
424424
Assert.assertFalse(thirdContent.getParent().isPresent());
425425
}
426426

427+
@Test
428+
public void allElementsRemovedFromHeader_elementsShouldNotHaveDialogAsParents() {
429+
Dialog dialog = new Dialog();
430+
Span content = new Span("content");
431+
Span secondContent = new Span("second_content");
432+
Span thirdContent = new Span("third_content");
433+
434+
dialog.getHeader().add(content, secondContent, thirdContent);
435+
436+
dialog.getHeader().removeAll();
437+
438+
Assert.assertFalse(content.getParent().isPresent());
439+
Assert.assertFalse(secondContent.getParent().isPresent());
440+
Assert.assertFalse(thirdContent.getParent().isPresent());
441+
}
442+
443+
@Test
444+
public void allElementsRemovedFromFooter_elementsShouldNotHaveDialogAsParents() {
445+
Dialog dialog = new Dialog();
446+
Span content = new Span("content");
447+
Span secondContent = new Span("second_content");
448+
Span thirdContent = new Span("third_content");
449+
450+
dialog.getFooter().add(content, secondContent, thirdContent);
451+
452+
dialog.getFooter().removeAll();
453+
454+
Assert.assertFalse(content.getParent().isPresent());
455+
Assert.assertFalse(secondContent.getParent().isPresent());
456+
Assert.assertFalse(thirdContent.getParent().isPresent());
457+
}
458+
427459
@Test(expected = NullPointerException.class)
428460
public void callAddToHeaderOrFooter_withNull_shouldThrowError() {
429461
Dialog dialog = new Dialog();

0 commit comments

Comments
 (0)