All files / lib/checkbox checkbox.component.ts

96.9% Statements 188/194
86.48% Branches 32/37
100% Functions 7/7
96.9% Lines 188/194

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 1951x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 505x 510x 510x 510x 510x   510x 508x 508x 507x 507x 507x 510x 505x 505x 505x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x     2x 2x 1x 1x 1x 506x 506x 506x 506x 506x 506x 506x 506x 506x 506x 506x 1x 1x 506x 506x 506x 506x 506x 1x 1x 1x 32x 29x 29x 29x 29x     29x 29x 32x 32x 32x 32x 32x 8x 5x 5x 3x 3x 24x 24x 24x 32x 32x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  NgZone,
  OnInit,
  computed,
  contentChild,
  inject,
  input,
  model,
  viewChild,
} from '@angular/core';
 
import { FocusableElementOwner } from '../utils/util.types';
import { createWidgetKeySet } from '../utils/utilities';
 
import { TextInputComponent } from '../text-input-field/text-input-field.component';
 
/**
 * A Checkbox element that allows the user to mark a value as selected.
 */
@Component({
  selector: 'gc-checkbox',
  templateUrl: './checkbox.component.html',
  styleUrls: ['./checkbox.component.css'],
  standalone: false,
})
export class CheckboxComponent implements FocusableElementOwner, OnInit {
  /**
   * Custom ID to use
   */
  public readonly id = input<string>();
 
  /**
   * Hides the label of the checkbox. May be used for embedded rendering like in tables.
   */
  public readonly hideLabel = input(false);
 
  /**
   * @ignore
   */
  public readonly _inputElement =
    viewChild<ElementRef<HTMLInputElement>>('input');
 
  /**
   * Text that will show near the checkbox
   */
  public readonly label = input('');
 
  /**
   * Decides if the checkbox will be rendered in a disabled/enabled state
   */
  public readonly disabled = input(false);
 
  /**
   * Decides if the checkbox will be rendered in a readonly state
   */
  public readonly readonly = input(false);
 
  /**
   * An error message that shows under a checkbox
   */
  public readonly errorMessage = input('');
 
  /**
   * Hint message that appears under a checkbox
   */
  public readonly hint = input('');
 
  /**
   * @ignore
   * Forces the checkbox to use error styling (independent of the error message)
   */
  public readonly _useErrorStyle = input(false);
 
  /**
   * Marks the input field as required.
   */
  public readonly required = input(false);
 
  /**
   * Enables setting of the value only when the Promise resolves with value "true".
   */
  public permitValueChange =
    input<(v: boolean) => Promise<boolean> | boolean>();
 
  /**
   * Current checkbox state.
   */
  public readonly value = model<boolean>(false);
 
  /** @ignore */
  protected readonly readonlyTextInput =
    contentChild<TextInputComponent>('readonlyTextInput');
 
  /**
   * @ignore
   */
  protected readonly _widgetKeys = createWidgetKeySet('gc-checkbox');
 
  /**
   * @ignore
   */
  protected readonly _hintId = this._widgetKeys.baseKey + '-hint';
 
  /** @ignore */
  protected readonly ariaDescribedBy = computed(() => {
    const errId =
      this.errorMessage().length > 0 ? this._widgetKeys.errorKey : null;
    const hintId = this.hint().length > 0 ? this._hintId : null;
    if (errId != null && hintId != null) {
      return hintId + ' ' + errId;
    } else if (errId) {
      return errId;
    } else if (hintId) {
      return hintId;
    } else {
      return null;
    }
  });
 
  private readonly zone = inject(NgZone);
 
  public focusChild(): boolean {
    if (this.disabled()) {
      return false;
    }
    const isCheckBoxReadonly = this.readonly();
    const readOnlyTextInput = this.readonlyTextInput();
    const inputElement = this._inputElement();
 
    if (isCheckBoxReadonly && readOnlyTextInput) {
      return readOnlyTextInput.focusChild();
    } else if (inputElement) {
      inputElement.nativeElement.focus();
      return true;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public ngOnInit(): void {
    // browser may "restore" previous values after navigating (e.g. with browser's "back" button),
    // potentially leading to an inconsistent state as no events are fired which could be used to
    // detect this (observed on Chromium) -> sync back state to the native element after it's rendered
    this.zone.runOutsideAngular(() => {
      setTimeout(() => {
        setTimeout(() => {
          const inputElement = this._inputElement();
          if (
            !this.readonly() &&
            inputElement &&
            this.value() !== inputElement.nativeElement.checked
          ) {
            inputElement.nativeElement.checked = this.value();
          }
        });
      });
    });
  }
 
  /** @ignore */
  protected async handleClick(): Promise<void> {
    const doChange = () => {
      this.value.set(!this.value());
      const inputElement = this._inputElement();
      if (inputElement) {
        inputElement.nativeElement.focus();
      } else {
        console.error('native input element not present'); // only expected to be missing in readonly state, otherwise should be always present
      }
    };
 
    const inputElement = this._inputElement();
    const permitValueChange = this.permitValueChange();
 
    if (permitValueChange) {
      const permit = await permitValueChange(!this.value());
      if (permit) {
        doChange();
      } else if (inputElement) {
        inputElement.nativeElement.checked = this.value();
      }
    } else {
      doChange();
    }
  }
}