Skip to content

Commit 34037c7

Browse files
committed
Trigger change detection when user enters text.
1 parent e8d558d commit 34037c7

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
@@ -43,6 +43,7 @@ describe('MdInputContainer', function () {
4343
MdInputContainerWithDisabled,
4444
MdInputContainerWithValueBinding,
4545
MdInputContainerWithFormControl,
46+
MdInputContainerWithStaticPlaceholder,
4647
MdInputContainerMissingMdInputTestController
4748
],
4849
});
@@ -130,6 +131,26 @@ describe('MdInputContainer', function () {
130131
expect(el.classList.contains('md-empty')).toBe(false, 'should not be empty');
131132
}));
132133

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

500+
@Component({
501+
template: `
502+
<md-input-container [floatingPlaceholder]="false">
503+
<input md-input placeholder="Label">
504+
</md-input-container>
505+
`
506+
})
507+
class MdInputContainerWithStaticPlaceholder {}
508+
479509
@Component({
480510
template: `
481511
<md-input-container>

src/lib/input/input-container.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class MdHint {
7474
'[id]': 'id',
7575
'(blur)': '_onBlur()',
7676
'(focus)': '_onFocus()',
77+
'(input)': '_onInput()'
7778
}
7879
})
7980
export class MdInputDirective {
@@ -156,6 +157,11 @@ export class MdInputDirective {
156157

157158
_onBlur() { this.focused = false; }
158159

160+
_onInput() {
161+
// This is a noop function and is used to let Angular recognize the changes
162+
// to the input element while typing. Listening to the `input` event is similar to NgControls.
163+
}
164+
159165
/** Make sure the input is a supported type. */
160166
private _validateType() {
161167
if (MD_INPUT_INVALID_TYPES.indexOf(this._type) != -1) {

0 commit comments

Comments
 (0)