diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AbstractAutoCompleteBehavior.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AbstractAutoCompleteBehavior.java index 6e9db0f21b..9b7c86b2bd 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AbstractAutoCompleteBehavior.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AbstractAutoCompleteBehavior.java @@ -187,6 +187,7 @@ protected final String constructSettingsJS() { sb.append(",className: '").append(settings.getCssClassName()).append('\''); } + sb.append(",focusInputOnTabSelection: ").append(settings.shouldFocusInputOnTabSelection()); sb.append('}'); return sb.toString(); } diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteSettings.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteSettings.java index c9c43ceac8..f07e932fad 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteSettings.java +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteSettings.java @@ -74,6 +74,8 @@ public final class AutoCompleteSettings implements IClusterable private int minInputLength = 1; + private boolean focusInputOnTabSelection = false; + /** * Indicates whether the first item in the list is automatically selected when the autocomplete * list is shown. @@ -379,4 +381,30 @@ public AutoCompleteSettings setMinInputLength(int minInputLength) this.minInputLength = minInputLength; return this; } + + /** + * Indicates how the Tab key should be handled when having an item in the autocomplete list + * selected. + * + * @return true if the focus should return to the input field, false + * moves the focus to the next component. + */ + public boolean shouldFocusInputOnTabSelection() + { + return focusInputOnTabSelection; + } + + /** + * Set how the tab key should be handled when having an item in the autocomplete list selected. + * + * @param focusInputOnTabSelection true if the focus should return to the input + * field when the Tab key pressed, false is the default which moves the focus to + * the next component. + * @return this {@link AutoCompleteSettings} + */ + public AutoCompleteSettings setFocusInputOnTabSelection(boolean focusInputOnTabSelection) + { + this.focusInputOnTabSelection = focusInputOnTabSelection; + return this; + } } diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js index 2b2998e60d..89f46e32ef 100644 --- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js +++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js @@ -169,6 +169,10 @@ } break; case KEY_TAB: + if (cfg.focusInputOnTabSelection && selected > -1) { + // prevent moving focus to the next component if an item in the dropdown is selected + jqEvent.preventDefault(); + } case KEY_ENTER: ignoreKeyEnter = false;