@@ -9,13 +9,25 @@ import {ConnectedPositionStrategy} from '../core/overlay/position/connected-posi
9
9
import { Observable } from 'rxjs/Observable' ;
10
10
import { MdOptionSelectEvent , MdOption } from '../core/option/option' ;
11
11
import { ActiveDescendantKeyManager } from '../core/a11y/activedescendant-key-manager' ;
12
- import { ENTER } from '../core/keyboard/keycodes' ;
12
+ import { ENTER , UP_ARROW , DOWN_ARROW } from '../core/keyboard/keycodes' ;
13
13
import { Subscription } from 'rxjs/Subscription' ;
14
14
import 'rxjs/add/observable/merge' ;
15
15
import { Dir } from '../core/rtl/dir' ;
16
16
import 'rxjs/add/operator/startWith' ;
17
17
import 'rxjs/add/operator/switchMap' ;
18
18
19
+ /**
20
+ * The following style constants are necessary to save here in order
21
+ * to properly calculate the scrollTop of the panel. Because we are not
22
+ * actually focusing the active item, scroll must be handled manually.
23
+ */
24
+
25
+ /** The height of each autocomplete option. */
26
+ export const AUTOCOMPLETE_OPTION_HEIGHT = 48 ;
27
+
28
+ /** The total height of the autocomplete panel. */
29
+ export const MD_AUTOCOMPLETE_PANEL_HEIGHT = 256 ;
30
+
19
31
@Directive ( {
20
32
selector : 'input[mdAutocomplete], input[matAutocomplete]' ,
21
33
host : {
@@ -117,9 +129,25 @@ export class MdAutocompleteTrigger implements AfterContentInit, OnDestroy {
117
129
} else {
118
130
this . openPanel ( ) ;
119
131
this . _keyManager . onKeydown ( event ) ;
132
+ if ( event . keyCode === UP_ARROW || event . keyCode === DOWN_ARROW ) {
133
+ this . _scrollToOption ( ) ;
134
+ }
120
135
}
121
136
}
122
137
138
+ /**
139
+ * Given that we are not actually focusing active options, we must manually adjust scroll
140
+ * to reveal options below the fold. First, we find the offset of the option from the top
141
+ * of the panel. The new scrollTop will be that offset - the panel height + the option
142
+ * height, so the active option will be just visible at the bottom of the panel.
143
+ */
144
+ private _scrollToOption ( ) : void {
145
+ const optionOffset = this . _keyManager . activeItemIndex * AUTOCOMPLETE_OPTION_HEIGHT ;
146
+ const newScrollTop =
147
+ Math . max ( 0 , optionOffset - MD_AUTOCOMPLETE_PANEL_HEIGHT + AUTOCOMPLETE_OPTION_HEIGHT ) ;
148
+ this . autocomplete . _setScrollTop ( newScrollTop ) ;
149
+ }
150
+
123
151
/**
124
152
* This method listens to a stream of panel closing actions and resets the
125
153
* stream every time the option list changes.
0 commit comments