@@ -97,6 +97,25 @@ define([
9797 return sorter ( data ) ;
9898 } ;
9999
100+ Results . prototype . focusFirstItem = function ( ) {
101+
102+ var $options = this . $results
103+ . find ( '.select2-results__option[aria-selected]' ) ;
104+
105+ var $selected = $options . filter ( '[aria-selected=true]' ) ;
106+
107+ // Check if there are any selected options
108+ if ( $selected . length > 0 ) {
109+ // If there are selected options, highlight the first
110+ $selected . first ( ) . trigger ( 'mouseenter' ) ;
111+ } else {
112+ // If there are no selected options, highlight the first option
113+ // in the dropdown
114+ $options . first ( ) . trigger ( 'mouseenter' ) ;
115+ }
116+
117+ } ;
118+
100119 Results . prototype . setClasses = function ( ) {
101120 var self = this ;
102121
@@ -124,17 +143,6 @@ define([
124143 }
125144 } ) ;
126145
127- var $selected = $options . filter ( '[aria-selected=true]' ) ;
128-
129- // Check if there are any selected options
130- if ( $selected . length > 0 ) {
131- // If there are selected options, highlight the first
132- $selected . first ( ) . trigger ( 'mouseenter' ) ;
133- } else {
134- // If there are no selected options, highlight the first option
135- // in the dropdown
136- $options . first ( ) . trigger ( 'mouseenter' ) ;
137- }
138146 } ) ;
139147 } ;
140148
@@ -245,6 +253,7 @@ define([
245253
246254 if ( container . isOpen ( ) ) {
247255 self . setClasses ( ) ;
256+ self . focusFirstItem ( ) ;
248257 }
249258 } ) ;
250259
@@ -267,6 +276,7 @@ define([
267276 }
268277
269278 self . setClasses ( ) ;
279+ self . focusFirstItem ( ) ;
270280 } ) ;
271281
272282 container . on ( 'unselect' , function ( ) {
@@ -275,6 +285,7 @@ define([
275285 }
276286
277287 self . setClasses ( ) ;
288+ self . focusFirstItem ( ) ;
278289 } ) ;
279290
280291 container . on ( 'open' , function ( ) {
0 commit comments