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

ShortcutListener on ENTER combined with select listener not working #21

Open ghost opened 7 years ago

ghost commented 7 years ago

Im using the autocomplete field as a search field. When the user is selecting a suggestion (either by mouse selection, or by scrolling with arrow up and down and pressing enter), i want to capture the event and send the user to the view of that item.

But if the user is just typing in the textfield and presses enter (without selection a suggestion), i want to send him to the search-result-view.

Now the problem is that if i add a shortcutlistener to the autocomplete-field on ENTER, then it will make the select-listener not work, as the shortcutlistner is run first and then doing a normal search instead of navigation to the selected object...

maxschuster commented 7 years ago

Which Vaadin version are you using? I updated the demo to Vaadin 8.1.3 and the problem fixed itself. In fact the shortcut listener is not triggered on the suggestion container, even if no suggestion listener was added. Maybe this is related to https://github.com/vaadin/framework/issues/9419, which was fixed in 8.1.0.beta2

ghost commented 7 years ago

We are running Vaadin 8.1.1

I have kind of worked around the problem to make it work. The problem was as i tried to explain that both a shortcutlistener and the selectlistener is both listening for ENTER. The way i solved it for now was that when the selectlistener is triggered, we disable the shortcutlistener to prevent it from also triggering. Then we reenable the shortcutlistener on valuechange or focus again.

ahoehma commented 6 years ago

I have a similar problem and this workaround works for me too :)

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
          final String value = searchInputField.getValue();
          if (!Strings.isNullOrEmpty(value)) {
            startDirectInput(value);
          } else {
            messageAreaUpdater.clear();
          }
        }
      }
    };
    searchInputField.addSelectListener(f -> {
      // we have to remove the shortcutListener here to prevent double triggering if the user hit's 'enter' in the suggestion overlay
      searchInputField.removeShortcutListener(shortcutListener);
      if (isDirectInputSuggestion(f)) {
        startDirectInput(f.getSuggestion().getValue());
      } else {
        startStandard(f.getSuggestion().getValue());
      }
    });
    searchInputField.addFocusListener(f -> searchInputField.addShortcutListener(shortcutListener));