FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(forms): flush debounced descendant values when a field is touched by MeAkib · Pull Request #71150 · angular/angular · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 4 additions & 4 deletions packages/forms/signals/src/field/node.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,18 @@ export class FieldNode implements FieldState<unknown> {
}
untracked(() => {
this.markAsTouchedInternal(options);
this.flushSync();
});
}

markAsTouchedInternal(options?: MarkAsTouchedOptions): void {
if (this.structure.isOrphaned()) {
return;
}
if (this.validationState.shouldSkipValidation()) {
return;
// Flush even when validation is skipped, so a pending value still reaches the model.
this.flushSync();
if (!this.validationState.shouldSkipValidation()) {
this.nodeState.markAsTouched();
}
this.nodeState.markAsTouched();
if (options?.skipDescendants) {
return;
}
Expand Down
73 changes: 72 additions & 1 deletion packages/forms/signals/test/node/api/debounce.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import {Injector, signal} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {applyWhenValue, debounce, form} from '@angular/forms/signals';
import {applyWhenValue, debounce, disabled, form, hidden, submit} from '@angular/forms/signals';
import {timeout} from '@angular/private/testing';

describe('debounce', () => {
Expand Down Expand Up @@ -310,6 +310,77 @@ describe('debounce', () => {
street.markAsTouched();
expect(street.value()).toBe('2000 N Shoreline Blvd');
});
it('should synchronize descendant values when an ancestor is touched', () => {
const address = signal({street: ''});
const addressForm = form(
address,
(address) => {
debounce(address.street, 'blur');
},
options(),
);
const street = addressForm.street();

street.controlValue.set('1600 Amphitheatre Pkwy');
addressForm().markAsTouched();
expect(street.value()).toBe('1600 Amphitheatre Pkwy');
});

it('should synchronize the value of a disabled field when it is touched', () => {
const locked = signal(false);
const address = signal({street: ''});
const addressForm = form(
address,
(address) => {
debounce(address.street, 'blur');
disabled(address.street, {when: () => locked()});
},
options(),
);
const street = addressForm.street();

street.controlValue.set('1600 Amphitheatre Pkwy');
locked.set(true);
street.markAsTouched();
expect(street.value()).toBe('1600 Amphitheatre Pkwy');
});

it('should synchronize values below a hidden field when an ancestor is touched', () => {
const showAddress = signal(true);
const user = signal({address: {street: ''}});
const userForm = form(
user,
(user) => {
debounce(user.address.street, 'blur');
hidden(user.address, {when: () => !showAddress()});
},
options(),
);
const street = userForm.address.street();

street.controlValue.set('1600 Amphitheatre Pkwy');
showAddress.set(false);
userForm().markAsTouched();
expect(street.value()).toBe('1600 Amphitheatre Pkwy');
});

it('should synchronize descendant values before running the submit action', async () => {
const address = signal({street: ''});
const addressForm = form(
address,
(address) => {
debounce(address.street, 'blur');
},
options(),
);
let submittedStreet: string | undefined;

addressForm.street().controlValue.set('1600 Amphitheatre Pkwy');
await submit(addressForm, async (form) => {
submittedStreet = form.street().value();
});
expect(submittedStreet).toBe('1600 Amphitheatre Pkwy');
});
});

describe('inheritance', () => {
Expand Down
Loading

Back | FazBrowse Home | New Git URL