Skip to content

Autocomplete: don't prevent keypress for multiline (Firefox) #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions tests/unit/autocomplete/autocomplete_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ test( "allow form submit on enter when menu is not active", function() {
test( "down arrow moves focus - contenteditable", function() {
arrowsMoveFocus( "#autocomplete-contenteditable", false );
});

test( "up arrow moves cursor - input", function() {
arrowsNavigateElement( "#autocomplete", true, false );
});

test( "down arrow moves cursor - input", function() {
arrowsNavigateElement( "#autocomplete", false, false );
});

test( "up arrow moves cursor - textarea", function() {
arrowsNavigateElement( "#autocomplete-textarea", true, true );
});

test( "down arrow moves cursor - textarea", function() {
arrowsNavigateElement( "#autocomplete-textarea", false, true );
});

test( "up arrow moves cursor - contenteditable", function() {
arrowsNavigateElement( "#autocomplete-contenteditable", true, true );
});

test( "down arrow moves cursor - contenteditable", function() {
arrowsNavigateElement( "#autocomplete-contenteditable", false, true );
});

function arrowsInvokeSearch( id, isKeyUp, shouldMove ) {
expect( 1 );
Expand Down Expand Up @@ -120,6 +144,23 @@ test( "allow form submit on enter when menu is not active", function() {
element.autocomplete( "search" );
element.simulate( "keydown", { keyCode: ( isKeyUp ? $.ui.keyCode.UP : $.ui.keyCode.DOWN ) } );
}

function arrowsNavigateElement( id, isKeyUp, shouldMove ) {
expect( 1 );

var didMove = false,
element = $( id ).autocomplete({
source: [ "a" ],
delay: 0,
minLength: 0
});
element.on( "keypress", function( e ) {
didMove = !e.isDefaultPrevented();
});
element.simulate( "keydown", { keyCode: ( isKeyUp ? $.ui.keyCode.UP : $.ui.keyCode.DOWN ) } );
element.simulate( "keypress" );
equal( didMove, shouldMove, "respond to arrow" );
}
})();

asyncTest( "handle race condition", function() {
Expand Down
4 changes: 3 additions & 1 deletion ui/jquery.ui.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ $.widget( "ui.autocomplete", {
keypress: function( event ) {
if ( suppressKeyPress ) {
suppressKeyPress = false;
event.preventDefault();
if ( !this.isMultiLine || this.menu.element.is( ":visible" ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the visibility check necessary here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So when it's multiline (textarea), we only want to preventDefault when the autocomplete menu is visible. Otherwise, we can't use up/down arrows in the textarea in Firefox when the autocomplete menu is hidden (normal textarea keyboard navigation).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I think I'm getting confused because I'm not seeing a difference if we remove the visibility check. The logic makes sense, and I'm sure if I spent enough time I'd find something that would break without it. So I'm just going to land this :-)

event.preventDefault();
}
return;
}
if ( suppressKeyPressRepeat ) {
Expand Down