@@ -524,9 +524,9 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
524
524
// `parseInt` ignores the trailing 'px' and converts this to a number.
525
525
this . _triggerFontSize = parseInt ( getComputedStyle ( this . trigger . nativeElement ) [ 'font-size' ] ) ;
526
526
527
+ this . _panelOpen = true ;
527
528
this . _calculateOverlayPosition ( ) ;
528
529
this . _highlightCorrectOption ( ) ;
529
- this . _panelOpen = true ;
530
530
this . _changeDetectorRef . markForCheck ( ) ;
531
531
532
532
// Set the font size on the panel element once it exists.
@@ -637,11 +637,15 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
637
637
638
638
/** Handles keyboard events while the select is closed. */
639
639
private _handleClosedKeydown ( event : KeyboardEvent ) : void {
640
- if ( event . keyCode === ENTER || event . keyCode === SPACE ) {
640
+ const keyCode = event . keyCode ;
641
+ const isArrowKey = keyCode === DOWN_ARROW || keyCode === UP_ARROW ;
642
+ const isOpenKey = keyCode === ENTER || keyCode === SPACE ;
643
+
644
+ if ( isOpenKey || ( this . multiple && isArrowKey ) ) {
641
645
event . preventDefault ( ) ; // prevents the page from scrolling down when pressing space
642
646
this . open ( ) ;
643
- } else if ( event . keyCode === UP_ARROW || event . keyCode === DOWN_ARROW ) {
644
- this . _handleClosedArrowKey ( event ) ;
647
+ } else if ( ! this . multiple ) {
648
+ this . _keyManager . onKeydown ( event ) ;
645
649
}
646
650
}
647
651
@@ -813,10 +817,13 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
813
817
this . _keyManager = new ActiveDescendantKeyManager < MatOption > ( this . options ) . withTypeAhead ( ) ;
814
818
this . _keyManager . tabOut . pipe ( takeUntil ( this . _destroy ) ) . subscribe ( ( ) => this . close ( ) ) ;
815
819
816
- this . _keyManager . change . pipe (
817
- takeUntil ( this . _destroy ) ,
818
- filter ( ( ) => this . _panelOpen && ! ! this . panel )
819
- ) . subscribe ( ( ) => this . _scrollActiveOptionIntoView ( ) ) ;
820
+ this . _keyManager . change . pipe ( takeUntil ( this . _destroy ) ) . subscribe ( ( ) => {
821
+ if ( this . _panelOpen && this . panel ) {
822
+ this . _scrollActiveOptionIntoView ( ) ;
823
+ } else if ( ! this . _panelOpen && ! this . multiple && this . _keyManager . activeItem ) {
824
+ this . _keyManager . activeItem . _selectViaInteraction ( ) ;
825
+ }
826
+ } ) ;
820
827
}
821
828
822
829
/** Drops current option subscriptions and IDs and resets from scratch. */
@@ -1171,29 +1178,6 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
1171
1178
return `50% ${ originY } px 0px` ;
1172
1179
}
1173
1180
1174
- /** Handles the user pressing the arrow keys on a closed select. */
1175
- private _handleClosedArrowKey ( event : KeyboardEvent ) : void {
1176
- if ( this . _multiple ) {
1177
- event . preventDefault ( ) ;
1178
- this . open ( ) ;
1179
- } else {
1180
- const prevActiveItem = this . _keyManager . activeItem ;
1181
-
1182
- // Cycle though the select options even when the select is closed,
1183
- // matching the behavior of the native select element.
1184
- // TODO(crisbeto): native selects also cycle through the options with left/right arrows,
1185
- // however the key manager only supports up/down at the moment.
1186
- this . _keyManager . onKeydown ( event ) ;
1187
-
1188
- const currentActiveItem = this . _keyManager . activeItem ;
1189
-
1190
- if ( currentActiveItem && currentActiveItem !== prevActiveItem ) {
1191
- this . _clearSelection ( ) ;
1192
- this . _setSelectionByValue ( currentActiveItem . value , true ) ;
1193
- }
1194
- }
1195
- }
1196
-
1197
1181
/** Calculates the amount of items in the select. This includes options and group labels. */
1198
1182
private _getItemCount ( ) : number {
1199
1183
return this . options . length + this . optionGroups . length ;
0 commit comments