Skip to content

Commit 06f6fa8

Browse files
meh-cflscottgonzalez
meh-cfl
authored andcommitted
Autocomplete: Don't invoke a search from arrow keys when the element can have multi-line text. Fixes #7639 - Key up/key down in textarea's should optionally not toggle auto-complete.
1 parent 74a3f2c commit 06f6fa8

File tree

2 files changed

+100
-12
lines changed

2 files changed

+100
-12
lines changed

tests/unit/autocomplete/autocomplete_core.js

+86
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,90 @@ test( "allow form submit on enter when menu is not active", function() {
8080
ok( !event.isDefaultPrevented(), "default action is prevented" );
8181
});
8282

83+
(function() {
84+
test( "up arrow invokes search - input", function() {
85+
arrowsInvokeSearch( "#autocomplete", true, true );
86+
});
87+
88+
test( "down arrow invokes search - input", function() {
89+
arrowsInvokeSearch( "#autocomplete", false, true );
90+
});
91+
92+
test( "up arrow invokes search - textarea", function() {
93+
arrowsInvokeSearch( "#autocomplete-textarea", true, false );
94+
});
95+
96+
test( "down arrow invokes search - textarea", function() {
97+
arrowsInvokeSearch( "#autocomplete-textarea", false, false );
98+
});
99+
100+
test( "up arrow invokes search - contenteditable", function() {
101+
arrowsInvokeSearch( "#autocomplete-contenteditable", true, false );
102+
});
103+
104+
test( "down arrow invokes search - contenteditable", function() {
105+
arrowsInvokeSearch( "#autocomplete-contenteditable", false, false );
106+
});
107+
108+
test( "up arrow moves focus - input", function() {
109+
arrowsMoveFocus( "#autocomplete", true );
110+
});
111+
112+
test( "down arrow moves focus - input", function() {
113+
arrowsMoveFocus( "#autocomplete", false );
114+
});
115+
116+
test( "up arrow moves focus - textarea", function() {
117+
arrowsMoveFocus( "#autocomplete-textarea", true );
118+
});
119+
120+
test( "down arrow moves focus - textarea", function() {
121+
arrowsMoveFocus( "#autocomplete-textarea", false );
122+
});
123+
124+
test( "up arrow moves focus - contenteditable", function() {
125+
arrowsMoveFocus( "#autocomplete-contenteditable", true );
126+
});
127+
128+
test( "down arrow moves focus - contenteditable", function() {
129+
arrowsMoveFocus( "#autocomplete-contenteditable", false );
130+
});
131+
132+
function arrowsInvokeSearch( id, isKeyUp, shouldMove ) {
133+
expect( 1 );
134+
135+
var didMove = false,
136+
element = $( id ).autocomplete({
137+
source: [ "a" ],
138+
delay: 0,
139+
minLength: 0
140+
});
141+
element.data( "autocomplete" )._move = function() {
142+
didMove = true;
143+
};
144+
element.simulate( "keydown", { keyCode: ( isKeyUp ? $.ui.keyCode.UP : $.ui.keyCode.DOWN ) } );
145+
equal( didMove, shouldMove, "respond to arrow" );
146+
}
147+
148+
function arrowsMoveFocus( id, isKeyUp ) {
149+
expect( 1 );
150+
151+
var didMove = false,
152+
element = $( id ).autocomplete({
153+
source: [ "a" ],
154+
delay: 0,
155+
minLength: 0
156+
});
157+
element.data( "autocomplete" )._move = function() {
158+
ok( true, "repsond to arrow" );
159+
};
160+
element.autocomplete( "search" );
161+
element.simulate( "keydown", { keyCode: ( isKeyUp ? $.ui.keyCode.UP : $.ui.keyCode.DOWN ) } );
162+
}
163+
})();
164+
165+
(function() {
166+
167+
})();
168+
83169
}( jQuery ) );

ui/jquery.ui.autocomplete.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ $.widget( "ui.autocomplete", {
5858
suppressKeyPressRepeat,
5959
suppressInput;
6060

61+
this.isMultiLine = this.element.is( "textarea,[contenteditable]" );
6162
this.valueMethod = this.element[ this.element.is( "input,textarea" ) ? "val" : "text" ];
6263

6364
this.element
@@ -92,15 +93,11 @@ $.widget( "ui.autocomplete", {
9293
break;
9394
case keyCode.UP:
9495
suppressKeyPress = true;
95-
self._move( "previous", event );
96-
// prevent moving cursor to beginning of text field in some browsers
97-
event.preventDefault();
96+
self._keyEvent( "previous", event );
9897
break;
9998
case keyCode.DOWN:
10099
suppressKeyPress = true;
101-
self._move( "next", event );
102-
// prevent moving cursor to end of text field in some browsers
103-
event.preventDefault();
100+
self._keyEvent( "next", event );
104101
break;
105102
case keyCode.ENTER:
106103
case keyCode.NUMPAD_ENTER:
@@ -151,14 +148,10 @@ $.widget( "ui.autocomplete", {
151148
self._move( "nextPage", event );
152149
break;
153150
case keyCode.UP:
154-
self._move( "previous", event );
155-
// prevent moving cursor to beginning of text field in some browsers
156-
event.preventDefault();
151+
self._keyEvent( "previous", event );
157152
break;
158153
case keyCode.DOWN:
159-
self._move( "next", event );
160-
// prevent moving cursor to end of text field in some browsers
161-
event.preventDefault();
154+
self._keyEvent( "next", event );
162155
break;
163156
}
164157
})
@@ -495,6 +488,15 @@ $.widget( "ui.autocomplete", {
495488

496489
_value: function( value ) {
497490
return this.valueMethod.apply( this.element, arguments );
491+
},
492+
493+
_keyEvent: function( keyEvent, event ) {
494+
if ( !this.isMultiLine || this.menu.element.is( ":visible" ) ) {
495+
this._move( keyEvent, event );
496+
497+
// prevents moving cursor to beginning/end of the text field in some browsers
498+
event.preventDefault();
499+
}
498500
}
499501
});
500502

0 commit comments

Comments
 (0)