Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Autocomplete: Only prevent the default action for enter when a menu i…
…tem is active. Fixes #6038 - Autocomplete: Allow default behaviour on enter when menu is open but inactive.

Thanks Ján Suchal.
  • Loading branch information
scottgonzalez committed Sep 27, 2010
1 parent dda7bcb commit adcafce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
33 changes: 31 additions & 2 deletions tests/unit/autocomplete/autocomplete_core.js
Expand Up @@ -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({
Expand All @@ -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);
4 changes: 2 additions & 2 deletions ui/jquery.ui.autocomplete.js
Expand Up @@ -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
Expand Down

0 comments on commit adcafce

Please sign in to comment.