Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

fix(typeahead): Scroll only parent element, not page #5212

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,23 +689,36 @@ describe('typeahead tests', function() {

it('should activate prev/next matches on up/down keys', function() {
changeInputValueTo(element, 'b');
expect(element).toBeOpenWithActive(2, 0);
var parentNode = element.find('ul').eq(0)[0];
var liIndex;

liIndex = 0;
expect(element).toBeOpenWithActive(2, liIndex);
expect(parentNode.scrollTop).toEqual(element.find('li').eq(liIndex)[0].offsetTop);

// Down arrow key
triggerKeyDown(element, 40);
expect(element).toBeOpenWithActive(2, 1);
liIndex = 1;
expect(element).toBeOpenWithActive(2, liIndex);
expect(parentNode.scrollTop).toEqual(element.find('li').eq(liIndex)[0].offsetTop);

// Down arrow key goes back to first element
triggerKeyDown(element, 40);
expect(element).toBeOpenWithActive(2, 0);
liIndex = 0;
expect(element).toBeOpenWithActive(2, liIndex);
expect(parentNode.scrollTop).toEqual(element.find('li').eq(liIndex)[0].offsetTop);

// Up arrow key goes back to last element
triggerKeyDown(element, 38);
expect(element).toBeOpenWithActive(2, 1);
liIndex = 1;
expect(element).toBeOpenWithActive(2, liIndex);
expect(parentNode.scrollTop).toEqual(element.find('li').eq(liIndex)[0].offsetTop);

// Up arrow key goes back to first element
triggerKeyDown(element, 38);
expect(element).toBeOpenWithActive(2, 0);
liIndex = 0;
expect(parentNode.scrollTop).toEqual(element.find('li').eq(liIndex)[0].offsetTop);
expect(element).toBeOpenWithActive(2, liIndex);
});

it('should close popup on escape key', function() {
Expand Down
8 changes: 5 additions & 3 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
}

evt.preventDefault();

var target;
switch (evt.which) {
case 9:
case 13:
Expand All @@ -395,12 +395,14 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.debounce', 'ui.bootstrap
case 38:
scope.activeIdx = (scope.activeIdx > 0 ? scope.activeIdx : scope.matches.length) - 1;
scope.$digest();
popUpEl.find('li')[scope.activeIdx].scrollIntoView(false);
target = popUpEl.find('li')[scope.activeIdx];
target.parentNode.scrollTop = target.offsetTop;
break;
case 40:
scope.activeIdx = (scope.activeIdx + 1) % scope.matches.length;
scope.$digest();
popUpEl.find('li')[scope.activeIdx].scrollIntoView(false);
target = popUpEl.find('li')[scope.activeIdx];
target.parentNode.scrollTop = target.offsetTop;
break;
}
});
Expand Down