unicorn-components
Version:
<a target="_blank" href="https://getunicorn.io"><img src="https://bitbucket-assetroot.s3.amazonaws.com/c/photos/2017/Jul/07/2615006260-5-nitsnetsstudios-ondemand-UNI_avatar.png" align="left"></a>
128 lines (113 loc) • 4.69 kB
text/typescript
import { Component, EventEmitter, HostBinding, Input, OnChanges, OnInit, Output } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { UniInputBaseComponent } from '../../base/input-base/input-base.component';
import { UniOption } from './../../../models/option';
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 UniInputComponent extends UniInputBaseComponent implements OnInit, OnChanges {
componentClass = true;
focused = false;
get hasLabel() { return this.label && this.label.length; }
get hasContent() { return this.model && this.model.length; }
get hasPrefix() { return this.prefix || this.prefixIcon || this.type === 'color'; }
get hasSuffix() { return this.suffix || this.suffixIcon; }
get hasChips() { return this.chips && this.chips.length; }
get hasIcon() { return this.icon; }
get hasIconRight() { return this.iconRight || this.clear; }
get hasTwoIconRight() { return this.iconRight && this.clear }
floating = false;
type: InputType = 'text';
max = -1;
icon: string;
iconRight: string;
placeholder = '';
prefix = '';
prefixIcon = '';
suffix = '';
suffixIcon = '';
hint = '';
mask = '';
error = '';
counter = false;
maxValue: number = null;
minValue: number = null;
readonly = false;
multiline = false;
autofocus = false;
clear = false;
colorSwatch = false;
caret = false;
chips: string[] | UniOption[];
chipsChange = new EventEmitter<string[] | UniOption[]>();
deleteChip = new EventEmitter<number>();
uniKeypress = new EventEmitter();
enter = new EventEmitter();
_mask: MaskArray = null;
ngOnChanges(changes) {
if (changes.mask && this.mask) {
this.parseMask();
}
if (changes.model) {
this.applyMask();
}
}
onKeyPress(event: KeyboardEvent) {
this.uniKeypress.emit(event);
if (event.key === 'Enter') {
this.enter.emit(event);
}
}
onInputFocus(ev) {
this.focused = true;
this.uniFocus.emit(ev);
}
onInputBlur(ev) {
this.focused = false;
if (this.maxValue && this.model > this.maxValue) {
this.model = this.maxValue;
this.modelChange.emit(this.model);
} else if (this.minValue && this.model < this.minValue) {
this.model = this.minValue;
this.modelChange.emit(this.model);
}
this.uniBlur.emit(ev);
}
onNgModelChange(ev): Observable<any> {
if (this.type === 'number') {
ev = parseFloat(ev.replace(',', '.'));
}
return super.onNgModelChange(ev);
}
onRemoveChip(index: number) {
if (index < 0 || index >= this.chips.length) { return; }
this.chips.splice(index, 1);
this.chipsChange.emit(this.chips);
this.deleteChip.emit(index);
}
private applyMask() {
if (!this._mask || !this.model) { return; }
console.log(conformToMask(this.model, this._mask, {}).conformedValue);
// this.model = conformToMask(this.model, 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;
}
});
}
}