Skip to content

Commit

Permalink
fix(input): not floating the label when updating the value programmat…
Browse files Browse the repository at this point in the history
…ically with reactive forms

Fixes the input placeholder not being floating when the value is updated programmatically via `FormControl.setValue`. This was because the listener that handles the value changes gets initialized before the `FormControl`.

Fixes angular#2441.
  • Loading branch information
crisbeto committed Dec 28, 2016
1 parent dccbe41 commit 72bc742
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
29 changes: 26 additions & 3 deletions src/lib/input/input-container.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {async, TestBed, inject} from '@angular/core/testing';
import {Component} from '@angular/core';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {FormsModule, ReactiveFormsModule, FormControl} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {MdInputModule} from './input';
import {MdInputContainer} from './input-container';
import {MdInputContainer, MdInputDirective} from './input-container';
import {Platform} from '../core/platform/platform';
import {PlatformModule} from '../core/platform/index';
import {
Expand Down Expand Up @@ -41,7 +41,8 @@ describe('MdInputContainer', function () {
MdInputContainerZeroTestController,
MdTextareaWithBindings,
MdInputContainerWithDisabled,
MdInputContainerMissingMdInputTestController
MdInputContainerMissingMdInputTestController,
MdInputContainerWithFormControl
],
});

Expand Down Expand Up @@ -293,6 +294,21 @@ describe('MdInputContainer', function () {
const textarea: HTMLTextAreaElement = fixture.nativeElement.querySelector('textarea');
expect(textarea).not.toBeNull();
});

it('should update the value when using FormControl.setValue', () => {
let fixture = TestBed.createComponent(MdInputContainerWithFormControl);
fixture.detectChanges();

let input = fixture.debugElement.query(By.directive(MdInputDirective))
.injector.get(MdInputDirective) as MdInputDirective;

expect(input.value).toBeFalsy();

fixture.componentInstance.formControl.setValue('something');

expect(input.value).toBe('something');
});

});

@Component({
Expand Down Expand Up @@ -446,6 +462,13 @@ class MdTextareaWithBindings {
})
class MdInputContainerMissingMdInputTestController {}

@Component({
template: `<md-input-container><input md-input [formControl]="formControl"></md-input-container>`
})
class MdInputContainerWithFormControl {
formControl = new FormControl();
}

/**
* Gets a RegExp used to detect an angular wrapped error message.
* See https://github.com/angular/angular/issues/8348
Expand Down
10 changes: 4 additions & 6 deletions src/lib/input/input-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,14 @@ export class MdInputDirective implements AfterContentInit {
@Optional() public _ngControl: NgControl) {
// Force setter to be called in case id was not specified.
this.id = this.id;

if (this._ngControl && this._ngControl.valueChanges) {
this._ngControl.valueChanges.subscribe((value) => {
this.value = value;
});
}
}

ngAfterContentInit() {
this.value = this._elementRef.nativeElement.value;

if (this._ngControl && this._ngControl.valueChanges) {
this._ngControl.valueChanges.subscribe(value => this.value = value);
}
}

/** Focuses the input element. */
Expand Down

0 comments on commit 72bc742

Please sign in to comment.