Skip to content

Commit f902781

Browse files
committed
move textarea autosize into cdk
1 parent c337588 commit f902781

File tree

24 files changed

+389
-326
lines changed

24 files changed

+389
-326
lines changed

src/cdk/input/BUILD.bazel

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ sass_library(
1919
)
2020

2121
sass_binary(
22-
name = "autofill_prebuilt_scss",
23-
src = "autofill-prebuilt.scss",
22+
name = "input_prebuilt_scss",
23+
src = "input-prebuilt.scss",
2424
)
2525

2626
# TODO(jelbourn): remove this when sass_binary supports specifying an output filename and dir.
2727
# Copy the output of the sass_binary such that the filename and path match what we expect.
2828
genrule(
29-
name = "autofill_prebuilt_css",
30-
srcs = [":autofill_prebuilt_scss"],
31-
outs = ["autofill-prebuilt.css"],
32-
cmd = "cat $(locations :autofill_prebuilt_scss) > $@",
29+
name = "input_prebuilt_css",
30+
srcs = [":input_prebuilt_scss"],
31+
outs = ["input-prebuilt.css"],
32+
cmd = "cat $(locations :input_prebuilt_scss) > $@",
3333
)

src/cdk/input/_autofill.scss renamed to src/cdk/input/_input.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Core styles that enable monitoring autofill state of inputs.
2-
@mixin cdk-input-autofill {
2+
@mixin cdk-input {
33
// Keyframes that apply no styles, but allow us to monitor when an input becomes autofilled
44
// by watching for the animation events that are fired when they start.
55
// Based on: https://medium.com/@brunn/detecting-autofilled-fields-in-javascript-aed598d25da7
@@ -13,6 +13,12 @@
1313
.cdk-input-autofill-monitored:not(:-webkit-autofill) {
1414
animation-name: cdk-input-autofill-end;
1515
}
16+
17+
// Remove the resize handle on autosizing textareas, because whatever height
18+
// the user resized to will be overwritten once they start typing again.
19+
textarea.cdk-textarea-autosize {
20+
resize: none;
21+
}
1622
}
1723

1824
// Used to generate UIDs for keyframes used to change the input autofill styles.

src/cdk/input/autofill-prebuilt.scss

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/lib/input/autosize.spec.ts renamed to src/cdk/input/autosize.spec.ts

Lines changed: 15 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
1+
import {dispatchFakeEvent} from '@angular/cdk/testing';
12
import {Component, ViewChild} from '@angular/core';
3+
import {async, ComponentFixture, fakeAsync, flush, TestBed, tick} from '@angular/core/testing';
24
import {FormsModule} from '@angular/forms';
3-
import {ComponentFixture, TestBed, async, fakeAsync, flush, tick} from '@angular/core/testing';
45
import {By} from '@angular/platform-browser';
5-
import {MatInputModule} from './index';
6-
import {MatTextareaAutosize} from './autosize';
7-
import {MatStepperModule} from '@angular/material/stepper';
8-
import {MatTabsModule} from '@angular/material/tabs';
96
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
10-
import {dispatchFakeEvent} from '@angular/cdk/testing';
7+
import {CdkTextareaAutosize} from './autosize';
8+
import {InputModule} from './input-module';
119

1210

13-
describe('MatTextareaAutosize', () => {
11+
describe('CdkTextareaAutosize', () => {
1412
let fixture: ComponentFixture<AutosizeTextAreaWithContent>;
1513
let textarea: HTMLTextAreaElement;
16-
let autosize: MatTextareaAutosize;
14+
let autosize: CdkTextareaAutosize;
1715

1816
beforeEach(async(() => {
1917
TestBed.configureTestingModule({
2018
imports: [
2119
FormsModule,
22-
MatInputModule,
23-
MatStepperModule,
24-
MatTabsModule,
20+
InputModule,
2521
NoopAnimationsModule,
2622
],
2723
declarations: [
28-
AutosizeTextareaInAStep,
29-
AutosizeTextareaInATab,
3024
AutosizeTextAreaWithContent,
3125
AutosizeTextAreaWithValue,
3226
AutosizeTextareaWithNgModel,
33-
AutosizeTextareaWithLongPlaceholder
3427
],
3528
});
3629

@@ -43,7 +36,7 @@ describe('MatTextareaAutosize', () => {
4336

4437
textarea = fixture.nativeElement.querySelector('textarea');
4538
autosize = fixture.debugElement.query(
46-
By.directive(MatTextareaAutosize)).injector.get<MatTextareaAutosize>(MatTextareaAutosize);
39+
By.directive(CdkTextareaAutosize)).injector.get<CdkTextareaAutosize>(CdkTextareaAutosize);
4740
});
4841

4942
it('should resize the textarea based on its content', () => {
@@ -116,7 +109,7 @@ describe('MatTextareaAutosize', () => {
116109
.toBeGreaterThan(previousMaxHeight, 'Expected increased max-height with maxRows increase.');
117110
});
118111

119-
it('should export the matAutosize reference', () => {
112+
it('should export the cdkAutosize reference', () => {
120113
expect(fixture.componentInstance.autosize).toBeTruthy();
121114
expect(fixture.componentInstance.autosize.resizeToFitContent).toBeTruthy();
122115
});
@@ -163,7 +156,7 @@ describe('MatTextareaAutosize', () => {
163156
// detection should be triggered after a multiline content is set.
164157
fixture = TestBed.createComponent(AutosizeTextAreaWithContent);
165158
textarea = fixture.nativeElement.querySelector('textarea');
166-
autosize = fixture.debugElement.query(By.css('textarea')).injector.get(MatTextareaAutosize);
159+
autosize = fixture.debugElement.query(By.css('textarea')).injector.get(CdkTextareaAutosize);
167160

168161
fixture.componentInstance.content = `
169162
Line
@@ -178,27 +171,6 @@ describe('MatTextareaAutosize', () => {
178171
.toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight');
179172
});
180173

181-
it('should not calculate wrong content height due to long placeholders', () => {
182-
const fixtureWithPlaceholder = TestBed.createComponent(AutosizeTextareaWithLongPlaceholder);
183-
fixtureWithPlaceholder.detectChanges();
184-
185-
textarea = fixtureWithPlaceholder.nativeElement.querySelector('textarea');
186-
autosize = fixtureWithPlaceholder.debugElement.query(
187-
By.directive(MatTextareaAutosize)).injector.get<MatTextareaAutosize>(MatTextareaAutosize);
188-
189-
autosize.resizeToFitContent(true);
190-
191-
const heightWithLongPlaceholder = textarea.clientHeight;
192-
193-
fixtureWithPlaceholder.componentInstance.placeholder = 'Short';
194-
fixtureWithPlaceholder.detectChanges();
195-
196-
autosize.resizeToFitContent(true);
197-
198-
expect(textarea.clientHeight).toBe(heightWithLongPlaceholder,
199-
'Expected the textarea height to be the same with a long placeholder.');
200-
});
201-
202174
it('should resize when an associated form control value changes', fakeAsync(() => {
203175
const fixtureWithForms = TestBed.createComponent(AutosizeTextareaWithNgModel);
204176
textarea = fixtureWithForms.nativeElement.querySelector('textarea');
@@ -237,20 +209,6 @@ describe('MatTextareaAutosize', () => {
237209
.toBeGreaterThan(previousHeight, 'Expected the textarea height to have increased.');
238210
}));
239211

240-
it('should work in a tab', () => {
241-
const fixtureWithForms = TestBed.createComponent(AutosizeTextareaInATab);
242-
fixtureWithForms.detectChanges();
243-
textarea = fixtureWithForms.nativeElement.querySelector('textarea');
244-
expect(textarea.getBoundingClientRect().height).toBeGreaterThan(1);
245-
});
246-
247-
it('should work in a step', () => {
248-
const fixtureWithForms = TestBed.createComponent(AutosizeTextareaInAStep);
249-
fixtureWithForms.detectChanges();
250-
textarea = fixtureWithForms.nativeElement.querySelector('textarea');
251-
expect(textarea.getBoundingClientRect().height).toBeGreaterThan(1);
252-
});
253-
254212
it('should trigger a resize when the window is resized', fakeAsync(() => {
255213
spyOn(autosize, 'resizeToFitContent');
256214

@@ -271,72 +229,31 @@ const textareaStyleReset = `
271229

272230
@Component({
273231
template: `
274-
<textarea matTextareaAutosize [matAutosizeMinRows]="minRows" [matAutosizeMaxRows]="maxRows"
275-
#autosize="matTextareaAutosize">
232+
<textarea cdkTextareaAutosize [cdkAutosizeMinRows]="minRows" [cdkAutosizeMaxRows]="maxRows"
233+
#autosize="cdkTextareaAutosize">
276234
{{content}}
277235
</textarea>`,
278236
styles: [textareaStyleReset],
279237
})
280238
class AutosizeTextAreaWithContent {
281-
@ViewChild('autosize') autosize: MatTextareaAutosize;
239+
@ViewChild('autosize') autosize: CdkTextareaAutosize;
282240
minRows: number | null = null;
283241
maxRows: number | null = null;
284242
content: string = '';
285243
}
286244

287245
@Component({
288-
template: `<textarea matTextareaAutosize [value]="value"></textarea>`,
246+
template: `<textarea cdkTextareaAutosize [value]="value"></textarea>`,
289247
styles: [textareaStyleReset],
290248
})
291249
class AutosizeTextAreaWithValue {
292250
value: string = '';
293251
}
294252

295253
@Component({
296-
template: `<textarea matTextareaAutosize [(ngModel)]="model"></textarea>`,
254+
template: `<textarea cdkTextareaAutosize [(ngModel)]="model"></textarea>`,
297255
styles: [textareaStyleReset],
298256
})
299257
class AutosizeTextareaWithNgModel {
300258
model = '';
301259
}
302-
303-
@Component({
304-
template: `
305-
<mat-form-field style="width: 100px">
306-
<textarea matInput matTextareaAutosize [placeholder]="placeholder"></textarea>
307-
</mat-form-field>`,
308-
styles: [textareaStyleReset],
309-
})
310-
class AutosizeTextareaWithLongPlaceholder {
311-
placeholder = 'Long Long Long Long Long Long Long Long Placeholder';
312-
}
313-
314-
@Component({
315-
template: `
316-
<mat-tab-group>
317-
<mat-tab label="Tab 1">
318-
<mat-form-field>
319-
<textarea matInput matTextareaAutosize>
320-
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
321-
</textarea>
322-
</mat-form-field>
323-
</mat-tab>
324-
</mat-tab-group>
325-
`
326-
})
327-
class AutosizeTextareaInATab {}
328-
329-
@Component({
330-
template: `
331-
<mat-horizontal-stepper>
332-
<mat-step label="Step 1">
333-
<mat-form-field>
334-
<textarea matInput matTextareaAautosize>
335-
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
336-
</textarea>
337-
</mat-form-field>
338-
</mat-step>
339-
</mat-horizontal-stepper>
340-
`
341-
})
342-
class AutosizeTextareaInAStep {}

0 commit comments

Comments
 (0)