File tree Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Expand file tree Collapse file tree 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', () => {
643643 expect ( formFieldEl . classList ) . toContain ( 'mat-form-field-should-float' ) ;
644644 } ) ) ;
645645
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+
646661 it ( 'should not float labels when select has no value, no option label, ' +
647662 'no option innerHtml' , fakeAsync ( ( ) => {
648663 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<
384384 // overlapping the label with the options.
385385 const selectElement = this . _elementRef . nativeElement as HTMLSelectElement ;
386386
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 ) ;
389391 } else {
390392 return this . focused || ! this . empty ;
391393 }
You can’t perform that action at this time.
0 commit comments