-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: conditional selection of grid items (#6740)
* implement items selectable provider for single selection * allow selectable provider to be null * hide select all checkbox when using conditional selection * add select all test cases * make item selectable check handle undefined values * update selected items when selecting null from server * update tests * handle null items
- Loading branch information
1 parent
7e54c05
commit ca44217
Showing
11 changed files
with
630 additions
and
15 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...ation-tests/src/main/java/com/vaadin/flow/component/grid/it/ConditionalSelectionPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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.grid.it; | ||
|
||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import com.vaadin.flow.component.grid.Grid; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.component.html.Span; | ||
import com.vaadin.flow.data.selection.SelectionEvent; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Route("vaadin-grid/conditional-selection") | ||
public class ConditionalSelectionPage extends Div { | ||
private final Span selectedItems; | ||
|
||
public ConditionalSelectionPage() { | ||
Grid<Integer> grid = new Grid<>(); | ||
grid.setItems(IntStream.range(0, 10).boxed().toList()); | ||
grid.addColumn(i -> i).setHeader("Item"); | ||
|
||
selectedItems = new Span(); | ||
selectedItems.setId("selected-items"); | ||
|
||
NativeButton enableSingleSelect = new NativeButton( | ||
"Enable single selection", e -> { | ||
grid.setSelectionMode(Grid.SelectionMode.SINGLE); | ||
grid.addSelectionListener(this::updateSelection); | ||
}); | ||
enableSingleSelect.setId("enable-single-selection"); | ||
|
||
NativeButton enableMultiSelect = new NativeButton( | ||
"Enable multi selection", e -> { | ||
grid.setSelectionMode(Grid.SelectionMode.MULTI); | ||
grid.addSelectionListener(this::updateSelection); | ||
}); | ||
enableMultiSelect.setId("enable-multi-selection"); | ||
|
||
NativeButton disableSelectionFirstFive = new NativeButton( | ||
"Disable selection for first five items", e -> { | ||
grid.setItemSelectableProvider(item -> item >= 5); | ||
}); | ||
disableSelectionFirstFive.setId("disable-selection-first-five"); | ||
|
||
NativeButton allowSelectionFirstFive = new NativeButton( | ||
"Allow selection for first five items", e -> { | ||
grid.setItemSelectableProvider(item -> item < 5); | ||
}); | ||
allowSelectionFirstFive.setId("allow-selection-first-five"); | ||
|
||
add(grid); | ||
add(new Div(enableSingleSelect, enableMultiSelect)); | ||
add(new Div(disableSelectionFirstFive, allowSelectionFirstFive)); | ||
add(new Div(new Span("Selected items: "), selectedItems)); | ||
} | ||
|
||
private void updateSelection( | ||
SelectionEvent<Grid<Integer>, Integer> selectionEvent) { | ||
String items = selectionEvent.getAllSelectedItems().stream() | ||
.map(Object::toString).collect(Collectors.joining(",")); | ||
selectedItems.setText(items); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...gration-tests/src/test/java/com/vaadin/flow/component/grid/it/ConditionalSelectionIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.grid.it; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.vaadin.flow.component.grid.testbench.GridElement; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import com.vaadin.testbench.TestBenchElement; | ||
import com.vaadin.tests.AbstractComponentIT; | ||
|
||
@TestPath("vaadin-grid/conditional-selection") | ||
public class ConditionalSelectionIT extends AbstractComponentIT { | ||
private GridElement grid; | ||
|
||
@Before | ||
public void init() { | ||
open(); | ||
grid = $(GridElement.class).waitForFirst(); | ||
} | ||
|
||
@Test | ||
public void singleSelect_clickRow_preventsSelection() { | ||
$("button").id("enable-single-selection").click(); | ||
$("button").id("disable-selection-first-five").click(); | ||
|
||
// Prevents selection of non-selectable item | ||
grid.select(0); | ||
assertSelectedItems(Set.of()); | ||
|
||
// Allows selection of selectable item | ||
grid.select(5); | ||
assertSelectedItems(Set.of(5)); | ||
} | ||
|
||
@Test | ||
public void singleSelect_clickRow_preventsDeselection() { | ||
$("button").id("enable-single-selection").click(); | ||
grid.select(0); | ||
|
||
$("button").id("disable-selection-first-five").click(); | ||
|
||
// Prevents deselection of non-selectable item | ||
grid.deselect(0); | ||
assertSelectedItems(Set.of(0)); | ||
|
||
// Allows deselection of selectable item | ||
grid.select(5); | ||
grid.deselect(5); | ||
assertSelectedItems(Set.of()); | ||
} | ||
|
||
@Test | ||
public void multiSelect_hidesCheckboxes() { | ||
$("button").id("enable-multi-selection").click(); | ||
$("button").id("disable-selection-first-five").click(); | ||
|
||
Assert.assertFalse(getItemCheckbox(0).isDisplayed()); | ||
Assert.assertTrue(getItemCheckbox(5).isDisplayed()); | ||
} | ||
|
||
@Test | ||
public void multiSelect_updateProvider_updatesCheckboxes() { | ||
$("button").id("enable-multi-selection").click(); | ||
$("button").id("disable-selection-first-five").click(); | ||
|
||
Assert.assertFalse(getItemCheckbox(0).isDisplayed()); | ||
Assert.assertTrue(getItemCheckbox(5).isDisplayed()); | ||
|
||
$("button").id("allow-selection-first-five").click(); | ||
|
||
Assert.assertTrue(getItemCheckbox(0).isDisplayed()); | ||
Assert.assertFalse(getItemCheckbox(5).isDisplayed()); | ||
} | ||
|
||
private TestBenchElement getItemCheckbox(int index) { | ||
return grid.getCell(index, 0).$("vaadin-checkbox").first(); | ||
} | ||
|
||
private Set<Integer> getServerSelectedItems() { | ||
var items = $("span").id("selected-items").getText(); | ||
return items.isEmpty() ? Set.of() | ||
: Stream.of(items.split(",")).map(Integer::parseInt) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Set<Integer> getClientSelectedItems() { | ||
var itemNames = (List<String>) getCommandExecutor().executeScript( | ||
"return arguments[0].selectedItems.map(item => item.col0)", | ||
grid); | ||
return itemNames.stream().map(Integer::parseInt) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private void assertSelectedItems(Set<Integer> items) { | ||
Assert.assertEquals(items, getServerSelectedItems()); | ||
Assert.assertEquals(items, getClientSelectedItems()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.