Skip to content

Commit ada8453

Browse files
committed
feat(ControlValue): Add exported controlValue$ so components can easily observer the current value of the component
Closes #86
1 parent 1a9e62d commit ada8453

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

projects/ngx-sub-form/src/lib/ngx-sub-form.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FormControl } from '@angular/forms';
22
import isEqual from 'fast-deep-equal';
33
import { getObservableLifecycle } from 'ngx-observable-lifecycle';
4-
import { EMPTY, forkJoin, Observable, of, timer } from 'rxjs';
4+
import { EMPTY, forkJoin, merge, Observable, of, timer } from 'rxjs';
55
import {
66
delay,
77
filter,
@@ -59,15 +59,15 @@ const isRoot = <ControlInterface, FormInterface>(
5959
export function createForm<ControlInterface, FormInterface = ControlInterface>(
6060
componentInstance: ControlValueAccessorComponentInstance,
6161
options: NgxRootFormOptions<ControlInterface, FormInterface>,
62-
): NgxRootForm<FormInterface>;
62+
): NgxRootForm<ControlInterface, FormInterface>;
6363
export function createForm<ControlInterface, FormInterface = ControlInterface>(
6464
componentInstance: ControlValueAccessorComponentInstance,
6565
options: NgxSubFormOptions<ControlInterface, FormInterface>,
66-
): NgxSubForm<FormInterface>;
66+
): NgxSubForm<ControlInterface, FormInterface>;
6767
export function createForm<ControlInterface, FormInterface>(
6868
componentInstance: ControlValueAccessorComponentInstance,
6969
options: NgxFormOptions<ControlInterface, FormInterface>,
70-
): NgxSubForm<FormInterface> {
70+
): NgxSubForm<ControlInterface, FormInterface> {
7171
const { formGroup, defaultValues, formControlNames, formArrays } = createFormDataFromOptions<
7272
ControlInterface,
7373
FormInterface
@@ -184,6 +184,14 @@ export function createForm<ControlInterface, FormInterface>(
184184
),
185185
);
186186

187+
// components often need to know what the current value of the FormControl that it is representing is, usually for
188+
// display purposes in the template. This value is the composition of the value written from the parent, and the
189+
// transformed current value that was most recently written to the parent
190+
const controlValue$: NgxSubForm<ControlInterface, FormInterface>['controlValue$'] = merge(
191+
writeValue$,
192+
broadcastValueToParent$,
193+
).pipe(shareReplay({ bufferSize: 1, refCount: true }));
194+
187195
const emitNullOnDestroy$: Observable<null> =
188196
// emit null when destroyed by default
189197
isNullOrUndefined(options.emitNullOnDestroy) || options.emitNullOnDestroy
@@ -250,5 +258,6 @@ export function createForm<ControlInterface, FormInterface>(
250258
return getFormGroupErrors<ControlInterface, FormInterface>(formGroup);
251259
},
252260
createFormArrayControl,
261+
controlValue$,
253262
};
254263
}

projects/ngx-sub-form/src/lib/ngx-sub-form.types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@ export type ControlValueAccessorComponentInstance = Object &
3030
// and this should *never* be overridden by the component
3131
Partial<Record<keyof ControlValueAccessor, never> & Record<keyof Validator, never>>;
3232

33-
export interface NgxSubForm<FormInterface> {
33+
export interface NgxSubForm<ControlInterface, FormInterface> {
3434
readonly formGroup: TypedFormGroup<FormInterface>;
3535
readonly formControlNames: ControlsNames<FormInterface>;
3636
readonly formGroupErrors: NewFormErrors<FormInterface>;
3737
readonly createFormArrayControl: CreateFormArrayControlMethod<FormInterface>;
38+
readonly controlValue$: Observable<Nilable<ControlInterface>>;
3839
}
3940

4041
export type CreateFormArrayControlMethod<FormInterface> = <K extends ArrayPropertyKey<FormInterface>>(
4142
key: K,
4243
initialValue: ArrayPropertyValue<FormInterface, K>,
4344
) => FormControl;
4445

45-
export interface NgxRootForm<ControlInterface> extends NgxSubForm<ControlInterface> {
46+
export interface NgxRootForm<ControlInterface, FormInterface> extends NgxSubForm<ControlInterface, FormInterface> {
4647
// @todo: anything else needed here?
4748
}
4849

0 commit comments

Comments
 (0)