File tree Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Original file line number Diff line number Diff line change @@ -456,7 +456,10 @@ export class MatFormField extends _MatFormFieldMixinBase
456
456
* appearance.
457
457
*/
458
458
updateOutlineGap ( ) {
459
- if ( this . appearance === 'outline' && this . _label && this . _label . nativeElement . children . length ) {
459
+ const labelEl = this . _label ? this . _label . nativeElement : null ;
460
+
461
+ if ( this . appearance === 'outline' && labelEl && labelEl . children . length &&
462
+ labelEl . textContent . trim ( ) ) {
460
463
if ( this . _platform && ! this . _platform . isBrowser ) {
461
464
// getBoundingClientRect isn't available on the server.
462
465
this . _initialGapCalculated = true ;
@@ -468,12 +471,13 @@ export class MatFormField extends _MatFormFieldMixinBase
468
471
469
472
const containerStart = this . _getStartEnd (
470
473
this . _connectionContainerRef . nativeElement . getBoundingClientRect ( ) ) ;
471
- const labelStart = this . _getStartEnd (
472
- this . _label . nativeElement . children [ 0 ] . getBoundingClientRect ( ) ) ;
474
+ const labelStart = this . _getStartEnd ( labelEl . children [ 0 ] . getBoundingClientRect ( ) ) ;
473
475
let labelWidth = 0 ;
474
- for ( const child of this . _label . nativeElement . children ) {
476
+
477
+ for ( const child of labelEl . children ) {
475
478
labelWidth += child . offsetWidth ;
476
479
}
480
+
477
481
this . _outlineGapStart = labelStart - containerStart - outlineGapPadding ;
478
482
this . _outlineGapWidth = labelWidth * floatingLabelScale + outlineGapPadding * 2 ;
479
483
} else {
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 ) ) . toBe ( 0 ) ;
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