Skip to content

Commit aa15cf0

Browse files
committed
feat(input): autosize sets default amount of rows to one
* The autosize directive now sets the rows property of the textarea to one by default. * This is necessary because browsers by default set the rows property to two and therefore setting the minRows and maxRows binding to something below two doesn't work. Fixes #4852
1 parent 945aa43 commit aa15cf0

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/lib/input/autosize.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ describe('MdTextareaAutosize', () => {
103103
expect(fixture.componentInstance.autosize.resizeToFitContent).toBeTruthy();
104104
});
105105

106+
it('should initially set the rows of a textarea to one', () => {
107+
expect(textarea.rows)
108+
.toBe(1, 'Expected the directive to initially set the rows property to one.');
109+
110+
fixture.componentInstance.minRows = 1;
111+
fixture.detectChanges();
112+
113+
expect(textarea.rows)
114+
.toBe(1, 'Expected the textarea to have the rows property set to one.');
115+
116+
const previousMinHeight = parseInt(textarea.style.minHeight);
117+
118+
fixture.componentInstance.minRows = 2;
119+
fixture.detectChanges();
120+
121+
expect(textarea.rows).toBe(1, 'Expected the rows property to be set to one. ' +
122+
'The amount of rows will be specified using CSS.');
123+
124+
expect(parseInt(textarea.style.minHeight))
125+
.toBeGreaterThan(previousMinHeight, 'Expected the textarea to grow to two rows.');
126+
});
106127

107128
it('should properly resize to content on init', () => {
108129
// Manually create the test component in this test, because in this test the first change

src/lib/input/autosize.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,23 @@ export class MdTextareaAutosize implements AfterViewInit {
7171

7272
ngAfterViewInit() {
7373
this._cacheTextareaLineHeight();
74+
this._setInitialRows();
7475
this.resizeToFitContent();
7576
}
7677

78+
/**
79+
* Sets the rows property of the textarea to one by default.
80+
* Browsers by default set the rows property to two. Therefore setting the minRows and maxRows
81+
* binding to something below two doesn't work.
82+
*/
83+
private _setInitialRows() {
84+
const textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
85+
86+
// By default textarea elements that have the mdTextareaAutosize directive applied will
87+
// have a single row by default. Browsers normally show two rows by default.
88+
textarea.rows = 1;
89+
}
90+
7791
/** Sets a style property on the textarea element. */
7892
private _setTextareaStyle(property: string, value: string): void {
7993
const textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
@@ -116,7 +130,8 @@ export class MdTextareaAutosize implements AfterViewInit {
116130

117131
/** Resize the textarea to fit its content. */
118132
resizeToFitContent() {
119-
let textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
133+
const textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
134+
120135
// Reset the textarea height to auto in order to shrink back to its default size.
121136
textarea.style.height = 'auto';
122137

0 commit comments

Comments
 (0)