File

projects/rebirth-ng/src/lib/auto-complete/mutiple-auto-complete.component.ts

Implements

AfterViewInit ControlValueAccessor

Metadata

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

Index

Properties
Methods
Inputs

Constructor

constructor(rebirthNGConfig: RebirthNGConfig, changeDetectorRef: ChangeDetectorRef)
Parameters :
Name Type Optional
rebirthNGConfig RebirthNGConfig no
changeDetectorRef ChangeDetectorRef no

Inputs

appendBody

Default value: false

cssClass

Type: string

dataSource

Type: any[]

delay

Type: number

disabled

Type: boolean

formatter

Type: function

itemTemplate

Type: TemplateRef<any>

minLength

Type: number

noResultItemTemplate

Type: TemplateRef<any>

onSearch

Type: function

popupCssClass

Type: string

Methods

ngAfterViewInit
ngAfterViewInit()
Returns : void
onMutipleSearch
onMutipleSearch()
Returns : void
onMutipleSearchBoxBackspace
onMutipleSearchBoxBackspace($event: Event)
Parameters :
Name Type Optional
$event Event no
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
toggle
toggle($event?: Event)
Parameters :
Name Type Optional
$event Event yes
Returns : void
writeValue
writeValue(obj: any)
Parameters :
Name Type Optional
obj any no
Returns : void

Properties

autoCompleteDirective
autoCompleteDirective: AutoCompleteDirective
Type : AutoCompleteDirective
Decorators : ViewChild
id
id: number
Type : number
Static ID_SEED
ID_SEED: number
Type : number
Default value : 0
mutipleItems
mutipleItems: any[]
Type : any[]
Private onChange
onChange:
Default value : (_: any) => null
Private onTouched
onTouched:
Default value : () => null
selectValue
selectValue: any
Type : any
import { Component, Input, TemplateRef, forwardRef, ViewChild, AfterViewInit, ChangeDetectorRef } from '@angular/core';
import { AutoCompleteDirective } from './auto-complete.directive';
import { Observable } from 'rxjs';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { RebirthNGConfig } from '../rebirth-ng.config';

@Component({
  selector: 're-mutiple-auto-complete',
  templateUrl: './mutiple-auto-complete.component.html',
  styleUrls: ['./mutiple-auto-complete.component.scss'],
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => MutipleAutoCompleteComponent),
    multi: true
  }]
})
export class MutipleAutoCompleteComponent implements AfterViewInit, ControlValueAccessor {
  static ID_SEED = 0;

  id: number;
  @Input() disabled: boolean;
  @Input() cssClass: string;
  @Input() popupCssClass: string;
  @Input() delay: number;
  @Input() minLength: number;
  @Input() appendBody = false;
  @Input() dataSource: any[];
  @Input() itemTemplate: TemplateRef<any>;
  @Input() noResultItemTemplate: TemplateRef<any>;
  @Input() formatter: (item: any) => string;
  @Input() onSearch: (term: string, target?: AutoCompleteDirective) => Observable<any[]>;

  selectValue: any;
  mutipleItems: any[];
  @ViewChild(AutoCompleteDirective) autoCompleteDirective: AutoCompleteDirective;
  private onChange = (_: any) => null;
  private onTouched = () => null;

  constructor(private rebirthNGConfig: RebirthNGConfig, private changeDetectorRef: ChangeDetectorRef) {
    this.delay = rebirthNGConfig.autoComplete.delay;
    this.minLength = rebirthNGConfig.autoComplete.minLength;
    this.itemTemplate = rebirthNGConfig.autoComplete.itemTemplate;
    this.noResultItemTemplate = rebirthNGConfig.autoComplete.noResultItemTemplate;
    this.formatter = rebirthNGConfig.autoComplete.formatter;
    this.id = MutipleAutoCompleteComponent.ID_SEED++;
  }


  writeValue(obj: any): void {
    this.mutipleItems = obj || [];
    this.changeDetectorRef.markForCheck();
  }

  registerOnChange(fn: any): void {
    this.onChange = fn;
  }

  registerOnTouched(fn: any): void {
    this.onTouched = fn;
  }

  setDisabledState(isDisabled: boolean): void {
    this.disabled = isDisabled;
  }

  ngAfterViewInit(): void {
    if (this.autoCompleteDirective) {
      this.autoCompleteDirective.registerOnTouched(() => this.onTouched());
    }
  }

  toggle($event?: Event) {
    if (this.autoCompleteDirective) {
      this.autoCompleteDirective.toggle($event);
    }
  }

  onMutipleSearch() {
    this.onTouched();
    if (this.mutipleItems.indexOf(this.selectValue) === -1) {
      this.mutipleItems.push(this.selectValue);
      this.onChange(this.mutipleItems);
    }

    this.selectValue = null;
  }

  removeLabel(label) {
    if (this.disabled) {
      return;
    }
    this.onTouched();
    if (this.mutipleItems.indexOf(label) !== -1) {
      this.mutipleItems = this.mutipleItems.filter(item => item !== label);
      this.onChange(this.mutipleItems);
    }
  }

  onMutipleSearchBoxBackspace($event: Event) {
    this.onTouched();
    if (!(<any>$event.target).value && this.mutipleItems.length) {
      this.mutipleItems.pop();
      this.onChange(this.mutipleItems);
      setTimeout(() => this.autoCompleteDirective.positionPopup(), 0);
    }
  }

}
<label for="mutiple-label-auto-complete-{{id}}" class="mutiple-label-auto-complete {{cssClass}}">
  <ul>
    <li *ngFor="let item of mutipleItems">
              <span class="label label-info" [ngClass]="{disabled: disabled}">
                 <i aria-hidden="true" class="label-close" (click)="removeLabel(item)">&times;</i>{{formatter(item)}}
              </span>
    </li>
    <li *ngIf="!disabled">
      <input id="mutiple-label-auto-complete-{{id}}" type="text" name="mutipleAutoComplete"
             class="auto-complete-control"
             [(ngModel)]="selectValue" (selectValueChange)="onMutipleSearch()" [onSearch]="onSearch"
             [cssClass]="popupCssClass" [delay]="delay" [minLength]="minLength" [itemTemplate]="itemTemplate"
             [noResultItemTemplate]="noResultItemTemplate" [formatter]="formatter"
             (keydown.backspace)="onMutipleSearchBoxBackspace($event)" [disabled]="disabled" [dataSource]="dataSource"
             [appendBody]="appendBody" reAutoComplete>
    </li>
  </ul>
</label>

Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""