Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autosize): not resizing on programmatic changes #6654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/lib/input/autosize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('MdTextareaAutosize', () => {
textarea = fixtureWithForms.nativeElement.querySelector('textarea');
fixtureWithForms.detectChanges();

const previousHeight = textarea.clientHeight;
const previousHeight = textarea.clientHeight;

fixtureWithForms.componentInstance.model = `
And the silken, sad, uncertain rustling of each purple curtain
Expand All @@ -181,10 +181,27 @@ describe('MdTextareaAutosize', () => {
This it is and nothing more.” `;
fixtureWithForms.detectChanges();
flushMicrotasks();
fixtureWithForms.detectChanges();

expect(textarea.clientHeight)
.toBeGreaterThan(previousHeight, 'Expected increased height when ngModel is updated.');
}));

it('should resize when the textarea value is changed programmatically', fakeAsync(() => {
const previousHeight = textarea.clientHeight;

textarea.value = `
How much wood would a woodchuck chuck
if a woodchuck could chuck wood?
`;

fixture.detectChanges();
flushMicrotasks();
fixture.detectChanges();

expect(textarea.clientHeight)
.toBeGreaterThan(previousHeight, 'Expected the textarea height to have increased.');
}));
});


Expand Down
26 changes: 11 additions & 15 deletions src/lib/input/autosize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, ElementRef, Input, AfterViewInit, Optional, Self} from '@angular/core';
import {NgControl} from '@angular/forms';
import {Directive, ElementRef, Input, AfterViewInit, DoCheck} from '@angular/core';
import {Platform} from '@angular/cdk/platform';


Expand All @@ -19,13 +18,12 @@ import {Platform} from '@angular/cdk/platform';
textarea[mat-autosize], textarea[matTextareaAutosize]`,
exportAs: 'mdTextareaAutosize',
host: {
'(input)': 'resizeToFitContent()',
// Textarea elements that have the directive applied should have a single row by default.
// Browsers normally show two rows by default and therefore this limits the minRows binding.
'rows': '1',
},
})
export class MdTextareaAutosize implements AfterViewInit {
export class MdTextareaAutosize implements AfterViewInit, DoCheck {
/** Keep track of the previous textarea value to avoid resizing when the value hasn't changed. */
private _previousValue: string;

Expand Down Expand Up @@ -58,15 +56,7 @@ export class MdTextareaAutosize implements AfterViewInit {
/** Cached height of a textarea with a single row. */
private _cachedLineHeight: number;

constructor(
private _elementRef: ElementRef,
private _platform: Platform,
@Optional() @Self() formControl: NgControl) {

if (formControl && formControl.valueChanges) {
formControl.valueChanges.subscribe(() => this.resizeToFitContent());
}
}
constructor(private _elementRef: ElementRef, private _platform: Platform) {}

/** Sets the minimum height of the textarea as determined by minRows. */
_setMinHeight(): void {
Expand Down Expand Up @@ -142,11 +132,17 @@ export class MdTextareaAutosize implements AfterViewInit {
this._setMaxHeight();
}

ngDoCheck() {
this.resizeToFitContent();
}

/** Resize the textarea to fit its content. */
resizeToFitContent() {
const textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
const value = textarea.value;

if (textarea.value === this._previousValue) {
// Only resize of the value changed since these calculations can be expensive.
if (value === this._previousValue) {
return;
}

Expand All @@ -159,6 +155,6 @@ export class MdTextareaAutosize implements AfterViewInit {
textarea.style.height = `${textarea.scrollHeight}px`;
textarea.style.overflow = '';

this._previousValue = textarea.value;
this._previousValue = value;
}
}