File tree 2 files changed +19
-2
lines changed
2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -643,6 +643,21 @@ describe('MatInput without forms', () => {
643
643
expect ( formFieldEl . classList ) . toContain ( 'mat-form-field-should-float' ) ;
644
644
} ) ) ;
645
645
646
+ it ( 'should not float the label if the selectedIndex is negative' , fakeAsync ( ( ) => {
647
+ const fixture = createComponent ( MatInputSelect ) ;
648
+ fixture . detectChanges ( ) ;
649
+
650
+ const formFieldEl = fixture . debugElement . query ( By . css ( '.mat-form-field' ) ) . nativeElement ;
651
+ const selectEl : HTMLSelectElement = formFieldEl . querySelector ( 'select' ) ;
652
+
653
+ expect ( formFieldEl . classList ) . toContain ( 'mat-form-field-should-float' ) ;
654
+
655
+ selectEl . selectedIndex = - 1 ;
656
+ fixture . detectChanges ( ) ;
657
+
658
+ expect ( formFieldEl . classList ) . not . toContain ( 'mat-form-field-should-float' ) ;
659
+ } ) ) ;
660
+
646
661
it ( 'should not float labels when select has no value, no option label, ' +
647
662
'no option innerHtml' , fakeAsync ( ( ) => {
648
663
const fixture = createComponent ( MatInputSelectWithNoLabelNoValue ) ;
Original file line number Diff line number Diff line change @@ -384,8 +384,10 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
384
384
// overlapping the label with the options.
385
385
const selectElement = this . _elementRef . nativeElement as HTMLSelectElement ;
386
386
387
- return selectElement . multiple || ! this . empty || ! ! selectElement . options [ 0 ] . label ||
388
- this . focused ;
387
+ // On most browsers the `selectedIndex` will always be 0, however on IE and Edge it'll be
388
+ // -1 if the `value` is set to something, that isn't in the list of options, at a later point.
389
+ return this . focused || selectElement . multiple || ! this . empty ||
390
+ ( selectElement . selectedIndex > - 1 && ! ! selectElement . options [ 0 ] . label ) ;
389
391
} else {
390
392
return this . focused || ! this . empty ;
391
393
}
You can’t perform that action at this time.
0 commit comments