@@ -201,6 +201,41 @@ define(function (require, exports, module) {
201201 }
202202 }
203203
204+
205+ /**
206+ * Selects the next or previous item in the list
207+ * @param {number } direction +1 for next, -1 for prev
208+ * @param $popUp
209+ */
210+ function selectNextItem ( direction , $popUp ) {
211+ const $selectedItem = $popUp . find ( ".selected" ) ;
212+ let $links = $popUp . find ( "a:visible" ) . not ( function ( ) {
213+ return $ ( this ) . closest ( '.sticky-li-top' ) . length > 0 ;
214+ } ) ,
215+ nextIndex = 0 ;
216+ const selectedIndex = $links . index ( $selectedItem ) ;
217+ if ( selectedIndex >= 0 ) {
218+ // the selected item is visible, move from this index
219+ nextIndex = ( selectedIndex + direction ) % $links . length ;
220+ } else if ( direction === - 1 ) {
221+ // nothing is selected and reverse direction, select the last element
222+ nextIndex = $links . length - 1 ;
223+ } else {
224+ // nothing is selected, select the first element
225+ nextIndex = 0 ;
226+ }
227+ if ( searchStr && $links . length === 0 ) {
228+ // no search result, only the top search field visible
229+ return ;
230+ }
231+
232+ const $newItem = $links . eq ( nextIndex ) ;
233+ if ( $selectedItem ) {
234+ $selectedItem . removeClass ( "selected" ) ;
235+ }
236+ $newItem . addClass ( "selected" ) ;
237+ }
238+
204239 function _processSelectionEvent ( event ) {
205240 const { $popUp, keyboardEventHandler} = currentEventPopups [ currentEventPopups . length - 1 ] ;
206241 if ( ! $popUp || ! $popUp . is ( ":visible" ) ) {
@@ -216,18 +251,19 @@ define(function (require, exports, module) {
216251
217252 switch ( event . keyCode ) {
218253 case KeyEvent . DOM_VK_UP :
219- // selectNextItem(-1);
254+ selectNextItem ( - 1 , $popUp ) ;
220255 keyHandled = true ;
221256 break ;
222257 case KeyEvent . DOM_VK_DOWN :
223- // selectNextItem(+1);
258+ selectNextItem ( + 1 , $popUp ) ;
224259 keyHandled = true ;
225260 break ;
226261 case KeyEvent . DOM_VK_ENTER :
227262 case KeyEvent . DOM_VK_RETURN :
228- // if ($dropdownItem) {
229- // $dropdownItem.trigger("click");
230- // }
263+ const $dropdownItem = $popUp . find ( ".selected" ) ;
264+ if ( $dropdownItem ) {
265+ $dropdownItem . trigger ( "click" ) ;
266+ }
231267 keyHandled = true ;
232268 break ;
233269 }
0 commit comments