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
444
444
* appearance.
445
445
*/
446
446
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 ( ) ) {
448
451
return ;
449
452
}
450
453
@@ -465,14 +468,14 @@ export class MatFormField extends _MatFormFieldMixinBase
465
468
466
469
const containerStart = this . _getStartEnd (
467
470
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 ( ) ) ;
470
472
let labelWidth = 0 ;
471
- for ( const child of this . _label . nativeElement . children ) {
473
+
474
+ for ( const child of labelEl . children ) {
472
475
labelWidth += child . offsetWidth ;
473
476
}
474
477
startWidth = labelStart - containerStart - outlineGapPadding ;
475
- gapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2 ;
478
+ gapWidth = labelWidth > 0 ? labelWidth * floatingLabelScale + outlineGapPadding * 2 : 0 ;
476
479
}
477
480
478
481
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', () => {
1151
1151
expect ( parseInt ( outlineStart . style . width ) ) . toBeGreaterThan ( 0 ) ;
1152
1152
expect ( parseInt ( outlineGap . style . width ) ) . toBeGreaterThan ( 0 ) ;
1153
1153
} ) ) ;
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
+
1154
1173
} ) ;
1155
1174
1156
1175
describe ( 'MatFormField default options' , ( ) => {
@@ -1594,13 +1613,14 @@ class MatInputWithAppearance {
1594
1613
@Component ( {
1595
1614
template : `
1596
1615
<mat-form-field [appearance]="appearance">
1597
- <mat-label>Label </mat-label>
1616
+ <mat-label>{{labelContent}} </mat-label>
1598
1617
<input matInput>
1599
1618
</mat-form-field>
1600
1619
`
1601
1620
} )
1602
1621
class MatInputWithAppearanceAndLabel {
1603
1622
appearance : MatFormFieldAppearance ;
1623
+ labelContent = 'Label' ;
1604
1624
}
1605
1625
1606
1626
@Component ( {
You can’t perform that action at this time.
0 commit comments