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(input): incorrect height with autosize #4084

Merged
merged 2 commits into from
Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 27 additions & 6 deletions src/lib/input/autosize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('MdTextareaAutosize', () => {
});

it('should resize the textarea based on its content', () => {
let previousHeight = textarea.offsetHeight;
let previousHeight = textarea.clientHeight;

fixture.componentInstance.content = `
Once upon a midnight dreary, while I pondered, weak and weary,
Expand All @@ -43,12 +43,12 @@ describe('MdTextareaAutosize', () => {
fixture.detectChanges();
autosize.resizeToFitContent();

expect(textarea.offsetHeight)
expect(textarea.clientHeight)
.toBeGreaterThan(previousHeight, 'Expected textarea to have grown with added content.');
expect(textarea.offsetHeight)
expect(textarea.clientHeight)
.toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight');

previousHeight = textarea.offsetHeight;
previousHeight = textarea.clientHeight;
fixture.componentInstance.content += `
Ah, distinctly I remember it was in the bleak December;
And each separate dying ember wrought its ghost upon the floor.
Expand All @@ -60,9 +60,9 @@ describe('MdTextareaAutosize', () => {
fixture.detectChanges();
autosize.resizeToFitContent();

expect(textarea.offsetHeight)
expect(textarea.clientHeight)
.toBeGreaterThan(previousHeight, 'Expected textarea to have grown with added content.');
expect(textarea.offsetHeight)
expect(textarea.clientHeight)
.toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight');
});

Expand Down Expand Up @@ -103,6 +103,27 @@ describe('MdTextareaAutosize', () => {
expect(fixture.componentInstance.autosize.resizeToFitContent).toBeTruthy();
});


it('should properly resize to content on init', () => {
// Manually create the test component in this test, because in this test the first change
// detection should be triggered after a multiline content is set.
fixture = TestBed.createComponent(AutosizeTextAreaWithContent);
textarea = fixture.nativeElement.querySelector('textarea');
autosize = fixture.debugElement.query(By.css('textarea')).injector.get(MdTextareaAutosize);

fixture.componentInstance.content = `
Line
Line
Line
Line
Line`;

fixture.detectChanges();

expect(textarea.clientHeight)
.toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight');
});

});


Expand Down
10 changes: 5 additions & 5 deletions src/lib/input/autosize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Directive, ElementRef, Input, OnInit} from '@angular/core';
import {Directive, ElementRef, Input, AfterViewInit} from '@angular/core';


/**
Expand All @@ -14,7 +14,7 @@ import {Directive, ElementRef, Input, OnInit} from '@angular/core';
'[style.max-height]': '_maxHeight',
},
})
export class MdTextareaAutosize implements OnInit {
export class MdTextareaAutosize implements AfterViewInit {
/** @deprecated Use mdAutosizeMinRows */
@Input() minRows: number;

Expand Down Expand Up @@ -46,7 +46,7 @@ export class MdTextareaAutosize implements OnInit {
return this.maxRows ? `${this.maxRows * this._cachedLineHeight}px` : null;
}

ngOnInit() {
ngAfterViewInit() {
this._cacheTextareaLineHeight();
this.resizeToFitContent();
}
Expand All @@ -71,13 +71,13 @@ export class MdTextareaAutosize implements OnInit {
textareaClone.style.position = 'absolute';
textareaClone.style.visibility = 'hidden';
textareaClone.style.border = 'none';
textareaClone.style.padding = '';
textareaClone.style.padding = '0px';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just '0'?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

textareaClone.style.height = '';
textareaClone.style.minHeight = '';
textareaClone.style.maxHeight = '';

textarea.parentNode.appendChild(textareaClone);
this._cachedLineHeight = textareaClone.offsetHeight;
this._cachedLineHeight = textareaClone.clientHeight;
textarea.parentNode.removeChild(textareaClone);
}

Expand Down