All files / element/element/projects/element-ng/search-bar si-search-bar.component.ts

96.82% Statements 61/63
94.59% Branches 35/37
90.9% Functions 20/22
96.42% Lines 54/56

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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                                            198x         212x 70x 70x 70x             70x       70x           70x           70x         70x             70x           70x               70x             70x                   70x         70x   70x   10x     74x   70x 70x       59x         43x         43x         35x       76x 3x         70x 14x         70x       27x 27x 27x 27x 27x 27x         27x 27x 17x     10x 22x 1x       9x         3x       1x       27x 27x 26x 14x   12x                   1x 1x       62x 62x      
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import {
  booleanAttribute,
  Component,
  computed,
  ElementRef,
  input,
  numberAttribute,
  OnChanges,
  OnDestroy,
  OnInit,
  output,
  signal,
  SimpleChanges,
  viewChild
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { elementCancel, elementSearch } from '@siemens/element-icons';
import { BackgroundColorVariant } from '@siemens/element-ng/common';
import { addIcons, SiIconComponent } from '@siemens/element-ng/icon';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
 
@Component({
  selector: 'si-search-bar',
  imports: [SiIconComponent, SiTranslatePipe],
  templateUrl: './si-search-bar.component.html',
  styleUrl: './si-search-bar.component.scss',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: SiSearchBarComponent,
      multi: true
    }
  ],
  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 });
 
  /**
   * Define search input content.
   */
  readonly value = input<string>();
 
  /**
   * Whether the input can be focused but not edited.
   *
   * @defaultValue false
   */
  readonly readonly = input(false, { transform: booleanAttribute });
  /**
   * Color to use for component background
   *
   * @defaultValue 'base-1'
   */
  readonly colorVariant = input<BackgroundColorVariant>('base-1');
 
  /**
   * Whether the input is disabled.
   *
   * @defaultValue false
   */
  // eslint-disable-next-line @angular-eslint/no-input-rename
  readonly disabledInput = input(false, { alias: 'disabled', transform: booleanAttribute });
 
  /**
   * Defines the maximum length of the search input.
   *
   * @defaultValue undefined
   */
  readonly maxlength = input(undefined, { transform: numberAttribute });
 
  /**
   * Aria label for the clear button.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_SEARCH_BAR.CLEAR_BUTTON:Clear`)
   * ```
   */
  readonly clearButtonAriaLabel = input(t(() => $localize`:@@SI_SEARCH_BAR.CLEAR_BUTTON:Clear`));
 
  /**
   * Output callback event will provide you with search term if search input changes.
   */
  readonly searchChange = output<string>();
 
  protected isInvalid = 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<this>): 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 ??= '';
    Eif (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 */
  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)) {
      if (this.debounceTime() > 0) {
        this.debouncer.next(value);
      } else {
        this.setSearch(value);
      }
    }
  }
 
  protected onBlur(): void {
    this.onTouch();
  }
 
  protected resetForm(): void {
    this.setSearch('');
    this.focus();
  }
 
  protected writeSearchValue(value: string): void {
    this.searchValue.set(value);
    this.inputRef().nativeElement.value = value;
  }
}