All files / select/selection si-select-single-value.directive.ts

100% Statements 5/5
50% Branches 1/2
100% Functions 2/2
100% Lines 4/4

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                                                                    1x         122x     159x       7x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { Directive } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
 
import { SiSelectSelectionStrategy } from './si-select-selection-strategy';
 
/**
 * The directive enables the single-select behavior.
 * Otherwise, use the {@link SiSelectMultiValueDirective} directive.
 *
 * @example
 * ```html
 * <si-select [(value)]="selectedValue" [options]="[
 *  { id: 'good', title: 'Good' },
 *  { id: 'average', title: 'Average' },
 *  { id: 'poor', title: 'Poor' }
 * ]"></si-select>
 * ```
 */
@Directive({
  // eslint-disable-next-line @angular-eslint/directive-selector
  selector: 'si-select:not([multi])',
  providers: [
    { provide: SiSelectSelectionStrategy, useExisting: SiSelectSingleValueDirective },
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: SiSelectSingleValueDirective,
      multi: true
    }
  ]
})
export class SiSelectSingleValueDirective<T> extends SiSelectSelectionStrategy<T, T> {
  /**
   * {@inheritDoc SiSelectSelectionStrategy#allowMultiple}
   * @defaultValue false
   */
  override allowMultiple = false;
 
  protected toArrayValue(value: T | undefined): readonly T[] {
    return value !== undefined ? [value] : [];
  }
 
  protected fromArrayValue([value]: readonly T[]): T {
    return value;
  }
}