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 c05d4b8f70527607fe60847d9fd2b6acffdb17f1
Author: Erik Strid <[email protected]>
AuthorDate: Wed Nov 20 06:49:02 2024 +0100

    Added ability to prevent autocomplete to move focus to the next component 
when selecting an item using the tab key in the autocomplete list
---
 .../autocomplete/AbstractAutoCompleteBehavior.java |  1 +
 .../html/autocomplete/AutoCompleteSettings.java    | 28 ++++++++++++++++++++++
 .../html/autocomplete/wicket-autocomplete.js       |  4 ++++
 3 files changed, 33 insertions(+)

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 @@ public abstract class AbstractAutoCompleteBehavior extends 
AbstractDefaultAjaxBe
                {
                        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 final class AutoCompleteSettings implements 
IClusterable
                this.minInputLength = minInputLength;
                return this;
        }
+
+       /**
+        * Indicates how the Tab key should be handled when having an item in 
the autocomplete list
+        * selected.
+        *
+        * @return <code>true</code> if the focus should return to the input 
field, <code>false</code>
+        * 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 <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.
+        * @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;
 

Reply via email to