Skip to content

Commit 3198f4c

Browse files
authored
refactor!: improve TreeGridElement APIs for scrolling to rows (#8093)
1 parent bb7510b commit 3198f4c

File tree

10 files changed

+186
-120
lines changed

10 files changed

+186
-120
lines changed

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/LazyLoadingTreeGridRefreshAllIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void setChildCountAndRefreshAll_resultIsNotEmpty() {
3838

3939
// The row index should be larger than 100 in order to replicate the
4040
// issue.
41-
treeGrid.scrollToRow(120);
41+
treeGrid.scrollToRowByPath(120);
4242

4343
// The count should be smaller than 100 in order to replicate the issue.
4444
IntegerFieldElement childCount = $(IntegerFieldElement.class)

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeComponentColumnsIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void treegridComponentRenderer_expandScrollExpand_expectedRowHeights() {
9898
var rowHeight = compThenGrid.getRow(1).getSize().getHeight();
9999
compThenGrid.expandWithClick(0);
100100
compThenGrid.expandWithClick(1);
101-
compThenGrid.scrollToRow(104);
101+
compThenGrid.scrollToRowByPath(104);
102102

103103
var visibleRows = compThenGrid.getVisibleRows();
104104
Assert.assertFalse(visibleRows.isEmpty());

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridDetachAttachIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void before() {
4141

4242
@Test
4343
public void scrollDown_detach_attach_firstItemsRendered() {
44-
grid.scrollToRow(150);
44+
grid.scrollToRowByPath(150);
4545

4646
toggleAttachedButton.click();
4747
toggleAttachedButton.click();

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridHugeTreeIT.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,24 @@ public void toggle_expand_when_row_out_of_cache() {
3939

4040
grid.expandWithClick(2);
4141
grid.expandWithClick(3);
42-
grid.scrollToRow(300);
42+
grid.scrollToRowByPath(300);
4343

4444
expandSecondRowButton.click();
4545

46-
grid.scrollToRow(0);
46+
grid.scrollToRowByPath(0);
4747
assertCellTexts(0, 0, new String[] { "Granddad 0", "Granddad 1",
4848
"Dad 1/0", "Dad 1/1", "Dad 1/2", "Granddad 2", "Dad 2/0" });
4949

50-
grid.scrollToRow(300);
50+
grid.scrollToRowByPath(300);
5151
collapseSecondRowButton.click();
52-
grid.scrollToRow(0);
52+
grid.scrollToRowByPath(0);
5353
assertCellTexts(0, 0, new String[] { "Granddad 0", "Granddad 1",
5454
"Granddad 2", "Dad 2/0" });
5555

56-
grid.scrollToRow(300);
56+
grid.scrollToRowByPath(300);
5757
expandSecondRowButton.click();
5858
collapseSecondRowButton.click();
59-
grid.scrollToRow(0);
59+
grid.scrollToRowByPath(0);
6060
assertCellTexts(0, 0, new String[] { "Granddad 0", "Granddad 1",
6161
"Granddad 2", "Dad 2/0" });
6262
}
@@ -96,7 +96,7 @@ public void root_keys_dropped_from_keymapper_properly() {
9696

9797
// Scroll first root item way out of viewport and check that the key was
9898
// dropped
99-
grid.scrollToRow(200);
99+
grid.scrollToRowByPath(200);
100100
checkFirstRootItemKey.click();
101101
Assert.assertEquals(
102102
"First root key was in KeyMapper when it should not be",
@@ -127,7 +127,7 @@ public void collapsed_subtrees_outside_of_cache_stay_expanded() {
127127
for (int i = 0; i < assumedCachedSize; i++) {
128128
cellTexts[i] = grid.getCellWaitForRow(i, 0).getText();
129129
}
130-
grid.scrollToRowAndWait(0);
130+
grid.scrollToRowByPath(0);
131131
grid.collapseWithClick(1);
132132
grid.expandWithClick(1);
133133

@@ -145,11 +145,11 @@ public void expanded_nodes_populate_after_scroll() {
145145
$("button").id("expand-recursively").click();
146146

147147
// Scroll as far as possible
148-
grid.scrollToRowAndWait(1000000);
148+
grid.scrollToRowByPath(1000000);
149149
assertExpandedNodesPopulated(grid);
150150

151151
// Repeat
152-
grid.scrollToRowAndWait(1000000);
152+
grid.scrollToRowByPath(1000000);
153153
assertExpandedNodesPopulated(grid);
154154
}
155155

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridPreloadIT.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void firstExpanded_initiallySorted_shouldPreLoadDataForExpandedChildren()
128128
@Test
129129
public void expandedOnSecondPage_scrollToIndex_shouldHaveItemExpanded() {
130130
open(Arrays.asList(70), null, 100, 1, null);
131-
getTreeGrid().scrollToRow(70);
131+
getTreeGrid().scrollToRowByPath(70);
132132
verifyRow(71, "/0/70/1/0");
133133
}
134134

@@ -137,7 +137,7 @@ public void expandedOnSecondPage_scrollToIndex_shouldPreLoadDataForExpandedChild
137137
open(Arrays.asList(70), null, 100, 1, null);
138138
requestCountReset.click();
139139

140-
getTreeGrid().scrollToRow(70);
140+
getTreeGrid().scrollToRowByPath(70);
141141
Assert.assertEquals("1", requestCount.getValue());
142142
}
143143

@@ -149,22 +149,9 @@ private void verifyRow(int rowActualIndex, String itemId) {
149149
@Test
150150
public void multipleExpanded_shouldExpandWhenScrolledTo() {
151151
open(Arrays.asList(0, 2), null, null, null, null);
152-
// Scroll to the last index
153-
// TODO: Update to use getTreeGrid().scrollToRowAndWait with multiple
154-
// arguments once the API is available
155-
getTreeGrid().getCommandExecutor().executeScript(
156-
"arguments[0].scrollToIndex(2, 2, 2, 2, 2)", getTreeGrid());
157-
158-
waitUntil(w -> {
159-
try {
160-
return getTreeGrid().getCell("/0/2/1/2/2/2/3/2/4/2") != null;
161-
} catch (RuntimeException e) {
162-
return false;
163-
}
164-
});
165-
166-
Assert.assertEquals("/0/2/1/2/2/2/3/2/4/2", getTreeGrid()
167-
.getCell(getTreeGrid().getLastVisibleRowIndex(), 0).getText());
152+
int rowIndex = getTreeGrid().scrollToRowByPath(2, 2, 2, 2, 2);
153+
Assert.assertEquals("/0/2/1/2/2/2/3/2/4/2",
154+
getTreeGrid().getCell(rowIndex, 0).getText());
168155
}
169156

170157
@Test

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridRefreshAllIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void expandItems_scroll_refreshAll_loadingStateResolved() {
5656
getTreeGrid().expandWithClick(0);
5757
getTreeGrid().expandWithClick(1);
5858

59-
getTreeGrid().scrollToRow(100);
59+
getTreeGrid().scrollToRowByPath(100);
6060

6161
refreshAllButton.click();
6262

@@ -68,11 +68,11 @@ public void expandItems_scroll_refreshAll_scrollBack_expandedItemsRendered() {
6868
getTreeGrid().expandWithClick(0);
6969
getTreeGrid().expandWithClick(1);
7070

71-
getTreeGrid().scrollToRow(100);
71+
getTreeGrid().scrollToRowByPath(100);
7272

7373
refreshAllButton.click();
7474

75-
getTreeGrid().scrollToRow(0);
75+
getTreeGrid().scrollToRowByPath(0);
7676

7777
assertCellTexts(0, 0,
7878
new String[] { "0 | 0", "1 | 0", "2 | 0", "2 | 1", "2 | 2" });
@@ -95,7 +95,7 @@ public void scrollToEnd_selectRootItem_refreshAll_lastRootItemRendered() {
9595
TreeGridElement grid = $(TreeGridElement.class)
9696
.id("grid-with-page-size");
9797

98-
grid.scrollToRow(20);
98+
grid.scrollToRowByPath(20);
9999

100100
grid.select(14);
101101

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridRefreshItemIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public void expandRootItem_refreshChildItem_childItemRefreshed() {
5555
public void expandRootItem_scrollToBottom_refreshChildItem_scrollToTop_childItemRefreshed() {
5656
treeGrid.expandWithClick(0);
5757

58-
treeGrid.scrollToRow(99);
58+
treeGrid.scrollToRowByPath(99);
5959
assertRowContents(treeGrid.getRowCount() - 1, "Item 99");
6060

6161
clickElementWithJs("refresh-item-0-1");
62-
treeGrid.scrollToRow(0);
62+
treeGrid.scrollToRowByPath(0);
6363

6464
assertRowContents(0, "Item 0", "Item 0-0", "Updated", "Item 0-2",
6565
"Item 1");

vaadin-grid-flow-parent/vaadin-grid-flow/src/main/resources/META-INF/resources/frontend/treeGridConnector.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ window.Vaadin.Flow.treeGridConnector.initLazy = function (grid) {
6262
grid._scrollToFlatIndex(flatIndex);
6363

6464
delete grid.__pendingScrollToIndexes;
65+
66+
return flatIndex;
6567
};
6668

6769
grid.__getRowLevel = function (row) {

vaadin-grid-flow-parent/vaadin-grid-testbench/src/main/java/com/vaadin/flow/component/grid/testbench/GridElement.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,9 @@ public void scrollToRow(int row) {
5454
waitUntilLoadingFinished();
5555
}
5656

57-
/**
58-
* Scrolls to the row with the given flat row index.
59-
*
60-
* @param row
61-
* the row to scroll to
62-
*/
63-
protected void scrollToFlatRow(int row) {
64-
callFunction("_scrollToFlatIndex", row);
57+
void scrollToRowByFlatIndex(int rowFlatIndex) {
58+
waitUntilLoadingFinished();
59+
callFunction("_scrollToFlatIndex", rowFlatIndex);
6560
waitUntilLoadingFinished();
6661
}
6762

@@ -130,7 +125,7 @@ public GridTHTDElement getCell(int rowIndex, int colIndex) {
130125
*/
131126
public GridTHTDElement getCell(int rowIndex, GridColumnElement column) {
132127
if (!isRowInView(rowIndex)) {
133-
scrollToFlatRow(rowIndex);
128+
scrollToRowByFlatIndex(rowIndex);
134129
}
135130

136131
GridTRElement row = getRow(rowIndex);
@@ -266,7 +261,7 @@ public GridTRElement getRow(int rowIndex) throws IndexOutOfBoundsException {
266261
public GridTRElement getRow(int rowIndex, boolean scroll)
267262
throws IndexOutOfBoundsException {
268263
if (scroll && !isRowInView(rowIndex)) {
269-
scrollToFlatRow(rowIndex);
264+
scrollToRowByFlatIndex(rowIndex);
270265
}
271266
var rows = getRows(rowIndex, rowIndex);
272267
return rows.size() == 1 ? rows.get(0) : null;

0 commit comments

Comments
 (0)