File

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

Implements

ControlValueAccessor

Metadata

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

Index

Properties
Methods
Inputs

Constructor

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

Inputs

cssClass

Type: string

disabled

Type: boolean

formatter

Type: function

isOpen

Type: boolean

itemTemplate

Type: TemplateRef<any>

noResultItemTemplate

Type: TemplateRef<any>

source

Type: any[]

term

Type: string

Methods

afterVisibilityAnimation
afterVisibilityAnimation(e: AnimationEvent)
Parameters :
Name Type Optional
e AnimationEvent no
Returns : void
hide
hide()
Returns : void
next
next()
Returns : void
onActiveIndexChange
onActiveIndexChange(index: )
Parameters :
Name Optional
index no
Returns : void
onSelect
onSelect(item: any)
Parameters :
Name Type Optional
item any no
Returns : void
prev
prev()
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
reset
reset()
Returns : void
selectCurrentItem
selectCurrentItem()
Returns : void
setDisabledState
setDisabledState(isDisabled: boolean)
Parameters :
Name Type Optional
isDisabled boolean no
Returns : void
show
show()
Returns : void
writeValue
writeValue(obj: any)
Parameters :
Name Type Optional
obj any no
Returns : void

Properties

activeIndex
activeIndex: number
Type : number
Default value : 0
animateState
animateState: string
Type : string
Default value : 'initial'
Private onChange
onChange:
Default value : (_: any) => null
Private onTouched
onTouched:
Default value : () => null
Private value
value: any
Type : any
import { Component, Input, TemplateRef, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { RebirthNGConfig } from '../rebirth-ng.config';
import { trigger, state, style, animate, transition, AnimationEvent } from '@angular/animations';


@Component({
  selector: 're-auto-complete-popup',
  templateUrl: './auto-complete-popup.component.html',
  host: {
    '[class]': '"dropdown-menu "  +  (cssClass ? cssClass : "")',
    '[style.display]': 'isOpen && (source?.length || noResultItemTemplate) ? "inline-block" : "none"',
    '[@state]': 'animateState',
    '(@state.done)': 'afterVisibilityAnimation($event)'
  },
  styleUrls: ['./auto-complete-popup.component.scss'],
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => AutoCompletePopupComponent),
    multi: true
  }],
  animations: [
    trigger('state', [
      state('void', style({ transform: 'scale(0)' })),
      state('initial', style({ transform: 'scale(0)' })),
      state('visible', style({ transform: 'scale(1)' })),
      state('hidden', style({ transform: 'scale(0)' })),
      transition('* => visible', animate('150ms cubic-bezier(0.0, 0.0, 0.2, 1)')),
      transition('* => hidden', animate('150ms cubic-bezier(0.4, 0.0, 1, 1)')),
    ])
  ]
})
export class AutoCompletePopupComponent implements ControlValueAccessor {
  activeIndex = 0;
  @Input() cssClass: string;
  @Input() disabled: boolean;
  @Input() source: any[];
  @Input() isOpen: boolean;
  @Input() term: string;
  @Input() itemTemplate: TemplateRef<any>;
  @Input() noResultItemTemplate: TemplateRef<any>;
  @Input() formatter: (item: any) => string;
  animateState = 'initial';

  private value: any;
  private onChange = (_: any) => null;
  private onTouched = () => null;

  constructor(rebirthNGConfig: RebirthNGConfig) {
    this.formatter = rebirthNGConfig.autoComplete.formatter;
  }

  writeValue(obj: any): void {
    this.value = obj;
  }

  show() {
    if (!this.isOpen) {
      this.isOpen = true;
      this.animateState = 'visible';
    }
  }

  hide() {
    if (this.isOpen) {
      this.animateState = 'hidden';
    }
  }

  afterVisibilityAnimation(e: AnimationEvent) {
    if (e.toState === 'hidden' && this.isOpen) {
      this.isOpen = false;
    }
  }

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

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

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

  onSelect(item: any) {
    this.value = item;
    this.onTouched();
    this.onChange(this.value);
  }

  selectCurrentItem() {
    if (this.source && this.source.length) {
      this.onSelect(this.source[this.activeIndex]);
    }
  }

  onActiveIndexChange(index) {
    this.activeIndex = index;
  }

  reset() {
    this.activeIndex = 0;
  }

  next() {
    if (this.isOpen && this.source && this.source.length) {
      if (this.activeIndex === this.source.length - 1) {
        this.activeIndex = 0;
        return;
      }
      this.activeIndex = this.activeIndex + 1;
    }
  }

  prev() {
    if (this.isOpen && this.source && this.source.length) {
      if (this.activeIndex === 0) {
        this.activeIndex = this.source.length - 1;
        return;
      }
      this.activeIndex = this.activeIndex - 1;
    }
  }
}
<ng-template #defaultItemTemplate let-item="item" let-term="term">
  <re-hightlight [value]="formatter(item)" [term]="term"></re-hightlight>
</ng-template>

<ul class="list-unstyled popup-list">
  <li *ngFor="let item of source; let $index = index;" class="dropdown-item"
      [ngClass]="{'bg-primary': $index == activeIndex}" (click)="onSelect(item)"
      (mouseenter)="onActiveIndexChange($index)">
    <ng-template [ngTemplateOutlet]="itemTemplate || defaultItemTemplate"
              [ngTemplateOutletContext]="{ formatter: formatter, term:term, source:source, item:item, $index:$index}"></ng-template>
  </li>
  <li class="dropdown-item" *ngIf="!source?.length && noResultItemTemplate != null">
    <ng-template [ngTemplateOutlet]="noResultItemTemplate"
              [ngTemplateOutletContext]="{ term:term, source:source}"> </ng-template>
  </li>
</ul>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""