Skip to content

Commit 8c0eef2

Browse files
crisbetokara
authored andcommitted
fix(input-container): reduce redundancy when forwarding the NgControl classes (#2442)
1 parent 715f4a9 commit 8c0eef2

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

src/lib/input/input-container.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ export class MdInputDirective {
214214
host: {
215215
// Remove align attribute to prevent it from interfering with layout.
216216
'[attr.align]': 'null',
217-
'[class.ng-untouched]': '_isUntouched()',
218-
'[class.ng-touched]': '_isTouched()',
219-
'[class.ng-pristine]': '_isPristine()',
220-
'[class.ng-dirty]': '_isDirty()',
221-
'[class.ng-valid]': '_isValid()',
222-
'[class.ng-invalid]': '_isInvalid()',
223-
'[class.ng-pending]': '_isPending()',
217+
'[class.ng-untouched]': '_shouldForward("untouched")',
218+
'[class.ng-touched]': '_shouldForward("touched")',
219+
'[class.ng-pristine]': '_shouldForward("pristine")',
220+
'[class.ng-dirty]': '_shouldForward("dirty")',
221+
'[class.ng-valid]': '_shouldForward("valid")',
222+
'[class.ng-invalid]': '_shouldForward("invalid")',
223+
'[class.ng-pending]': '_shouldForward("pending")',
224224
'(click)': '_focusInput()',
225225
},
226226
encapsulation: ViewEncapsulation.None,
@@ -262,35 +262,21 @@ export class MdInputContainer implements AfterContentInit {
262262
this._validatePlaceholders();
263263

264264
// Re-validate when things change.
265-
this._hintChildren.changes.subscribe(() => {
266-
this._validateHints();
267-
});
268-
this._mdInputChild._placeholderChange.subscribe(() => {
269-
this._validatePlaceholders();
270-
});
265+
this._hintChildren.changes.subscribe(() => this._validateHints());
266+
this._mdInputChild._placeholderChange.subscribe(() => this._validatePlaceholders());
271267
}
272268

273-
_isUntouched() { return this._hasNgControl() && this._mdInputChild._ngControl.untouched; }
274-
275-
_isTouched() { return this._hasNgControl() && this._mdInputChild._ngControl.touched; }
276-
277-
_isPristine() { return this._hasNgControl() && this._mdInputChild._ngControl.pristine; }
278-
279-
_isDirty() { return this._hasNgControl() && this._mdInputChild._ngControl.dirty; }
280-
281-
_isValid() { return this._hasNgControl() && this._mdInputChild._ngControl.valid; }
282-
283-
_isInvalid() { return this._hasNgControl() && this._mdInputChild._ngControl.invalid; }
284-
285-
_isPending() { return this._hasNgControl() && this._mdInputChild._ngControl.pending; }
269+
/** Determines whether a class from the NgControl should be forwarded to the host element. */
270+
_shouldForward(prop: string): boolean {
271+
let control = this._mdInputChild ? this._mdInputChild._ngControl : null;
272+
return control && (control as any)[prop];
273+
}
286274

287275
/** Whether the input has a placeholder. */
288276
_hasPlaceholder() { return !!(this._mdInputChild.placeholder || this._placeholderChild); }
289277

290278
_focusInput() { this._mdInputChild.focus(); }
291279

292-
private _hasNgControl() { return !!(this._mdInputChild && this._mdInputChild._ngControl); }
293-
294280
/**
295281
* Ensure that there is only one placeholder (either `input` attribute or child element with the
296282
* `md-placeholder` attribute.

0 commit comments

Comments
 (0)