Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>easy-grid-addon</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
<name>Easy Grid Add-on</name>
<description>Easy Grid Add-on for Vaadin Flow</description>
<url>https://www.flowingcode.com/en/open-source/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,34 @@ public EasyRowAction<T> withConfirmation(String title,
return withConfirmation(title, messageProvider, "Ok", "Cancel");
}

private EasyRowAction<T> withConfirmation(String title,
ValueProvider<T, String> messageProvider, String confirmText, String cancelText) {
/**
* Configures a confirmation dialog with a title, a message and custom texts for the confirm and
* cancel buttons before the action is executed.
*
* @param title the dialog title, or {@code null} for a dialog without a heading
* @param message the confirmation message
* @param confirmText the text of the confirm button
* @param cancelText the text of the cancel button
* @return this action, for method chaining
*/
public EasyRowAction<T> withConfirmation(String title, String message,
@NonNull String confirmText, @NonNull String cancelText) {
return withConfirmation(title, Constant.of(message), confirmText, cancelText);
}

/**
* Configures a confirmation dialog with a static title and custom texts for the confirm and
* cancel buttons, whose message is computed from the row item when the action is clicked.
*
* @param title the dialog title, or {@code null} for a dialog without a heading
* @param messageProvider a function that returns the confirmation message for a given row item
* @param confirmText the text of the confirm button
* @param cancelText the text of the cancel button
* @return this action, for method chaining
*/
public EasyRowAction<T> withConfirmation(String title,
@NonNull ValueProvider<T, String> messageProvider, @NonNull String confirmText,
@NonNull String cancelText) {
confirmDialogFactory = item -> {
var dialog = new ConfirmDialog();
dialog.setHeader(title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public RowActionsDemo() {
Notification.show("Deactivated: " + person.getFirstName() + " " + person.getLastName()))
.visibleWhen(Person::isActive)
.withConfirmation("Deactivate person",
"Are you sure you want to deactivate this person?")
"Are you sure you want to deactivate this person?", "Deactivate", "Keep active")
.addThemeVariants(ButtonVariant.LUMO_ERROR);

// Visible only for inactive persons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ public void tooltipOverridesManualTitle() {
}

// --- confirmation dialog contents ---
// The dialog is built at click time, so its title and message are read back from the
// ConfirmDialog element properties that setHeader/setText write.
// The dialog is built at click time, so its texts are read back from the ConfirmDialog
// element properties that setHeader/setText/setConfirmText/setCancelText write.

private static String headerOf(ConfirmDialog dialog) {
return dialog.getElement().getProperty("header");
Expand All @@ -233,6 +233,14 @@ private static String messageOf(ConfirmDialog dialog) {
return dialog.getElement().getProperty("message");
}

private static String confirmTextOf(ConfirmDialog dialog) {
return dialog.getElement().getProperty("confirmText");
}

private static String cancelTextOf(ConfirmDialog dialog) {
return dialog.getElement().getProperty("cancelText");
}

@Test
public void withConfirmation_noConfirmation_hasNoDialog() {
var action = new EasyRowAction<Integer>(null, Constant.of("X"), null, item -> {});
Expand Down Expand Up @@ -287,6 +295,61 @@ public void withConfirmation_providerIsEvaluatedPerDialog() {
assertEquals("Message 2", messageOf(action.getConfirmDialog(7)));
}

@Test
public void withConfirmation_defaultButtonTexts() {
var action = new EasyRowAction<Integer>(null, Constant.of("X"), null, item -> {});
action.withConfirmation("Confirm", "Proceed?");
ConfirmDialog dialog = action.getConfirmDialog(7);
assertEquals("Ok", confirmTextOf(dialog));
assertEquals("Cancel", cancelTextOf(dialog));
}

@Test
public void withConfirmation_titleMessageAndButtonTexts_setsAllTexts() {
var action = new EasyRowAction<Integer>(null, Constant.of("X"), null, item -> {});
action.withConfirmation("Confirm", "Proceed?", "Yes", "No");
ConfirmDialog dialog = action.getConfirmDialog(7);
assertEquals("Confirm", headerOf(dialog));
assertEquals("Proceed?", messageOf(dialog));
assertEquals("Yes", confirmTextOf(dialog));
assertEquals("No", cancelTextOf(dialog));
}

@Test
public void withConfirmation_nullTitleMessageAndButtonTexts_hasNoHeader() {
var action = new EasyRowAction<Integer>(null, Constant.of("X"), null, item -> {});
action.withConfirmation(null, "Proceed?", "Yes", "No");
ConfirmDialog dialog = action.getConfirmDialog(7);
assertNull(headerOf(dialog));
assertEquals("Proceed?", messageOf(dialog));
assertEquals("Yes", confirmTextOf(dialog));
assertEquals("No", cancelTextOf(dialog));
}

@Test
public void withConfirmation_messageProviderAndButtonTexts_setsAllTexts() {
var action = new EasyRowAction<Integer>(null, Constant.of("X"), null, item -> {});
action.withConfirmation("Delete", item -> "Delete item " + item + "?", "Delete", "Keep");
ConfirmDialog dialog = action.getConfirmDialog(7);
assertEquals("Delete", headerOf(dialog));
assertEquals("Delete item 7?", messageOf(dialog));
assertEquals("Delete", confirmTextOf(dialog));
assertEquals("Keep", cancelTextOf(dialog));
assertEquals("Delete item 8?", messageOf(action.getConfirmDialog(8)));
}

@Test(expected = NullPointerException.class)
public void withConfirmation_nullConfirmText_throws() {
var action = new EasyRowAction<Integer>(null, Constant.of("X"), null, item -> {});
action.withConfirmation("Confirm", "Proceed?", null, "Cancel");
}

@Test(expected = NullPointerException.class)
public void withConfirmation_nullCancelText_throws() {
var action = new EasyRowAction<Integer>(null, Constant.of("X"), null, item -> {});
action.withConfirmation("Confirm", item -> "Proceed?", "Ok", null);
}

// --- execute: server-side enabledWhen guard ---

@Test
Expand Down
Loading