Skip to content

Commit 56c878a

Browse files
fix: register custom editor onAttach (#2409)
Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com> Co-authored-by: Sascha Ißbrücker <sascha.issbruecker@googlemail.com>
1 parent eb47630 commit 56c878a

3 files changed

Lines changed: 153 additions & 6 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2000-2019 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.gridpro.it;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import com.vaadin.flow.component.gridpro.GridPro;
22+
import com.vaadin.flow.component.html.Div;
23+
import com.vaadin.flow.component.html.NativeButton;
24+
import com.vaadin.flow.component.textfield.TextField;
25+
import com.vaadin.flow.router.Route;
26+
27+
@Route(value = "gridpro-detach-attach")
28+
public class GridProDetachAttachPage extends Div {
29+
30+
public GridProDetachAttachPage() {
31+
List<SamplePerson> persons = new ArrayList<>();
32+
33+
persons.add(new SamplePerson("Henry"));
34+
persons.add(new SamplePerson("Indiana"));
35+
persons.add(new SamplePerson("Jones"));
36+
37+
GridPro<SamplePerson> grid = new GridPro<>();
38+
grid.setId("grid-pro");
39+
40+
TextField field = new TextField();
41+
42+
grid.addEditColumn(SamplePerson::getName).custom(field,
43+
SamplePerson::setName);
44+
grid.setItems(persons);
45+
46+
NativeButton button = new NativeButton("Detach", event -> {
47+
if (grid.getParent().isPresent()) {
48+
remove(grid);
49+
event.getSource().setText("Reattach");
50+
} else {
51+
add(grid);
52+
event.getSource().setText("Detach");
53+
}
54+
});
55+
button.setId("toggle-attached");
56+
add(grid, button);
57+
}
58+
59+
public class SamplePerson {
60+
private String name;
61+
62+
public SamplePerson(String name) {
63+
this.name = name;
64+
}
65+
66+
public String getName() {
67+
return name;
68+
}
69+
70+
public void setName(String name) {
71+
this.name = name;
72+
}
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2000-2019 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.gridpro.test;
17+
18+
import org.junit.Assert;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
import com.vaadin.flow.component.gridpro.testbench.GridProElement;
23+
import com.vaadin.flow.component.gridpro.testbench.GridTHTDElement;
24+
import com.vaadin.tests.AbstractComponentIT;
25+
import com.vaadin.flow.testutil.TestPath;
26+
import com.vaadin.testbench.TestBenchElement;
27+
28+
@TestPath("gridpro-detach-attach")
29+
public class GridProDetachAttachIT extends AbstractComponentIT {
30+
31+
private GridProElement grid;
32+
private TestBenchElement toggleAttachedButton;
33+
34+
@Before
35+
public void before() {
36+
open();
37+
grid = $(GridProElement.class).first();
38+
toggleAttachedButton = $("button").id("toggle-attached");
39+
}
40+
41+
@Test
42+
public void detach_attach_customEditOpens() {
43+
toggleAttachedButton.click();
44+
toggleAttachedButton.click();
45+
46+
grid = $(GridProElement.class).waitForFirst();
47+
assertCellEnterEditModeOnDoubleClick(grid, 0, 0, "vaadin-text-field");
48+
}
49+
50+
private void assertCellEnterEditModeOnDoubleClick(GridProElement grid,
51+
Integer rowIndex, Integer colIndex, String editorTag) {
52+
GridTHTDElement cell = grid.getCell(rowIndex, colIndex);
53+
54+
// Not in edit mode initially
55+
Assert.assertFalse(cell.innerHTMLContains(editorTag));
56+
57+
// Entering edit mode with double click
58+
executeScript(
59+
"arguments[0].dispatchEvent(new CustomEvent('dblclick', {composed: true, bubbles: true}));",
60+
cell);
61+
Assert.assertTrue(cell.innerHTMLContains(editorTag));
62+
// Assert there are no null reference errors
63+
checkLogsForErrors();
64+
}
65+
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* %%
99
* This program is available under Commercial Vaadin Developer License
1010
* 4.0 (CVDLv4).
11-
*
11+
*
1212
* See the file license.html distributed with this software for more
1313
* information about licensing.
14-
*
14+
*
1515
* For the full License, see <https://vaadin.com/license/cvdl-4.0>.
1616
* #L%
1717
*/
@@ -31,6 +31,7 @@
3131
import com.vaadin.flow.component.gridpro.GridPro.EditColumn;
3232
import com.vaadin.flow.function.SerializableFunction;
3333
import com.vaadin.flow.function.ValueProvider;
34+
import com.vaadin.flow.shared.Registration;
3435

3536
/**
3637
* Configuration for the editor of an edit column.
@@ -43,6 +44,8 @@ public class EditColumnConfigurator<T> implements Serializable {
4344

4445
private final EditColumn<T> column;
4546

47+
private Registration attachRegistration;
48+
4649
/**
4750
* Creates a new configurator for the given column.
4851
*
@@ -106,16 +109,21 @@ public Column<T> text(ItemUpdater<T, String> itemUpdater) {
106109
public <V> Column<T> custom(AbstractField<?, V> component,
107110
ItemUpdater<T, V> itemUpdater) {
108111
column.getElement().appendVirtualChild(component.getElement());
109-
column.getElement().getNode()
110-
.runWhenAttached(ui -> ui.beforeClientResponse(column,
111-
context -> setEditModeRenderer(component)));
112+
if (attachRegistration != null) {
113+
attachRegistration.remove();
114+
attachRegistration = null;
115+
}
116+
attachRegistration = column.getElement()
117+
.addAttachListener(e -> column.getUI()
118+
.ifPresent(ui -> ui.beforeClientResponse(column,
119+
context -> setEditModeRenderer(component))));
112120

113121
return configureColumn((item, ignore) -> itemUpdater.accept(item,
114122
component.getValue()), EditorType.CUSTOM, component);
115123
}
116124

117125
private <V> void setEditModeRenderer(AbstractField<?, V> component) {
118-
UI.getCurrent().getPage().executeJavaScript(
126+
UI.getCurrent().getPage().executeJs(
119127
"window.Vaadin.Flow.gridProConnector.setEditModeRenderer($0, $1)",
120128
column.getElement(), component.getElement());
121129
}

0 commit comments

Comments
 (0)