Skip to content

Commit 8d0cd04

Browse files
crisbetokara
authored andcommitted
fix(autocomplete): not scrolling to active option when pressing home/end (#3709)
1 parent 78985b7 commit 8d0cd04

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,20 @@ export class MdAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
228228
this.activeOption._selectViaInteraction();
229229
event.preventDefault();
230230
} else {
231+
const prevActiveItem = this.autocomplete._keyManager.activeItem;
232+
const isArrowKey = event.keyCode === UP_ARROW || event.keyCode === DOWN_ARROW;
233+
231234
this.autocomplete._keyManager.onKeydown(event);
232-
if (event.keyCode === UP_ARROW || event.keyCode === DOWN_ARROW) {
235+
236+
if (isArrowKey) {
233237
this.openPanel();
234-
Promise.resolve().then(() => this._scrollToOption());
235238
}
239+
240+
Promise.resolve().then(() => {
241+
if (isArrowKey || this.autocomplete._keyManager.activeItem !== prevActiveItem) {
242+
this._scrollToOption();
243+
}
244+
});
236245
}
237246
}
238247

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {MdInputModule} from '../input/index';
1616
import {Dir, LayoutDirection} from '../core/rtl/dir';
1717
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
1818
import {Subscription} from 'rxjs/Subscription';
19-
import {ENTER, DOWN_ARROW, SPACE, UP_ARROW} from '../core/keyboard/keycodes';
19+
import {ENTER, DOWN_ARROW, SPACE, UP_ARROW, HOME, END} from '../core/keyboard/keycodes';
2020
import {MdOption} from '../core/option/option';
2121
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
2222
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
@@ -747,6 +747,36 @@ describe('MdAutocomplete', () => {
747747
expect(scrollContainer.scrollTop).toEqual(272, `Expected panel to reveal last option.`);
748748
}));
749749

750+
it('should scroll the active option into view when pressing END', fakeAsync(() => {
751+
tick();
752+
const scrollContainer =
753+
document.querySelector('.cdk-overlay-pane .mat-autocomplete-panel');
754+
755+
const END_EVENT = new MockKeyboardEvent(END) as KeyboardEvent;
756+
fixture.componentInstance.trigger._handleKeydown(END_EVENT);
757+
tick();
758+
fixture.detectChanges();
759+
760+
// Expect option bottom minus the panel height (528 - 256 = 272)
761+
expect(scrollContainer.scrollTop).toEqual(272, 'Expected panel to reveal the last option.');
762+
}));
763+
764+
it('should scroll the active option into view when pressing HOME', fakeAsync(() => {
765+
tick();
766+
const scrollContainer =
767+
document.querySelector('.cdk-overlay-pane .mat-autocomplete-panel');
768+
769+
scrollContainer.scrollTop = 100;
770+
fixture.detectChanges();
771+
772+
const HOME_EVENT = new MockKeyboardEvent(HOME) as KeyboardEvent;
773+
fixture.componentInstance.trigger._handleKeydown(HOME_EVENT);
774+
tick();
775+
fixture.detectChanges();
776+
777+
expect(scrollContainer.scrollTop).toEqual(0, 'Expected panel to reveal the first option.');
778+
}));
779+
750780
});
751781

752782
describe('aria', () => {

0 commit comments

Comments
 (0)