feat(forms): add NgForm method that resets submit state (#10715)

This commit is contained in:
Kara 2016-08-11 23:27:33 -07:00 committed by vikerman
parent 00e5b7d30a
commit 97f35714f7
4 changed files with 41 additions and 2 deletions

View File

@ -174,7 +174,12 @@ export class NgForm extends ControlContainer implements Form {
return false;
}
onReset(): void { this.form.reset(); }
onReset(): void { this.resetForm(); }
resetForm(value: any = undefined): void {
this.form.reset(value);
this._submitted = false;
}
/** @internal */
_findContainer(path: string[]): FormGroup {

View File

@ -181,7 +181,12 @@ export class FormGroupDirective extends ControlContainer implements Form,
return false;
}
onReset(): void { this.form.reset(); }
onReset(): void { this.resetForm(); }
resetForm(value: any = undefined): void {
this.form.reset(value);
this._submitted = false;
}
/** @internal */
_updateDomValue() {

View File

@ -150,6 +150,33 @@ export function main() {
expect(form.value.name).toEqual(null);
})));
it('should reset the form submit state when reset button is clicked',
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
const t = `
<form>
<input name="name" [(ngModel)]="name">
</form>
`;
const fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8);
tick();
fixture.debugElement.componentInstance.name = '';
fixture.detectChanges();
tick();
const form = fixture.debugElement.children[0].injector.get(NgForm);
const formEl = fixture.debugElement.query(By.css('form'));
dispatchEvent(formEl.nativeElement, 'submit');
fixture.detectChanges();
tick();
dispatchEvent(formEl.nativeElement, 'reset');
fixture.detectChanges();
tick();
expect(form.submitted).toEqual(false);
})));
it('should emit valueChanges and statusChanges on init',
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {

View File

@ -294,6 +294,7 @@ export declare class FormGroupDirective extends ControlContainer implements Form
removeControl(dir: NgControl): void;
removeFormArray(dir: FormArrayName): void;
removeFormGroup(dir: FormGroupName): void;
resetForm(value?: any): void;
updateModel(dir: NgControl, value: any): void;
}
@ -371,6 +372,7 @@ export declare class NgForm extends ControlContainer implements Form {
onSubmit(): boolean;
removeControl(dir: NgModel): void;
removeFormGroup(dir: NgModelGroup): void;
resetForm(value?: any): void;
setValue(value: {
[key: string]: any;
}): void;