66 * found in the LICENSE file at https://angular.io/license
77 */
88
9+ import { coerceBooleanProperty } from '@angular/cdk/coercion' ;
910import {
1011 Directive ,
1112 ElementRef ,
@@ -35,10 +36,16 @@ import {fromEvent, Subject} from 'rxjs';
3536export class CdkTextareaAutosize implements AfterViewInit , DoCheck , OnDestroy {
3637 /** Keep track of the previous textarea value to avoid resizing when the value hasn't changed. */
3738 private _previousValue : string ;
39+ private _initialHeight : string | null ;
3840 private readonly _destroyed = new Subject < void > ( ) ;
3941
4042 private _minRows : number ;
4143 private _maxRows : number ;
44+ private _enabled : boolean = true ;
45+
46+ private get textarea ( ) : HTMLTextAreaElement {
47+ return this . _elementRef . nativeElement as HTMLTextAreaElement ;
48+ }
4249
4350 /** Minimum amount of rows in the textarea. */
4451 @Input ( 'cdkAutosizeMinRows' )
@@ -56,6 +63,19 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
5663 this . _setMaxHeight ( ) ;
5764 }
5865
66+ /** Whether autosizing is enabled or not */
67+ @Input ( 'cdkTextareaAutosize' )
68+ get enabled ( ) : boolean { return this . _enabled ; }
69+ set enabled ( value : boolean ) {
70+ value = coerceBooleanProperty ( value ) ;
71+
72+ // Only act if the actual value changed. This specifically helps to not run
73+ // resizeToFitContent too early (i.e. before ngAfterViewInit)
74+ if ( this . _enabled !== value ) {
75+ ( this . _enabled = value ) ? this . resizeToFitContent ( true ) : this . reset ( ) ;
76+ }
77+ }
78+
5979 /** Cached height of a textarea with a single row. */
6080 private _cachedLineHeight : number ;
6181
@@ -86,6 +106,9 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
86106
87107 ngAfterViewInit ( ) {
88108 if ( this . _platform . isBrowser ) {
109+ // Remember the height which we started with in case autosizing is disabled
110+ this . _initialHeight = this . textarea . style . height ;
111+
89112 this . resizeToFitContent ( ) ;
90113
91114 this . _ngZone . runOutsideAngular ( ( ) => {
@@ -103,8 +126,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
103126
104127 /** Sets a style property on the textarea element. */
105128 private _setTextareaStyle ( property : string , value : string ) : void {
106- const textarea = this . _elementRef . nativeElement as HTMLTextAreaElement ;
107- textarea . style [ property ] = value ;
129+ this . textarea . style [ property ] = value ;
108130 }
109131
110132 /**
@@ -119,10 +141,8 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
119141 return ;
120142 }
121143
122- let textarea = this . _elementRef . nativeElement as HTMLTextAreaElement ;
123-
124144 // Use a clone element because we have to override some styles.
125- let textareaClone = textarea . cloneNode ( false ) as HTMLTextAreaElement ;
145+ let textareaClone = this . textarea . cloneNode ( false ) as HTMLTextAreaElement ;
126146 textareaClone . rows = 1 ;
127147
128148 // Use `position: absolute` so that this doesn't cause a browser layout and use
@@ -143,9 +163,9 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
143163 // See Firefox bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=33654
144164 textareaClone . style . overflow = 'hidden' ;
145165
146- textarea . parentNode ! . appendChild ( textareaClone ) ;
166+ this . textarea . parentNode ! . appendChild ( textareaClone ) ;
147167 this . _cachedLineHeight = textareaClone . clientHeight ;
148- textarea . parentNode ! . removeChild ( textareaClone ) ;
168+ this . textarea . parentNode ! . removeChild ( textareaClone ) ;
149169
150170 // Min and max heights have to be re-calculated if the cached line height changes
151171 this . _setMinHeight ( ) ;
@@ -164,6 +184,11 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
164184 * recalculated only if the value changed since the last call.
165185 */
166186 resizeToFitContent ( force : boolean = false ) {
187+ // If autosizing is disabled, just skip everything else
188+ if ( ! this . _enabled ) {
189+ return ;
190+ }
191+
167192 this . _cacheTextareaLineHeight ( ) ;
168193
169194 // If we haven't determined the line-height yet, we know we're still hidden and there's no point
@@ -211,6 +236,16 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
211236 this . _previousValue = value ;
212237 }
213238
239+ /**
240+ * Resets the textarea to it's original size
241+ */
242+ reset ( ) {
243+ if ( this . _initialHeight === undefined ) {
244+ return ;
245+ }
246+ this . textarea . style . height = this . _initialHeight ;
247+ }
248+
214249 _noopInputHandler ( ) {
215250 // no-op handler that ensures we're running change detection on input events.
216251 }
0 commit comments