Skip to content

Commit

Permalink
Fix form group.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Sep 13, 2023
1 parent 9e9093d commit 2915bbc
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ describe('UndefinableFormArray', () => {
valueActual: [1],
}];

it('should initialize with undefined', () => {
const control = new UndefinableFormArray();

expect(control.value).toBeUndefined();
});

it('should initialize with empty array', () => {
const control = new UndefinableFormArray([]);

expect(control.value).toEqual([]);
});

it('should provide value even if controls are disabled', () => {
const control = new UndefinableFormArray([
new UntypedFormControl('1'),
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/app/framework/angular/forms/extended-form-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export class ExtendedFormArray extends UntypedFormArray {
}

export class UndefinableFormArray extends ExtendedFormArray {
private isUndefined = true;
private isUndefined = false;

constructor(controls: AbstractControl[], validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null) {
super(controls, validatorOrOpts, asyncValidator);
constructor(controls?: AbstractControl[], validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null) {
super(controls || [], validatorOrOpts, asyncValidator);

const reduce = (this as any)['_reduceValue'];

Expand All @@ -37,6 +37,12 @@ export class UndefinableFormArray extends ExtendedFormArray {
return reduce.apply(this);
}
};

if (Types.isUndefined(controls)) {
this.isUndefined = true;

super.reset([], { emitEvent: false });
}
}

public getRawValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { ExtendedFormGroup, UndefinableFormGroup } from './extended-form-group';

describe('UndefinableFormGroup', () => {
describe('ExtendedFormGroup', () => {
it('should provide value even if controls are disabled', () => {
const control = new ExtendedFormGroup({
test1: new UntypedFormControl('1'),
Expand All @@ -23,7 +23,7 @@ describe('UndefinableFormGroup', () => {
});
});

describe('ExtendedFormGroup', () => {
describe('UndefinableFormGroup', () => {
const tests = [{
name: 'undefined (on)',
undefinable: true,
Expand All @@ -41,8 +41,20 @@ describe('ExtendedFormGroup', () => {
valueActual: { field: 1 },
}];

it('should initialize with undefined', () => {
const control = new UndefinableFormGroup();

expect(control.value).toBeUndefined();
});

it('should initialize with empty array', () => {
const control = new UndefinableFormGroup({});

expect(control.value).toEqual({});
});

it('should provide value even if controls are disabled', () => {
const control = new ExtendedFormGroup({
const control = new UndefinableFormGroup({
test1: new UntypedFormControl('1'),
test2: new UntypedFormControl('2'),
});
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/app/framework/angular/forms/extended-form-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export class ExtendedFormGroup extends UntypedFormGroup {
}

export class UndefinableFormGroup extends ExtendedFormGroup {
private isUndefined = true;
private isUndefined = false;

constructor(controls: { [key: string]: AbstractControl }, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null) {
super(controls, validatorOrOpts, asyncValidator);
constructor(controls?: { [key: string]: AbstractControl }, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null) {
super(controls || {}, validatorOrOpts, asyncValidator);

const reduce = (this as any)['_reduceValue'];

Expand All @@ -43,6 +43,12 @@ export class UndefinableFormGroup extends ExtendedFormGroup {
return reduce.apply(this);
}
};

if (Types.isUndefined(controls)) {
this.isUndefined = true;

super.reset({}, { emitEvent: false });
}
}

public getRawValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TemplatedFormArray extends UndefinableFormArray {
constructor(public readonly template: FormArrayTemplate,
validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null,
) {
super([], validatorOrOpts, asyncValidator);
super(undefined, validatorOrOpts, asyncValidator);
}

public setValue(value?: any[], options?: { onlySelf?: boolean; emitEvent?: boolean }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class TemplatedFormGroup extends UndefinableFormGroup {
constructor(public readonly template: FormGroupTemplate,
validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null,
) {
super({}, validatorOrOpts, asyncValidator);
super(undefined, validatorOrOpts, asyncValidator);
}

public setValue(value?: {}, options?: { onlySelf?: boolean; emitEvent?: boolean }) {
Expand Down

0 comments on commit 2915bbc

Please sign in to comment.