Skip to content

feat(input): autosize sets default amount of rows to one #4906

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

Merged
merged 1 commit into from
Jun 5, 2017
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
21 changes: 21 additions & 0 deletions src/lib/input/autosize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ describe('MdTextareaAutosize', () => {
expect(fixture.componentInstance.autosize.resizeToFitContent).toBeTruthy();
});

it('should initially set the rows of a textarea to one', () => {
expect(textarea.rows)
.toBe(1, 'Expected the directive to initially set the rows property to one.');

fixture.componentInstance.minRows = 1;
fixture.detectChanges();

expect(textarea.rows)
.toBe(1, 'Expected the textarea to have the rows property set to one.');

const previousMinHeight = parseInt(textarea.style.minHeight);

fixture.componentInstance.minRows = 2;
fixture.detectChanges();

expect(textarea.rows).toBe(1, 'Expected the rows property to be set to one. ' +
'The amount of rows will be specified using CSS.');

expect(parseInt(textarea.style.minHeight))
.toBeGreaterThan(previousMinHeight, 'Expected the textarea to grow to two rows.');
});

it('should properly resize to content on init', () => {
// Manually create the test component in this test, because in this test the first change
Expand Down
6 changes: 5 additions & 1 deletion src/lib/input/autosize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {Directive, ElementRef, Input, AfterViewInit} from '@angular/core';
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 {
Expand Down Expand Up @@ -116,7 +119,8 @@ export class MdTextareaAutosize implements AfterViewInit {

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

// Reset the textarea height to auto in order to shrink back to its default size.
textarea.style.height = 'auto';

Expand Down