All files / search-bar si-search-bar.component.ts

94.44% Statements 51/54
100% Branches 11/11
84.21% Functions 16/19
94.33% Lines 50/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                                                                        1x 43x 43x 43x             43x       43x           43x           43x           43x         43x     43x           43x       43x         43x   43x 43x   43x 43x   45x   43x 43x       47x         38x         38x         29x       45x 3x         43x 15x         43x       16x 16x 15x 15x 15x 15x         19x 19x 13x     6x 14x 1x       5x           1x               19x 19x 18x                   1x       50x 50x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import {
  booleanAttribute,
  ChangeDetectionStrategy,
  Component,
  computed,
  ElementRef,
  HostListener,
  input,
  numberAttribute,
  OnChanges,
  OnDestroy,
  OnInit,
  output,
  signal,
  SimpleChanges,
  viewChild
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { BackgroundColorVariant } from '@siemens/element-ng/common';
import { elementCancel, elementSearch, addIcons, SiIconComponent } from '@siemens/element-ng/icon';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
 
@Component({
  selector: 'si-search-bar',
  imports: [SiIconComponent],
  templateUrl: './si-search-bar.component.html',
  styleUrl: './si-search-bar.component.scss',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: SiSearchBarComponent,
      multi: true
    }
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: {
    '[class.readonly]': 'readonly()'
  }
})
export class SiSearchBarComponent implements OnInit, OnDestroy, ControlValueAccessor, OnChanges {
  private readonly inputRef = viewChild.required<ElementRef<HTMLInputElement>>('inputRef');
  private debouncer = new Subject<string>();
  private readonly disabledNgControl = signal(false);
 
  /**
   * Time unit change of search input takes effect.
   *
   * @defaultValue 400
   */
  readonly debounceTime = input(400, { transform: numberAttribute });
  /**
   * Prohibited characters restricting search.
   */
  readonly prohibitedCharacters = input<string>();
  /**
   * Define search input placeholder.
   *
   * @defaultValue ''
   */
  readonly placeholder = input('');
  /**
   * Display search icon before search input.
   *
   * @defaultValue false
   */
  readonly showIcon = input(false, { transform: booleanAttribute });
  /**
   * Whether the search is tabbable or not.
   *
   * @defaultValue true
   */
  readonly tabbable = input(true, { transform: booleanAttribute });
 
  /**
   * Define search input content.
   */
  readonly value = input<string>();
 
  /** @defaultValue false */
  readonly readonly = input(false, { transform: booleanAttribute });
  /**
   * Color to use for component background
   *
   * @defaultValue 'base-1'
   */
  readonly colorVariant = input<BackgroundColorVariant>('base-1');
 
  /** @defaultValue false */
  // eslint-disable-next-line @angular-eslint/no-input-rename
  readonly disabledInput = input(false, { alias: 'disabled', transform: booleanAttribute });
 
  /**
   * Output callback event will provide you with search term if search input changes.
   */
  readonly searchChange = output<string>();
 
  protected isInvalid = false;
  protected inFocus = false;
 
  protected onChange = (val: any): void => {};
  protected onTouch = (): void => {};
 
  protected readonly disabled = computed(() => this.disabledInput() || this.disabledNgControl());
 
  protected readonly searchValue = signal('');
  protected readonly icons = addIcons({ elementCancel, elementSearch });
 
  /** @internal */
  writeValue(value: string): void {
    this.writeSearchValue(value ?? '');
  }
 
  /** @internal */
  registerOnChange(fn: any): void {
    this.onChange = fn;
  }
 
  /** @internal */
  registerOnTouched(fn: () => void): void {
    this.onTouch = fn;
  }
 
  /** @internal */
  setDisabledState(disabled: boolean): void {
    this.disabledNgControl.set(disabled);
  }
 
  ngOnChanges(changes: SimpleChanges): void {
    if (changes.value) {
      this.writeSearchValue(changes.value.currentValue);
    }
  }
 
  ngOnInit(): void {
    this.debouncer.pipe(debounceTime(this.debounceTime())).subscribe(value => {
      this.setSearch(value);
    });
  }
 
  ngOnDestroy(): void {
    this.debouncer.complete();
  }
 
  private setSearch(value?: string): void {
    value ??= '';
    if (value !== this.searchValue()) {
      this.searchValue.set(value);
      this.inputRef().nativeElement.value = value;
      this.onChange(value);
      this.searchChange.emit(value);
    }
  }
 
  private isProhibitedCharactersUsed(searchString: string | null): boolean {
    const prohibitedCharacters = this.prohibitedCharacters();
    if (!prohibitedCharacters || !searchString) {
      return false;
    }
 
    for (const prohibitedCharacter of prohibitedCharacters) {
      if (searchString.includes(prohibitedCharacter)) {
        return true;
      }
    }
 
    return false;
  }
 
  /** @internal */
  @HostListener('focus')
  focus(): void {
    this.inputRef().nativeElement.focus();
  }
 
  protected onCancelFocus(event: Event): void {
    event.stopPropagation();
  }
 
  protected input(event: Event): void {
    const value = (event.target as HTMLInputElement).value;
    if (!this.isProhibitedCharactersUsed(value)) {
      this.debouncer.next(value);
    }
  }
 
  protected onBlur(): void {
    this.inFocus = false;
    this.onTouch();
  }
 
  protected resetForm(): void {
    this.setSearch('');
  }
 
  protected writeSearchValue(value: string): void {
    this.searchValue.set(value);
    this.inputRef().nativeElement.value = value;
  }
}