Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit f1c8ae1

Browse files
committed
Removing Control from form if it's destroyed.
This gets it out of the validation stream and appears to resolve this issues document in MUSIC 1339.
1 parent ce89561 commit f1c8ae1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

source/components/inputs/input.tests.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface IFormMock {
1010

1111
interface IControlGroupMock {
1212
addControl: sinon.SinonSpy;
13+
removeControl: sinon.SinonSpy;
1314
}
1415

1516
interface IGuidMock {
@@ -29,7 +30,7 @@ describe('InputComponent', (): void => {
2930

3031
beforeEach((): void => {
3132
rlForm = {
32-
form: { addControl: sinon.spy() },
33+
form: { addControl: sinon.spy(), removeControl: sinon.spy() },
3334
};
3435
guid = { random: sinon.spy() };
3536

@@ -148,4 +149,11 @@ describe('InputComponent', (): void => {
148149

149150
expect(input.hidePlaceholder).to.be.true;
150151
});
152+
153+
it('should remove the control from the form when ngOnDestroy is called', () => {
154+
input.ngOnDestroy();
155+
156+
sinon.assert.calledOnce(rlForm.form.removeControl);
157+
sinon.assert.calledWith(rlForm.form.removeControl, input.name);
158+
});
151159
});

source/components/inputs/input.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, AfterViewInit, OnInit, EventEmitter, AnimationEntryMetadata } from '@angular/core';
1+
import { Component, AfterViewInit, OnInit, EventEmitter, AnimationEntryMetadata, OnDestroy } from '@angular/core';
22
import { FormControl } from '@angular/forms';
33

44
import { services } from 'typescript-angular-utilities';
@@ -12,7 +12,7 @@ export const baseInputs: string[] = ['name', 'label', 'value', 'disabled','warni
1212
export const baseOutputs: string[] = ['change', 'valueChange'];
1313
export const baseAnimations = [slide.animation];
1414

15-
export class InputComponent<T> implements AfterViewInit, OnInit {
15+
export class InputComponent<T> implements AfterViewInit, OnInit, OnDestroy {
1616
name: string;
1717
label: string = '';
1818
disabled: boolean;
@@ -57,6 +57,12 @@ export class InputComponent<T> implements AfterViewInit, OnInit {
5757
});
5858
}
5959

60+
ngOnDestroy() {
61+
if (this.rlForm) {
62+
this.rlForm.form.removeControl(this.name);
63+
}
64+
}
65+
6066
initControl(): void {
6167
if (!this.control) {
6268
this.control = new FormControl('');

0 commit comments

Comments
 (0)