Created
July 14, 2023 10:17
-
-
Save TatuLund/5ed628305f4420b2ec7698d8ebf7a8f9 to your computer and use it in GitHub Desktop.
This is an example of preventing enter key event of the GridPro cell editor bubbling to Shortcut listener hooked to GridPro.
This file contains hidden or 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
| package com.example.application.views; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| import java.util.stream.IntStream; | |
| import com.vaadin.flow.component.Key; | |
| import com.vaadin.flow.component.Shortcuts; | |
| import com.vaadin.flow.component.UI; | |
| import com.vaadin.flow.component.button.Button; | |
| import com.vaadin.flow.component.combobox.ComboBox; | |
| import com.vaadin.flow.component.confirmdialog.ConfirmDialog; | |
| import com.vaadin.flow.component.dependency.CssImport; | |
| import com.vaadin.flow.component.grid.Grid.SelectionMode; | |
| import com.vaadin.flow.component.gridpro.GridPro; | |
| import com.vaadin.flow.component.html.Div; | |
| import com.vaadin.flow.component.textfield.TextField; | |
| import com.vaadin.flow.dom.DomListenerRegistration; | |
| import com.vaadin.flow.router.Route; | |
| @Route("gridpro") | |
| @CssImport(value = "./empty.css", themeFor = "vaadin-text-field", include = "lumo-grid-pro-editor") | |
| public class GridProView extends Div { | |
| public GridProView() { | |
| GridPro<Bean> grid = new GridPro<>(); | |
| grid.addColumn(bean -> bean.getValue()); | |
| TextField field = new TextField(); | |
| field.addThemeName("grid-pro-editor"); | |
| DomListenerRegistration keyReg = field.getElement() | |
| .addEventListener("keydown", e -> grid.getElement() | |
| .executeJs("this._stopEdit(false,true);")); | |
| keyReg.addEventData("event.keyCode"); | |
| keyReg.addEventData( | |
| "([13, 10].includes(event.keyCode)) ? event.stopPropagation() : undefined"); | |
| keyReg.setFilter("[13, 10].includes(event.keyCode)"); | |
| grid.addEditColumn(Bean::getValue).custom(field, Bean::setValue) | |
| .setHeader("Text"); | |
| List<Bean> beans = Arrays | |
| .asList("Hello", "World", "I", "am", "feeling", "good!") | |
| .stream().map(s -> new Bean(s)).collect(Collectors.toList()); | |
| grid.setItems(beans); | |
| grid.setSelectionMode(SelectionMode.SINGLE); | |
| Shortcuts.addShortcutListener(grid, e -> { | |
| ConfirmDialog dialog = new ConfirmDialog(); | |
| String value = grid.asSingleSelect().getValue() != null | |
| ? grid.asSingleSelect().getValue().getValue() | |
| : "nothing here"; | |
| dialog.setText(value); | |
| dialog.setConfirmText("Ok"); | |
| dialog.open(); | |
| }, Key.ENTER).listenOn(grid); | |
| ComboBox<Bean> combo = new ComboBox<>(); | |
| combo.setPageSize(100); | |
| List<Bean> items = IntStream.range(0, 140) | |
| .mapToObj(i -> new Bean("Item " + i)) | |
| .collect(Collectors.toList()); | |
| combo.setItems(items); | |
| combo.setItemLabelGenerator(b -> b.getValue()); | |
| add(grid, combo); | |
| } | |
| public class Bean { | |
| private String value; | |
| public Bean(String value) { | |
| this.value = value; | |
| } | |
| public String getValue() { | |
| return value; | |
| } | |
| public void setValue(String value) { | |
| this.value = value; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment