* Resize the textarea to fit its content. * @param force Whether to force a height recalculation. By default the height will be * recalculated only if the value changed since the last call.
(force: boolean = false)
| 295 | * recalculated only if the value changed since the last call. |
| 296 | */ |
| 297 | resizeToFitContent(force: boolean = false) { |
| 298 | // If autosizing is disabled, just skip everything else |
| 299 | if (!this._enabled) { |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | this._cacheTextareaLineHeight(); |
| 304 | this._cacheTextareaPlaceholderHeight(); |
| 305 | |
| 306 | // If we haven't determined the line-height yet, we know we're still hidden and there's no point |
| 307 | // in checking the height of the textarea. |
| 308 | if (!this._cachedLineHeight) { |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | const textarea = this._elementRef.nativeElement as HTMLTextAreaElement; |
| 313 | const value = textarea.value; |
| 314 | |
| 315 | // Only resize if the value or minRows have changed since these calculations can be expensive. |
| 316 | if (!force && this._minRows === this._previousMinRows && value === this._previousValue) { |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | const scrollHeight = this._measureScrollHeight(); |
| 321 | const height = Math.max(scrollHeight, this._cachedPlaceholderHeight || 0); |
| 322 | |
| 323 | // Use the scrollHeight to know how large the textarea *would* be if fit its entire value. |
| 324 | textarea.style.height = `${height}px`; |
| 325 | |
| 326 | this._ngZone.runOutsideAngular(() => { |
| 327 | if (typeof requestAnimationFrame !== 'undefined') { |
| 328 | requestAnimationFrame(() => this._scrollToCaretPosition(textarea)); |
| 329 | } else { |
| 330 | setTimeout(() => this._scrollToCaretPosition(textarea)); |
| 331 | } |
| 332 | }); |
| 333 | |
| 334 | this._previousValue = value; |
| 335 | this._previousMinRows = this._minRows; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Resets the textarea to its original size |
no test coverage detected