import { Component, ElementRef, EventEmitter, Input, OnInit, Output } from '@angular/core';
-import { FormGroup } from '@angular/forms';
+import { AbstractControl, FormGroup, FormGroupDirective, NgForm } from '@angular/forms';
import * as _ from 'lodash';
styleUrls: ['./submit-button.component.scss']
})
export class SubmitButtonComponent implements OnInit {
- @Input() form: FormGroup;
+ @Input() form: FormGroup | NgForm;
@Input() type = 'submit';
@Output() submitAction = new EventEmitter();
if (_.has(this.form.errors, 'cdSubmitButton')) {
this.loading = false;
_.unset(this.form.errors, 'cdSubmitButton');
- this.form.updateValueAndValidity();
+ // Handle Reactive forms.
+ if (this.form instanceof AbstractControl) {
+ (<AbstractControl>this.form).updateValueAndValidity();
+ }
}
});
}
- submit() {
+ submit($event) {
this.focusButton();
+ // Special handling for Template driven forms.
+ if (this.form instanceof FormGroupDirective) {
+ (<FormGroupDirective>this.form).onSubmit($event);
+ }
+
if (this.form.invalid) {
this.focusInvalid();
return;