feat(forms): add NgForm method that resets submit state (#10715)
This commit is contained in:
parent
00e5b7d30a
commit
97f35714f7
|
@ -174,7 +174,12 @@ export class NgForm extends ControlContainer implements Form {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
onReset(): void { this.form.reset(); }
|
onReset(): void { this.resetForm(); }
|
||||||
|
|
||||||
|
resetForm(value: any = undefined): void {
|
||||||
|
this.form.reset(value);
|
||||||
|
this._submitted = false;
|
||||||
|
}
|
||||||
|
|
||||||
/** @internal */
|
/** @internal */
|
||||||
_findContainer(path: string[]): FormGroup {
|
_findContainer(path: string[]): FormGroup {
|
||||||
|
|
|
@ -181,7 +181,12 @@ export class FormGroupDirective extends ControlContainer implements Form,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
onReset(): void { this.form.reset(); }
|
onReset(): void { this.resetForm(); }
|
||||||
|
|
||||||
|
resetForm(value: any = undefined): void {
|
||||||
|
this.form.reset(value);
|
||||||
|
this._submitted = false;
|
||||||
|
}
|
||||||
|
|
||||||
/** @internal */
|
/** @internal */
|
||||||
_updateDomValue() {
|
_updateDomValue() {
|
||||||
|
|
|
@ -150,6 +150,33 @@ export function main() {
|
||||||
expect(form.value.name).toEqual(null);
|
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',
|
it('should emit valueChanges and statusChanges on init',
|
||||||
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||||
|
|
|
@ -294,6 +294,7 @@ export declare class FormGroupDirective extends ControlContainer implements Form
|
||||||
removeControl(dir: NgControl): void;
|
removeControl(dir: NgControl): void;
|
||||||
removeFormArray(dir: FormArrayName): void;
|
removeFormArray(dir: FormArrayName): void;
|
||||||
removeFormGroup(dir: FormGroupName): void;
|
removeFormGroup(dir: FormGroupName): void;
|
||||||
|
resetForm(value?: any): void;
|
||||||
updateModel(dir: NgControl, value: any): void;
|
updateModel(dir: NgControl, value: any): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -371,6 +372,7 @@ export declare class NgForm extends ControlContainer implements Form {
|
||||||
onSubmit(): boolean;
|
onSubmit(): boolean;
|
||||||
removeControl(dir: NgModel): void;
|
removeControl(dir: NgModel): void;
|
||||||
removeFormGroup(dir: NgModelGroup): void;
|
removeFormGroup(dir: NgModelGroup): void;
|
||||||
|
resetForm(value?: any): void;
|
||||||
setValue(value: {
|
setValue(value: {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}): void;
|
}): void;
|
||||||
|
|
Loading…
Reference in New Issue