File

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

Implements

OnChanges

Metadata

changeDetection ChangeDetectionStrategy.OnPush
host {
}
selector re-hightlight

Index

Properties
Methods
Inputs

Inputs

term

Type: string

value

Type: string

Methods

hightlight
hightlight(value: string, term: string)
Parameters :
Name Type Optional
value string no
term string no
Returns : any
ngOnChanges
ngOnChanges()
Returns : void
transform
transform(value: string, term: string)
Parameters :
Name Type Optional
value string no
term string no
Returns : any

Properties

hightlightHtml
hightlightHtml: string
Type : string
import { ChangeDetectionStrategy, Component, Input, OnChanges } from '@angular/core';

@Component({
  selector: 're-hightlight',
  template: ``,
  host: {
    '[innerHTML]': 'hightlightHtml',
    'display': 'inline'
  },
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class HightlightComponent implements OnChanges {
  @Input() value: string;
  @Input() term: string;
  hightlightHtml: string;

  ngOnChanges(): void {
    this.hightlightHtml = this.transform(this.value, this.term);
  }


  transform(value: string, term: string): any {
    return value && term ? this.hightlight(value, term) : value;
  }

  hightlight(value: string, term: string) {
    const termValue = term || '';
    if (termValue.replace) {
      const regExp = new RegExp(termValue.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'), 'gi');
      return value.replace(regExp, function (match) {
        return `<b class="re-hightlight">${match}</b>`;
      });
    }
    return value;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""