File

projects/rebirth-ng/src/lib/tags/tags.component.ts

Implements

ControlValueAccessor

Metadata

exportAs tags
providers { : , : (() => ), : true }
selector re-tags
styleUrls tags.component.scss
templateUrl ./tags.component.html

Index

Properties
Methods
Inputs

Constructor

constructor(rebirthNGConfig: RebirthNGConfig)
Parameters :
Name Type Optional
rebirthNGConfig RebirthNGConfig no

Inputs

cssClass

Type: string

maxlength

Type: number

maxSize

Type: number

newTagText

Type: string

plusIcon

Type: string

removeIcon

Type: string

type

Type: "" | "default" | "primary" | "success" | "info" | "warning" | "danger"

Methods

addLabel
addLabel(label: )
Parameters :
Name Optional
label no
Returns : void
boxBlur
boxBlur()
Returns : void
canAddMore
canAddMore()
Returns : boolean
newTag
newTag()
Returns : void
registerOnChange
registerOnChange(fn: any)
Parameters :
Name Type Optional
fn any no
Returns : void
registerOnTouched
registerOnTouched(fn: any)
Parameters :
Name Type Optional
fn any no
Returns : void
removeLabel
removeLabel(label: )
Parameters :
Name Optional
label no
Returns : void
setDisabledState
setDisabledState(isDisabled: boolean)
Parameters :
Name Type Optional
isDisabled boolean no
Returns : void
writeValue
writeValue(obj: any)
Parameters :
Name Type Optional
obj any no
Returns : void

Properties

disabled
disabled: boolean
Type : boolean
isTyping
isTyping: boolean
Type : boolean
mutipleItems
mutipleItems: string[]
Type : string[]
Default value : []
Private onChange
onChange:
Default value : (_: any) => null
Private onTouched
onTouched:
Default value : () => null
selectValue
selectValue: string
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>

Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""