Skip to content

Commit 323d64c

Browse files
author
Erik Strid
committed
Change the solution to use an enum to make the code clearer and make it easier to add more behaviours later
1 parent f86b073 commit 323d64c

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AbstractAutoCompleteBehavior.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ protected final String constructSettingsJS()
187187
{
188188
sb.append(",className: '").append(settings.getCssClassName()).append('\'');
189189
}
190-
sb.append(",focusInputOnTabSelection: ").append(settings.shouldFocusInputOnTabSelection());
190+
sb.append(",tabBehavior: '").append(
191+
settings.getTabBehavior().getValue()).append('\'');
191192
sb.append('}');
192193
return sb.toString();
193194
}

wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteSettings.java

+37-12
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public final class AutoCompleteSettings implements IClusterable
7474

7575
private int minInputLength = 1;
7676

77-
private boolean focusInputOnTabSelection = false;
77+
private TabBehavior tabBehavior = TabBehavior.SELECT_FOCUS_NEXT_ELEMENT;
7878

7979
/**
8080
* Indicates whether the first item in the list is automatically selected when the autocomplete
@@ -384,27 +384,52 @@ public AutoCompleteSettings setMinInputLength(int minInputLength)
384384

385385
/**
386386
* Indicates how the Tab key should be handled when having an item in the autocomplete list
387-
* selected.
387+
* selected, {@link TabBehavior#SELECT_FOCUS_NEXT_ELEMENT} is the default behavior.
388388
*
389-
* @return <code>true</code> if the focus should return to the input field, <code>false</code>
390-
* moves the focus to the next component.
389+
* @return the behavior that should be used when the Tab key is pressed
391390
*/
392-
public boolean shouldFocusInputOnTabSelection()
391+
public TabBehavior getTabBehavior()
393392
{
394-
return focusInputOnTabSelection;
393+
return tabBehavior;
395394
}
396395

397396
/**
398-
* Set how the tab key should be handled when having an item in the autocomplete list selected.
397+
* Set how the Tab key should be handled when having an item in the autocomplete list selected.
399398
*
400-
* @param focusInputOnTabSelection <code>true</code> if the focus should return to the input
401-
* field when the Tab key pressed, <code>false</code> is the default which moves the focus to
402-
* the next component.
399+
* @param tabSelectBehavior the behavior that should be used when the Tab key is pressed,
400+
* {@link TabBehavior#SELECT_FOCUS_NEXT_ELEMENT} is the default behavior
403401
* @return this {@link AutoCompleteSettings}
404402
*/
405-
public AutoCompleteSettings setFocusInputOnTabSelection(boolean focusInputOnTabSelection)
403+
public AutoCompleteSettings setTabBehavior(TabBehavior tabSelectBehavior)
406404
{
407-
this.focusInputOnTabSelection = focusInputOnTabSelection;
405+
this.tabBehavior = tabSelectBehavior;
408406
return this;
409407
}
408+
409+
/**
410+
* A behavior that can be used to control how the Tab key should be handled when having an item
411+
* in the autocomplete list is marked.
412+
*/
413+
public enum TabBehavior {
414+
/**
415+
* Select the currently marked item and move the focus to the next focusable element.
416+
*/
417+
SELECT_FOCUS_NEXT_ELEMENT("selectFocusNextElement"),
418+
/**
419+
* Select the currently marked item and move the focus to the auto complete input field.
420+
*/
421+
SELECT_FOCUS_INPUT("selectFocusAutocompleteInput");
422+
423+
private final String value;
424+
425+
TabBehavior(String value)
426+
{
427+
this.value = value;
428+
}
429+
430+
public String getValue()
431+
{
432+
return value;
433+
}
434+
}
410435
}

wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/wicket-autocomplete.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184

185185
hideAutoComplete();
186186

187-
if (cfg.focusInputOnTabSelection && keyCode === KEY_TAB) {
187+
if (cfg.tabBehavior === 'selectFocusAutocompleteInput' && keyCode === KEY_TAB) {
188188
// prevent moving focus to the next component if an item in the dropdown is selected
189189
// using the Tab key
190190
jqEvent.preventDefault();

0 commit comments

Comments
 (0)