Browse Source

Show empty selection caption in ComboBox (#9468)

Fixes #9079
tags/8.1.0.beta2
Teemu Suo-Anttila 7 years ago
parent
commit
3b3c647e5b

+ 30
- 5
client/src/main/java/com/vaadin/client/ui/VComboBox.java View File

@@ -243,12 +243,12 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
return $entry(function(e) {
var deltaX = e.deltaX ? e.deltaX : -0.5*e.wheelDeltaX;
var deltaY = e.deltaY ? e.deltaY : -0.5*e.wheelDeltaY;
// IE8 has only delta y
if (isNaN(deltaY)) {
deltaY = -0.5*e.wheelDelta;
}
@com.vaadin.client.ui.VComboBox.JsniUtil::moveScrollFromEvent(*)(widget, deltaX, deltaY, e, e.deltaMode);
});
}-*/;
@@ -1669,6 +1669,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
* field even for filtering.
*/
private boolean textInputEnabled = true;
private String emptySelectionCaption = "";

private final DataReceivedHandler dataReceivedHandler = new DataReceivedHandler();

@@ -1949,7 +1950,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
dataReceivedHandler.cancelPendingPostFiltering();

currentSuggestion = null;
setText("");
setText(getEmptySelectionCaption());
setSelectedItemIcon(null);

if (!"".equals(selectedOptionKey) || selectedOptionKey != null) {
@@ -1958,6 +1959,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
connector.sendSelection(null);
// currentPage = 0;
}

updatePlaceholder();

suggestionPopup.hide();
@@ -2028,7 +2030,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
if (selectedKey == null || "".equals(selectedKey)) {
currentSuggestion = null; // #13217
selectedOptionKey = null;
setText("");
setText(getEmptySelectionCaption());
}
// some item selected
for (ComboBoxSuggestion suggestion : currentSuggestions) {
@@ -2305,7 +2307,7 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,

// just fetch selected information from state
String text = connector.getState().selectedItemCaption;
setText(text == null ? "" : text);
setText(text == null ? getEmptySelectionCaption() : text);
setSelectedItemIcon(connector.getState().selectedItemIcon);
selectedOptionKey = (connector.getState().selectedItemKey);
if (selectedOptionKey == null || "".equals(selectedOptionKey)) {
@@ -2820,4 +2822,27 @@ public class VComboBox extends Composite implements Field, KeyDownHandler,
public boolean getNullSelectionItemShouldBeVisible() {
return nullSelectionAllowed && "".equals(lastFilter);
}

/**
* Gets the empty selection caption.
*
* @return the empty selection caption
*/
public String getEmptySelectionCaption() {
return emptySelectionCaption;
}

/**
* Sets the empty selection caption for this VComboBox. The text is
* displayed in the text input when nothing is selected.
*
* @param emptySelectionCaption
* the empty selection caption
*/
public void setEmptySelectionCaption(String emptySelectionCaption) {
this.emptySelectionCaption = emptySelectionCaption;
if (selectedOptionKey == null) {
setText(emptySelectionCaption);
}
}
}

+ 3
- 1
client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java View File

@@ -105,9 +105,11 @@ public class ComboBoxConnector extends AbstractListingConnector
suggestions.remove(0);
addEmptySelectionItem();
}
getWidget().setEmptySelectionCaption(getState().emptySelectionCaption);
}

@OnStateChange({ "selectedItemKey", "selectedItemCaption", "selectedItemIcon" })
@OnStateChange({ "selectedItemKey", "selectedItemCaption",
"selectedItemIcon" })
private void onSelectionChange() {
getDataReceivedHandler().updateSelectionFromServer(
getState().selectedItemKey, getState().selectedItemCaption,

+ 2
- 0
uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaption.java View File

@@ -18,6 +18,7 @@ package com.vaadin.tests.components.combobox;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Button;
@@ -27,6 +28,7 @@ import com.vaadin.ui.ComboBox;
* @author Vaadin Ltd
*
*/
@Widgetset("com.vaadin.DefaultWidgetSet")
public class ComboBoxEmptyCaption extends AbstractTestUI {

@Override

+ 2
- 0
uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelecting.java View File

@@ -3,12 +3,14 @@ package com.vaadin.tests.components.combobox;
import java.util.ArrayList;
import java.util.List;

import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUI;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;

@Widgetset("com.vaadin.DefaultWidgetSet")
public class ComboBoxSelecting extends AbstractReindeerTestUI {
protected ComboBox<String> comboBox;
protected List<String> items = new ArrayList<>();

+ 19
- 1
uitest/src/test/java/com/vaadin/tests/components/combobox/ComboBoxEmptyCaptionTest.java View File

@@ -21,6 +21,8 @@ import java.util.Arrays;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.interactions.Actions;

import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.ComboBoxElement;
@@ -74,11 +76,27 @@ public class ComboBoxEmptyCaptionTest extends MultiBrowserTest {
"item6", "item7", "item8", "item9", "item10");
}

@Test
public void emptyItemCaptionInTextBox() {
ComboBoxElement combo = $(ComboBoxElement.class).first();

Assert.assertEquals("", combo.getInputField().getAttribute("value"));

// set some caption for the empty selection element
$(ButtonElement.class).first().click();

Assert.assertEquals("empty",
combo.getInputField().getAttribute("value"));

}

private void ensureSuggestions(ComboBoxElement element,
String... suggestions) {
element.openPopup();
System.out.println(element.getPopupSuggestions());
Assert.assertEquals(Arrays.asList(suggestions),
new ArrayList<>(element.getPopupSuggestions()));
// Close popup
new Actions(getDriver()).sendKeys(Keys.ESCAPE).perform();
}

}

Loading…
Cancel
Save