Skip to content

Commit 1055720

Browse files
devversionandrewseguin
authored andcommitted
feat(input): autosize sets default amount of rows to one (#4906)
* 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 41c43cc commit 1055720

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {Directive, ElementRef, Input, AfterViewInit} from '@angular/core';
1010
exportAs: 'mdTextareaAutosize',
1111
host: {
1212
'(input)': 'resizeToFitContent()',
13+
// Textarea elements that have the directive applied should have a single row by default.
14+
// Browsers normally show two rows by default and therefore this limits the minRows binding.
15+
'rows': '1',
1316
},
1417
})
1518
export class MdTextareaAutosize implements AfterViewInit {
@@ -116,7 +119,8 @@ export class MdTextareaAutosize implements AfterViewInit {
116119

117120
/** Resize the textarea to fit its content. */
118121
resizeToFitContent() {
119-
let textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
122+
const textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
123+
120124
// Reset the textarea height to auto in order to shrink back to its default size.
121125
textarea.style.height = 'auto';
122126

0 commit comments

Comments
 (0)