@@ -13,6 +13,8 @@ import {
13
13
ElementRef ,
14
14
Input ,
15
15
NgZone ,
16
+ OnChanges ,
17
+ SimpleChanges ,
16
18
ViewChild ,
17
19
ViewEncapsulation ,
18
20
inject ,
@@ -36,21 +38,27 @@ import {
36
38
changeDetection : ChangeDetectionStrategy . OnPush ,
37
39
encapsulation : ViewEncapsulation . None ,
38
40
} )
39
- export class MatFormFieldNotchedOutline implements AfterViewInit {
41
+ export class MatFormFieldNotchedOutline implements AfterViewInit , OnChanges {
40
42
private _elementRef = inject < ElementRef < HTMLElement > > ( ElementRef ) ;
41
43
private _ngZone = inject ( NgZone ) ;
42
44
43
45
/** Whether the notch should be opened. */
44
46
@Input ( 'matFormFieldNotchedOutlineOpen' ) open : boolean = false ;
45
47
48
+ /** Whether the floating label is present. */
49
+ @Input ( 'matFormFieldHasFloatingLabel' ) hasFloatingLabel : boolean = false ;
50
+
46
51
@ViewChild ( 'notch' ) _notch : ElementRef < HTMLElement > ;
47
52
48
- ngAfterViewInit ( ) : void {
49
- const element = this . _elementRef . nativeElement ;
50
- const label = element . querySelector < HTMLElement > ( '.mdc-floating-label' ) ;
53
+ /** Gets the HTML element for the floating label. */
54
+ get element ( ) : HTMLElement {
55
+ return this . _elementRef . nativeElement ;
56
+ }
51
57
58
+ ngAfterViewInit ( ) : void {
59
+ const label = this . element . querySelector < HTMLElement > ( '.mdc-floating-label' ) ;
52
60
if ( label ) {
53
- element . classList . add ( 'mdc-notched-outline--upgraded' ) ;
61
+ this . element . classList . add ( 'mdc-notched-outline--upgraded' ) ;
54
62
55
63
if ( typeof requestAnimationFrame === 'function' ) {
56
64
label . style . transitionDuration = '0s' ;
@@ -59,7 +67,18 @@ export class MatFormFieldNotchedOutline implements AfterViewInit {
59
67
} ) ;
60
68
}
61
69
} else {
62
- element . classList . add ( 'mdc-notched-outline--no-label' ) ;
70
+ this . element . classList . add ( 'mdc-notched-outline--no-label' ) ;
71
+ }
72
+ }
73
+
74
+ ngOnChanges ( changes : SimpleChanges ) {
75
+ if (
76
+ changes [ 'hasFloatingLabel' ] &&
77
+ this . hasFloatingLabel &&
78
+ this . element . classList . contains ( 'mdc-notched-outline--no-label' )
79
+ ) {
80
+ this . element . classList . add ( 'mdc-notched-outline--upgraded' ) ;
81
+ this . element . classList . remove ( 'mdc-notched-outline--no-label' ) ;
63
82
}
64
83
}
65
84
0 commit comments