This is an automated email from the ASF dual-hosted git repository.

adelbene pushed a commit to branch wicket-9.x
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit fc290a0aff934a49c279bbca0174b0e6c23b0369
Author: Erik Strid <[email protected]>
AuthorDate: Fri Nov 22 15:46:12 2024 +0100

    Change the solution to use an enum to make the code clearer and make it 
easier to add more behaviours later
---
 .../autocomplete/AbstractAutoCompleteBehavior.java |  3 +-
 .../html/autocomplete/AutoCompleteSettings.java    | 49 ++++++++++++++++------
 .../html/autocomplete/wicket-autocomplete.js       |  2 +-
 3 files changed, 40 insertions(+), 14 deletions(-)

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 9b7c86b2bd..7b766b7a89 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,7 +187,8 @@ public abstract class AbstractAutoCompleteBehavior extends 
AbstractDefaultAjaxBe
                {
                        sb.append(",className: 
'").append(settings.getCssClassName()).append('\'');
                }
-               sb.append(",focusInputOnTabSelection: 
").append(settings.shouldFocusInputOnTabSelection());
+               sb.append(",tabBehavior: '").append(
+                       settings.getTabBehavior().getValue()).append('\'');
                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 f07e932fad..d243350f76 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,7 +74,7 @@ public final class AutoCompleteSettings implements 
IClusterable
 
        private int minInputLength = 1;
 
-       private boolean focusInputOnTabSelection = false;
+       private TabBehavior tabBehavior = TabBehavior.SELECT_FOCUS_NEXT_ELEMENT;
 
        /**
         * Indicates whether the first item in the list is automatically 
selected when the autocomplete
@@ -384,27 +384,52 @@ public final class AutoCompleteSettings implements 
IClusterable
 
        /**
         * Indicates how the Tab key should be handled when having an item in 
the autocomplete list
-        * selected.
+        * selected, {@link TabBehavior#SELECT_FOCUS_NEXT_ELEMENT} is the 
default behavior.
         *
-        * @return <code>true</code> if the focus should return to the input 
field, <code>false</code>
-        * moves the focus to the next component.
+        * @return the behavior that should be used when the Tab key is pressed
         */
-       public boolean shouldFocusInputOnTabSelection()
+       public TabBehavior getTabBehavior()
        {
-               return focusInputOnTabSelection;
+               return tabBehavior;
        }
 
        /**
-        * Set how the tab key should be handled when having an item in the 
autocomplete list selected.
+        * Set how the Tab key should be handled when having an item in the 
autocomplete list selected.
         *
-        * @param focusInputOnTabSelection <code>true</code> if the focus 
should return to the input
-        * field when the Tab key pressed, <code>false</code> is the default 
which moves the focus to
-        * the next component.
+        * @param tabSelectBehavior the behavior that should be used when the 
Tab key is pressed,
+        * {@link TabBehavior#SELECT_FOCUS_NEXT_ELEMENT} is the default behavior
         * @return this {@link AutoCompleteSettings}
         */
-       public AutoCompleteSettings setFocusInputOnTabSelection(boolean 
focusInputOnTabSelection)
+       public AutoCompleteSettings setTabBehavior(TabBehavior 
tabSelectBehavior)
        {
-               this.focusInputOnTabSelection = focusInputOnTabSelection;
+               this.tabBehavior = tabSelectBehavior;
                return this;
        }
+
+       /**
+        * A behavior that can be used to control how the Tab key should be 
handled when having an item
+        * in the autocomplete list is marked.
+        */
+       public enum TabBehavior {
+               /**
+                * Select the currently marked item and move the focus to the 
next focusable element.
+                */
+               SELECT_FOCUS_NEXT_ELEMENT("selectFocusNextElement"),
+               /**
+                * Select the currently marked item and move the focus to the 
auto complete input field.
+                */
+               SELECT_FOCUS_INPUT("selectFocusAutocompleteInput");
+
+               private final String value;
+
+               TabBehavior(String value)
+               {
+                       this.value = value;
+               }
+
+               public String getValue()
+               {
+                       return value;
+               }
+       }
 }
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 2b1426bb78..7f338fb7cd 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
@@ -184,7 +184,7 @@
 
                                                        hideAutoComplete();
 
-                                                       if 
(cfg.focusInputOnTabSelection && keyCode === KEY_TAB) {
+                                                       if (cfg.tabBehavior === 
'selectFocusAutocompleteInput' && keyCode === KEY_TAB) {
                                                                // prevent 
moving focus to the next component if an item in the dropdown is selected
                                                                // using the 
Tab key
                                                                
jqEvent.preventDefault();

Reply via email to