From adcafce7a24156c503061eb354867e41064fd89f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Scott=20Gonz=C3=A1lez?= Date: Mon, 27 Sep 2010 10:10:00 -0400 Subject: [PATCH] Autocomplete: Only prevent the default action for enter when a menu item is active. Fixes #6038 - Autocomplete: Allow default behaviour on enter when menu is open but inactive. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Thanks Ján Suchal. --- tests/unit/autocomplete/autocomplete_core.js | 33 ++++++++++++++++++-- ui/jquery.ui.autocomplete.js | 4 +-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/tests/unit/autocomplete/autocomplete_core.js b/tests/unit/autocomplete/autocomplete_core.js index 9b8709abb..fcd835d1a 100644 --- a/tests/unit/autocomplete/autocomplete_core.js +++ b/tests/unit/autocomplete/autocomplete_core.js @@ -23,7 +23,7 @@ test("close-on-blur is properly delayed", function() { same( $(".ui-menu:visible").length, 0 ); start(); }, 200); -}) +}); test("close-on-blur is cancelled when starting a search", function() { var ac = $("#autocomplete").autocomplete({ @@ -38,6 +38,35 @@ test("close-on-blur is cancelled when starting a search", function() { same( $(".ui-menu:visible").length, 1 ); start(); }, 200); -}) +}); + +test( "prevent form submit on enter when menu is active", function() { + var event; + var ac = $( "#autocomplete" ).autocomplete({ + source: [ "java", "javascript" ] + }).val( "ja" ).autocomplete( "search" ); + + event = $.Event( "keydown" ); + event.keyCode = $.ui.keyCode.DOWN; + ac.trigger( event ); + same( $( ".ui-menu-item:has(.ui-state-hover)" ).length, 1, "menu item is active" ); + + event = $.Event( "keydown" ); + event.keyCode = $.ui.keyCode.ENTER; + ac.trigger( event ); + ok( event.isDefaultPrevented(), "default action is prevented" ); +}); + +test( "allow form submit on enter when menu is not active", function() { + var event; + var ac = $( "#autocomplete" ).autocomplete({ + source: [ "java", "javascript" ] + }).val( "ja" ).autocomplete( "search" ); + + event = $.Event( "keydown" ); + event.keyCode = $.ui.keyCode.ENTER; + ac.trigger( event ); + ok( !event.isDefaultPrevented(), "default action is prevented" ); +}); })(jQuery); diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js index 680a81f29..2d1d7f94b 100644 --- a/ui/jquery.ui.autocomplete.js +++ b/ui/jquery.ui.autocomplete.js @@ -63,8 +63,8 @@ $.widget( "ui.autocomplete", { break; case keyCode.ENTER: case keyCode.NUMPAD_ENTER: - // when menu is open or has focus - if ( self.menu.element.is( ":visible" ) ) { + // when menu is open and has focus + if ( self.menu.active ) { event.preventDefault(); } //passthrough - ENTER and TAB both select the current element -- 2.39.5