projects/rebirth-ng/src/lib/tags/tags.component.ts
exportAs | tags |
providers |
{
: , : (() => ), : true
}
|
selector | re-tags |
styleUrls | tags.component.scss |
templateUrl | ./tags.component.html |
Properties |
|
Methods |
Inputs |
constructor(rebirthNGConfig: RebirthNGConfig)
|
||||||
Parameters :
|
cssClass
|
Type: |
maxlength
|
Type: |
maxSize
|
Type: |
newTagText
|
Type: |
plusIcon
|
Type: |
removeIcon
|
Type: |
type
|
Type: |
addLabel | ||||
addLabel(label: )
|
||||
Parameters :
Returns :
void
|
boxBlur |
boxBlur()
|
Returns :
void
|
canAddMore |
canAddMore()
|
Returns :
boolean
|
newTag |
newTag()
|
Returns :
void
|
registerOnChange | ||||||
registerOnChange(fn: any)
|
||||||
Parameters :
Returns :
void
|
registerOnTouched | ||||||
registerOnTouched(fn: any)
|
||||||
Parameters :
Returns :
void
|
removeLabel | ||||
removeLabel(label: )
|
||||
Parameters :
Returns :
void
|
setDisabledState | ||||||
setDisabledState(isDisabled: boolean)
|
||||||
Parameters :
Returns :
void
|
writeValue | ||||||
writeValue(obj: any)
|
||||||
Parameters :
Returns :
void
|
disabled |
disabled:
|
Type : boolean
|
isTyping |
isTyping:
|
Type : boolean
|
mutipleItems |
mutipleItems:
|
Type : string[]
|
Default value : []
|
Private onChange |
onChange:
|
Default value : (_: any) => null
|
Private onTouched |
onTouched:
|
Default value : () => null
|
selectValue |
selectValue:
|
Type : string
|
import { Component, forwardRef, Input } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { RebirthNGConfig } from '../rebirth-ng.config';
@Component({
selector: 're-tags',
templateUrl: './tags.component.html',
styleUrls: ['./tags.component.scss'],
exportAs: 'tags',
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => TagsComponent),
multi: true
}]
})
export class TagsComponent implements ControlValueAccessor {
@Input() cssClass: string;
@Input() newTagText: string;
@Input() plusIcon: string;
@Input() removeIcon: string;
@Input() maxlength: number;
@Input() maxSize: number;
@Input() type: '' | 'default' | 'primary' | 'success' | 'info' | 'warning' | 'danger';
mutipleItems: string[] = [];
disabled: boolean;
selectValue: string;
isTyping: boolean;
private onChange = (_: any) => null;
private onTouched = () => null;
constructor(private rebirthNGConfig: RebirthNGConfig) {
this.type = this.rebirthNGConfig.tags.type as any;
this.newTagText = this.rebirthNGConfig.tags.newTagText;
this.plusIcon = this.rebirthNGConfig.tags.plusIcon;
this.removeIcon = this.rebirthNGConfig.tags.removeIcon;
this.maxlength = this.rebirthNGConfig.tags.maxlength;
this.maxSize = this.rebirthNGConfig.tags.maxSize;
}
writeValue(obj: any): void {
this.mutipleItems = typeof obj === 'string' ? [obj] : (obj || []);
}
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
newTag() {
if (this.canAddMore()) {
this.isTyping = true;
}
}
removeLabel(label) {
this.mutipleItems = this.mutipleItems.filter(item => label !== item);
this.onTouched();
this.onChange(this.mutipleItems);
}
addLabel(label) {
this.onTouched();
if (label && this.mutipleItems.indexOf(label) === -1 && this.canAddMore()) {
this.mutipleItems.push(label);
this.onChange(this.mutipleItems);
}
this.selectValue = '';
this.isTyping = false;
}
canAddMore() {
return !this.maxSize || this.mutipleItems.length < this.maxSize;
}
boxBlur() {
this.isTyping = false;
}
}
<ul class="tags {{cssClass}}">
<li *ngFor="let item of mutipleItems" class="tag-item">
<span class="label label-{{type}} tag" [ngClass]="{disabled: disabled}">{{item}}<i
aria-hidden="true" class="label-btn label-remove {{removeIcon}}" *ngIf="!disabled"
(click)="removeLabel(item)"></i>
</span>
</li>
<li *ngIf="!disabled && !isTyping" (click)="newTag()" class="new-tag-item">
<button class="btn label label-{{type}} btn-new-tag" [disabled]="!canAddMore()">{{newTagText}}<i
aria-hidden="true" class="label-btn label-plus {{plusIcon}}"></i>
</button>
</li>
<li *ngIf="!disabled && isTyping" class="tag-input-panel">
<input [reAutoFocus]="isTyping" type="text" name="tagsInput" class="form-control tag-input-control"
[maxlength]="maxlength"
[(ngModel)]="selectValue" (keydown.enter)="addLabel(selectValue)" (blur)="boxBlur()">
</li>
</ul>