import { ChangeDetectionStrategy, Component, computed, input, linkedSignal, model, signal, 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'; type AppAutocompleteSelectedDisplay = 'text' | 'tag'; @Component({ selector: 'app-autocomplete', standalone: true, imports: [Combobox, LucideLoaderCircle, AppAutocompletePopupComponent, AppAutocompleteSelectedTagsComponent, AppControlTransitionDirective], host: { class: 'block w-full' }, changeDetection: ChangeDetectionStrategy.OnPush, template: `
@if (selectedTags().length) { }
`, }) export class AppAutocompleteComponent extends AppAutocompleteBaseDirective implements FormValueControl { readonly value = model(''); readonly selectedDisplay = input('text'); readonly removeTagLabel = input(''); private readonly popup = viewChild(AppAutocompletePopupComponent); private readonly committedOption = signal(null); protected readonly selectedLabel = computed(() => { const value = this.value(); const committed = this.committedOption(); if (committed && committed.value === value) return committed.label; return this.normalizedSuggestions().find((option) => option.value === value)?.label ?? value; }); protected readonly query = linkedSignal({ source: () => ({ display: this.selectedDisplay(), label: this.selectedLabel() }), computation: ({ display, label }) => (display === 'tag' && label.trim() ? '' : label), }); protected readonly showSelectedTag = computed(() => this.selectedDisplay() === 'tag' && !!this.value().trim()); protected readonly selectedTags = computed(() => this.showSelectedTag() ? [{ value: this.value(), label: this.selectedLabel() }] : [], ); protected readonly inputReadonly = computed(() => this.readonly() || this.showSelectedTag()); protected readonly placeholderText = computed(() => (this.showSelectedTag() ? '' : this.placeholder())); protected onType(text: string): void { if (this.isUnavailable() || this.showSelectedTag()) return; if (this.selectedDisplay() !== 'tag' && (this.allowCustom() || !text.trim())) this.value.set(text); this.complete.emit(text); } protected onEnter(event: Event): void { if (this.isUnavailable() || this.selectedDisplay() !== 'tag' || this.showSelectedTag()) return; event.preventDefault(); event.stopPropagation(); const active = this.popup()?.activeOption(); if (active) { this.selectOption(active); return; } const trimmed = this.query().trim(); if (!trimmed) return; const match = this.normalizedSuggestions().find((option) => option.label === trimmed); if (match) { this.selectOption(match); return; } if (this.allowCustom()) this.selectOption({ value: trimmed, label: trimmed }); } protected override onInputFocus(): void { if (this.showSelectedTag()) return; super.onInputFocus(); } protected onInputBlur(): void { if (this.allowCustom()) return; queueMicrotask(() => { if (!this.allowCustom()) this.query.set(this.selectedLabel()); }); } protected selectOption(option: AppAutocompleteOption): void { if (this.isUnavailable()) return; this.committedOption.set(option); this.value.set(option.value); this.query.set(this.selectedDisplay() === 'tag' ? '' : option.label); this.closePopup(); } protected clearSelectedTag(input: HTMLInputElement): void { if (this.isUnavailable()) return; this.committedOption.set(null); this.value.set(''); this.query.set(''); this.complete.emit(''); queueMicrotask(() => input.focus()); } }