All files / autocomplete si-autocomplete-option.directive.ts

100% Statements 12/12
100% Branches 0/0
100% Functions 4/4
100% Lines 12/12

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                                                    1x 1x 413x 413x               413x       413x     23x     413x           34x         206x 206x         120x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { Highlightable } from '@angular/cdk/a11y';
import {
  booleanAttribute,
  Directive,
  ElementRef,
  HostBinding,
  HostListener,
  inject,
  input
} from '@angular/core';
 
import { AUTOCOMPLETE_LISTBOX } from './si-autocomplete.model';
 
@Directive({
  selector: '[siAutocompleteOption]',
  host: {
    role: 'option',
    '[id]': 'id()',
    '[attr.aria-disabled]': 'disabledInput()'
  },
  exportAs: 'siAutocompleteOption'
})
export class SiAutocompleteOptionDirective<T = unknown> implements Highlightable {
  private static idCounter = 0;
  private element = inject<ElementRef<HTMLElement>>(ElementRef);
  private parent = inject(AUTOCOMPLETE_LISTBOX);
 
  /**
   * @defaultValue
   * ```
   * `__si-autocomplete-option-${SiAutocompleteOptionDirective.idCounter++}`
   * ```
   */
  readonly id = input(`__si-autocomplete-option-${SiAutocompleteOptionDirective.idCounter++}`);
 
  /** @defaultValue false */
  // eslint-disable-next-line @angular-eslint/no-input-rename
  readonly disabledInput = input(false, { alias: 'disabled', transform: booleanAttribute });
 
  get disabled(): boolean {
    return this.disabledInput();
  }
  /** @defaultValue undefined */
  readonly value = input<T>(undefined, { alias: 'siAutocompleteOption' });
 
  @HostBinding('class.active') protected active?: boolean;
 
  @HostListener('click')
  protected click(): void {
    this.parent.siAutocompleteOptionSubmitted.emit(this.value());
  }
 
  /** @internal */
  setActiveStyles(): void {
    this.active = true;
    this.element.nativeElement.scrollIntoView({ block: 'nearest' });
  }
 
  /** @internal */
  setInactiveStyles(): void {
    this.active = false;
  }
}