1+ import { FormControl } from '@angular/forms' ;
12import isEqual from 'fast-deep-equal' ;
23import { getObservableLifecycle } from 'ngx-observable-lifecycle' ;
34import { EMPTY , forkJoin , Observable , of , timer } from 'rxjs' ;
@@ -14,7 +15,13 @@ import {
1415 tap ,
1516 withLatestFrom ,
1617} from 'rxjs/operators' ;
17- import { isNullOrUndefined } from './shared/ngx-sub-form-utils' ;
18+ import {
19+ ArrayPropertyValue ,
20+ isNullOrUndefined ,
21+ OneOfControlsTypes ,
22+ TypedAbstractControl ,
23+ TypedFormControl ,
24+ } from './shared/ngx-sub-form-utils' ;
1825import {
1926 createFormDataFromOptions ,
2027 getControlValueAccessorBindings ,
@@ -25,6 +32,7 @@ import {
2532import {
2633 ComponentHooks ,
2734 ControlValueAccessorComponentInstance ,
35+ CreateFormArrayControlMethod ,
2836 FormBindings ,
2937 FormType ,
3038 NgxFormOptions ,
@@ -36,8 +44,9 @@ import {
3644} from './ngx-sub-form.types' ;
3745
3846const optionsHaveInstructionsToCreateArrays = < ControlInterface , FormInterface > (
39- options : NgxSubFormOptions < ControlInterface , FormInterface > ,
40- ) : options is NgxSubFormOptions < ControlInterface , FormInterface > & NgxSubFormArrayOptions < FormInterface > => true ;
47+ options : NgxFormOptions < ControlInterface , FormInterface > & Partial < NgxSubFormArrayOptions < FormInterface > > ,
48+ ) : options is NgxSubFormOptions < ControlInterface , FormInterface > & NgxSubFormArrayOptions < FormInterface > =>
49+ ! ! options . createFormArrayControl ;
4150
4251// @todo find a better name
4352const isRoot = < ControlInterface , FormInterface > (
@@ -181,19 +190,18 @@ export function createForm<ControlInterface, FormInterface>(
181190 ? lifecyleHooks . onDestroy . pipe ( mapTo ( null ) )
182191 : EMPTY ;
183192
193+ const createFormArrayControl : Required < NgxSubFormArrayOptions < FormInterface > > [ 'createFormArrayControl' ] =
194+ optionsHaveInstructionsToCreateArrays < ControlInterface , FormInterface > ( options ) && options . createFormArrayControl
195+ ? options . createFormArrayControl
196+ : ( key , initialValue ) => new FormControl ( initialValue ) ;
197+
184198 const sideEffects = {
185199 broadcastValueToParent$ : registerOnChange$ . pipe (
186200 switchMap ( onChange => broadcastValueToParent$ . pipe ( tap ( value => onChange ( value ) ) ) ) ,
187201 ) ,
188202 applyUpstreamUpdateOnLocalForm$ : transformedValue$ . pipe (
189203 tap ( value => {
190- handleFormArrays < FormInterface > (
191- formArrays ,
192- value ,
193- optionsHaveInstructionsToCreateArrays < ControlInterface , FormInterface > ( options )
194- ? options . createFormArrayControl
195- : null ,
196- ) ;
204+ handleFormArrays < FormInterface > ( formArrays , value , createFormArrayControl ) ;
197205
198206 formGroup . reset ( value , { emitEvent : false } ) ;
199207
@@ -241,7 +249,6 @@ export function createForm<ControlInterface, FormInterface>(
241249 get formGroupErrors ( ) {
242250 return getFormGroupErrors < ControlInterface , FormInterface > ( formGroup ) ;
243251 } ,
244- // todo
245- createFormArrayControl : ( options as any ) . createFormArrayControl ,
252+ createFormArrayControl,
246253 } ;
247254}
0 commit comments