import {ChangeDetectionStrategy, Component, computed, ElementRef, input, linkedSignal, output, viewChild} from '@angular/core'; import {Menu} from '@angular/aria/menu'; import {TranslocoPipe} from '@jsverse/transloco'; import {LucideSearch} from '@lucide/angular'; import {AppMenuComponent} from '../../ui/menu/app-menu.component'; import {AppMenuCheckboxComponent} from '../../ui/menu/app-menu-checkbox.component'; import {AppMenuContentDirective} from '../../ui/menu/app-menu-content.directive'; import {AppMenuItemComponent} from '../../ui/menu/app-menu-item.component'; import {AppMenuSeparatorComponent} from '../../ui/menu/app-menu-separator.component'; export interface ShelfMembershipItem { id: number; name: string; checked: boolean; mixed?: boolean; } const FILTER_THRESHOLD = 8; @Component({ selector: 'app-shelf-membership-menu', standalone: true, changeDetection: ChangeDetectionStrategy.OnPush, host: {class: 'contents'}, imports: [ TranslocoPipe, LucideSearch, AppMenuComponent, AppMenuCheckboxComponent, AppMenuContentDirective, AppMenuItemComponent, AppMenuSeparatorComponent, ], template: ` @if (filterVisible()) { } @for (shelf of filteredShelves(); track shelf.id) { {{ shelf.name }} } @empty { @if (shelves().length > 0) { {{ 'shared.ui.select.noResults' | transloco }} } } @if (shelves().length > 0) { } {{ 'shared.ui.shelfMenu.newShelf' | transloco }} `, }) export class ShelfMembershipMenuComponent { readonly shelves = input.required(); readonly toggleShelf = output<{shelfId: number; checked: boolean}>(); readonly createShelf = output(); readonly menu = viewChild.required(AppMenuComponent); readonly ariaMenu = viewChild.required(AppMenuComponent, {read: Menu}); private readonly filterInput = viewChild>('filterInput'); private readonly filterRequired = computed(() => this.shelves().length > FILTER_THRESHOLD); protected readonly filterVisible = linkedSignal({ source: this.filterRequired, computation: (required, previous) => required || (previous?.value ?? false), }); protected readonly query = linkedSignal({ source: this.filterRequired, computation: (required, previous) => required ? (previous?.value ?? '') : '', }); protected readonly filteredShelves = computed(() => { if (!this.filterVisible()) { return this.shelves(); } const needle = this.query().trim().toLowerCase(); if (!needle) { return this.shelves(); } return this.shelves().filter(shelf => shelf.name.toLowerCase().includes(needle)); }); protected onOpened(): void { this.filterVisible.set(this.filterRequired()); queueMicrotask(() => this.filterInput()?.nativeElement.focus({preventScroll: true})); } protected onClosed(): void { this.query.set(''); this.filterVisible.set(false); } protected onFilterKeydown(event: KeyboardEvent): void { if (event.key === 'ArrowDown' || event.key === 'ArrowUp') { event.preventDefault(); event.stopPropagation(); const key = event.key === 'ArrowDown' ? 'Home' : 'End'; this.ariaMenu().element.dispatchEvent(new KeyboardEvent('keydown', {key, bubbles: true, cancelable: true})); return; } if (event.key === 'Escape' || event.key === 'Tab') { return; } if (event.key === 'Enter') { event.preventDefault(); event.stopPropagation(); return; } event.stopPropagation(); } }
{{ 'shared.ui.select.noResults' | transloco }}