@@ -16,29 +16,32 @@ import {
1616 FormUpdate ,
1717 MissingFormControlsError ,
1818 ArrayNotTransformedBeforeWriteValueError ,
19+ FormErrors ,
1920} from './ngx-sub-form-utils' ;
2021import { FormGroupOptions , OnFormUpdate , TypedFormGroup } from './ngx-sub-form.types' ;
2122
23+ type MapControlFunction < FormInterface , MapValue > = ( ctrl : AbstractControl , key : keyof FormInterface ) => MapValue ;
24+ type FilterControlFunction < FormInterface > = ( ctrl : AbstractControl , key : keyof FormInterface ) => boolean ;
25+
2226export abstract class NgxSubFormComponent < ControlInterface , FormInterface = ControlInterface >
2327 implements ControlValueAccessor , Validator , OnDestroy , OnFormUpdate < FormInterface > {
2428 public get formGroupControls ( ) : ControlMap < FormInterface , AbstractControl > {
25- // @note form-group-undefined we need the as syntax here because we do not want to expose the fact that
26- // the form can be undefined, it's hanlded internally to contain an Angular bug
27- return this . mapControls ( ) as ControlMap < FormInterface , AbstractControl > ;
29+ // @note form-group-undefined we need the no-null-assertion here because we do not want to expose the fact that
30+ // the form can be undefined, it's handled internally to contain an Angular bug
31+ // tslint:disable-next-line:no-non-null-assertion
32+ return this . mapControls < AbstractControl > ( ) ! ;
2833 }
2934
3035 public get formGroupValues ( ) : Required < FormInterface > {
31- // see @note form-group-undefined for as syntax
32- return this . mapControls ( ctrl => ctrl . value ) as Required < FormInterface > ;
36+ // see @note form-group-undefined for non-null assertion reason
37+ // tslint:disable-next-line:no-non-null-assertion
38+ return this . mapControls ( ctrl => ctrl . value ) ! ;
3339 }
3440
35- public get formGroupErrors ( ) : null | Partial <
36- ControlMap < FormInterface , ValidationErrors | null > & { formGroup : ValidationErrors }
37- > {
38- const errors = this . mapControls < ValidationErrors | null , ControlMap < FormInterface , ValidationErrors | null > > (
39- ctrl => ctrl . errors ,
40- ctrl => ctrl . invalid ,
41- ) ;
41+ public get formGroupErrors ( ) : FormErrors < FormInterface > {
42+ const errors : Partial <
43+ ControlMap < FormInterface , ValidationErrors | null >
44+ > | null = this . mapControls < ValidationErrors | null > ( ctrl => ctrl . errors , ctrl => ctrl . invalid ) ;
4245
4346 if ( ! this . formGroup . errors && ( ! errors || ! Object . keys ( errors ) . length ) ) {
4447 return null ;
@@ -57,10 +60,10 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont
5760 // when developing the lib it's a good idea to set the formGroup type
5861 // to current + `| undefined` to catch a bunch of possible issues
5962 // see @note form-group-undefined
60- public formGroup : TypedFormGroup < FormInterface > = new FormGroup (
63+ public formGroup : TypedFormGroup < FormInterface > = ( new FormGroup (
6164 this . _getFormControls ( ) ,
6265 this . getFormGroupControlOptions ( ) as AbstractControlOptions ,
63- ) as any ;
66+ ) as unknown ) as TypedFormGroup < FormInterface > ;
6467
6568 protected onChange : Function | undefined = undefined ;
6669 protected onTouched : Function | undefined = undefined ;
@@ -92,10 +95,17 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont
9295 return controls ;
9396 }
9497
95- private mapControls < MapValue , T extends ControlMap < FormInterface , MapValue > > (
96- mapControl ?: ( ctrl : Controls < FormInterface > [ keyof FormInterface ] , key : keyof FormInterface ) => MapValue ,
97- filterControl : ( ctrl : Controls < FormInterface > [ keyof FormInterface ] ) => boolean = ( ) => true ,
98- ) : T | null {
98+ private mapControls < MapValue > (
99+ mapControl : MapControlFunction < FormInterface , MapValue > ,
100+ filterControl : FilterControlFunction < FormInterface > ,
101+ ) : Partial < ControlMap < FormInterface , MapValue > > | null ;
102+ private mapControls < MapValue > (
103+ mapControl ?: MapControlFunction < FormInterface , MapValue > ,
104+ ) : ControlMap < FormInterface , MapValue > | null ;
105+ private mapControls < MapValue > (
106+ mapControl ?: MapControlFunction < FormInterface , MapValue > ,
107+ filterControl : FilterControlFunction < FormInterface > = ( ) => true ,
108+ ) : Partial < ControlMap < FormInterface , MapValue > > | null {
99109 if ( ! this . formGroup ) {
100110 return null ;
101111 }
@@ -106,18 +116,18 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont
106116 return formControls as any ;
107117 }
108118
109- const controls : Partial < T > = { } ;
119+ const controls : Partial < ControlMap < FormInterface , MapValue > > = { } ;
110120
111121 for ( const key in formControls ) {
112122 if ( this . formGroup . controls . hasOwnProperty ( key ) ) {
113123 const control = formControls [ key ] ;
114- if ( control && filterControl ( control ) ) {
124+ if ( control && filterControl ( control , key ) ) {
115125 controls [ key ] = mapControl ( control , key ) ;
116126 }
117127 }
118128 }
119129
120- return controls as Required < T > ;
130+ return controls ;
121131 }
122132
123133 public onFormUpdate ( formUpdate : FormUpdate < FormInterface > ) : void { }
0 commit comments