Fix the bug where you cannot use up arrow to enter the suggestions box
Review at http://gwt-code-reviews.appspot.com/239801
Review by: jlabanca@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7814 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/SuggestBox.java b/user/src/com/google/gwt/user/client/ui/SuggestBox.java
index 4ac452e..a82dbe2 100644
--- a/user/src/com/google/gwt/user/client/ui/SuggestBox.java
+++ b/user/src/com/google/gwt/user/client/ui/SuggestBox.java
@@ -366,6 +366,8 @@
// Make sure that the menu is actually showing. These keystrokes
// are only relevant when choosing a suggestion.
if (isSuggestionListShowing()) {
+ // If nothing is selected, getSelectedItemIndex will return -1 and we
+ // will select index 0 (the first item) by default.
suggestionMenu.selectItem(suggestionMenu.getSelectedItemIndex() + 1);
}
}
@@ -375,7 +377,17 @@
// Make sure that the menu is actually showing. These keystrokes
// are only relevant when choosing a suggestion.
if (isSuggestionListShowing()) {
- suggestionMenu.selectItem(suggestionMenu.getSelectedItemIndex() - 1);
+ // if nothing is selected, then we should select the last suggestion by
+ // default. This is because, in some cases, the suggestions menu will
+ // appear above the text box rather than below it (for example, if the
+ // text box is at the bottom of the window and the suggestions will not
+ // fit below the text box). In this case, users would expect to be able
+ // to use the up arrow to navigate to the suggestions.
+ if (suggestionMenu.getSelectedItemIndex() == -1) {
+ suggestionMenu.selectItem(suggestionMenu.getNumItems() - 1);
+ } else {
+ suggestionMenu.selectItem(suggestionMenu.getSelectedItemIndex() - 1);
+ }
}
}
diff --git a/user/test/com/google/gwt/user/client/ui/SuggestBoxTest.java b/user/test/com/google/gwt/user/client/ui/SuggestBoxTest.java
index 1bd7fb6..87b8a9e 100644
--- a/user/test/com/google/gwt/user/client/ui/SuggestBoxTest.java
+++ b/user/test/com/google/gwt/user/client/ui/SuggestBoxTest.java
@@ -170,6 +170,41 @@
assertEquals("A", display.getSuggestion(0).getReplacementString());
assertEquals("B", display.getSuggestion(1).getReplacementString());
}
+
+ public void testSuggestionSelection() {
+ MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
+ oracle.setDefaultSuggestionsFromText(Arrays.asList("A", "B"));
+ TestSuggestionDisplay display = new TestSuggestionDisplay();
+ SuggestBox box = new SuggestBox(oracle, new TextBox(), display);
+ box.setAutoSelectEnabled(false);
+ RootPanel.get().add(box);
+ box.showSuggestionList();
+
+ // If nothing is selected, moving down will select the first item
+ assertNull(display.getCurrentSelection());
+ display.moveSelectionDown();
+ assertEquals("A", display.getCurrentSelection().getReplacementString());
+
+ // Once something is selected, selections are made as expected, but we do
+ // not move outside the box
+ display.moveSelectionDown();
+ assertEquals("B", display.getCurrentSelection().getReplacementString());
+ display.moveSelectionDown();
+ assertEquals("B", display.getCurrentSelection().getReplacementString());
+ display.moveSelectionUp();
+ assertEquals("A", display.getCurrentSelection().getReplacementString());
+ display.moveSelectionUp();
+ assertEquals("A", display.getCurrentSelection().getReplacementString());
+
+ // Reset the suggestions so that nothing is selected again
+ display.hideSuggestions();
+ box.showSuggestionList();
+ assertNull(display.getCurrentSelection());
+
+ // If nothing is selected, moving up will select the last item
+ display.moveSelectionUp();
+ assertEquals("B", display.getCurrentSelection().getReplacementString());
+ }
public void testShowFirst() {
SuggestBox box = createSuggestBox();