Skip to content

Commit bdfbd54

Browse files
JeanMechethePunderWoman
authored andcommitted
feat(forms): Allow to reset a form without emitting events (#60354)
This change add an option paramter to `resetForm` that is passed to the FormGroup. fixes #60274 PR Close #60354
1 parent e4be32d commit bdfbd54

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

goldens/public-api/forms/index.api.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,10 @@ export class FormGroupDirective extends ControlContainer implements Form, OnChan
502502
removeControl(dir: FormControlName): void;
503503
removeFormArray(dir: FormArrayName): void;
504504
removeFormGroup(dir: FormGroupName): void;
505-
resetForm(value?: any): void;
505+
resetForm(value?: any, options?: {
506+
onlySelf?: boolean;
507+
emitEvent?: boolean;
508+
}): void;
506509
get submitted(): boolean;
507510
updateModel(dir: FormControlName, value: any): void;
508511
// (undocumented)

packages/forms/src/directives/reactive_directives/form_group_directive.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,14 @@ export class FormGroupDirective extends ControlContainer implements Form, OnChan
339339
* @description
340340
* Resets the form to an initial value and resets its submitted status.
341341
*
342-
* @param value The new value for the form.
342+
* @param value The new value for the form, `undefined` by default
343343
*/
344-
resetForm(value: any = undefined): void {
345-
this.form.reset(value);
344+
resetForm(value: any = undefined, options: {onlySelf?: boolean; emitEvent?: boolean} = {}): void {
345+
this.form.reset(value, options);
346346
this._submittedReactive.set(false);
347-
this.form._events.next(new FormResetEvent(this.form));
347+
if (options?.emitEvent !== false) {
348+
this.form._events.next(new FormResetEvent(this.form));
349+
}
348350
}
349351

350352
/** @internal */

packages/forms/test/reactive_integration_spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,59 @@ describe('reactive forms integration tests', () => {
14221422
expect(events[3].source).toBe(form);
14231423
});
14241424

1425+
it('formControl should not emit an event when resetting a form with emit:false', () => {
1426+
const fixture = initTest(FormGroupComp);
1427+
const form = new FormGroup({'login': new FormControl('', Validators.required)});
1428+
fixture.componentInstance.form = form;
1429+
fixture.detectChanges();
1430+
1431+
const formGroupDir = fixture.debugElement.children[0].injector.get(FormGroupDirective);
1432+
1433+
const events: ControlEvent[] = [];
1434+
fixture.componentInstance.form.events.subscribe((event) => events.push(event));
1435+
1436+
formGroupDir.resetForm(undefined, {emitEvent: false});
1437+
1438+
expect(events.length).toBe(1);
1439+
expect(events[0]).toBeInstanceOf(TouchedChangeEvent);
1440+
});
1441+
1442+
it('formControl should only update self', () => {
1443+
const fixture = initTest(FormGroupComp);
1444+
const form = new FormGroup({'login': new FormControl('', Validators.required)});
1445+
const parentForm = new FormGroup({form});
1446+
fixture.componentInstance.form = form;
1447+
fixture.detectChanges();
1448+
1449+
const formGroupDir = fixture.debugElement.children[0].injector.get(FormGroupDirective);
1450+
1451+
const events: ControlEvent[] = [];
1452+
fixture.componentInstance.form.events.subscribe((event) => events.push(event));
1453+
const parentEvents: ControlEvent[] = [];
1454+
parentForm.events.subscribe((event) => parentEvents.push(event));
1455+
1456+
formGroupDir.resetForm(undefined, {onlySelf: true});
1457+
1458+
expect(events.length).toBe(4);
1459+
expect(events[0]).toBeInstanceOf(TouchedChangeEvent);
1460+
expect(events[1]).toBeInstanceOf(ValueChangeEvent);
1461+
expect(events[2]).toBeInstanceOf(StatusChangeEvent);
1462+
1463+
// The event that matters
1464+
expect(events[3]).toBeInstanceOf(FormResetEvent);
1465+
expect(events[3].source).toBe(form);
1466+
1467+
// Self:false = parent won't receive the events
1468+
expect(parentEvents.length).toBe(0);
1469+
1470+
// Not only self
1471+
formGroupDir.resetForm({login: 'new value'});
1472+
expect(parentEvents.length).toBe(3);
1473+
expect(parentEvents[0]).toBeInstanceOf(TouchedChangeEvent);
1474+
expect(parentEvents[1]).toBeInstanceOf(ValueChangeEvent);
1475+
expect(parentEvents[2]).toBeInstanceOf(StatusChangeEvent);
1476+
});
1477+
14251478
it('formControl should emit an event when submitting a form', () => {
14261479
const fixture = initTest(FormGroupComp);
14271480
const form = new FormGroup({'login': new FormControl('', Validators.required)});

0 commit comments

Comments
 (0)