Verified Commit fc949e31 authored by Merzough Münker's avatar Merzough Münker
Browse files

fix(form-array): correct control insertion logic and ID updates

Ensure proper handling of ID updates for moved controls during insertion. Adjust logic to set parent and collection change registration for controls when `_controlInsertedFn` doesn't directly add them.
parent d33a5e6f
Loading
Loading
Loading
Loading
+23 −15
Original line number Diff line number Diff line
@@ -175,8 +175,6 @@ export class RxapFormArray<T = any,

    this._controlInsertedFn(insertIndex, controlOrDefinition);

    if (length === this.controls.length && insertIndex !== length) {
      // the _controlInsertedFn has not yet added the new control to the form array
    if (insertIndex < this.controls.length) {
      // update the control ids for all controls, that are moved.
      for (let i = insertIndex; i < this.controls.length; i++) {
@@ -185,8 +183,8 @@ export class RxapFormArray<T = any,
        ).toFixed(0));
      }
    }
    }

    if (length === this.controls.length) {
      // the _controlInsertedFn has not yet added the new control to the form array
      // call the super insert after the update, bc the insert method will
      // trigger a change detection
      if (controlOrDefinition instanceof NgAbstractControl) {
@@ -194,6 +192,16 @@ export class RxapFormArray<T = any,
      } else {
        super.insert(insertIndex, controlOrDefinition.rxapFormGroup!, options);
      }
    } else {
      if (controlOrDefinition instanceof NgAbstractControl) {
        controlOrDefinition.setParent(this);
        (controlOrDefinition as any)['_registerOnCollectionChange']?.((this as any)['_onCollectionChange']);
      } else {
        controlOrDefinition.rxapFormGroup!.setParent(this);
        (controlOrDefinition.rxapFormGroup as any)['_registerOnCollectionChange']?.((this as any)['_onCollectionChange']);
      }
      this.updateValueAndValidity({ emitEvent: options?.emitEvent });
    }
  }

  public disabledWhile(
+35 −1
Original line number Diff line number Diff line
@@ -381,6 +381,40 @@ describe('@rxap/forms', () => {

    });

    it('should add new array group to empty array', () => {

      const formBuilder = new RxapFormBuilder<ITestFormWithSubArray>(TestFormWithSubArray, Injector.NULL, [
        {
          provide: TestFormWithSubArray,
          useClass: TestFormWithSubArray,
          deps: [],
        },
      ]);

      const form = formBuilder.build<TestFormWithSubArray>({
        username: 'rxap',
        contacts: [],
      });

      expect(form.rxapFormGroup.value).toEqual({
        username: 'rxap',
        contacts: [],
      });
      expect(form.contacts.length).toBe(0);
      form.contacts.rxapFormArray.insertAt(undefined, { zip: '44444' });
      expect(form.contacts.length).toBe(1);
      expect(form.rxapFormGroup.value).toEqual({
        username: 'rxap',
        contacts: [
          { zip: '44444' },
        ],
      });
      expect(form.contacts[0].rxapFormGroup.controlId).toEqual('0');
      expect(form.contacts[0].rxapFormGroup.controlPath).toEqual('contacts.0');
      expect(form.contacts[0].rxapFormGroup.fullControlPath).toEqual('test.contacts.0');

    });

    it('should add new array group', () => {

      const formBuilder = new RxapFormBuilder<ITestFormWithSubArray>(TestFormWithSubArray, Injector.NULL, [
@@ -423,7 +457,7 @@ describe('@rxap/forms', () => {

    });

    it('should insert new array group', () => {
    it('should insert new array group and overite existing group', () => {

      const formBuilder = new RxapFormBuilder<ITestFormWithSubArray>(TestFormWithSubArray, Injector.NULL, [
        {