useFormUntouched
Returns whether a FormGroup is untouched (has not been interacted with) as a signal. The signal updates reactively whenever the form's untouched state changes.
Usage
typescript
import { useFormUntouched } from 'ng-reactive-utils';
@Component({
template: `
<form [formGroup]="form">
<input formControlName="email" />
@if (isUntouched()) {
<p class="hint">Click on the field to start</p>
}
</form>
`,
})
class GuidedFormComponent {
form = new FormGroup({
email: new FormControl(''),
});
isUntouched = useFormUntouched(this.form);
}Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
form | FormGroup | required | The FormGroup to check untouched state for |
Returns
Signal<boolean> - A readonly signal containing the untouched state (true if not interacted with)
Notes
- Merges
TouchedChangeEventevents from theFormGroupand all direct child controls — this is necessary because Angular does not propagateTouchedChangeEventfrom child controls up to the parent group - Uses
control.events(notstatusChanges) to listen for touched-state changes;statusChangesdoes not emit on touch changes - Returns the group-level
form.untouchedvalue on each event, so the signal reflects the overall untouched state of the form - Only tracks direct child controls — grandchild controls inside nested
FormGrouporFormArraychildren are not observed; useuseControlUntouchedon the nested group directly if needed - Returns
falseonce any direct child control loses focus ormarkAsTouched()is called - Opposite of
useFormTouched
Source
ts
import { Signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormGroup, TouchedChangeEvent } from '@angular/forms';
import { filter, map, merge } from 'rxjs';
/**
* Returns whether a FormGroup is untouched (has not been interacted with) as a signal.
* The signal updates reactively whenever any control in the form is touched or
* untouched, including programmatic calls to markAsTouched() / markAsUntouched().
*
* @param form - The FormGroup to check untouched state for
* @returns A signal containing the untouched state (true if not interacted with)
*
* @example
* ```typescript
* @Component({
* template: `
* <form [formGroup]="form">
* <input formControlName="email" />
* @if (isUntouched()) {
* <span>Please fill out the form</span>
* }
* </form>
* `
* })
* class MyComponent {
* form = new FormGroup({
* email: new FormControl('')
* });
* isUntouched = useFormUntouched(this.form);
* }
* ```
*/
export const useFormUntouched = (form: FormGroup): Signal<boolean> => {
// TouchedChangeEvent on a FormGroup only fires when markAsTouched() / markAsUntouched()
// is called on the form itself — it does not propagate from child control blur events.
// Merging events from all child controls ensures the signal stays accurate when a user
// interacts with any field in the form.
const allControls = [form, ...Object.values(form.controls)];
const anyTouchedChange$ = merge(...allControls.map((control) => control.events)).pipe(
filter((event): event is TouchedChangeEvent => event instanceof TouchedChangeEvent),
map(() => form.untouched),
);
return toSignal(anyTouchedChange$, { initialValue: form.untouched }) as Signal<boolean>;
};