66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { Directive , ElementRef , Input , AfterViewInit , DoCheck } from '@angular/core' ;
9+ import {
10+ Directive ,
11+ ElementRef ,
12+ Input ,
13+ AfterViewInit ,
14+ DoCheck ,
15+ OnDestroy ,
16+ NgZone ,
17+ } from '@angular/core' ;
1018import { Platform } from '@angular/cdk/platform' ;
19+ import { fromEvent } from 'rxjs/observable/fromEvent' ;
20+ import { debounceTime } from 'rxjs/operators/debounceTime' ;
21+ import { takeUntil } from 'rxjs/operators/takeUntil' ;
22+ import { Subject } from 'rxjs/Subject' ;
1123
1224
1325/**
@@ -22,9 +34,10 @@ import {Platform} from '@angular/cdk/platform';
2234 'rows' : '1' ,
2335 } ,
2436} )
25- export class MatTextareaAutosize implements AfterViewInit , DoCheck {
37+ export class MatTextareaAutosize implements AfterViewInit , DoCheck , OnDestroy {
2638 /** Keep track of the previous textarea value to avoid resizing when the value hasn't changed. */
2739 private _previousValue : string ;
40+ private _destroyed = new Subject < void > ( ) ;
2841
2942 private _minRows : number ;
3043 private _maxRows : number ;
@@ -47,7 +60,10 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
4760 /** Cached height of a textarea with a single row. */
4861 private _cachedLineHeight : number ;
4962
50- constructor ( private _elementRef : ElementRef , private _platform : Platform ) { }
63+ constructor (
64+ private _elementRef : ElementRef ,
65+ private _platform : Platform ,
66+ private _ngZone : NgZone ) { }
5167
5268 /** Sets the minimum height of the textarea as determined by minRows. */
5369 _setMinHeight ( ) : void {
@@ -72,9 +88,20 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
7288 ngAfterViewInit ( ) {
7389 if ( this . _platform . isBrowser ) {
7490 this . resizeToFitContent ( ) ;
91+
92+ this . _ngZone . runOutsideAngular ( ( ) => {
93+ fromEvent ( window , 'resize' )
94+ . pipe ( debounceTime ( 10 ) , takeUntil ( this . _destroyed ) )
95+ . subscribe ( ( ) => this . resizeToFitContent ( true ) ) ;
96+ } ) ;
7597 }
7698 }
7799
100+ ngOnDestroy ( ) {
101+ this . _destroyed . next ( ) ;
102+ this . _destroyed . complete ( ) ;
103+ }
104+
78105 /** Sets a style property on the textarea element. */
79106 private _setTextareaStyle ( property : string , value : string ) : void {
80107 const textarea = this . _elementRef . nativeElement as HTMLTextAreaElement ;
@@ -132,8 +159,12 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
132159 }
133160 }
134161
135- /** Resize the textarea to fit its content. */
136- resizeToFitContent ( ) {
162+ /**
163+ * Resize the textarea to fit its content.
164+ * @param force Whether to force a height recalculation. By default the height will be
165+ * recalculated only if the value changed since the last call.
166+ */
167+ resizeToFitContent ( force = false ) {
137168 this . _cacheTextareaLineHeight ( ) ;
138169
139170 // If we haven't determined the line-height yet, we know we're still hidden and there's no point
@@ -146,7 +177,7 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
146177 const value = textarea . value ;
147178
148179 // Only resize of the value changed since these calculations can be expensive.
149- if ( value === this . _previousValue ) {
180+ if ( value === this . _previousValue && ! force ) {
150181 return ;
151182 }
152183
0 commit comments