@@ -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,30 @@ 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
+ }
57
+
58
+ constructor ( ...args : unknown [ ] ) ;
59
+ constructor ( ) { }
51
60
61
+ ngAfterViewInit ( ) : void {
62
+ const label = this . element . querySelector < HTMLElement > ( '.mdc-floating-label' ) ;
52
63
if ( label ) {
53
- element . classList . add ( 'mdc-notched-outline--upgraded' ) ;
64
+ this . element . classList . add ( 'mdc-notched-outline--upgraded' ) ;
54
65
55
66
if ( typeof requestAnimationFrame === 'function' ) {
56
67
label . style . transitionDuration = '0s' ;
@@ -59,7 +70,18 @@ export class MatFormFieldNotchedOutline implements AfterViewInit {
59
70
} ) ;
60
71
}
61
72
} else {
62
- element . classList . add ( 'mdc-notched-outline--no-label' ) ;
73
+ this . element . classList . add ( 'mdc-notched-outline--no-label' ) ;
74
+ }
75
+ }
76
+
77
+ ngOnChanges ( changes : SimpleChanges ) {
78
+ if (
79
+ changes [ 'hasFloatingLabel' ] &&
80
+ this . hasFloatingLabel &&
81
+ this . element . classList . contains ( 'mdc-notched-outline--no-label' )
82
+ ) {
83
+ this . element . classList . add ( 'mdc-notched-outline--upgraded' ) ;
84
+ this . element . classList . remove ( 'mdc-notched-outline--no-label' ) ;
63
85
}
64
86
}
65
87
0 commit comments