Skip to content

Commit

Permalink
Merge pull request #419 from TheOriginalJosh/form-fixes
Browse files Browse the repository at this point in the history
Removing Control from form if it's destroyed.
  • Loading branch information
TheOriginalJosh authored May 2, 2017
2 parents ce89561 + f1c8ae1 commit 83de5e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion source/components/inputs/input.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface IFormMock {

interface IControlGroupMock {
addControl: sinon.SinonSpy;
removeControl: sinon.SinonSpy;
}

interface IGuidMock {
Expand All @@ -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() };

Expand Down Expand Up @@ -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);
});
});
10 changes: 8 additions & 2 deletions source/components/inputs/input.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<T> implements AfterViewInit, OnInit {
export class InputComponent<T> implements AfterViewInit, OnInit, OnDestroy {
name: string;
label: string = '';
disabled: boolean;
Expand Down Expand Up @@ -57,6 +57,12 @@ export class InputComponent<T> implements AfterViewInit, OnInit {
});
}

ngOnDestroy() {
if (this.rlForm) {
this.rlForm.form.removeControl(this.name);
}
}

initControl(): void {
if (!this.control) {
this.control = new FormControl('');
Expand Down

0 comments on commit 83de5e0

Please sign in to comment.