@@ -245,24 +245,6 @@ $.widget( "ui.autocomplete", {
245
245
this . element . trigger ( "focus" ) ;
246
246
}
247
247
} ) ;
248
-
249
- // Clicking on the scrollbar causes focus to shift to the body
250
- // but we can't detect a mouseup or a click immediately afterward
251
- // so we have to track the next mousedown and close the menu if
252
- // the user clicks somewhere outside of the autocomplete
253
- var menuElement = this . menu . element [ 0 ] ;
254
- if ( ! $ ( event . target ) . closest ( ".ui-menu-item" ) . length ) {
255
- this . _delay ( function ( ) {
256
- var that = this ;
257
- this . document . one ( "mousedown" , function ( event ) {
258
- if ( event . target !== that . element [ 0 ] &&
259
- event . target !== menuElement &&
260
- ! $ . contains ( menuElement , event . target ) ) {
261
- that . close ( ) ;
262
- }
263
- } ) ;
264
- } ) ;
265
- }
266
248
} ,
267
249
menufocus : function ( event , ui ) {
268
250
var label , item ;
@@ -368,6 +350,20 @@ $.widget( "ui.autocomplete", {
368
350
}
369
351
} ,
370
352
353
+ _isEventTargetInWidget : function ( event ) {
354
+ var menuElement = this . menu . element [ 0 ] ;
355
+
356
+ return event . target === this . element [ 0 ] ||
357
+ event . target === menuElement ||
358
+ $ . contains ( menuElement , event . target ) ;
359
+ } ,
360
+
361
+ _closeOnClickOutside : function ( event ) {
362
+ if ( ! this . _isEventTargetInWidget ( event ) ) {
363
+ this . close ( ) ;
364
+ }
365
+ } ,
366
+
371
367
_appendTo : function ( ) {
372
368
var element = this . options . appendTo ;
373
369
@@ -496,6 +492,10 @@ $.widget( "ui.autocomplete", {
496
492
} ,
497
493
498
494
_close : function ( event ) {
495
+
496
+ // Remove the handler that closes the menu on outside clicks
497
+ this . _off ( this . document , "mousedown" ) ;
498
+
499
499
if ( this . menu . element . is ( ":visible" ) ) {
500
500
this . menu . element . hide ( ) ;
501
501
this . menu . blur ( ) ;
@@ -546,6 +546,11 @@ $.widget( "ui.autocomplete", {
546
546
if ( this . options . autoFocus ) {
547
547
this . menu . next ( ) ;
548
548
}
549
+
550
+ // Listen for interactions outside of the widget (#6642)
551
+ this . _on ( this . document , {
552
+ mousedown : "_closeOnClickOutside"
553
+ } ) ;
549
554
} ,
550
555
551
556
_resizeMenu : function ( ) {
0 commit comments