Skip to content

Commit d2cd80f

Browse files
fix(cdk/text-field): defer autosize checks until view initialization
1 parent 9187233 commit d2cd80f

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎src/cdk/text-field/autosize.spec.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ViewChild, ChangeDetectionStrategy} from '@angular/core';
1+
import {Component, Directive, ViewChild, ChangeDetectionStrategy} from '@angular/core';
22
import {ComponentFixture, TestBed} from '@angular/core/testing';
33
import {FormsModule} from '@angular/forms';
44
import {By} from '@angular/platform-browser';
@@ -419,3 +419,30 @@ class AutosizeTextareaWithNgModel {
419419
class AutosizeTextareaWithoutAutosize {
420420
content: string = '';
421421
}
422+
423+
@Directive({
424+
selector: '[largeLineHeight]',
425+
host: {'[class.large-line-height]': 'true'},
426+
})
427+
class LargeLineHeight {}
428+
429+
@Component({
430+
template: `<textarea cdkTextareaAutosize largeLineHeight cdkAutosizeMinRows="2" cdkAutosizeMaxRows="5"></textarea>`,
431+
styles: [`.large-line-height { font-size: 40px; line-height: 50px; padding: 0; border: 0; }`],
432+
imports: [TextFieldModule, LargeLineHeight],
433+
changeDetection: ChangeDetectionStrategy.Eager,
434+
})
435+
class AutosizeTextareaWithHostStyles {}
436+
437+
describe('CdkTextareaAutosize with host binding styles', () => {
438+
it('should base row limits on the rendered line height', async () => {
439+
const fixture = TestBed.createComponent(AutosizeTextareaWithHostStyles);
440+
fixture.detectChanges();
441+
await fixture.whenStable();
442+
const textarea = fixture.nativeElement.querySelector('textarea') as HTMLTextAreaElement;
443+
const lineHeight = parseFloat(getComputedStyle(textarea).lineHeight);
444+
expect(lineHeight).toBe(50);
445+
expect(parseFloat(textarea.style.minHeight)).toBe(lineHeight * 2);
446+
expect(parseFloat(textarea.style.maxHeight)).toBe(lineHeight * 5);
447+
});
448+
});

‎src/cdk/text-field/autosize.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
284284
};
285285

286286
ngDoCheck() {
287-
if (this._platform.isBrowser) {
287+
if (this._platform.isBrowser && this._isViewInited) {
288288
this.resizeToFitContent();
289289
}
290290
}

‎src/material/input/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ ng_project(
6161
"//:node_modules/@angular/platform-browser",
6262
"//src/cdk/platform",
6363
"//src/cdk/testing/private",
64+
"//src/cdk/text-field",
6465
"//src/material/core",
6566
"//src/material/form-field",
6667
"//src/material/icon",

‎src/material/input/input.spec.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {CdkTextareaAutosize} from '@angular/cdk/text-field';
12
import {getSupportedInputTypes} from '@angular/cdk/platform';
23
import {dispatchFakeEvent, wrappedErrorMessage} from '@angular/cdk/testing/private';
34
import {
@@ -2454,3 +2455,26 @@ class MatInputWithRequiredAssignableFormControl {
24542455
this.formControl.updateValueAndValidity();
24552456
}
24562457
}
2458+
2459+
@Component({
2460+
template: `
2461+
<mat-form-field>
2462+
<textarea matInput cdkTextareaAutosize cdkAutosizeMinRows="2" cdkAutosizeMaxRows="5"></textarea>
2463+
</mat-form-field>
2464+
`,
2465+
styles: [`mat-form-field { font-size: 40px; line-height: 50px; }`],
2466+
imports: [MatFormField, MatInputModule, CdkTextareaAutosize],
2467+
changeDetection: ChangeDetectionStrategy.Eager,
2468+
})
2469+
class AutosizeMatInput {}
2470+
2471+
describe('MatInput with autosize', () => {
2472+
it('should use the inherited line height for the initial row limits', () => {
2473+
const fixture = TestBed.createComponent(AutosizeMatInput);
2474+
fixture.detectChanges();
2475+
const textarea = fixture.nativeElement.querySelector('textarea') as HTMLTextAreaElement;
2476+
expect(getComputedStyle(textarea).lineHeight).toBe('50px');
2477+
expect(textarea.style.minHeight).toBe('100px');
2478+
expect(textarea.style.maxHeight).toBe('250px');
2479+
});
2480+
});

0 commit comments

Comments
 (0)