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 authored and devversion committed Dec 29, 2016
1 parent dccbe41 commit ef4c6bf
Show file tree
Hide file tree
Showing 2 changed files with 33 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
13 changes: 7 additions & 6 deletions src/lib/input/input-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
Optional,
Output,
EventEmitter,
Renderer
Renderer,
OnInit,
} from '@angular/core';
import {coerceBooleanProperty} from '../core';
import {NgControl} from '@angular/forms';
Expand Down Expand Up @@ -77,7 +78,7 @@ export class MdHint {
'(input)': '_onInput()',
}
})
export class MdInputDirective implements AfterContentInit {
export class MdInputDirective implements AfterContentInit, OnInit {
/** Whether the element is disabled. */
@Input()
get disabled() { return this._disabled; }
Expand Down Expand Up @@ -142,14 +143,14 @@ export class MdInputDirective implements AfterContentInit {

constructor(private _elementRef: ElementRef,
private _renderer: Renderer,
@Optional() public _ngControl: NgControl) {
@Optional() public _ngControl: NgControl) { }

ngOnInit() {
// 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;
});
this._ngControl.valueChanges.subscribe(value => this.value = value);
}
}

Expand Down

0 comments on commit ef4c6bf

Please sign in to comment.