import { ChangeDetectionStrategy, Component, computed, input, linkedSignal, model, viewChild, } from '@angular/core'; import { type FormValueControl } from '@angular/forms/signals'; import { Combobox } from '@angular/aria/combobox'; import { LucideLoaderCircle } from '@lucide/angular'; import { AppControlTransitionDirective } from '../control.styles'; import { AppAutocompleteBaseDirective } from './app-autocomplete-base.directive'; import { type AppAutocompleteOption } from './app-autocomplete-option'; import { AppAutocompletePopupComponent } from './app-autocomplete-popup.component'; import { AppAutocompleteSelectedTagsComponent } from './app-autocomplete-selected-tags.component'; @Component({ selector: 'app-multi-autocomplete', standalone: true, imports: [Combobox, LucideLoaderCircle, AppAutocompletePopupComponent, AppAutocompleteSelectedTagsComponent, AppControlTransitionDirective], host: { class: 'block w-full' }, changeDetection: ChangeDetectionStrategy.OnPush, template: `
@if (selectedTags().length) { }
`, }) export class AppMultiAutocompleteComponent extends AppAutocompleteBaseDirective implements FormValueControl { readonly value = model([]); readonly removeTagLabel = input(''); private readonly popup = viewChild(AppAutocompletePopupComponent); private readonly chipLabels = new Map(); protected readonly query = linkedSignal({ source: this.value, computation: () => '', }); protected readonly selectedTags = computed(() => this.value().map((value) => ({ value, label: this.chipLabels.get(value) ?? this.normalizedSuggestions().find((option) => option.value === value)?.label ?? value, })), ); protected readonly placeholderText = computed(() => (this.value().length ? '' : this.placeholder())); protected onType(text: string): void { if (this.isUnavailable()) return; this.complete.emit(text); } protected onEnter(event: Event): void { if (this.isUnavailable()) return; event.preventDefault(); event.stopPropagation(); const active = this.popup()?.activeOption(); if (active) { this.addChip(active); return; } const trimmed = this.query().trim(); if (!trimmed) return; const match = this.normalizedSuggestions().find((option) => option.label === trimmed); if (match) { this.addChip(match); return; } if (this.allowCustom()) this.addChip({ value: trimmed, label: trimmed }); } protected onBackspace(): void { if (this.isUnavailable() || this.query()) return; const current = this.value(); if (!current.length) return; this.value.set(current.slice(0, -1)); } protected selectOption(option: AppAutocompleteOption): void { this.addChip(option); } protected removeChip(value: string): void { this.chipLabels.delete(value); this.value.set(this.value().filter((existing) => existing !== value)); } private addChip(option: AppAutocompleteOption): void { if (this.isUnavailable()) return; const current = this.value(); if (current.includes(option.value)) { this.clearTagInput(); return; } this.chipLabels.set(option.value, option.label); this.value.set([...current, option.value]); this.clearTagInput(); this.complete.emit(''); } private clearTagInput(): void { this.closePopup(); this.popup()?.clearSelection(); this.query.set(''); } }