Skip to content

Commit de5fca9

Browse files
mmalerbathePunderWoman
authored andcommitted
fix(forms): run reset as untracked
Run the signal forms `reset()` as untracked so it does not trigger `effect` to rerun when the model changes Fixes #65322 (cherry picked from commit 7ddf4a6)
1 parent 47fc721 commit de5fca9

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

packages/forms/signals/src/field/node.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {computed, linkedSignal, type Signal, type WritableSignal} from '@angular/core';
9+
import {computed, linkedSignal, type Signal, untracked, type WritableSignal} from '@angular/core';
1010
import type {Field} from '../api/field_directive';
1111
import {
1212
AggregateMetadataKey,
@@ -225,11 +225,15 @@ export class FieldNode implements FieldState<unknown> {
225225
* Note this does not change the data model, which can be reset directly if desired.
226226
*/
227227
reset(): void {
228+
untracked(() => this._reset());
229+
}
230+
231+
private _reset() {
228232
this.nodeState.markAsUntouched();
229233
this.nodeState.markAsPristine();
230234

231235
for (const child of this.structure.children()) {
232-
child.reset();
236+
child._reset();
233237
}
234238
}
235239

packages/forms/signals/test/node/field_node.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {computed, Injector, signal} from '@angular/core';
9+
import {computed, effect, Injector, signal} from '@angular/core';
1010
import {TestBed} from '@angular/core/testing';
1111
import {
1212
apply,
@@ -389,6 +389,26 @@ describe('FieldNode', () => {
389389
expect(f().touched()).toBe(false);
390390
});
391391

392+
it('reset should not track model changes', () => {
393+
const f = form(signal(''), {injector: TestBed.inject(Injector)});
394+
const spy = jasmine.createSpy();
395+
effect(
396+
() => {
397+
spy();
398+
f().reset();
399+
},
400+
{injector: TestBed.inject(Injector)},
401+
);
402+
403+
TestBed.tick();
404+
expect(spy).toHaveBeenCalledTimes(1);
405+
406+
f().value.set('hi');
407+
408+
TestBed.tick();
409+
expect(spy).toHaveBeenCalledTimes(1);
410+
});
411+
392412
it('should not be marked as touched when is readonly', () => {
393413
const f = form(
394414
signal({

0 commit comments

Comments
 (0)