Skip to content

Commit c6824d5

Browse files
mmalerbakara
authored andcommitted
fix(input): make autosize work inside tabs & stepper (#7341)
1 parent 38bec5c commit c6824d5

File tree

5 files changed

+96
-5
lines changed

5 files changed

+96
-5
lines changed

src/demo-app/stepper/stepper-demo.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,13 @@ <h3>Horizontal Stepper Demo with Templated Label</h3>
195195
</div>
196196
</mat-step>
197197
</mat-horizontal-stepper>
198+
199+
<h3>Stepper with autosize textarea</h3>
200+
<mat-horizontal-stepper>
201+
<mat-step label="Step 1">
202+
<mat-form-field>
203+
<textarea matInput placeholder="Autosize textarea" matTextareaAutosize>This is an autosize textarea, it should adjust to the size of its content.</textarea>
204+
</mat-form-field>
205+
</mat-step>
206+
</mat-horizontal-stepper>
207+

src/demo-app/tabs/tabs-demo.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,14 @@ <h1>Tabs with background color</h1>
277277
</div>
278278
</mat-tab>
279279
</mat-tab-group>
280+
281+
<h1>Tabs with autosize textarea</h1>
282+
<mat-tab-group class="demo-tab-group">
283+
<mat-tab label="Tab 1">
284+
<div class="tab-content">
285+
<mat-form-field>
286+
<textarea matInput placeholder="Autosize textarea" matTextareaAutosize>This is an autosize textarea, it should adjust to the size of its content.</textarea>
287+
</mat-form-field>
288+
</div>
289+
</mat-tab>
290+
</mat-tab-group>

src/e2e-app/tabs/tabs-e2e.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
<mat-tab-group>
33
<mat-tab>
44
<ng-template mat-tab-label>One</ng-template>
5-
First tab's content
5+
<mat-form-field>
6+
<textarea matInput placeholder="Autosize textarea" matTextareaAutosize>This is an autosize textarea, it should adjust to the size of its content.</textarea>
7+
</mat-form-field>
68
</mat-tab>
79
<mat-tab>
810
<ng-template mat-tab-label>Two</ng-template>

src/lib/input/autosize.spec.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {ComponentFixture, TestBed, async, fakeAsync, flushMicrotasks} from '@ang
44
import {By} from '@angular/platform-browser';
55
import {MatInputModule} from './index';
66
import {MatTextareaAutosize} from './autosize';
7+
import {MatStepperModule} from '@angular/material/stepper';
8+
import {MatTabsModule} from '@angular/material/tabs';
9+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
710

811

912
describe('MatTextareaAutosize', () => {
@@ -13,8 +16,16 @@ describe('MatTextareaAutosize', () => {
1316

1417
beforeEach(async(() => {
1518
TestBed.configureTestingModule({
16-
imports: [MatInputModule, FormsModule],
19+
imports: [
20+
FormsModule,
21+
MatInputModule,
22+
MatStepperModule,
23+
MatTabsModule,
24+
NoopAnimationsModule,
25+
],
1726
declarations: [
27+
AutosizeTextareaInAStep,
28+
AutosizeTextareaInATab,
1829
AutosizeTextAreaWithContent,
1930
AutosizeTextAreaWithValue,
2031
AutosizeTextareaWithNgModel
@@ -202,6 +213,20 @@ describe('MatTextareaAutosize', () => {
202213
expect(textarea.clientHeight)
203214
.toBeGreaterThan(previousHeight, 'Expected the textarea height to have increased.');
204215
}));
216+
217+
it('should work in a tab', () => {
218+
const fixtureWithForms = TestBed.createComponent(AutosizeTextareaInATab);
219+
fixtureWithForms.detectChanges();
220+
textarea = fixtureWithForms.nativeElement.querySelector('textarea');
221+
expect(textarea.getBoundingClientRect().height).toBeGreaterThan(1);
222+
});
223+
224+
it('should work in a step', () => {
225+
const fixtureWithForms = TestBed.createComponent(AutosizeTextareaInAStep);
226+
fixtureWithForms.detectChanges();
227+
textarea = fixtureWithForms.nativeElement.querySelector('textarea');
228+
expect(textarea.getBoundingClientRect().height).toBeGreaterThan(1);
229+
});
205230
});
206231

207232

@@ -243,3 +268,33 @@ class AutosizeTextAreaWithValue {
243268
class AutosizeTextareaWithNgModel {
244269
model = '';
245270
}
271+
272+
@Component({
273+
template: `
274+
<mat-tab-group>
275+
<mat-tab label="Tab 1">
276+
<mat-form-field>
277+
<textarea matInput matTextareaAutosize>
278+
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
279+
</textarea>
280+
</mat-form-field>
281+
</mat-tab>
282+
</mat-tab-group>
283+
`
284+
})
285+
class AutosizeTextareaInATab {}
286+
287+
@Component({
288+
template: `
289+
<mat-horizontal-stepper>
290+
<mat-step label="Step 1">
291+
<mat-form-field>
292+
<textarea matInput matTextareaAautosize>
293+
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
294+
</textarea>
295+
</mat-form-field>
296+
</mat-step>
297+
</mat-horizontal-stepper>
298+
`
299+
})
300+
class AutosizeTextareaInAStep {}

src/lib/input/autosize.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
7171

7272
ngAfterViewInit() {
7373
if (this._platform.isBrowser) {
74-
this._cacheTextareaLineHeight();
7574
this.resizeToFitContent();
7675
}
7776
}
@@ -83,13 +82,17 @@ export class MatTextareaAutosize implements AfterViewInit, DoCheck {
8382
}
8483

8584
/**
86-
* Cache the height of a single-row textarea.
85+
* Cache the height of a single-row textarea if it has not already been cached.
8786
*
8887
* We need to know how large a single "row" of a textarea is in order to apply minRows and
8988
* maxRows. For the initial version, we will assume that the height of a single line in the
9089
* textarea does not ever change.
9190
*/
9291
private _cacheTextareaLineHeight(): void {
92+
if (this._cachedLineHeight) {
93+
return;
94+
}
95+
9396
let textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
9497

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

126129
ngDoCheck() {
127-
this.resizeToFitContent();
130+
if (this._platform.isBrowser) {
131+
this.resizeToFitContent();
132+
}
128133
}
129134

130135
/** Resize the textarea to fit its content. */
131136
resizeToFitContent() {
137+
this._cacheTextareaLineHeight();
138+
139+
// If we haven't determined the line-height yet, we know we're still hidden and there's no point
140+
// in checking the height of the textarea.
141+
if (!this._cachedLineHeight) {
142+
return;
143+
}
144+
132145
const textarea = this._elementRef.nativeElement as HTMLTextAreaElement;
133146
const value = textarea.value;
134147

0 commit comments

Comments
 (0)