Skip to content

Commit 46679a3

Browse files
authored
fix: hide the grid-pro editor component until updated (#6820)
1 parent ff7ce6a commit 46679a3

File tree

3 files changed

+81
-3
lines changed
  • vaadin-grid-pro-flow-parent
    • vaadin-grid-pro-flow-integration-tests/src/test/java/com/vaadin/flow/component/gridpro/tests
    • vaadin-grid-pro-flow/src/main

3 files changed

+81
-3
lines changed

vaadin-grid-pro-flow-parent/vaadin-grid-pro-flow-integration-tests/src/test/java/com/vaadin/flow/component/gridpro/tests/BasicIT.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,61 @@ public void customComboBox_circularReferencesInData_isEdited() {
135135
getPanelText("prop-panel"));
136136
}
137137

138+
private String LOADING_EDITOR_ATTRIBUTE = "loading-editor";
139+
140+
@Test
141+
public void customComboBox_loadingEditorStateOnEdit() {
142+
var cell = grid.getCell(0, 4);
143+
144+
var hasLoadingStateOnEditStart = (Boolean) executeScript(
145+
"""
146+
const [cell, grid, attribute] = arguments;
147+
148+
// Enter edit mode with double click
149+
cell.dispatchEvent(new CustomEvent('dblclick', {composed: true, bubbles: true}));
150+
151+
return grid.hasAttribute(attribute);
152+
""",
153+
cell, grid, LOADING_EDITOR_ATTRIBUTE);
154+
// Expect the editor to be hidden when the edit starts
155+
Assert.assertTrue(hasLoadingStateOnEditStart);
156+
157+
// After the round trip to the server...
158+
var editor = cell.$("vaadin-combo-box").first();
159+
// The editor should be visible
160+
Assert.assertFalse(grid.hasAttribute(LOADING_EDITOR_ATTRIBUTE));
161+
// The editor should have focus
162+
Assert.assertTrue("Editor should have focus",
163+
(Boolean) executeScript(
164+
"return arguments[0].contains(document.activeElement)",
165+
editor));
166+
}
167+
168+
@Test
169+
public void customComboBox_loadingEditorStateClearedOnEditStop() {
170+
var cell = grid.getCell(0, 4);
171+
var nonCustomEditorCell = grid.getCell(0, 1);
172+
173+
var hasLoadingStateAttribute = (Boolean) executeScript(
174+
"""
175+
const [cell, nonCustomEditorCell, grid, attribute] = arguments;
176+
177+
// Enter edit mode with double click
178+
cell.dispatchEvent(new CustomEvent('dblclick', {composed: true, bubbles: true}));
179+
await new Promise(resolve => requestAnimationFrame(resolve));
180+
181+
// Focus another cell
182+
nonCustomEditorCell.focus();
183+
await new Promise(resolve => requestAnimationFrame(resolve));
184+
185+
return grid.hasAttribute(attribute);
186+
187+
""",
188+
cell, nonCustomEditorCell, grid, LOADING_EDITOR_ATTRIBUTE);
189+
190+
Assert.assertFalse(hasLoadingStateAttribute);
191+
}
192+
138193
@Test
139194
public void textEditorIsUsedForTextColumn() {
140195
assertCellEnterEditModeOnDoubleClick(0, 1,

vaadin-grid-pro-flow-parent/vaadin-grid-pro-flow/src/main/java/com/vaadin/flow/component/gridpro/GridPro.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ private void setup() {
148148
if (column.getEditorType().equals("custom")) {
149149
column.getEditorField()
150150
.setValue(column.getValueProvider().apply(e.getItem()));
151+
var itemKey = getDataCommunicator().getKeyMapper()
152+
.key(e.getItem());
151153
UI.getCurrent().getPage().executeJs(
152-
"window.Vaadin.Flow.gridProConnector.selectAll($0)",
153-
column.getEditorField().getElement());
154+
"window.Vaadin.Flow.gridProConnector.selectAll($0, $1, $2)",
155+
column.getEditorField().getElement(), itemKey,
156+
this.getElement());
154157
}
155158
});
156159
}

vaadin-grid-pro-flow-parent/vaadin-grid-pro-flow/src/main/resources/META-INF/resources/frontend/gridProConnector.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@ function isEditedRow(grid, rowData) {
1111
return grid.__edited && grid.__edited.model.item.key === rowData.item.key;
1212
}
1313

14+
const LOADING_EDITOR_CELL_ATTRIBUTE = 'loading-editor';
15+
1416
window.Vaadin.Flow.gridProConnector = {
15-
selectAll: (editor) => {
17+
selectAll: (editor, itemKey, grid) => {
18+
if (editor.__itemKey !== itemKey) {
19+
// This is an outdated call that can occur if the user starts editing a cell,
20+
// and quickly starts editing another cell on the same column before the editor
21+
// is unhidden for the first cell.
22+
return;
23+
}
24+
25+
grid.toggleAttribute(LOADING_EDITOR_CELL_ATTRIBUTE, false);
26+
1627
if (editor instanceof HTMLInputElement) {
1728
editor.select();
1829
} else if (editor.focusElement && editor.focusElement instanceof HTMLInputElement) {
@@ -34,11 +45,20 @@ window.Vaadin.Flow.gridProConnector = {
3445
root.appendChild(component);
3546
this._grid._cancelStopEdit();
3647
component.focus();
48+
49+
component.__itemKey = rowData.item.key;
50+
this._grid.toggleAttribute(LOADING_EDITOR_CELL_ATTRIBUTE, true);
3751
};
3852

3953
// Not needed in case of custom editor as value is set on server-side.
4054
// Overridden in order to avoid blinking of the cell content.
4155
column._setEditorValue = function (editor, value) {};
56+
57+
const stopCellEdit = column._stopCellEdit;
58+
column._stopCellEdit = function () {
59+
stopCellEdit.apply(this, arguments);
60+
this._grid.toggleAttribute(LOADING_EDITOR_CELL_ATTRIBUTE, false);
61+
};
4262
},
4363

4464
patchEditModeRenderer(column) {

0 commit comments

Comments
 (0)