@@ -157,7 +157,8 @@ describe('MatMdcInput without forms', () => {
157
157
fixture . detectChanges ( ) ;
158
158
159
159
expect ( formField . _control . empty ) . toBe ( false ) ;
160
- expect ( formField . _shouldLabelFloat ( ) ) . toBe ( true ) ;
160
+ // should not float label if there is no label
161
+ expect ( formField . _shouldLabelFloat ( ) ) . toBe ( false ) ;
161
162
} ) ) ;
162
163
163
164
it ( 'should not be empty when the value set before view init' , fakeAsync ( ( ) => {
@@ -1531,6 +1532,62 @@ describe('MatFormField default options', () => {
1531
1532
) . toBe ( true ) ;
1532
1533
} ) ;
1533
1534
} ) ;
1535
+ describe ( 'MatFormField without label' , ( ) => {
1536
+ it ( 'should not float the label when no label is defined.' , ( ) => {
1537
+ let fixture = createComponent ( MatInputWithoutDefinedLabel ) ;
1538
+ fixture . detectChanges ( ) ;
1539
+
1540
+ const inputEl = fixture . debugElement . query ( By . css ( 'input' ) ) ! ;
1541
+ const formField = fixture . debugElement . query ( By . directive ( MatFormField ) ) !
1542
+ . componentInstance as MatFormField ;
1543
+
1544
+ // Update the value of the input and set focus.
1545
+ inputEl . nativeElement . value = 'Text' ;
1546
+ fixture . detectChanges ( ) ;
1547
+
1548
+ // should not float label if there is no label
1549
+ expect ( formField . _shouldLabelFloat ( ) ) . toBe ( false ) ;
1550
+ } ) ;
1551
+
1552
+ it ( 'should not float the label when the label is removed after it has been shown' , ( ) => {
1553
+ let fixture = createComponent ( MatInputWithCondictionalLabel ) ;
1554
+ fixture . detectChanges ( ) ;
1555
+ const inputEl = fixture . debugElement . query ( By . css ( 'input' ) ) ! ;
1556
+ const formField = fixture . debugElement . query ( By . directive ( MatFormField ) ) !
1557
+ . componentInstance as MatFormField ;
1558
+
1559
+ // initially, label is present
1560
+ expect ( fixture . nativeElement . querySelector ( 'label' ) ) . not . toBeNull ( ) ;
1561
+
1562
+ // removing label after it has been shown
1563
+ fixture . componentInstance . hasLabel = false ;
1564
+ inputEl . nativeElement . value = 'Text' ;
1565
+ fixture . changeDetectorRef . markForCheck ( ) ;
1566
+ fixture . detectChanges ( ) ;
1567
+
1568
+ // now expected to not have a label
1569
+ expect ( fixture . nativeElement . querySelector ( 'label' ) ) . toBeNull ( ) ;
1570
+ // should not float label since there is no label
1571
+ expect ( formField . _shouldLabelFloat ( ) ) . toBe ( false ) ;
1572
+ } ) ;
1573
+
1574
+ it ( 'should float the label when the label is not removed' , ( ) => {
1575
+ let fixture = createComponent ( MatInputWithCondictionalLabel ) ;
1576
+ fixture . detectChanges ( ) ;
1577
+ const inputEl = fixture . debugElement . query ( By . css ( 'input' ) ) ! ;
1578
+ const formField = fixture . debugElement . query ( By . directive ( MatFormField ) ) !
1579
+ . componentInstance as MatFormField ;
1580
+
1581
+ inputEl . nativeElement . value = 'Text' ;
1582
+ fixture . changeDetectorRef . markForCheck ( ) ;
1583
+ fixture . detectChanges ( ) ;
1584
+
1585
+ // Expected to have a label
1586
+ expect ( fixture . nativeElement . querySelector ( 'label' ) ) . not . toBeNull ( ) ;
1587
+ // should float label since there is a label
1588
+ expect ( formField . _shouldLabelFloat ( ) ) . toBe ( true ) ;
1589
+ } ) ;
1590
+ } ) ;
1534
1591
1535
1592
function configureTestingModule (
1536
1593
component : Type < any > ,
@@ -1787,6 +1844,28 @@ class MatInputWithDynamicLabel {
1787
1844
shouldFloat : 'always' | 'auto' = 'always' ;
1788
1845
}
1789
1846
1847
+ @Component ( {
1848
+ template : `
1849
+ <mat-form-field>
1850
+ <input matInput placeholder="Label">
1851
+ </mat-form-field>
1852
+ ` ,
1853
+ } )
1854
+ class MatInputWithoutDefinedLabel { }
1855
+
1856
+ @Component ( {
1857
+ template : `
1858
+ <mat-form-field>
1859
+ @if (hasLabel) {
1860
+ <mat-label>Label</mat-label>
1861
+ }
1862
+ <input matInput>
1863
+ </mat-form-field>` ,
1864
+ } )
1865
+ class MatInputWithCondictionalLabel {
1866
+ hasLabel = true ;
1867
+ }
1868
+
1790
1869
@Component ( {
1791
1870
template : `
1792
1871
<mat-form-field>
0 commit comments