Skip to content

Commit

Permalink
chore: On GridElement, use _itemEquals to compare with the activeItem…
Browse files Browse the repository at this point in the history
… instead of == (#689)
  • Loading branch information
tulioag authored Mar 3, 2021
1 parent f62abc0 commit abaf46e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.data.bean.Person;
import com.vaadin.flow.data.renderer.TemplateRenderer;
import com.vaadin.flow.router.Route;

@Route(value = "vaadin-grid/beangridpage")
public class BeanGridPage extends Div {

public BeanGridPage() {
Grid<Person> grid = new Grid<>(Person.class);
grid.setItems(new Person("Jorma", 2018));
grid.setItems(new Person("Jorma", 2018),
new Person("Jarvi", 33));
grid.setItemDetailsRenderer(
TemplateRenderer.<Person>of("<div>[[item.name]] [[item.age]]</div>")
.withProperty("name", Person::getFirstName)
.withProperty("age", Person::getAge));
add(grid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/
package com.vaadin.flow.component.grid.it;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.flow.component.grid.testbench.GridElement;
import com.vaadin.tests.AbstractComponentIT;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.tests.AbstractComponentIT;
import org.junit.Assert;
import org.junit.Test;

@TestPath("vaadin-grid/beangridpage")
public class BeanGridIT extends AbstractComponentIT {
Expand All @@ -33,4 +32,28 @@ public void gridNullValuesRenderedAsEmptyStrings() {
Assert.assertFalse("Null values should be presented as empty strings", text.contains("null"));
}

@Test
public void rowCanBeDeselectedOnSingleSelectMode() {
open();
GridElement grid = $(GridElement.class).first();

// Select first row.
assertRowSelectionStatus(grid,0,false);
grid.select(0);
assertRowSelectionStatus(grid,0,true);

// Try to deselect the wrong row
grid.deselect(1);
assertRowSelectionStatus(grid,0,true);

// Deselect the first row
grid.deselect(0);
assertRowSelectionStatus(grid,0,false);
}

private void assertRowSelectionStatus(GridElement grid, int rowIndex,
boolean expectedStatus) {
Assert.assertEquals("Unexpected selection status for row " + rowIndex,
expectedStatus, grid.getRow(rowIndex).isSelected());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public void deselect(int rowIndex) {
/**
* Deselects the row with the given index.
*
* @param rowIndex
* @param row
* the row to deselect
*/
void deselect(GridTRElement row) {
Expand All @@ -388,7 +388,10 @@ private void setActiveItem(GridTRElement row) {
}

private void removeActiveItem(GridTRElement row) {
executeScript("if(arguments[0].activeItem == arguments[1]._item) { arguments[0].activeItem=null;}", this, row);
final String JS_DEACTIVATE_IF_ACTIVE =
"if(arguments[0]._itemsEqual(arguments[0].activeItem, "
+ "arguments[1]._item)) { arguments[0].activeItem=null;}";
executeScript(JS_DEACTIVATE_IF_ACTIVE, this, row);
}

/**
Expand Down

0 comments on commit abaf46e

Please sign in to comment.