maxschuster / Vaadin-AutocompleteTextField

A Vaadin TextField with autocomplete (aka word completion) functionality
https://vaadin.maxschuster.eu/AutocompleteTextField/
Apache License 2.0
9 stars 6 forks source link

prevent enter event from bubbling when selecting suggestion #25

Open bbaumgartner opened 6 years ago

bbaumgartner commented 6 years ago

Hi

Thanks for this cool addon, works great and is easy to use. Just one thing: I use the field in a Window/Dialog with an enter key shortcut on the close/save button of the dialog. Now when the user types into the autocomplete field, presses arrow down to select a suggestion and then hits enter, the suggestion is selected (as expected) but then the dialog closes. Is there a way to work around this or can you stop the enter key event when the user selects from the suggestion popup?

Best Regards Benno

ahoehma commented 6 years ago

I have a similar problem ... here is my code:

final AutocompleteTextField searchInputField = new AutocompleteTextField();
    final ShortcutListener shortcutListener = new ShortcutListener(null, KeyCode.ENTER, null) {

      @Override
      public void handleAction(final Object sender, final Object target) {
        if (target == searchInputField) { // NOSONAR
          if (!Strings.isNullOrEmpty(searchInputField.getValue())) {
            startDirectInput(searchInputField.getValue());
          } 
        }
      }
    };
    searchInputField.addSelectListener(f -> {
      if (isDirectInputSuggestion(f)) {
        startDirectInput(f.getSuggestion().getValue());
      } else {
        startStandard(f.getSuggestion().getValue());
      }
    });
    searchInputField.addFocusListener(f -> searchInputField.addShortcutListener(shortcutListener));
    searchInputField.addBlurListener(f -> searchInputField.removeShortcutListener(shortcutListener));

My problem is that in case of "enter" in the suggestion overlay my shortkey listener for "enter" is also triggered and so I end up with two instead of one "actions" :) I tried to use a focus/blur listener to enabled input-field-enter only on focus but this doesn't work.