All files / src/app/shared/components/common/switch switch.component.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 3/3
100% Lines 13/13

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 462x 2x                                         2x   8x 8x 8x 8x 8x   8x   8x         4x       4x 4x      
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { v4 as uuid } from 'uuid';
 
@Component({
  selector: 'ish-switch',
  templateUrl: './switch.component.html',
  styleUrls: ['./switch.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
})
/**
 * The Switch Component is a reusable component that allows toggling between two states for a given object.
 * The bootstrap switch component is used to render the switch.
 *
 * @example
 * <ish-switch
 *   [id]="recurringOrder.id"
 *   [active]="recurringOrder.active"
 *   (toggleSwitch)="switchActiveStatus($event)"
 *   ariaLabel="{{'account.recurring_orders.table.switch.aria_label' | translate : { '0': recurringOrder.documentNo } }}"
 * />
 */
export class SwitchComponent implements OnChanges {
  // id is not required but can be used to identify the switch context
  @Input() id: string = uuid();
  @Input() active = false;
  @Input() labelActive = '';
  @Input() labelInactive = '';
  @Input() disabled = false;
  // ariaLabel can be used to provide a label for screen readers (translated text is accepted)
  @Input() ariaLabel = '';
 
  @Output() toggleSwitch = new EventEmitter<{ active: boolean; id: string }>();
 
  activeState: boolean;
 
  ngOnChanges() {
    this.activeState = this.active;
  }
 
  toggleState() {
    this.activeState = !this.activeState;
    this.toggleSwitch.emit({ active: this.activeState, id: this.id });
  }
}