Skip to content

Commit

Permalink
Merge pull request #206 from zakhenry/feat/optional-on-touched
Browse files Browse the repository at this point in the history
feat(Touched): Add ability for sub form components to mark sub control as touched with an observable
  • Loading branch information
zak-cloudnc authored Mar 17, 2021
2 parents b04afc5 + 57178d0 commit 543357e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion projects/ngx-sub-form/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const getControlValueAccessorBindings = <ControlInterface>(
): FormBindings<ControlInterface> => {
const writeValue$$: ReplaySubject<Nilable<ControlInterface>> = 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<boolean> = new ReplaySubject(1);

const controlValueAccessorPatch: Required<ControlValueAccessor> = {
Expand Down
6 changes: 5 additions & 1 deletion projects/ngx-sub-form/src/lib/ngx-sub-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -229,6 +229,10 @@ export function createForm<ControlInterface, FormInterface>(
formGroup.updateValueAndValidity({ emitEvent: false });
}),
),
bindTouched$: combineLatest([componentHooks.registerOnTouched$, options.touched$ ?? EMPTY]).pipe(
delay(0),
tap(([onTouched]) => onTouched()),
),
};

forkJoin(sideEffects)
Expand Down
4 changes: 3 additions & 1 deletion projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ComponentHooks {
export interface FormBindings<ControlInterface> {
readonly writeValue$: Observable<Nilable<ControlInterface>>;
readonly registerOnChange$: Observable<(formValue: ControlInterface | null) => void>;
readonly registerOnTouched$: Observable<(_: any) => void>;
readonly registerOnTouched$: Observable<() => void>;
readonly setDisabledState$: Observable<boolean>;
}

Expand Down Expand Up @@ -75,6 +75,8 @@ export type NgxSubFormOptions<ControlInterface, FormInterface = ControlInterface
formGroupOptions?: FormGroupOptions<FormInterface>;
emitNullOnDestroy?: boolean;
componentHooks?: ComponentHooks;
// emit on this observable to mark the control as touched
touched$?: Observable<void>;
} & NgxSubFormRemap<ControlInterface, FormInterface> &
NgxSubFormArray<FormInterface>;

Expand Down

0 comments on commit 543357e

Please sign in to comment.