All files / lib/internal listbox-item.directive.ts

98.46% Statements 128/130
93.93% Branches 31/33
93.75% Functions 15/16
98.46% Lines 128/130

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 1311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 53x 1x 534x 534x 534x 534x 534x 534x 534x 534x 534x 534x 534x 534x 534x 534x 534x 14932x 14932x 534x 534x 14298x 14298x 534x 534x     534x 534x 534x 14298x 14298x 11706x 2592x 2592x 14298x 14298x 534x 534x 14298x 14298x 14298x 534x 534x 14298x 14298x 14298x 534x 534x 534x 534x 534x 534x 534x 534x 1x 1x 1x 53x 46x 53x 53x 53x 53x 1x 1x 1x 53x 46x 53x 53x 1x 1x 1x 17x 17x 1x 1x 557x 557x 546x 11x 11x 11x 11x 557x 557x 1x 1x 14348x 13814x 534x 534x 14348x 14348x 1x 1x 116x 116x 1x 1x 35x 35x 1x 1x 70x 70x 70x 70x 70x 1x  
import {
  Directive,
  ElementRef,
  HostBinding,
  HostListener,
  Input,
  inject,
} from '@angular/core';
 
import { BaseIndexedItem } from '../utils/internal-utils';
import { Key } from '../utils/util.types';
 
import { ListBoxDirective } from './listbox.directive';
 
@Directive({
  selector: '[data-gc-listbox-item]',
  standalone: false,
})
export class ListBoxItemDirective {
  @Input('data-gc-listbox-item')
  public item!: BaseIndexedItem;
 
  @HostBinding('attr.id')
  public itemId?: string;
 
  @HostBinding('attr.role')
  public readonly role = 'option';
 
  // Does not work on JAWS
  // some roles where this is used currently handle aria-selected manually, check usages of the directive before (re-)enabling this in the future
  //  @HostBinding('attr.aria-selected')
  //  get ariaSelected(): boolean {
  //    return this.selected;
  //  }
 
  get key(): Key | symbol {
    return this.item.key;
  }
 
  get selected(): boolean {
    return this._listbox ? this._listbox.isKeySelected(this.key) : false;
  }
 
  get label(): string {
    return this.item.label;
  }
 
  @HostBinding('attr.tabindex')
  public get tabindex(): number | null {
    // a tabindex of -1 allows the node to get the native focus
    if (this._listbox?.focusMode === 'native') {
      return -1;
    } else {
      return null;
    }
  }
 
  @HostBinding('class.gc-selected')
  get classSelected(): boolean {
    return this.selected;
  }
 
  @HostBinding('class.gc-focused')
  get classFocused(): boolean {
    return this.isFocused();
  }
 
  /** @ignore */
  private readonly el = inject<ElementRef<HTMLElement>>(ElementRef);
 
  // Injecting the parent listbox fails in Storybook, hence the ListBoxDirective
  // registers itself via `updateItem()` instead (formerly MenuDirective):
  // private readonly listbox = inject(ListBoxDirective);
  private _listbox?: ListBoxDirective;
 
  @HostListener('click', ['$event'])
  _handleMenuItemClicked(event: MouseEvent): void {
    if (this._listbox?.focusMode === 'virtual') {
      this.focus();
    }
    this._listbox?.selectItem(this.item.key, 'space');
    event.preventDefault();
  }
 
  @HostListener('mousedown', ['$event'])
  _handleMenuItemMouseDown(event: MouseEvent): void {
    if (this._listbox?.focusMode === 'virtual') {
      event.preventDefault();
    }
  }
 
  @HostListener('keydown', ['$event'])
  _handleMenuItemKeyDown(event: KeyboardEvent): void {
    this._listbox?.handleKeyDown(event);
  }
 
  updateItem(listbox: ListBoxDirective): void {
    this._listbox = listbox;
    if (typeof this.key === 'string') {
      this.itemId = `${listbox.listboxId ?? ''}_k_${this.key}`;
    } else {
      this.itemId = `${listbox.listboxId ?? ''}_s_${
        this.key.description ?? String(this.key)
      }`;
    }
  }
 
  isFocused(): boolean {
    if (this._listbox) {
      return this._listbox.isFocused(this);
    } else {
      return false;
    }
  }
 
  focus(): void {
    this._listbox?.focusItem(this);
  }
 
  focusNative(): void {
    this.el.nativeElement.focus();
  }
 
  scrollIntoView(): void {
    this.el.nativeElement.scrollIntoView({
      block: 'nearest',
      inline: 'nearest',
    });
  }
}