File tree Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Expand file tree Collapse file tree 2 files changed +29
-6
lines changed Original file line number Diff line number Diff line change @@ -444,7 +444,10 @@ export class MatFormField extends _MatFormFieldMixinBase
444444 * appearance.
445445 */
446446 updateOutlineGap ( ) {
447- if ( this . appearance !== 'outline' ) {
447+ const labelEl = this . _label ? this . _label . nativeElement : null ;
448+
449+ if ( this . appearance !== 'outline' || ! labelEl || ! labelEl . children . length ||
450+ ! labelEl . textContent . trim ( ) ) {
448451 return ;
449452 }
450453
@@ -465,14 +468,14 @@ export class MatFormField extends _MatFormFieldMixinBase
465468
466469 const containerStart = this . _getStartEnd (
467470 this . _connectionContainerRef . nativeElement . getBoundingClientRect ( ) ) ;
468- const labelStart = this . _getStartEnd (
469- this . _label . nativeElement . children [ 0 ] . getBoundingClientRect ( ) ) ;
471+ const labelStart = this . _getStartEnd ( labelEl . children [ 0 ] . getBoundingClientRect ( ) ) ;
470472 let labelWidth = 0 ;
471- for ( const child of this . _label . nativeElement . children ) {
473+
474+ for ( const child of labelEl . children ) {
472475 labelWidth += child . offsetWidth ;
473476 }
474477 startWidth = labelStart - containerStart - outlineGapPadding ;
475- gapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2 ;
478+ gapWidth = labelWidth > 0 ? labelWidth * floatingLabelScale + outlineGapPadding * 2 : 0 ;
476479 }
477480
478481 for ( let i = 0 ; i < startEls . length ; i ++ ) {
Original file line number Diff line number Diff line change @@ -1151,6 +1151,25 @@ describe('MatInput with appearance', () => {
11511151 expect ( parseInt ( outlineStart . style . width ) ) . toBeGreaterThan ( 0 ) ;
11521152 expect ( parseInt ( outlineGap . style . width ) ) . toBeGreaterThan ( 0 ) ;
11531153 } ) ) ;
1154+
1155+ it ( 'should not set an outline gap if the label is empty' , fakeAsync ( ( ) => {
1156+ fixture . destroy ( ) ;
1157+ TestBed . resetTestingModule ( ) ;
1158+
1159+ const outlineFixture = createComponent ( MatInputWithAppearanceAndLabel ) ;
1160+
1161+ outlineFixture . componentInstance . labelContent = '' ;
1162+ outlineFixture . detectChanges ( ) ;
1163+ outlineFixture . componentInstance . appearance = 'outline' ;
1164+ outlineFixture . detectChanges ( ) ;
1165+ flush ( ) ;
1166+ outlineFixture . detectChanges ( ) ;
1167+
1168+ const outlineGap = outlineFixture . nativeElement . querySelector ( '.mat-form-field-outline-gap' ) ;
1169+
1170+ expect ( parseInt ( outlineGap . style . width ) ) . toBeFalsy ( ) ;
1171+ } ) ) ;
1172+
11541173} ) ;
11551174
11561175describe ( 'MatFormField default options' , ( ) => {
@@ -1594,13 +1613,14 @@ class MatInputWithAppearance {
15941613@Component ( {
15951614 template : `
15961615 <mat-form-field [appearance]="appearance">
1597- <mat-label>Label </mat-label>
1616+ <mat-label>{{labelContent}} </mat-label>
15981617 <input matInput>
15991618 </mat-form-field>
16001619 `
16011620} )
16021621class MatInputWithAppearanceAndLabel {
16031622 appearance : MatFormFieldAppearance ;
1623+ labelContent = 'Label' ;
16041624}
16051625
16061626@Component ( {
You can’t perform that action at this time.
0 commit comments