Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: virtual list item selection #6912

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<groupId>com.vaadin</groupId>
<artifactId>flow-html-components</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-lumo-theme</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-data</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.virtuallist.tests;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.virtuallist.VirtualList;
import com.vaadin.flow.component.virtuallist.VirtualList.SelectionMode;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.renderer.LitRenderer;
import com.vaadin.flow.data.selection.SelectionListener;
import com.vaadin.flow.data.selection.SelectionModel;
import com.vaadin.flow.router.Route;

/**
* Test view for {@link VirtualList}
*
* @author Vaadin Ltd.
*/
@Route("vaadin-virtual-list/selection")
public class VirtualListSelectionPage extends Div {

public VirtualListSelectionPage() {
var list = new VirtualList<Item>();
list.setHeight("200px");

var items = createItems();
list.setDataProvider(DataProvider.ofCollection(items));

list.setRenderer(LitRenderer.<Item> of("<div>${item.name}</div>")
.withProperty("name", item -> item.name));

list.setItemAccessibleNameGenerator(item -> "Accessible " + item.name);

add(list);

var selectedIndexes = new Div();
selectedIndexes.setHeight("30px");
selectedIndexes.setId("selected-indexes");
SelectionListener<VirtualList<Item>, Item> selectionListener = event -> {
selectedIndexes.setText(event.getAllSelectedItems().stream()
.map(item -> String.valueOf(items.indexOf(item)))
.collect(Collectors.joining(", ")));
};

add(new Div(new H2("Selected item indexes"), selectedIndexes));

var selectFirstButton = new NativeButton("Select first item", e -> {
list.select(items.get(0));
});
selectFirstButton.setId("select-first");

var deselectAllButton = new NativeButton("Deselect all", e -> {
list.deselectAll();
});
deselectAllButton.setId("deselect-all");

add(new Div(new H2("Actions"), selectFirstButton, deselectAllButton));

var noneSelectionModeButton = new NativeButton("None", e -> {
list.setSelectionMode(SelectionMode.NONE);
});
noneSelectionModeButton.setId("none-selection-mode");

var singleSelectionModeButton = new NativeButton("Single", e -> {
list.setSelectionMode(SelectionMode.SINGLE);
list.addSelectionListener(selectionListener);
});
singleSelectionModeButton.setId("single-selection-mode");

var singleSelectionModeDeselectionDisallowedButton = new NativeButton(
"Single (deselection disallowed)", e -> {
var model = list.setSelectionMode(SelectionMode.SINGLE);
((SelectionModel.Single<VirtualList<Item>, Item>) model)
.setDeselectAllowed(false);
list.addSelectionListener(selectionListener);
});
singleSelectionModeDeselectionDisallowedButton
.setId("single-selection-mode-deselection-disallowed");

var multiSelectionModeButton = new NativeButton("Multi", e -> {
list.setSelectionMode(SelectionMode.MULTI);
list.addSelectionListener(selectionListener);
});
multiSelectionModeButton.setId("multi-selection-mode");

add(new Div(new H2("Selection mode"), noneSelectionModeButton,
singleSelectionModeButton,
singleSelectionModeDeselectionDisallowedButton,
multiSelectionModeButton));

}

private List<Item> createItems() {
return IntStream.range(0, 1000).mapToObj(i -> new Item("Item " + i, i))
.collect(Collectors.toList());
}

public static record Item(String name, int value) {
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.virtuallist.tests;

import java.util.Set;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;

import com.vaadin.flow.component.virtuallist.testbench.VirtualListElement;
import com.vaadin.flow.testutil.TestPath;
import com.vaadin.testbench.TestBenchElement;
import com.vaadin.tests.AbstractComponentIT;

@TestPath("vaadin-virtual-list/selection")
public class VirtualListSelectionIT extends AbstractComponentIT {
private VirtualListElement virtualList;
private TestBenchElement singleSelectionModeButton;
private TestBenchElement singleSelectionModeDeselectionDisallowedButton;
private TestBenchElement multiSelectionModeButton;
private TestBenchElement selectFirstButton;
private TestBenchElement deselectAllButton;
private TestBenchElement selectedIndexes;

@Before
public void init() {
open();
virtualList = $(VirtualListElement.class).waitForFirst();
singleSelectionModeButton = $("button").id("single-selection-mode");
singleSelectionModeDeselectionDisallowedButton = $("button")
.id("single-selection-mode-deselection-disallowed");
multiSelectionModeButton = $("button").id("multi-selection-mode");
selectFirstButton = $("button").id("select-first");
deselectAllButton = $("button").id("deselect-all");
selectedIndexes = $("div").id("selected-indexes");
}

@Test
public void select_shouldNotSelect() {
virtualList.select(0);

Assert.assertFalse(virtualList.isRowSelected(0));
}

@Test
public void singleSelectionMode_select() {
singleSelectionModeButton.click();
virtualList.select(0);
Assert.assertTrue(virtualList.isRowSelected(0));
}

@Test
public void singleSelectionMode_selectAnother() {
singleSelectionModeButton.click();
virtualList.select(0);
virtualList.select(1);
Assert.assertFalse(virtualList.isRowSelected(0));
Assert.assertTrue(virtualList.isRowSelected(1));
}

@Test
public void singleSelectionMode_deselect() {
singleSelectionModeButton.click();
virtualList.select(0);
virtualList.deselect(0);
Assert.assertFalse(virtualList.isRowSelected(0));
}

@Test
public void singleSelectionModeDeselectionDisallowed_deselectionNotAllowed() {
singleSelectionModeDeselectionDisallowedButton.click();
Assert.assertFalse(virtualList.isRowSelected(0));
virtualList.select(0);
virtualList.deselect(0);
Assert.assertTrue(virtualList.isRowSelected(0));
}

@Test
public void multiSelectionMode_selectMultiple() {
multiSelectionModeButton.click();
virtualList.select(0);
virtualList.select(2);
Assert.assertTrue(virtualList.isRowSelected(0));
Assert.assertFalse(virtualList.isRowSelected(1));
Assert.assertTrue(virtualList.isRowSelected(2));
}

@Test
public void multiSelectionMode_deselect() {
multiSelectionModeButton.click();
virtualList.select(0);
virtualList.select(2);
virtualList.select(1);
virtualList.deselect(0);
Assert.assertFalse(virtualList.isRowSelected(0));
Assert.assertTrue(virtualList.isRowSelected(1));
Assert.assertTrue(virtualList.isRowSelected(2));
}

@Test
public void multiSelectionMode_serverSideSelection() {
multiSelectionModeButton.click();
selectFirstButton.click();
virtualList.select(3);
virtualList.select(80);
virtualList.deselect(0);

var selectedIndexesSet = Set.of(selectedIndexes.getText().split(", "));
Assert.assertEquals(Set.of("3", "80"), selectedIndexesSet);
}

@Test
public void programmaticSelection() {
singleSelectionModeButton.click();
selectFirstButton.click();
Assert.assertTrue(virtualList.isRowSelected(0));

var selectedIndexesSet = Set.of(selectedIndexes.getText().split(", "));
Assert.assertEquals(Set.of("0"), selectedIndexesSet);

deselectAllButton.click();
Assert.assertFalse(virtualList.isRowSelected(0));
Assert.assertTrue(selectedIndexes.getText().isEmpty());
}

@Test
public void accessibleName() {
var firstChildElement = virtualList
.findElement(By.xpath("child::div[@aria-posinset='1']"));
Assert.assertEquals("Accessible Item 0",
firstChildElement.getAttribute("aria-label"));
}

@Test
public void changeSelectionMode_resetSelection() {
singleSelectionModeButton.click();
virtualList.select(0);
multiSelectionModeButton.click();
Assert.assertFalse(virtualList.isRowSelected(0));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<artifactId>vaadin-renderer-flow</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>flow-test-generic</artifactId>
Expand Down
Loading