From 57178d0d0bdf2ff0ffe7c5b9aa9cb6e607e1e45b Mon Sep 17 00:00:00 2001 From: Zak Henry Date: Wed, 17 Mar 2021 10:11:49 +1300 Subject: [PATCH] feat(Touched): Add ability for sub form components to mark sub control as touched with an observable --- projects/ngx-sub-form/src/lib/helpers.ts | 2 +- projects/ngx-sub-form/src/lib/ngx-sub-form.ts | 6 +++++- projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/projects/ngx-sub-form/src/lib/helpers.ts b/projects/ngx-sub-form/src/lib/helpers.ts index 55e7304b..752fb0cd 100644 --- a/projects/ngx-sub-form/src/lib/helpers.ts +++ b/projects/ngx-sub-form/src/lib/helpers.ts @@ -35,7 +35,7 @@ export const getControlValueAccessorBindings = ( ): FormBindings => { const writeValue$$: ReplaySubject> = new ReplaySubject(1); const registerOnChange$$: ReplaySubject<(formValue: ControlInterface | null) => void> = new ReplaySubject(1); - const registerOnTouched$$: ReplaySubject<(_: any) => void> = new ReplaySubject(1); + const registerOnTouched$$: ReplaySubject<() => void> = new ReplaySubject(1); const setDisabledState$$: ReplaySubject = new ReplaySubject(1); const controlValueAccessorPatch: Required = { diff --git a/projects/ngx-sub-form/src/lib/ngx-sub-form.ts b/projects/ngx-sub-form/src/lib/ngx-sub-form.ts index 8716f3ce..0686046e 100644 --- a/projects/ngx-sub-form/src/lib/ngx-sub-form.ts +++ b/projects/ngx-sub-form/src/lib/ngx-sub-form.ts @@ -2,7 +2,7 @@ import { ɵmarkDirty as markDirty } from '@angular/core'; import { FormControl } from '@angular/forms'; import isEqual from 'fast-deep-equal'; import { getObservableLifecycle } from 'ngx-observable-lifecycle'; -import { EMPTY, forkJoin, identity, merge, Observable, of, timer } from 'rxjs'; +import { combineLatest, EMPTY, forkJoin, identity, merge, Observable, of, timer } from 'rxjs'; import { delay, exhaustMap, @@ -232,6 +232,10 @@ export function createForm( formGroup.updateValueAndValidity({ emitEvent: false }); }), ), + bindTouched$: combineLatest([componentHooks.registerOnTouched$, options.touched$ ?? EMPTY]).pipe( + delay(0), + tap(([onTouched]) => onTouched()), + ), }; forkJoin(sideEffects) diff --git a/projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts b/projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts index b4559471..5f06f579 100644 --- a/projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts +++ b/projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts @@ -19,7 +19,7 @@ export interface ComponentHooks { export interface FormBindings { readonly writeValue$: Observable>; readonly registerOnChange$: Observable<(formValue: ControlInterface | null) => void>; - readonly registerOnTouched$: Observable<(_: any) => void>; + readonly registerOnTouched$: Observable<() => void>; readonly setDisabledState$: Observable; } @@ -75,6 +75,8 @@ export type NgxSubFormOptions; emitNullOnDestroy?: boolean; componentHooks?: ComponentHooks; + // emit on this observable to mark the control as touched + touched$?: Observable; } & NgxSubFormRemap & NgxSubFormArray;