Skip to content

Commit de266a1

Browse files
committed
Autocomplete: Handle clicks outside the autocomplete after scrolling the results (which causes the body to gain focus). Fixes #5903 - Autocomplete doesn't close after scrolling.
1 parent ed07f0a commit de266a1

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

ui/jquery.ui.autocomplete.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,24 @@ $.widget( "ui.autocomplete", {
119119
.addClass( "ui-autocomplete" )
120120
.appendTo( $( this.options.appendTo || "body", doc )[0] )
121121
// prevent the close-on-blur in case of a "slow" click on the menu (long mousedown)
122-
.mousedown(function() {
122+
.mousedown(function( event ) {
123+
// clicking on the scrollbar causes focus to shift to the body
124+
// but we can't detect a mouseup or a click immediately afterward
125+
// so we have to track the next mousedown and close the menu if
126+
// the user clicks somewhere outside of the autocomplete
127+
var menuElement = self.menu.element[ 0 ];
128+
if ( event.target === menuElement ) {
129+
setTimeout(function() {
130+
$( document ).one( 'mousedown', function( event ) {
131+
if ( event.target !== self.element[ 0 ] &&
132+
event.target !== menuElement &&
133+
!$.ui.contains( menuElement, event.target ) ) {
134+
self.close();
135+
}
136+
});
137+
}, 1 );
138+
}
139+
123140
// use another timeout to make sure the blur-event-handler on the input was already triggered
124141
setTimeout(function() {
125142
clearTimeout( self.closing );

0 commit comments

Comments
 (0)