Browse Source

Fixed infinite scrolling issue using down key.

Alexander Leonov 9 years ago
parent
commit
e897d008a6
1 changed files with 22 additions and 11 deletions
  1. 22 11
      src/js/select2/results.js

+ 22 - 11
src/js/select2/results.js

@@ -97,6 +97,25 @@ define([
     return sorter(data);
   };
 
+  Results.prototype.focusFirstItem = function () {
+
+    var $options = this.$results
+      .find('.select2-results__option[aria-selected]');
+
+    var $selected = $options.filter('[aria-selected=true]');
+
+    // Check if there are any selected options
+    if ($selected.length > 0) {
+      // If there are selected options, highlight the first
+      $selected.first().trigger('mouseenter');
+    } else {
+      // If there are no selected options, highlight the first option
+      // in the dropdown
+      $options.first().trigger('mouseenter');
+    }
+
+  };
+
   Results.prototype.setClasses = function () {
     var self = this;
 
@@ -124,17 +143,6 @@ define([
         }
       });
 
-      var $selected = $options.filter('[aria-selected=true]');
-
-      // Check if there are any selected options
-      if ($selected.length > 0) {
-        // If there are selected options, highlight the first
-        $selected.first().trigger('mouseenter');
-      } else {
-        // If there are no selected options, highlight the first option
-        // in the dropdown
-        $options.first().trigger('mouseenter');
-      }
     });
   };
 
@@ -245,6 +253,7 @@ define([
 
       if (container.isOpen()) {
         self.setClasses();
+        self.focusFirstItem();
       }
     });
 
@@ -267,6 +276,7 @@ define([
       }
 
       self.setClasses();
+      self.focusFirstItem();
     });
 
     container.on('unselect', function () {
@@ -275,6 +285,7 @@ define([
       }
 
       self.setClasses();
+      self.focusFirstItem();
     });
 
     container.on('open', function () {