6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
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' ;
10
18
import { Platform } from '@angular/cdk/platform' ;
19
+ import { fromEvent } from 'rxjs/observable/fromEvent' ;
20
+ import { debounceTime } from 'rxjs/operators/debounceTime' ;
21
+ import { Subscription } from 'rxjs/Subscription' ;
11
22
12
23
13
24
/**
@@ -22,9 +33,10 @@ import {Platform} from '@angular/cdk/platform';
22
33
'rows' : '1' ,
23
34
} ,
24
35
} )
25
- export class MatTextareaAutosize implements AfterViewInit , DoCheck {
36
+ export class MatTextareaAutosize implements AfterViewInit , DoCheck , OnDestroy {
26
37
/** Keep track of the previous textarea value to avoid resizing when the value hasn't changed. */
27
38
private _previousValue : string ;
39
+ private _resize = Subscription . EMPTY ;
28
40
29
41
private _minRows : number ;
30
42
private _maxRows : number ;
@@ -47,7 +59,10 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
47
59
/** Cached height of a textarea with a single row. */
48
60
private _cachedLineHeight : number ;
49
61
50
- constructor ( private _elementRef : ElementRef , private _platform : Platform ) { }
62
+ constructor (
63
+ private _elementRef : ElementRef ,
64
+ private _platform : Platform ,
65
+ private _ngZone : NgZone ) { }
51
66
52
67
/** Sets the minimum height of the textarea as determined by minRows. */
53
68
_setMinHeight ( ) : void {
@@ -72,9 +87,19 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
72
87
ngAfterViewInit ( ) {
73
88
if ( this . _platform . isBrowser ) {
74
89
this . resizeToFitContent ( ) ;
90
+
91
+ this . _resize = this . _ngZone . runOutsideAngular ( ( ) => {
92
+ return fromEvent ( window , 'resize' )
93
+ . pipe ( debounceTime ( 10 ) )
94
+ . subscribe ( ( ) => this . resizeToFitContent ( true ) ) ;
95
+ } ) ;
75
96
}
76
97
}
77
98
99
+ ngOnDestroy ( ) {
100
+ this . _resize . unsubscribe ( ) ;
101
+ }
102
+
78
103
/** Sets a style property on the textarea element. */
79
104
private _setTextareaStyle ( property : string , value : string ) : void {
80
105
const textarea = this . _elementRef . nativeElement as HTMLTextAreaElement ;
@@ -132,8 +157,12 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
132
157
}
133
158
}
134
159
135
- /** Resize the textarea to fit its content. */
136
- resizeToFitContent ( ) {
160
+ /**
161
+ * Resize the textarea to fit its content.
162
+ * @param force Whether to force a height recalculation. By default the height will be
163
+ * recalculated only if the value changed since the last call.
164
+ */
165
+ resizeToFitContent ( force = false ) {
137
166
this . _cacheTextareaLineHeight ( ) ;
138
167
139
168
// If we haven't determined the line-height yet, we know we're still hidden and there's no point
@@ -146,7 +175,7 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
146
175
const value = textarea . value ;
147
176
148
177
// Only resize of the value changed since these calculations can be expensive.
149
- if ( value === this . _previousValue ) {
178
+ if ( value === this . _previousValue && ! force ) {
150
179
return ;
151
180
}
152
181
0 commit comments