Skip to content

Commit f0c4148

Browse files
mmalerbajelbourn
authored andcommitted
fix(input): copy input state classes to md-input-container (#2191)
1 parent 78d54fc commit f0c4148

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ <h4>Textarea</h4>
249249
</md-card-content>
250250
</md-card>
251251

252+
<md-card class="demo-card demo-basic">
253+
<md-toolbar color="primary">Forms</md-toolbar>
254+
<md-card-content>
255+
<md-input-container><input md-input placeholder="reactive" [formControl]="formControl"></md-input-container>
256+
<md-input-container><input md-input placeholder="template" [(ngModel)]="model" required></md-input-container>
257+
</md-card-content>
258+
</md-card>
252259

253260
<md-card class="demo-card demo-basic">
254261
<md-toolbar color="primary">Textarea Autosize</md-toolbar>

src/demo-app/input/input-container-demo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Component} from '@angular/core';
2+
import {FormControl, Validators} from '@angular/forms';
23

34

45
let max = 5;
@@ -22,6 +23,8 @@ export class InputContainerDemo {
2223
{ value: 50 },
2324
];
2425
rows = 8;
26+
formControl = new FormControl('hello', Validators.required);
27+
model = 'hello';
2528

2629
addABunch(n: number) {
2730
for (let x = 0; x < n; x++) {

src/lib/input/input-container.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ export class MdInputDirective implements AfterContentInit {
135135

136136
constructor(private _elementRef: ElementRef,
137137
private _renderer: Renderer,
138-
@Optional() private _ngControl: NgControl) {
138+
@Optional() public _ngControl: NgControl) {
139139
// Force setter to be called in case id was not specified.
140140
this.id = this.id;
141141

142-
if (this._ngControl) {
142+
if (this._ngControl && this._ngControl.valueChanges) {
143143
this._ngControl.valueChanges.subscribe((value) => {
144144
this.value = value;
145145
});
@@ -182,6 +182,13 @@ export class MdInputDirective implements AfterContentInit {
182182
host: {
183183
// Remove align attribute to prevent it from interfering with layout.
184184
'[attr.align]': 'null',
185+
'[class.ng-untouched]': '_isUntouched()',
186+
'[class.ng-touched]': '_isTouched()',
187+
'[class.ng-pristine]': '_isPristine()',
188+
'[class.ng-dirty]': '_isDirty()',
189+
'[class.ng-valid]': '_isValid()',
190+
'[class.ng-invalid]': '_isInvalid()',
191+
'[class.ng-pending]': '_isPending()',
185192
'(click)': '_focusInput()',
186193
},
187194
encapsulation: ViewEncapsulation.None,
@@ -223,13 +230,27 @@ export class MdInputContainer implements AfterContentInit {
223230
});
224231
}
225232

233+
_isUntouched() { return this._hasNgControl() && this._mdInputChild._ngControl.untouched; }
234+
235+
_isTouched() { return this._hasNgControl() && this._mdInputChild._ngControl.touched; }
236+
237+
_isPristine() { return this._hasNgControl() && this._mdInputChild._ngControl.pristine; }
238+
239+
_isDirty() { return this._hasNgControl() && this._mdInputChild._ngControl.dirty; }
240+
241+
_isValid() { return this._hasNgControl() && this._mdInputChild._ngControl.valid; }
242+
243+
_isInvalid() { return this._hasNgControl() && this._mdInputChild._ngControl.invalid; }
244+
245+
_isPending() { return this._hasNgControl() && this._mdInputChild._ngControl.pending; }
246+
226247
/** Whether the input has a placeholder. */
227-
_hasPlaceholder(): boolean {
228-
return !!this._mdInputChild.placeholder || !!this._placeholderChild;
229-
}
248+
_hasPlaceholder() { return !!(this._mdInputChild.placeholder || this._placeholderChild); }
230249

231250
_focusInput() { this._mdInputChild.focus(); }
232251

252+
private _hasNgControl() { return !!(this._mdInputChild && this._mdInputChild._ngControl); }
253+
233254
/**
234255
* Ensure that there is only one placeholder (either `input` attribute or child element with the
235256
* `md-placeholder` attribute.

0 commit comments

Comments
 (0)