From 104eb9bb3543046eb9762fbdf9620fa045aa67e0 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Gueriaud Date: Tue, 6 Apr 2021 18:55:18 +0300 Subject: [PATCH 1/3] Bump the version and add enable selection + focus --- lookup-field-flow-demo/pom.xml | 4 +- .../lookupfield/EnableSelectButtonView.java | 34 +++++++++++ .../lookupfield/MainLayout.java | 4 +- lookup-field-flow/pom.xml | 4 +- .../lookupfield/LookupField.java | 57 ++++++++++++++++++- pom.xml | 4 +- 6 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 lookup-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/EnableSelectButtonView.java diff --git a/lookup-field-flow-demo/pom.xml b/lookup-field-flow-demo/pom.xml index 50e4077..9398ebe 100644 --- a/lookup-field-flow-demo/pom.xml +++ b/lookup-field-flow-demo/pom.xml @@ -6,7 +6,7 @@ com.vaadin.componentfactory lookup-field-flow-demo - 1.0.6 + 1.1.0 Lookup Field Demo war @@ -96,7 +96,7 @@ com.vaadin.componentfactory lookup-field-flow - 1.0.6 + 1.1.0 diff --git a/lookup-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/EnableSelectButtonView.java b/lookup-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/EnableSelectButtonView.java new file mode 100644 index 0000000..4313de5 --- /dev/null +++ b/lookup-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/EnableSelectButtonView.java @@ -0,0 +1,34 @@ +package com.vaadin.componentfactory.lookupfield; + +import com.vaadin.componentfactory.lookupfield.bean.Person; +import com.vaadin.componentfactory.lookupfield.service.PersonService; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.data.provider.DataProvider; +import com.vaadin.flow.router.Route; +import com.vaadin.flow.router.RouteAlias; + +import java.util.List; + +/** + * Selection button enabled by default + */ +@Route(value = "enable-selection", layout = MainLayout.class) +public class EnableSelectButtonView extends Div { + + + public EnableSelectButtonView() { + LookupField lookupField = new LookupField<>(Person.class); + List items = getItems(); + lookupField.setDataProvider(DataProvider.ofCollection(items)); + lookupField.setGridWidth("900px"); + lookupField.setHeader("Person Search"); + lookupField.setSelectionDisabledIfEmpty(false); + add(lookupField); + } + + private List getItems() { + PersonService personService = new PersonService(); + return personService.fetchAll(); + } +} diff --git a/lookup-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/MainLayout.java b/lookup-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/MainLayout.java index f612ce9..bc75965 100644 --- a/lookup-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/MainLayout.java +++ b/lookup-field-flow-demo/src/main/java/com/vaadin/componentfactory/lookupfield/MainLayout.java @@ -15,7 +15,9 @@ public MainLayout() { final RouterLink i18nView = new RouterLink("I18n example", I18nView.class); final RouterLink binderView = new RouterLink("Binder example", BinderView.class); final RouterLink customHeader = new RouterLink("Custom Header", CustomHeaderView.class); - final VerticalLayout menuLayout = new VerticalLayout(personLookupField, simple, personLabelLookupField, i18nView, binderView, customHeader); + final RouterLink enableSelectButtonView = new RouterLink("Selection Button enabled", EnableSelectButtonView.class); + final VerticalLayout menuLayout = new VerticalLayout(personLookupField, simple, personLabelLookupField, i18nView, + binderView, customHeader, enableSelectButtonView); addToDrawer(menuLayout); addToNavbar(drawerToggle); } diff --git a/lookup-field-flow/pom.xml b/lookup-field-flow/pom.xml index f6bdc59..cd178f0 100644 --- a/lookup-field-flow/pom.xml +++ b/lookup-field-flow/pom.xml @@ -6,7 +6,7 @@ com.vaadin.componentfactory lookup-field-flow - 1.0.6 + 1.1.0 jar Lookup Field @@ -104,7 +104,7 @@ com.vaadin.componentfactory enhanced-dialog - 1.0.3 + 1.0.4 diff --git a/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java b/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java index 1172e68..97ac829 100644 --- a/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java +++ b/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java @@ -43,6 +43,7 @@ import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.icon.Icon; +import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.data.binder.HasFilterableDataProvider; import com.vaadin.flow.data.provider.ConfigurableFilterDataProvider; @@ -74,7 +75,7 @@ @Uses(value = EnhancedDialog.class) @Tag("vcf-lookup-field") @JsModule("@vaadin-component-factory/vcf-lookup-field") -@NpmPackage(value = "@vaadin-component-factory/vcf-lookup-field", version = "1.0.8") +@NpmPackage(value = "@vaadin-component-factory/vcf-lookup-field", version = "1.1.0") public class LookupField extends Div implements HasFilterableDataProvider, HasValueAndElement, T>, T>, HasValidation, HasHelper, HasSize, HasTheme { @@ -90,6 +91,7 @@ public class LookupField extends Div implements HasFilterableDataProvider gridDataProvider; private Component header; private Component footer; + private Notification emptyNotification; public LookupField() { this(new Grid<>(), new ComboBox<>()); @@ -379,6 +381,28 @@ public boolean isResizable() { return getElement().getProperty("resizable", false); } + /** + * Sets whether the select button is disabled or send an error when the selection is empty or not. + * + * @param defaultselectdisabled + * {@code true} to disabled the button if no item is disabled, + * {@code false} otherwise. + */ + public void setSelectionDisabledIfEmpty(boolean defaultselectdisabled) { + getElement().setProperty("defaultselectdisabled", defaultselectdisabled); + } + + /** + * Gets whether the select button is disabled or send an error when the selection is empty or not. + * + * @return + * {@code true} if resizing is enabled, + * {@code false} otherwiser (default). + */ + public boolean getSelectionDisabledIfEmpty() { + return getElement().getProperty("defaultselectdisabled", true); + } + /** * Gets the internationalization object previously set for this component. *

@@ -548,6 +572,27 @@ public void footerCloseAction() { getElement().executeJs("$0.__close()", getElement()); } + /** + * Copy the selected value of the field into the grid + */ + @ClientCallable + private void openErrorNotification() { + getEmptyNotification().open(); + } + + + public Notification getEmptyNotification() { + if (emptyNotification == null) { + String emptySelection = (getI18n() == null)? "Please select an item.":getI18n().getEmptyselection(); + emptyNotification = new Notification(emptySelection); + emptyNotification.setPosition(Notification.Position.TOP_CENTER); + } + return emptyNotification; + } + + public void setEmptyNotification(Notification emptyNotification) { + this.emptyNotification = emptyNotification; + } /** * The internationalization properties for {@link LookupField}. @@ -559,6 +604,7 @@ public static class LookupFieldI18n implements Serializable { private String headerprefix; private String headerpostfix; private String search; + private String emptyselection; public String getSearch() { return search; @@ -613,5 +659,14 @@ public LookupFieldI18n setHeaderpostfix(String headerpostfix) { this.headerpostfix = headerpostfix; return this; } + + public String getEmptyselection() { + return emptyselection; + } + + public LookupFieldI18n setEmptyselection(String emptyselection) { + this.emptyselection = emptyselection; + return this; + } } } diff --git a/pom.xml b/pom.xml index f89e176..ecb3284 100755 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.vaadin.componentfactory lookup-field-flow-root - 1.0.6 + 1.1.0 pom lookup-field-flow @@ -18,7 +18,7 @@ 1.8 UTF-8 UTF-8 - 1.0.6 + 1.1.0 2020 From ed7ebfec4322d711d0791044308851c74d501122 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Gueriaud Date: Wed, 7 Apr 2021 09:23:21 +0300 Subject: [PATCH 2/3] Add a timeout to the notification --- .../com/vaadin/componentfactory/lookupfield/LookupField.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java b/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java index 97ac829..490cb45 100644 --- a/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java +++ b/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java @@ -584,8 +584,7 @@ private void openErrorNotification() { public Notification getEmptyNotification() { if (emptyNotification == null) { String emptySelection = (getI18n() == null)? "Please select an item.":getI18n().getEmptyselection(); - emptyNotification = new Notification(emptySelection); - emptyNotification.setPosition(Notification.Position.TOP_CENTER); + emptyNotification = new Notification(emptySelection, 2000, Notification.Position.TOP_CENTER); } return emptyNotification; } From 38dd4ee1ed6dbc35fb63a0c20c4a9344ca2e61d0 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Gueriaud Date: Wed, 7 Apr 2021 09:48:05 +0300 Subject: [PATCH 3/3] Focus on Esc + use a listener to replace the notification when selection is empty --- .../lookupfield/LookupField.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java b/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java index 490cb45..8404493 100644 --- a/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java +++ b/lookup-field-flow/src/main/java/com/vaadin/componentfactory/lookupfield/LookupField.java @@ -75,7 +75,7 @@ @Uses(value = EnhancedDialog.class) @Tag("vcf-lookup-field") @JsModule("@vaadin-component-factory/vcf-lookup-field") -@NpmPackage(value = "@vaadin-component-factory/vcf-lookup-field", version = "1.1.0") +@NpmPackage(value = "@vaadin-component-factory/vcf-lookup-field", version = "1.1.1") public class LookupField extends Div implements HasFilterableDataProvider, HasValueAndElement, T>, T>, HasValidation, HasHelper, HasSize, HasTheme { @@ -91,7 +91,7 @@ public class LookupField extends Div implements HasFilterableDataProvider gridDataProvider; private Component header; private Component footer; - private Notification emptyNotification; + private Runnable notificationWhenEmptySelection; public LookupField() { this(new Grid<>(), new ComboBox<>()); @@ -577,20 +577,26 @@ public void footerCloseAction() { */ @ClientCallable private void openErrorNotification() { - getEmptyNotification().open(); + getNotificationWhenEmptySelection().run(); } - - public Notification getEmptyNotification() { - if (emptyNotification == null) { - String emptySelection = (getI18n() == null)? "Please select an item.":getI18n().getEmptyselection(); - emptyNotification = new Notification(emptySelection, 2000, Notification.Position.TOP_CENTER); + private Runnable getNotificationWhenEmptySelection() { + if (notificationWhenEmptySelection == null) { + return () -> { + String emptySelection = (getI18n() == null)? "Please select an item.":getI18n().getEmptyselection(); + new Notification(emptySelection, 2000, Notification.Position.TOP_CENTER).open(); + }; } - return emptyNotification; + return notificationWhenEmptySelection; } - public void setEmptyNotification(Notification emptyNotification) { - this.emptyNotification = emptyNotification; + /** + * Replace the default notification to an action + * + * @param notificationWhenEmptySelection action to run when the selection is empty and the select button is clicked + */ + public void addEmptySelectionListener(Runnable notificationWhenEmptySelection) { + this.notificationWhenEmptySelection = notificationWhenEmptySelection; } /**