-
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: add ClientItemToggleEvent to notify when user toggles an item (#…
- Loading branch information
Showing
12 changed files
with
422 additions
and
30 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
...-tests/src/main/java/com/vaadin/flow/component/grid/it/GridClientItemToggleEventPage.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,64 @@ | ||
/* | ||
* 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.stream.IntStream; | ||
|
||
import com.vaadin.flow.component.grid.Grid; | ||
import com.vaadin.flow.component.grid.GridMultiSelectionModel; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
|
||
import elemental.json.Json; | ||
import elemental.json.JsonObject; | ||
|
||
@Route("vaadin-grid/grid-client-item-toggle-event") | ||
public class GridClientItemToggleEventPage extends Div { | ||
public GridClientItemToggleEventPage() { | ||
Grid<String> grid = new Grid<>(); | ||
grid.addColumn(s -> s); | ||
grid.setItems(createItems(0, 1000)); | ||
grid.setSelectionMode(Grid.SelectionMode.MULTI); | ||
|
||
Div eventLog = new Div(); | ||
eventLog.setId("event-log"); | ||
|
||
((GridMultiSelectionModel<String>) grid.getSelectionModel()) | ||
.addClientItemToggleListener(event -> { | ||
JsonObject record = Json.createObject(); | ||
record.put("isFromClient", event.isFromClient()); | ||
record.put("item", event.getItem()); | ||
record.put("isSelected", event.isSelected()); | ||
record.put("isShiftKey", event.isShiftKey()); | ||
eventLog.add(new Div(record.toString())); | ||
}); | ||
|
||
NativeButton clearEventLog = new NativeButton("Clear event log", | ||
event -> { | ||
eventLog.removeAll(); | ||
}); | ||
clearEventLog.setId("clear-event-log"); | ||
|
||
add(grid, eventLog, clearEventLog); | ||
} | ||
|
||
private List<String> createItems(int start, int end) { | ||
return IntStream.range(start, end).mapToObj((i) -> "Item " + i) | ||
.toList(); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...on-tests/src/test/java/com/vaadin/flow/component/grid/it/GridClientItemToggleEventIT.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,98 @@ | ||
/* | ||
* 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 org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.Keys; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.interactions.Actions; | ||
|
||
import com.vaadin.flow.component.grid.testbench.GridElement; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import com.vaadin.tests.AbstractComponentIT; | ||
|
||
import elemental.json.Json; | ||
import elemental.json.JsonObject; | ||
|
||
@TestPath("vaadin-grid/grid-client-item-toggle-event") | ||
public class GridClientItemToggleEventIT extends AbstractComponentIT { | ||
private GridElement grid; | ||
|
||
@Before | ||
public void init() { | ||
open(); | ||
grid = $(GridElement.class).first(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
shiftUp(); | ||
} | ||
|
||
@Test | ||
public void toggleWithClick_eventIsFired() { | ||
grid.select(0); | ||
assertEvent("Item 0", true, false); | ||
|
||
grid.deselect(0); | ||
assertEvent("Item 0", false, false); | ||
} | ||
|
||
@Test | ||
public void toggleWithShiftClick_eventIsFired() { | ||
shiftDown(); | ||
grid.select(0); | ||
assertEvent("Item 0", true, true); | ||
|
||
grid.deselect(0); | ||
assertEvent("Item 0", false, true); | ||
} | ||
|
||
private void assertEvent(String item, boolean isSelected, | ||
boolean isShiftKey) { | ||
List<WebElement> records = findElements( | ||
By.cssSelector("#event-log > div")); | ||
Assert.assertEquals( | ||
"GridClientItemToggleEvent should be fired only once", 1, | ||
records.size()); | ||
|
||
JsonObject record = Json.parse(records.get(0).getText()); | ||
Assert.assertTrue("isFromClient should be true", | ||
record.getBoolean("isFromClient")); | ||
Assert.assertEquals("Item should match the toggled item", item, | ||
record.getString("item")); | ||
Assert.assertEquals("isSelected should match the selected state", | ||
isSelected, record.getBoolean("isSelected")); | ||
Assert.assertEquals("isShiftKey should match the shift key state", | ||
isShiftKey, record.getBoolean("isShiftKey")); | ||
|
||
findElement(By.id("clear-event-log")).click(); | ||
} | ||
|
||
private void shiftDown() { | ||
new Actions(getDriver()).keyDown(Keys.SHIFT).perform(); | ||
} | ||
|
||
private void shiftUp() { | ||
new Actions(getDriver()).keyUp(Keys.SHIFT).perform(); | ||
} | ||
} |
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.