Skip to content

Commit f891b90

Browse files
committed
more wp value
1 parent e78659d commit f891b90

File tree

4 files changed

+83
-28
lines changed

4 files changed

+83
-28
lines changed

src/demo-app/input/input-demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,5 @@ <h4>Textarea</h4>
178178

179179
<md-card>
180180
<h2>textarea autosize</h2>
181-
<textarea md-autosize></textarea>
181+
<textarea md-autosize class="demo-textarea"></textarea>
182182
</md-card>

src/demo-app/input/input-demo.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@
1818
.demo-card {
1919
margin: 16px;
2020
}
21+
22+
.demo-textarea {
23+
resize: none;
24+
border: none;
25+
overflow: auto;
26+
padding: 0;
27+
background: lightblue;
28+
}

src/lib/input/autosize.spec.ts

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import {MdInputModule} from './input';
55
import {MdTextareaAutosize} from './autosize';
66

77

8-
describe('MdTextareaAutosize', () => {
9-
let fixture: ComponentFixture<TextareaWithAutosize>;
8+
fdescribe('MdTextareaAutosize', () => {
9+
let fixture: ComponentFixture<AutosizeTextAreaWithContent>;
1010
let textarea: HTMLTextAreaElement;
1111
let autosize: MdTextareaAutosize;
1212

1313
beforeEach(async(() => {
1414
TestBed.configureTestingModule({
1515
imports: [MdInputModule],
16-
declarations: [TextareaWithAutosize],
16+
declarations: [AutosizeTextAreaWithContent, AutosizeTextAreaWithValue],
1717
});
1818

1919
TestBed.compileComponents();
2020
}));
2121

2222
beforeEach(() => {
23-
fixture = TestBed.createComponent(TextareaWithAutosize);
23+
fixture = TestBed.createComponent(AutosizeTextAreaWithContent);
2424
fixture.detectChanges();
2525

2626
textarea = fixture.nativeElement.querySelector('textarea');
@@ -97,22 +97,83 @@ describe('MdTextareaAutosize', () => {
9797
expect(parseInt(textarea.style.maxHeight))
9898
.toBeGreaterThan(previousMaxHeight, 'Expected increased max-height with maxRows increase.');
9999
});
100+
101+
describe('with value binding', () => {
102+
let fixture: ComponentFixture<AutosizeTextAreaWithValue>;
103+
104+
beforeEach(() => {
105+
fixture = TestBed.createComponent(AutosizeTextAreaWithValue);
106+
fixture.detectChanges();
107+
108+
textarea = fixture.nativeElement.querySelector('textarea');
109+
autosize = fixture.debugElement.query(
110+
By.directive(MdTextareaAutosize)).injector.get(MdTextareaAutosize);
111+
});
112+
113+
it('should resize the textarea when its value property changes', () => {
114+
let previousHeight = textarea.offsetHeight;
115+
116+
fixture.componentInstance.value = `
117+
Once upon a midnight dreary, while I pondered, weak and weary,
118+
Over many a quaint and curious volume of forgotten lore—
119+
While I nodded, nearly napping, suddenly there came a tapping,
120+
As of some one gently rapping, rapping at my chamber door.
121+
“’Tis some visitor,” I muttered, “tapping at my chamber door—
122+
Only this and nothing more.”`;
123+
124+
// TODO(jelbourn): remove `resizeToFitContent` call here when we support resizing based
125+
// on setting value directly.
126+
fixture.detectChanges();
127+
autosize.resizeToFitContent();
128+
129+
expect(textarea.offsetHeight)
130+
.toBeGreaterThan(previousHeight, 'Expected textarea to have grown with added content.');
131+
expect(textarea.offsetHeight)
132+
.toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight');
133+
134+
previousHeight = textarea.offsetHeight;
135+
fixture.componentInstance.value += `
136+
Ah, distinctly I remember it was in the bleak December;
137+
And each separate dying ember wrought its ghost upon the floor.
138+
Eagerly I wished the morrow;—vainly I had sought to borrow
139+
From my books surcease of sorrow—sorrow for the lost Lenore—
140+
For the rare and radiant maiden whom the angels name Lenore—
141+
Nameless here for evermore.`;
142+
143+
fixture.detectChanges();
144+
autosize.resizeToFitContent();
145+
146+
expect(textarea.offsetHeight)
147+
.toBeGreaterThan(previousHeight, 'Expected textarea to have grown with added content.');
148+
expect(textarea.offsetHeight)
149+
.toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight');
150+
});
151+
});
100152
});
101153

102154

103-
@Component({
104-
template: `<textarea md-autosize [minRows]="minRows" [maxRows]="maxRows">{{content}}</textarea>`,
105-
styles: [`
106-
/** Reset padding and border to make measurement comparisons easier. */
155+
// Styles to reset padding and border to make measurement comparisons easier.
156+
const textareaStyleReset = `
107157
textarea {
108158
padding: 0;
109159
border: none;
110160
overflow: auto;
111-
}
112-
`]
161+
}`;
162+
163+
@Component({
164+
template: `<textarea md-autosize [minRows]="minRows" [maxRows]="maxRows">{{content}}</textarea>`,
165+
styles: [textareaStyleReset],
113166
})
114-
class TextareaWithAutosize {
167+
class AutosizeTextAreaWithContent {
115168
minRows: number = null;
116169
maxRows: number = null;
117170
content: string = '';
118171
}
172+
173+
@Component({
174+
template: `<textarea md-autosize [value]="value"></textarea>`,
175+
styles: [textareaStyleReset],
176+
})
177+
class AutosizeTextAreaWithValue {
178+
value: string = '';
179+
}

src/lib/input/autosize.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Directive, ElementRef, Input, OnInit, NgZone} from '@angular/core';
1+
import {Directive, ElementRef, Input, OnInit} from '@angular/core';
22

33

44
/**
@@ -19,24 +19,10 @@ export class MdTextareaAutosize implements OnInit {
1919
/** Maximum number of rows for this textarea. */
2020
@Input() maxRows: number;
2121

22-
/** The value of the textarea. */
23-
private _value: string;
24-
25-
@Input()
26-
get value() { return this._value }
27-
set value(newValue: string) {
28-
this._value = newValue;
29-
30-
// Wait for the DOM to be updated with the new value before resizing.
31-
this._ngZone.runOutsideAngular(() => {
32-
this._ngZone.onStable.first().subscribe(() => this.resizeToFitContent());
33-
});
34-
}
35-
3622
/** Cached height of a textarea with a single row. */
3723
private _cachedLineHeight: number;
3824

39-
constructor(private _elementRef: ElementRef, private _ngZone: NgZone) { }
25+
constructor(private _elementRef: ElementRef) { }
4026

4127
/** The minimum height of the textarea as determined by minRows. */
4228
get _minHeight() {

0 commit comments

Comments
 (0)