nts-ng2-components
Version:
Paquete de componentes para Angular2 desarrollado por NITSNETS.
90 lines (76 loc) • 2.79 kB
text/typescript
import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core';
import { NtsInputBaseComponent } from '../../base/input-base.component';
import { conformToMask } from 'angular2-text-mask';
// https://github.com/text-mask/text-mask/tree/master/angular2
export type InputType = 'text' | 'number' | 'email' | 'password' | 'color';
export type MaskArray = (string | RegExp)[];
export class NtsInputComponent extends NtsInputBaseComponent implements OnInit, OnChanges {
type: InputType = 'text';
max = -1;
step = 1;
icon: string;
iconRight: string;
placeholder = '';
prefix = '';
hint = '';
mask = '';
error = '';
counter = false;
maxValue: number = null;
minValue: number = null;
floating = false;
multiline = false;
autofocus = false;
clear = false;
colorSwatch = false;
caret = false;
ntsKeypress = new EventEmitter();
focused = false;
_mask: MaskArray = null;
ngOnChanges(changes) {
if (changes.mask && this.mask) {
this.parseMask();
}
if (changes.ntsModel) {
this.applyMask();
}
}
onInputFocus(ev) {
this.focused = true;
this.ntsFocus.emit(ev);
}
onInputBlur(ev) {
this.focused = false;
if (this.maxValue && this.ntsModel > this.maxValue) {
this.ntsModel = this.maxValue;
this.ntsModelChange.emit(this.ntsModel);
} else if (this.minValue && this.ntsModel < this.minValue) {
this.ntsModel = this.minValue;
this.ntsModelChange.emit(this.ntsModel);
}
this.ntsBlur.emit(ev);
}
private applyMask() {
if (!this._mask || !this.ntsModel) { return; }
console.log(conformToMask(this.ntsModel, this._mask, {}).conformedValue);
// this.ntsModel = conformToMask(this.ntsModel, this._mask, {}).conformedValue;
}
private parseMask(): MaskArray {
if (typeof this.mask !== 'string') { return this.mask; }
this._mask = this.mask.split('').map(c => {
switch (c) {
case '9': return /\d/;
case 'A': return /[A-Z]/;
case 'a': return /[a-z]/;
case 'x': return /[A-z]/;
case 'X': return /[A-z|\d]/;
default: return c;
}
});
}
}