Skip to content

Commit a9c3bb2

Browse files
committed
Trigger change detection when user enters text.
1 parent 02e5cea commit a9c3bb2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/lib/input/input-container.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('MdInputContainer', function () {
4545
MdInputContainerWithType,
4646
MdInputContainerWithValueBinding,
4747
MdInputContainerWithFormControl,
48+
MdInputContainerWithStaticPlaceholder,
4849
MdInputContainerMissingMdInputTestController
4950
],
5051
});
@@ -132,6 +133,26 @@ describe('MdInputContainer', function () {
132133
expect(el.classList.contains('md-empty')).toBe(false, 'should not be empty');
133134
}));
134135

136+
it('should update the placeholder when input entered', async(() => {
137+
let fixture = TestBed.createComponent(MdInputContainerWithStaticPlaceholder);
138+
fixture.detectChanges();
139+
140+
let inputEl = fixture.debugElement.query(By.css('input'));
141+
let labelEl = fixture.debugElement.query(By.css('label')).nativeElement;
142+
143+
expect(labelEl.classList).toContain('md-empty');
144+
expect(labelEl.classList).not.toContain('md-float');
145+
146+
// Update the value of the input.
147+
inputEl.nativeElement.value = 'Text';
148+
149+
// Fake behavior of the `(input)` event which should trigger a change detection.
150+
fixture.detectChanges();
151+
152+
expect(labelEl.classList).not.toContain('md-empty');
153+
expect(labelEl.classList).not.toContain('md-float');
154+
}));
155+
135156
it('should not be empty when the value set before view init', async(() => {
136157
let fixture = TestBed.createComponent(MdInputContainerWithValueBinding);
137158
fixture.detectChanges();
@@ -529,6 +550,15 @@ class MdInputContainerWithValueBinding {
529550
value: string = 'Initial';
530551
}
531552

553+
@Component({
554+
template: `
555+
<md-input-container [floatingPlaceholder]="false">
556+
<input md-input placeholder="Label">
557+
</md-input-container>
558+
`
559+
})
560+
class MdInputContainerWithStaticPlaceholder {}
561+
532562
@Component({
533563
template: `
534564
<md-input-container>

src/lib/input/input-container.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export class MdHint {
8383
'[required]': 'required',
8484
'(blur)': '_onBlur()',
8585
'(focus)': '_onFocus()',
86+
'(input)': '_onInput()'
8687
}
8788
})
8889
export class MdInputDirective {
@@ -174,6 +175,11 @@ export class MdInputDirective {
174175

175176
_onBlur() { this.focused = false; }
176177

178+
_onInput() {
179+
// This is a noop function and is used to let Angular recognize the changes
180+
// to the input element while typing. Listening to the `input` event is similar to NgControls.
181+
}
182+
177183
/** Make sure the input is a supported type. */
178184
private _validateType() {
179185
if (MD_INPUT_INVALID_TYPES.indexOf(this._type) !== -1) {

0 commit comments

Comments
 (0)