All files / password-toggle si-password-toggle.component.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 4/4
100% Lines 10/10

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                                  1x           30x           30x                   30x                 30x   30x 30x       46x       2x 2x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { Component, input, output, signal } from '@angular/core';
import { addIcons, elementHide, elementShow, SiIconComponent } from '@siemens/element-ng/icon';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
 
@Component({
  selector: 'si-password-toggle',
  imports: [SiIconComponent, SiTranslatePipe],
  templateUrl: './si-password-toggle.component.html',
  styleUrl: './si-password-toggle.component.scss',
  host: {
    '[class.show-visibility-icon]': 'showVisibilityIcon()'
  }
})
export class SiPasswordToggleComponent {
  /**
   * Whether to show the visibility toggle icon.
   *
   * @defaultValue true
   */
  readonly showVisibilityIcon = input(true);
 
  /**
   * Emits the `type` attribute for the `<input>` ('password' | 'text')
   * whenever the password visibility is getting toggled.
   */
  readonly typeChange = output<string>();
 
  /**
   * The aria-label (translatable) for the password show icon.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PASSWORD_TOGGLE.SHOW:show password`)
   * ```
   */
  readonly showLabel = input(t(() => $localize`:@@SI_PASSWORD_TOGGLE.SHOW:show password`));
  /**
   * The aria-label (translatable) for the password hide icon.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_PASSWORD_TOGGLE.HIDE:hide password`)
   * ```
   */
  readonly hideLabel = input(t(() => $localize`:@@SI_PASSWORD_TOGGLE.HIDE:hide password`));
 
  protected readonly showPassword = signal<boolean>(false);
  protected readonly icons = addIcons({ elementHide, elementShow });
 
  /** The `type` attribute for the `<input>` ('password' | 'text'). */
  get inputType(): string {
    return this.showPassword() ? 'text' : 'password';
  }
 
  protected toggle(): void {
    this.showPassword.set(!this.showPassword());
    this.typeChange.emit(this.inputType);
  }
}