Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(form-debug): track touched/untouched/pristine/dirty events #18

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions libs/reactive-forms-utils/src/lib/form-debug.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { AbstractControl, Form, FormControlStatus, FormGroup, ValidationErrors } from '@angular/forms';
import { combineLatest, EMPTY, map, Observable, of, startWith } from 'rxjs';
import {
AbstractControl,
ControlEvent,
FormControlStatus,
FormGroup,
PristineChangeEvent,
TouchedChangeEvent,
ValidationErrors,
} from '@angular/forms';
import { combineLatest, EMPTY, filter, map, Observable, of, startWith } from 'rxjs';

function touchedEvents$<T>(form: AbstractControl<T>) {
return form.events.pipe(
filter((event: ControlEvent): event is TouchedChangeEvent => event instanceof TouchedChangeEvent),
);
}

function pristineEvents$<T>(form: AbstractControl<T>) {
return form.events.pipe(
filter((event: ControlEvent): event is PristineChangeEvent => event instanceof PristineChangeEvent),
);
}

export enum FormDebugFieldEnum {
Value = 'Value',
Expand All @@ -8,14 +28,22 @@ export enum FormDebugFieldEnum {
Status = 'Status',
Valid = 'Valid',
Invalid = 'Invalid',
Touched = 'Touched',
Untouched = 'Untouched',
Dirty = 'Dirty',
Pristine = 'Pristine',
}
export type FormDebugField =
| FormDebugFieldEnum.Value
| FormDebugFieldEnum.FormErrors
| FormDebugFieldEnum.ControlErrors
| FormDebugFieldEnum.Status
| FormDebugFieldEnum.Valid
| FormDebugFieldEnum.Invalid;
| FormDebugFieldEnum.Invalid
| FormDebugFieldEnum.Touched
| FormDebugFieldEnum.Untouched
| FormDebugFieldEnum.Dirty
| FormDebugFieldEnum.Pristine;

export interface ControlErrorStatusDisplay {
errors: ValidationErrors | null;
Expand All @@ -28,6 +56,10 @@ export interface FormDebugValue {
status?: FormControlStatus;
valid?: boolean;
invalid?: boolean;
touched?: boolean;
untouched?: boolean;
dirty?: boolean;
pristine?: boolean;
}

export const DEFAULT_DEBUG_FIELDS: FormDebugField[] = Object.keys(FormDebugFieldEnum).map(
Expand All @@ -45,6 +77,8 @@ export function debugForm(
return combineLatest([
form.valueChanges.pipe(startWith(form.value)),
form.statusChanges.pipe(startWith(form.status)),
touchedEvents$(form).pipe(startWith(form.touched)),
pristineEvents$(form).pipe(startWith(form.pristine)),
]).pipe(
map(() => {
const returnObject: FormDebugValue = {};
Expand Down Expand Up @@ -83,6 +117,22 @@ export function debugForm(
returnObject.valid = form.valid;
}

if (debugFields.includes(FormDebugFieldEnum.Touched)) {
returnObject.touched = form.touched;
}

if (debugFields.includes(FormDebugFieldEnum.Untouched)) {
returnObject.untouched = form.untouched;
}

if (debugFields.includes(FormDebugFieldEnum.Dirty)) {
returnObject.dirty = form.dirty;
}

if (debugFields.includes(FormDebugFieldEnum.Pristine)) {
returnObject.pristine = form.pristine;
}

return returnObject;
}),
);
Expand Down