useFormPristine
Returns whether a FormGroup is pristine (has not been modified) as a signal. The signal updates reactively whenever the form's pristine state changes.
Usage
typescript
import { useFormPristine } from 'ng-reactive-utils';
@Component({
template: `
<form [formGroup]="form">
<input formControlName="name" />
@if (isPristine()) {
<p class="hint">Start typing to make changes</p>
} @else {
<p class="info">Form has been modified</p>
}
</form>
`,
})
class SimpleFormComponent {
form = new FormGroup({
name: new FormControl(''),
});
isPristine = useFormPristine(this.form);
}Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
form | FormGroup | required | The FormGroup to check pristine state for |
Returns
Signal<boolean> - A readonly signal containing the pristine state (true if not modified)
Notes
- Uses
toSignalwithform.valueChangesobservable - Returns
truewhen no control has been modified - Returns
falsewhen any control value has been changed - Opposite of
useFormDirty
Source
ts
import { Signal } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { FormGroup } from '@angular/forms';
import { map } from 'rxjs';
/**
* Returns whether a FormGroup is pristine (has not been modified) as a signal.
* The signal updates reactively whenever the form's pristine state changes.
*
* @param form - The FormGroup to check pristine state for
* @returns A signal containing the pristine state (true if not modified)
*
* @example
* ```typescript
* @Component({
* template: `
* <form [formGroup]="form">
* <input formControlName="name" />
* @if (isPristine()) {
* <span>Form has not been modified</span>
* }
* </form>
* `
* })
* class MyComponent {
* form = new FormGroup({
* name: new FormControl('')
* });
* isPristine = useFormPristine(this.form);
* }
* ```
*/
export const useFormPristine = (form: FormGroup): Signal<boolean> => {
return toSignal(form.valueChanges.pipe(map(() => form.pristine)), {
initialValue: form.pristine,
}) as Signal<boolean>;
};