Skip to content

Commit

Permalink
Allow null Components in ComponentRenderer (#9692)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsuoanttila authored and hesara committed Jul 19, 2017
1 parent ce566c5 commit e8f7de7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ public SimplePanel createWidget() {
@Override
public void render(RendererCellReference cell, String connectorId,
SimplePanel widget) {
ComponentConnector connector = (ComponentConnector) ConnectorMap
.get(getConnection()).getConnector(connectorId);
widget.setWidget(connector.getWidget());
if (connectorId != null) {
ComponentConnector connector = (ComponentConnector) ConnectorMap
.get(getConnection()).getConnector(connectorId);
widget.setWidget(connector.getWidget());
} else if (widget.getWidget() != null) {
widget.remove(widget.getWidget());
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
* instances.
* <p>
* Example of how to add a {@link Label} component to {@link Grid}:
*
* <pre>
* Grid<Person> grid;
* grid.addColumn(person -> new Label(person.getFullName()),
* new ComponentRenderer()).setCaption("Full Name");
* grid.addColumn(person -> new Label(person.getFullName()),
* new ComponentRenderer()).setCaption("Full Name");
* </pre>
*
* @author Vaadin Ltd
Expand All @@ -59,7 +60,7 @@ public ComponentRenderer() {

@Override
public JsonValue encode(Component value) {
return Json.create(value.getConnectorId());
return value != null ? Json.create(value.getConnectorId()) : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ protected void setup(VaadinRequest request) {
return textField;
}).setId("textField").setCaption("TextField");
grid.addColumn(string -> {
if (string.contains("30")) {
return null;
}
Button button = new Button("Click Me!",
e -> Notification.show(
"Clicked button on row for: " + string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.vaadin.tests.components.grid;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebElement;
Expand Down Expand Up @@ -98,6 +101,13 @@ public void testRow999() {
assertRowExists(999, "Row 999");
}

@Test
public void testRow30() {
openTestURL();
Stream.of(30, 130, 230, 330).forEach(this::assertNoButton);
IntStream.range(300, 310).forEach(this::assertNoButton);
}

@Test
public void testHeaders() {
openTestURL();
Expand All @@ -118,4 +128,10 @@ private void assertRowExists(int i, String string) {
$(NotificationElement.class).first().getText()
.contains(string));
}

private void assertNoButton(int i) {
GridRowElement row = $(GridElement.class).first().getRow(i);
Assert.assertFalse("Row " + i + " should not have a button",
row.getCell(2).isElementPresent(ButtonElement.class));
}
}

0 comments on commit e8f7de7

Please sign in to comment.