Reviewers: Ray Ryan,

Description:
Fix the bug where you cannot use up arrow to enter the suggestions box


Please review this at http://gwt-code-reviews.appspot.com/239801/show

Affected files:
  D samples/hello/war/WEB-INF/classes/marker
  D samples/showcase/war/WEB-INF/classes/marker
  M user/src/com/google/gwt/user/client/ui/SuggestBox.java
  M user/test/com/google/gwt/user/client/ui/SuggestBoxTest.java


Index: samples/hello/war/WEB-INF/classes/marker
===================================================================
--- samples/hello/war/WEB-INF/classes/marker    (revision 7742)
+++ samples/hello/war/WEB-INF/classes/marker    (working copy)
Index: samples/showcase/war/WEB-INF/classes/marker
===================================================================
--- samples/showcase/war/WEB-INF/classes/marker (revision 7742)
+++ samples/showcase/war/WEB-INF/classes/marker (working copy)
Index: user/src/com/google/gwt/user/client/ui/SuggestBox.java
===================================================================
--- user/src/com/google/gwt/user/client/ui/SuggestBox.java      (revision 7742)
+++ user/src/com/google/gwt/user/client/ui/SuggestBox.java      (working copy)
@@ -362,6 +362,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);
       }
     }
@@ -371,7 +373,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);
+        }
       }
     }

Index: user/test/com/google/gwt/user/client/ui/SuggestBoxTest.java
===================================================================
--- user/test/com/google/gwt/user/client/ui/SuggestBoxTest.java (revision 7742) +++ user/test/com/google/gwt/user/client/ui/SuggestBoxTest.java (working copy)
@@ -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();


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to