Skip to content

Commit

Permalink
fix(input): make autosize work inside tabs & stepper (#7341)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalerba authored and kara committed Oct 3, 2017
1 parent 38bec5c commit c6824d5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/demo-app/stepper/stepper-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,13 @@ <h3>Horizontal Stepper Demo with Templated Label</h3>
</div>
</mat-step>
</mat-horizontal-stepper>

<h3>Stepper with autosize textarea</h3>
<mat-horizontal-stepper>
<mat-step label="Step 1">
<mat-form-field>
<textarea matInput placeholder="Autosize textarea" matTextareaAutosize>This is an autosize textarea, it should adjust to the size of its content.</textarea>
</mat-form-field>
</mat-step>
</mat-horizontal-stepper>

11 changes: 11 additions & 0 deletions src/demo-app/tabs/tabs-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,14 @@ <h1>Tabs with background color</h1>
</div>
</mat-tab>
</mat-tab-group>

<h1>Tabs with autosize textarea</h1>
<mat-tab-group class="demo-tab-group">
<mat-tab label="Tab 1">
<div class="tab-content">
<mat-form-field>
<textarea matInput placeholder="Autosize textarea" matTextareaAutosize>This is an autosize textarea, it should adjust to the size of its content.</textarea>
</mat-form-field>
</div>
</mat-tab>
</mat-tab-group>
4 changes: 3 additions & 1 deletion src/e2e-app/tabs/tabs-e2e.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<mat-tab-group>
<mat-tab>
<ng-template mat-tab-label>One</ng-template>
First tab's content
<mat-form-field>
<textarea matInput placeholder="Autosize textarea" matTextareaAutosize>This is an autosize textarea, it should adjust to the size of its content.</textarea>
</mat-form-field>
</mat-tab>
<mat-tab>
<ng-template mat-tab-label>Two</ng-template>
Expand Down
57 changes: 56 additions & 1 deletion src/lib/input/autosize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {ComponentFixture, TestBed, async, fakeAsync, flushMicrotasks} from '@ang
import {By} from '@angular/platform-browser';
import {MatInputModule} from './index';
import {MatTextareaAutosize} from './autosize';
import {MatStepperModule} from '@angular/material/stepper';
import {MatTabsModule} from '@angular/material/tabs';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';


describe('MatTextareaAutosize', () => {
Expand All @@ -13,8 +16,16 @@ describe('MatTextareaAutosize', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MatInputModule, FormsModule],
imports: [
FormsModule,
MatInputModule,
MatStepperModule,
MatTabsModule,
NoopAnimationsModule,
],
declarations: [
AutosizeTextareaInAStep,
AutosizeTextareaInATab,
AutosizeTextAreaWithContent,
AutosizeTextAreaWithValue,
AutosizeTextareaWithNgModel
Expand Down Expand Up @@ -202,6 +213,20 @@ describe('MatTextareaAutosize', () => {
expect(textarea.clientHeight)
.toBeGreaterThan(previousHeight, 'Expected the textarea height to have increased.');
}));

it('should work in a tab', () => {
const fixtureWithForms = TestBed.createComponent(AutosizeTextareaInATab);
fixtureWithForms.detectChanges();
textarea = fixtureWithForms.nativeElement.querySelector('textarea');
expect(textarea.getBoundingClientRect().height).toBeGreaterThan(1);
});

it('should work in a step', () => {
const fixtureWithForms = TestBed.createComponent(AutosizeTextareaInAStep);
fixtureWithForms.detectChanges();
textarea = fixtureWithForms.nativeElement.querySelector('textarea');
expect(textarea.getBoundingClientRect().height).toBeGreaterThan(1);
});
});


Expand Down Expand Up @@ -243,3 +268,33 @@ class AutosizeTextAreaWithValue {
class AutosizeTextareaWithNgModel {
model = '';
}

@Component({
template: `
<mat-tab-group>
<mat-tab label="Tab 1">
<mat-form-field>
<textarea matInput matTextareaAutosize>
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
</textarea>
</mat-form-field>
</mat-tab>
</mat-tab-group>
`
})
class AutosizeTextareaInATab {}

@Component({
template: `
<mat-horizontal-stepper>
<mat-step label="Step 1">
<mat-form-field>
<textarea matInput matTextareaAautosize>
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
</textarea>
</mat-form-field>
</mat-step>
</mat-horizontal-stepper>
`
})
class AutosizeTextareaInAStep {}
19 changes: 16 additions & 3 deletions src/lib/input/autosize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {

ngAfterViewInit() {
if (this._platform.isBrowser) {
this._cacheTextareaLineHeight();
this.resizeToFitContent();
}
}
Expand All @@ -83,13 +82,17 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
}

/**
* Cache the height of a single-row textarea.
* Cache the height of a single-row textarea if it has not already been cached.
*
* We need to know how large a single "row" of a textarea is in order to apply minRows and
* maxRows. For the initial version, we will assume that the height of a single line in the
* textarea does not ever change.
*/
private _cacheTextareaLineHeight(): void {
if (this._cachedLineHeight) {
return;
}

let textarea = this._elementRef.nativeElement as HTMLTextAreaElement;

// Use a clone element because we have to override some styles.
Expand Down Expand Up @@ -124,11 +127,21 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
}

ngDoCheck() {
this.resizeToFitContent();
if (this._platform.isBrowser) {
this.resizeToFitContent();
}
}

/** Resize the textarea to fit its content. */
resizeToFitContent() {
this._cacheTextareaLineHeight();

// If we haven't determined the line-height yet, we know we're still hidden and there's no point
// in checking the height of the textarea.
if (!this._cachedLineHeight) {
return;
}

const textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
const value = textarea.value;

Expand Down

0 comments on commit c6824d5

Please sign in to comment.