diff --git a/source/components/inputs/input.tests.ts b/source/components/inputs/input.tests.ts index 66d5e10c..19afc441 100644 --- a/source/components/inputs/input.tests.ts +++ b/source/components/inputs/input.tests.ts @@ -10,6 +10,7 @@ interface IFormMock { interface IControlGroupMock { addControl: sinon.SinonSpy; + removeControl: sinon.SinonSpy; } interface IGuidMock { @@ -29,7 +30,7 @@ describe('InputComponent', (): void => { beforeEach((): void => { rlForm = { - form: { addControl: sinon.spy() }, + form: { addControl: sinon.spy(), removeControl: sinon.spy() }, }; guid = { random: sinon.spy() }; @@ -148,4 +149,11 @@ describe('InputComponent', (): void => { expect(input.hidePlaceholder).to.be.true; }); + + it('should remove the control from the form when ngOnDestroy is called', () => { + input.ngOnDestroy(); + + sinon.assert.calledOnce(rlForm.form.removeControl); + sinon.assert.calledWith(rlForm.form.removeControl, input.name); + }); }); diff --git a/source/components/inputs/input.ts b/source/components/inputs/input.ts index 284efc14..a39dfd6a 100644 --- a/source/components/inputs/input.ts +++ b/source/components/inputs/input.ts @@ -1,4 +1,4 @@ -import { Component, AfterViewInit, OnInit, EventEmitter, AnimationEntryMetadata } from '@angular/core'; +import { Component, AfterViewInit, OnInit, EventEmitter, AnimationEntryMetadata, OnDestroy } from '@angular/core'; import { FormControl } from '@angular/forms'; import { services } from 'typescript-angular-utilities'; @@ -12,7 +12,7 @@ export const baseInputs: string[] = ['name', 'label', 'value', 'disabled','warni export const baseOutputs: string[] = ['change', 'valueChange']; export const baseAnimations = [slide.animation]; -export class InputComponent implements AfterViewInit, OnInit { +export class InputComponent implements AfterViewInit, OnInit, OnDestroy { name: string; label: string = ''; disabled: boolean; @@ -57,6 +57,12 @@ export class InputComponent implements AfterViewInit, OnInit { }); } + ngOnDestroy() { + if (this.rlForm) { + this.rlForm.form.removeControl(this.name); + } + } + initControl(): void { if (!this.control) { this.control = new FormControl('');