@@ -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,22 +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 ;
47
52
53
+ /** Gets the HTML element for the floating label. */
54
+ get element ( ) : HTMLElement {
55
+ return this . _elementRef . nativeElement ;
56
+ }
57
+
48
58
constructor ( ...args : unknown [ ] ) ;
49
59
constructor ( ) { }
50
60
51
61
ngAfterViewInit ( ) : void {
52
- const label = this . _elementRef . nativeElement . querySelector < HTMLElement > ( '.mdc-floating-label' ) ;
62
+ const label = this . element . querySelector < HTMLElement > ( '.mdc-floating-label' ) ;
53
63
if ( label ) {
54
- this . _elementRef . nativeElement . classList . add ( 'mdc-notched-outline--upgraded' ) ;
64
+ this . element . classList . add ( 'mdc-notched-outline--upgraded' ) ;
55
65
56
66
if ( typeof requestAnimationFrame === 'function' ) {
57
67
label . style . transitionDuration = '0s' ;
@@ -60,7 +70,18 @@ export class MatFormFieldNotchedOutline implements AfterViewInit {
60
70
} ) ;
61
71
}
62
72
} else {
63
- this . _elementRef . nativeElement . 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' ) ;
64
85
}
65
86
}
66
87
0 commit comments