Skip to content

Commit

Permalink
fix: Fix setting value to lazy loading ComboBox (#478)
Browse files Browse the repository at this point in the history
ComboBox should only throw when no items/callback has been set to it.
There is a possibility that the value is set but the backend doesn't have
this item available, but that is an edge case and something that should
not happen and should be possible to prevent by resetting the data and
thus a responsibility of the application developer.

Fixes #391

(cherry picked from commit eb91269)
  • Loading branch information
pleku authored and mshabarov committed Dec 4, 2020
1 parent 2c80798 commit 8c762be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ private static <T> String modelToPresentation(ComboBox<T> comboBox,

@Override
public void setValue(T value) {
if (dataCommunicator == null || dataCommunicator.getItemCount() == 0) {
if (dataCommunicator == null || dataCommunicator
.getDataProvider() instanceof DataCommunicator.EmptyDataProvider) {
if (value == null) {
return;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.vaadin.flow.component.combobox;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -47,6 +45,7 @@
import com.vaadin.flow.shared.Registration;

import elemental.json.Json;
import static org.junit.Assert.assertEquals;

public class ComboBoxTest {

Expand Down Expand Up @@ -307,6 +306,16 @@ public void setValueWithoutItems_throw() {
combo.setValue("foo");
}

// https://github.com/vaadin/vaadin-flow-components/issues/391
@Test
public void setValueWithLazyItems_doesntThrow() {
final ComboBox<String> comboBox = new ComboBox<>();
comboBox.setItems(query -> Stream.of("foo", "bar"));
comboBox.setValue("foo");

Assert.assertEquals("foo", comboBox.getValue());
}

@Test
public void clearWithoutItems_doesNotThrow() {
ComboBox<String> combo = new ComboBox<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.vaadin.flow.component.grid;

import java.util.Arrays;
import java.util.stream.Stream;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -51,4 +53,13 @@ public void dataViewForFaultyDataProvider_throwsException() {
grid.getListDataView();
}

@Test
public void selectItem_lazyDataSet_selectionWorks() {
final Grid<String> grid = new Grid<>();
grid.setItems(query -> Stream.of("foo", "bar"));
grid.select("foo");
Assert.assertEquals(1, grid.getSelectedItems().size());
Assert.assertTrue(grid.getSelectedItems().contains("foo"));
}

}

0 comments on commit 8c762be

Please sign in to comment.