All files / select/options si-select-simple-options.directive.ts

100% Statements 11/11
100% Branches 4/4
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 65 66 67 68 69                                                1x         99x                   428x         99x 103x 103x 102x 331x                       1x         103x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { computed, Directive, input, OnChanges } from '@angular/core';
 
import { SelectItem, SelectOption, SelectOptionLegacy } from '../si-select.types';
import { SI_SELECT_OPTIONS_STRATEGY } from './si-select-options-strategy';
import { SiSelectOptionsStrategyBase } from './si-select-options-strategy.base';
 
/**
 * This directive allows passing {@link SelectItem} to the {@link SiSelectComponent}.
 *
 * @example
 * ```html
 * <si-select [options]="[{ type: 'option', value: 'one', label: 'One' }, { type: 'option', value: 'two', label: 'Two' }]"></si-select>
 * <si-select [options]="[{ type: 'group', label: 'Group', options [{ type: 'option', value: 1, label: 'One' }, { type: 'option', value: 2, label: 'Two' }] }]"></si-select>
 * ```
 */
@Directive({
  // eslint-disable-next-line @angular-eslint/directive-selector
  selector: 'si-select[options]',
  providers: [{ provide: SI_SELECT_OPTIONS_STRATEGY, useExisting: SiSelectSimpleOptionsDirective }]
})
export class SiSelectSimpleOptionsDirective<T = string>
  extends SiSelectOptionsStrategyBase<T>
  implements OnChanges
{
  /** Options to be shown in select dropdown */
  readonly options = input<(SelectOptionLegacy | SelectItem<T>)[] | null>();
 
  /**
   * By default, values are check on referential equality. Provide a function to customize the behavior.
   *
   * @defaultValue
   * ```
   * (a: T, b: T): boolean => a === b
   * ```
   */
  override readonly optionsEqual = input((a: T, b: T): boolean => a === b, {
    // eslint-disable-next-line @angular-eslint/no-input-rename
    alias: 'optionEqualCheckFn'
  });
 
  override readonly allRows = computed(() => {
    const options = this.options();
    if (options) {
      return options?.map(option =>
        option.type
          ? option
          : ({
              type: 'option',
              value: option.id as T,
              label: option.title,
              iconColor: option.color,
              icon: option.icon,
              disabled: option.disabled
            } as SelectOption<T>)
      );
    } else {
      return [];
    }
  });
 
  ngOnChanges(): void {
    this.onFilter();
  }
}