Skip to content

Commit

Permalink
fix(lib): formControlNames returns array when control is array type
Browse files Browse the repository at this point in the history
This closes #73
  • Loading branch information
maxime1992 committed Jul 20, 2019
1 parent 4edefaf commit 8bdf874
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ Then our view will look like the following:

<div
class="crew-member"
formArrayName="crewMembers"
[formArrayName]="formControlNames.crewMembers"
*ngFor="let crewMember of formGroupControls.crewMembers.controls; let i = index"
>
<app-crew-member [formControl]="crewMember"></app-crew-member>
Expand Down
7 changes: 7 additions & 0 deletions projects/ngx-sub-form/src/lib/ngx-sub-form.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,13 @@ describe(`SubArrayComponent`, () => {
setTimeout(done);
});

it(`should have the 'formControlNames' property returning the name of the 'FormArray' instead of an array`, () => {
const onChangeSpy = jasmine.createSpy('onChangeSpy');
subArrayComponent.registerOnChange(onChangeSpy);

expect(subArrayComponent.formControlNames).toEqual({ vehicles: 'vehicles' });
});

it(`should have the correct values within the 'FormArray'`, () => {
const onChangeSpy = jasmine.createSpy('onChangeSpy');

Expand Down
7 changes: 5 additions & 2 deletions projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont
const errors: FormErrors<FormInterface> = this.mapControls<ValidationErrors | ValidationErrors[] | null>(
ctrl => ctrl.errors,
ctrl => ctrl.invalid,
true,
) as FormErrors<FormInterface>;

if (!this.formGroup.errors && (!errors || !Object.keys(errors).length)) {
Expand All @@ -60,7 +61,7 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont

public get formControlNames(): ControlsNames<FormInterface> {
// see @note form-group-undefined for as syntax
return this.mapControls((_, key) => key) as ControlsNames<FormInterface>;
return this.mapControls((_, key) => key, () => true, false) as ControlsNames<FormInterface>;
}

private controlKeys: (keyof FormInterface)[] = [];
Expand Down Expand Up @@ -106,13 +107,15 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont
private mapControls<MapValue>(
mapControl: MapControlFunction<FormInterface, MapValue>,
filterControl: FilterControlFunction<FormInterface>,
recursiveIfArray: boolean,
): Partial<ControlMap<FormInterface, MapValue | MapValue[]>> | null;
private mapControls<MapValue>(
mapControl: MapControlFunction<FormInterface, MapValue>,
): ControlMap<FormInterface, MapValue | MapValue[]> | null;
private mapControls<MapValue>(
mapControl: MapControlFunction<FormInterface, MapValue>,
filterControl: FilterControlFunction<FormInterface> = () => true,
recursiveIfArray: boolean = true,
): Partial<ControlMap<FormInterface, MapValue | MapValue[]>> | null {
if (!this.formGroup) {
return null;
Expand All @@ -126,7 +129,7 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont
if (this.formGroup.controls.hasOwnProperty(key)) {
const control = formControls[key];

if (control instanceof FormArray) {
if (recursiveIfArray && control instanceof FormArray) {
const values: MapValue[] = [];

for (let i = 0; i < control.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<div
class="crew-member"
formArrayName="crewMembers"
[formArrayName]="formControlNames.crewMembers"
*ngFor="let crewMember of formGroupControls.crewMembers.controls; let i = index"
>
<app-crew-member [formControl]="crewMember"></app-crew-member>
Expand Down

0 comments on commit 8bdf874

Please sign in to comment.