|
1 | 1 | import { FormControl } from '@angular/forms'; |
2 | 2 | import isEqual from 'fast-deep-equal'; |
3 | 3 | 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'; |
5 | 5 | import { |
6 | 6 | delay, |
7 | 7 | filter, |
@@ -59,15 +59,15 @@ const isRoot = <ControlInterface, FormInterface>( |
59 | 59 | export function createForm<ControlInterface, FormInterface = ControlInterface>( |
60 | 60 | componentInstance: ControlValueAccessorComponentInstance, |
61 | 61 | options: NgxRootFormOptions<ControlInterface, FormInterface>, |
62 | | -): NgxRootForm<FormInterface>; |
| 62 | +): NgxRootForm<ControlInterface, FormInterface>; |
63 | 63 | export function createForm<ControlInterface, FormInterface = ControlInterface>( |
64 | 64 | componentInstance: ControlValueAccessorComponentInstance, |
65 | 65 | options: NgxSubFormOptions<ControlInterface, FormInterface>, |
66 | | -): NgxSubForm<FormInterface>; |
| 66 | +): NgxSubForm<ControlInterface, FormInterface>; |
67 | 67 | export function createForm<ControlInterface, FormInterface>( |
68 | 68 | componentInstance: ControlValueAccessorComponentInstance, |
69 | 69 | options: NgxFormOptions<ControlInterface, FormInterface>, |
70 | | -): NgxSubForm<FormInterface> { |
| 70 | +): NgxSubForm<ControlInterface, FormInterface> { |
71 | 71 | const { formGroup, defaultValues, formControlNames, formArrays } = createFormDataFromOptions< |
72 | 72 | ControlInterface, |
73 | 73 | FormInterface |
@@ -184,6 +184,14 @@ export function createForm<ControlInterface, FormInterface>( |
184 | 184 | ), |
185 | 185 | ); |
186 | 186 |
|
| 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 | + |
187 | 195 | const emitNullOnDestroy$: Observable<null> = |
188 | 196 | // emit null when destroyed by default |
189 | 197 | isNullOrUndefined(options.emitNullOnDestroy) || options.emitNullOnDestroy |
@@ -250,5 +258,6 @@ export function createForm<ControlInterface, FormInterface>( |
250 | 258 | return getFormGroupErrors<ControlInterface, FormInterface>(formGroup); |
251 | 259 | }, |
252 | 260 | createFormArrayControl, |
| 261 | + controlValue$, |
253 | 262 | }; |
254 | 263 | } |
0 commit comments