@@ -2704,6 +2704,41 @@ describe('MatSelect', () => {
2704
2704
2705
2705
} ) ;
2706
2706
2707
+ describe ( 'with option centering disabled' , ( ) => {
2708
+ beforeEach ( async ( ( ) => configureMatSelectTestingModule ( [
2709
+ SelectWithoutOptionCentering ,
2710
+ ] ) ) ) ;
2711
+
2712
+ let fixture : ComponentFixture < SelectWithoutOptionCentering > ;
2713
+ let trigger : HTMLElement ;
2714
+ let select : HTMLElement ;
2715
+ let formField : HTMLElement ;
2716
+
2717
+ beforeEach ( fakeAsync ( ( ) => {
2718
+ fixture = TestBed . createComponent ( SelectWithoutOptionCentering ) ;
2719
+ fixture . detectChanges ( ) ;
2720
+ trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
2721
+ select = fixture . debugElement . query ( By . css ( 'mat-select' ) ) . nativeElement ;
2722
+ formField = fixture . debugElement . query ( By . css ( 'mat-form-field' ) ) . nativeElement ;
2723
+ } ) ) ;
2724
+
2725
+ it ( 'should not align the active option with the trigger if centering is disabled' ,
2726
+ fakeAsync ( ( ) => {
2727
+ trigger . click ( ) ;
2728
+ fixture . detectChanges ( ) ;
2729
+ flush ( ) ;
2730
+
2731
+ const scrollContainer = document . querySelector ( '.cdk-overlay-pane .mat-select-panel' ) ! ;
2732
+
2733
+ // The panel should be scrolled to 0 because centering the option disabled.
2734
+ expect ( scrollContainer . scrollTop ) . toEqual ( 0 , `Expected panel not to be scrolled.` ) ;
2735
+ // The trigger should contain 'Pizza' because it was preselected
2736
+ expect ( trigger . textContent ) . toContain ( 'Pizza' ) ;
2737
+ // The selected index should be 1 because it was preselected
2738
+ expect ( fixture . componentInstance . options . toArray ( ) [ 1 ] . selected ) . toBe ( true ) ;
2739
+ } ) ) ;
2740
+ } ) ;
2741
+
2707
2742
describe ( 'positioning' , ( ) => {
2708
2743
beforeEach ( async ( ( ) => configureMatSelectTestingModule ( [
2709
2744
BasicSelect ,
@@ -4445,3 +4480,32 @@ class SingleSelectWithPreselectedArrayValues {
4445
4480
@ViewChild ( MatSelect ) select : MatSelect ;
4446
4481
@ViewChildren ( MatOption ) options : QueryList < MatOption > ;
4447
4482
}
4483
+
4484
+ @Component ( {
4485
+ selector : 'select-without-option-centering' ,
4486
+ template : `
4487
+ <mat-form-field>
4488
+ <mat-select placeholder="Food" [formControl]="control" disableOptionCentering>
4489
+ <mat-option *ngFor="let food of foods" [value]="food.value">
4490
+ {{ food.viewValue }}
4491
+ </mat-option>
4492
+ </mat-select>
4493
+ </mat-form-field>
4494
+ `
4495
+ } )
4496
+ class SelectWithoutOptionCentering {
4497
+ foods : any [ ] = [
4498
+ { value : 'steak-0' , viewValue : 'Steak' } ,
4499
+ { value : 'pizza-1' , viewValue : 'Pizza' } ,
4500
+ { value : 'tacos-2' , viewValue : 'Tacos' } ,
4501
+ { value : 'sandwich-3' , viewValue : 'Sandwich' } ,
4502
+ { value : 'chips-4' , viewValue : 'Chips' } ,
4503
+ { value : 'eggs-5' , viewValue : 'Eggs' } ,
4504
+ { value : 'pasta-6' , viewValue : 'Pasta' } ,
4505
+ { value : 'sushi-7' , viewValue : 'Sushi' } ,
4506
+ ] ;
4507
+ control = new FormControl ( 'pizza-1' ) ;
4508
+
4509
+ @ViewChild ( MatSelect ) select : MatSelect ;
4510
+ @ViewChildren ( MatOption ) options : QueryList < MatOption > ;
4511
+ }
0 commit comments