Skip to content

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

ParameterTypeDefaultDescription
formFormGrouprequiredThe FormGroup to check untouched state for

Returns

Signal<boolean> - A readonly signal containing the untouched state (true if not interacted with)

Notes

  • Merges TouchedChangeEvent events from the FormGroup and all direct child controls — this is necessary because Angular does not propagate TouchedChangeEvent from child controls up to the parent group
  • Uses control.events (not statusChanges) to listen for touched-state changes; statusChanges does not emit on touch changes
  • Returns the group-level form.untouched value on each event, so the signal reflects the overall untouched state of the form
  • Only tracks direct child controls — grandchild controls inside nested FormGroup or FormArray children are not observed; use useControlUntouched on the nested group directly if needed
  • Returns false once any direct child control loses focus or markAsTouched() 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>;
};

Released under the MIT License.