All files / lib/checkbox-group checkbox-group.component.ts

93.51% Statements 245/262
86.95% Branches 40/46
100% Functions 17/17
93.51% Lines 245/262

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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 2631x 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 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 14x 18x 4x 18x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 135x 135x 1x 1x 1x 1x 1x 17x 17x 17x 14x 3x 3x 3x 17x 17x 17x 17x 1x 1x 1x 1215x 1215x 1215x 1215x 1215x 1x 1x 1x 648x 648x 648x 648x 648x 8x 8x 7x               648x 648x 648x 1x 1x 1x 1x 1x 665x 665x       665x 1x 1x 1x 1x 1x 4215x 4215x           4215x 4215x 4215x 1x 1x 1x 1x 1x 4x 4x 4x 4x 2x 2x 2x 2x 18x 18x 18x 18x 18x 18x 18x 18x 2x 4x 1x 1x 1x 1x 1x 17x 17x 17x 17x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  computed,
  effect,
  input,
  model,
  viewChild,
  viewChildren,
} from '@angular/core';
 
import { isSetEqual } from '../utils/internal-utils';
import {
  Converter,
  FocusableElementOwner,
  Item,
  WidgetKeySet,
} from '../utils/util.types';
import { createWidgetKeySet, isItem, itemTrack } from '../utils/utilities';
 
import { CheckboxComponent } from '../checkbox/checkbox.component';
import { TextInputComponent } from '../text-input-field/text-input-field.component';
 
/**
 * A Checkbox Group element that allows the user to mark an arbitrary number of values as
 * selected from a set of different checkboxes.
 */
@Component({
  selector: 'gc-checkbox-group',
  templateUrl: './checkbox-group.component.html',
  styleUrls: ['./checkbox-group.component.css'],
  standalone: false,
})
export class CheckboxGroupComponent<T, I extends Item = Item>
  implements FocusableElementOwner
{
  /**
   * Text that shows within the checkbox group border.
   */
  public readonly label = input('');
 
  /**
   * List of items that will be rendered as checkboxes.
   */
  public readonly items = input<readonly T[]>([]);
 
  /**
   * Adapter that manages the conversion of item types.
   */
  public readonly itemAdapter = input<Converter<T, I>>();
 
  /**
   * Keys of items which are in the readonly mode.
   * @deprecated - use new readonly input instead.
   */
  public readonly readonlyItems = input<ReadonlySet<string>>(new Set());
 
  /**
   * Keys of items which are in the disabled mode.
   */
  public readonly disabledItems = input<ReadonlySet<string>>(new Set());
 
  /**
   * An error message that shows within a checkbox.
   */
  public readonly errorMessage = input('');
 
  /**
   * Marks the input field as required.
   */
  public readonly required = input(false);
 
  /**
   * Decides if the checkboxgroup will be rendered in a readonly state
   */
  public readonly readonly = input(false);
 
  /**
   * Enables setting of the value only when the Promise resolves with value "true".
   */
  public readonly permitValueChange =
    input<(t: ReadonlySet<T>) => Promise<boolean> | boolean>();
 
  /**
   * Set of checked items.
   */
  public readonly value = model<ReadonlySet<T>>(new Set());
 
  /** @ignore */
  protected readonly readonlyTextInput =
    viewChild<TextInputComponent>('readonlyTextInput');
 
  /** @ignore */
  protected readonly _widgetKeys: WidgetKeySet =
    createWidgetKeySet('gc-checkbox-group');
 
  /**
   * @ignore
   */
  protected readonly convertedItems = computed(() =>
    Array.from(this.items()).map(i => this.getAsItem(i)),
  );
 
  /**
   * @ignore
   */
  protected readonly readonlyValues = computed(() => {
    return this.convertedItems()
      .filter(i => this.isChecked(i.key))
      .map(i => i.label)
      .join('; ');
  });
 
  /**
   * @ignore
   */
  private readonly errorMessageElement =
    viewChild<ElementRef<HTMLParagraphElement>>('error');
 
  /**
   * @ignore
   */
  private readonly checkboxElements = viewChildren(CheckboxComponent);
 
  public constructor() {
    effect(() => {
      if (this.errorMessage().length > 0) {
        this.setErrorMessageProviderToInputs();
      }
    });
  }
 
  public focusChild(): boolean {
    const readonly = this.readonly();
    const readonlyTextInput = this.readonlyTextInput();
    if (readonly && readonlyTextInput) {
      return readonlyTextInput.focusChild();
    } else {
      const firstNonDisabledElement = this.checkboxElements().find(
        el => !el.disabled(),
      );
      if (firstNonDisabledElement) {
        return firstNonDisabledElement.focusChild();
      } else {
        return false;
      }
    }
  }
 
  /** @ignore */
  protected itemTrackBy(index: number, item: Item): string {
    return itemTrack(item);
  }
 
  /**
   * @ignore
   */
  protected valueChanged(key: string, checked: boolean): void {
    const foundValue = this.findValueByKey(key);
    if (foundValue) {
      if (checked) {
        this.updateValue(new Set([...this.value(), foundValue]));
      } else {
        this.updateValue(
          new Set(Array.from(this.value()).filter(val => foundValue != val)),
        );
      }
    }
  }
 
  /** @ignore */
  protected isChecked(key: string): boolean {
    const foundItem = Array.from(this.value()).find(
      i => this.getAsItem(i).key == key,
    );
    return foundItem !== undefined;
  }
 
  /** @ignore */
  protected convertPermitValueChange(
    key: string,
  ): (b: boolean) => boolean | Promise<boolean> {
    const foundValue = this.findValueByKey(key);
    const permitValueChange = this.permitValueChange();
    return checked => {
      if (foundValue && permitValueChange !== undefined) {
        if (checked) {
          return permitValueChange(new Set([...this.value(), foundValue]));
        } else {
          return permitValueChange(
            new Set(Array.from(this.value()).filter(val => foundValue != val)),
          );
        }
      } else {
        return true;
      }
    };
  }
 
  /**
   * @ignore
   */
  private findValueByKey(key: string): T | null {
    const foundVal = Array.from(this.items()).find(
      i => this.getAsItem(i).key == key,
    );

    return foundVal ?? null;
  }
 
  /**
   * @ignore
   */
  private getAsItem(item: T): Item {
    const itemAdapter = this.itemAdapter();
    if (itemAdapter === undefined) {
      if (isItem(item)) {
        return item;
      }
      throw new Error('missing converter');
    } else {
      return itemAdapter(item);
    }
  }
 
  /**
   * @ignore
   */
  private setErrorMessageProviderToInputs(): void {
    const errorMessageElement = this.errorMessageElement();
    const checkboxElements = this.checkboxElements();
 
    if (errorMessageElement) {
      const errorProvider =
        errorMessageElement.nativeElement.getAttribute('id');
 
      for (const el of checkboxElements) {
        const inputElement = el._inputElement();
        if (errorProvider && inputElement) {
          inputElement.nativeElement.setAttribute(
            'errormessage',
            errorProvider,
          );
        }
      }
    }
  }
 
  /**
   * @ignore
   */
  private updateValue(value: ReadonlySet<T>): void {
    if (!isSetEqual(this.value(), value)) {
      this.value.set(value);
    }
  }
}