Skip to content

Commit

Permalink
feat(ComponentHasModel): Criar metodo para receber/validar nova classe
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamAguera committed May 29, 2017
1 parent c9d97b7 commit f5f9022
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/core/base/ComponentHasModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
SOFTWARE.
*/

import { ElementRef, Input, ViewChild } from '@angular/core';
import { ElementRef, Input, ViewChild, OnInit } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import { ComponentCustom } from './ComponentCustom';
import { TabIndexGenerator } from '../helper/tabindex-generator';
Expand All @@ -34,7 +34,8 @@ const noop = () => {
/**
* Class that controls all Components that have Models.
*/
export class ComponentHasModel extends ComponentCustom implements ControlValueAccessor {
export class ComponentHasModel extends ComponentCustom implements ControlValueAccessor, OnInit {

/**
* Controller to define if the tabulation is with key Enter or key Tab.
* @type {boolean}
Expand All @@ -47,23 +48,25 @@ export class ComponentHasModel extends ComponentCustom implements ControlValueAc
*/
@Input() name = '';

@ViewChild( 'inputModel' ) public inputModel;
@Input() validations = {};

/**
* Variable type of TabIndexGenerator in charge of instantiate a new Generator.
* Text to display in Input Placeholder.
* @type {string}
*/
public tabIndex : TabIndexGenerator;
@Input() placeholder = '';

@ViewChild( 'model' ) public inputModel;

/**
* Value of ngModel returned to user.
* Variable type of TabIndexGenerator in charge of instantiate a new Generator.
*/
public ngValue = '';
tabIndex : TabIndexGenerator;

/**
* Text to display in Input Placeholder.
* @type {string}
* Value of ngModel returned to user.
*/
@Input() placeholder = '';
ngValue = '';

/**
* Function that returns value of ngModel
Expand Down Expand Up @@ -95,6 +98,14 @@ export class ComponentHasModel extends ComponentCustom implements ControlValueAc
*/
onChangeCallback : ( _ : any) => void = noop;


ngOnInit () {
let self = this;
Object.keys( this.validations ).forEach(function ( key ) {
self[key] = self.validations[key];
})
}

/**
* Function to set tabIndex of Elements received.
* @param element
Expand All @@ -113,15 +124,8 @@ export class ComponentHasModel extends ComponentCustom implements ControlValueAc
if (value !== this.ngValue) {
this.ngValue = value;
this.onChangeCallback(this.ngValue);
this.requiredValidation();
}

}

requiredValidation() {
if (!this.inputModel.valid && this.inputModel.touched) {
this.placeholder = 'Campo Obrigatório';
}
}

/**
Expand Down Expand Up @@ -181,5 +185,10 @@ export class ComponentHasModel extends ComponentCustom implements ControlValueAc
return document.getElementById('tl-' + this.element.nativeElement.localName + '-' + (currentTabIndex + 1));
}


hasValidation() {
return Object.keys( this.validations ).length > 0;
}

}

0 comments on commit f5f9022

Please sign in to comment.