refactor(ui): autocomplete pagination and infinite scroll (#1965)

This commit is contained in:
Alex
2026-07-09 20:23:52 +01:00
committed by GitHub
parent b4570e3366
commit 0b1fa83a65
28 changed files with 518 additions and 209 deletions
@@ -1,26 +1,19 @@
import { ChangeDetectionStrategy, Component, linkedSignal, model } from '@angular/core';
import { ChangeDetectionStrategy, Component, computed, input, linkedSignal, model, signal, viewChild } from '@angular/core';
import { type FormValueControl } from '@angular/forms/signals';
import { Combobox, ComboboxInput, ComboboxPopupContainer } from '@angular/aria/combobox';
import { Listbox, Option } from '@angular/aria/listbox';
import { OverlayModule } from '@angular/cdk/overlay';
import { TranslocoPipe } from '@jsverse/transloco';
import { Combobox, ComboboxInput } from '@angular/aria/combobox';
import { LucideLoaderCircle } from '@lucide/angular';
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: [
OverlayModule,
Combobox,
ComboboxInput,
ComboboxPopupContainer,
Listbox,
Option,
TranslocoPipe,
LucideLoaderCircle,
],
imports: [Combobox, ComboboxInput, LucideLoaderCircle, AppAutocompletePopupComponent, AppAutocompleteSelectedTagsComponent],
host: { class: 'block w-full' },
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@@ -35,6 +28,14 @@ import { AppAutocompleteBaseDirective } from './app-autocomplete-base.directive'
#origin
[class]="boxClass()"
(pointerdown)="focusInputOnPointerdown($event, input)">
@if (selectedTags().length) {
<app-autocomplete-selected-tags
[tags]="selectedTags()"
[size]="selectedTagSize()"
[removable]="!isUnavailable()"
[removeTagLabel]="removeTagLabel()"
(remove)="clearSelectedTag(input)" />
}
<input
ngComboboxInput
#input
@@ -44,87 +45,127 @@ import { AppAutocompleteBaseDirective } from './app-autocomplete-base.directive'
[(value)]="query"
[attr.id]="resolvedInputId()"
[attr.name]="name() || null"
[placeholder]="placeholder()"
[placeholder]="placeholderText()"
[attr.aria-label]="ariaLabel() || null"
[attr.aria-invalid]="showInvalid() ? 'true' : null"
[attr.aria-readonly]="readonly() ? 'true' : null"
[attr.aria-readonly]="inputReadonly() ? 'true' : null"
[attr.aria-busy]="pending() ? 'true' : null"
[attr.aria-describedby]="resolvedDescribedBy()"
[disabled]="disabled()"
[required]="required()"
[readonly]="readonly()"
[readonly]="inputReadonly()"
(input)="onType(input.value)"
(focus)="onInputFocus()"
(keydown.enter)="onEnter($event)"
(blur)="onInputBlur()"
[class]="innerInputClass" />
@if (pending()) {
<svg lucideLoaderCircle class="size-4 shrink-0 animate-spin text-text-muted" aria-hidden="true"></svg>
}
<svg
lucideLoaderCircle
class="size-4 shrink-0 text-text-muted motion-reduce:animate-none"
[class.animate-spin]="pending()"
[class.invisible]="!pending()"
aria-hidden="true"></svg>
</div>
<ng-template ngComboboxPopupContainer>
<ng-template
[cdkConnectedOverlay]="{
origin,
usePopover: 'inline',
matchWidth: true,
positions: overlayPositions,
viewportMargin: 8,
push: true
}"
[cdkConnectedOverlayOpen]="cb.expanded()"
[cdkConnectedOverlayScrollStrategy]="overlayScrollStrategy"
(attach)="onOverlayAttach()">
<div [class]="surfaceClass">
<ul
ngListbox
tabindex="-1"
focusMode="activedescendant"
selectionMode="explicit"
[readonly]="readonly()"
[disabled]="disabled()"
(valuesChange)="onSelect($event)"
[class]="listClass">
@for (option of suggestions(); track option) {
<li ngOption tabindex="-1" [value]="option" [label]="option" [class]="optionClass">
<span class="truncate leading-5">{{ option }}</span>
</li>
} @empty {
<li [class]="emptyClass">{{ emptyMessage() || ('shared.ui.autocomplete.noResults' | transloco) }}</li>
}
</ul>
</div>
</ng-template>
</ng-template>
<app-autocomplete-popup
[origin]="origin"
[open]="cb.expanded()"
[disabled]="disabled()"
[readonly]="readonly()"
[pending]="pending()"
[loadingMore]="loadingMore()"
[hasMore]="hasMore()"
[errored]="errored()"
[suggestions]="normalizedSuggestions()"
[emptyMessage]="emptyMessage()"
[optionTemplate]="optionTemplate()?.template ?? null"
(optionSelected)="selectOption($event)"
(loadMore)="loadMore.emit()" />
</div>
`,
})
export class AppAutocompleteComponent extends AppAutocompleteBaseDirective implements FormValueControl<string> {
readonly value = model('');
readonly selectedDisplay = input<AppAutocompleteSelectedDisplay>('text');
readonly removeTagLabel = input('');
protected readonly query = linkedSignal(() => this.value());
private readonly popup = viewChild(AppAutocompletePopupComponent);
private readonly committedOption = signal<AppAutocompleteOption | null>(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<readonly AppAutocompleteOption[]>(() =>
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()) return;
if (this.allowCustom() || !text.trim()) this.value.set(text);
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 {
this.touched.set(true);
if (this.allowCustom()) return;
queueMicrotask(() => {
if (!this.allowCustom()) this.query.set(this.value());
if (!this.allowCustom()) this.query.set(this.selectedLabel());
});
}
protected onSelect(values: readonly string[]): void {
if (this.isUnavailable() || !values.length) return;
const picked = values[values.length - 1];
this.value.set(picked);
this.query.set(picked);
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.touched.set(true);
this.combobox()?.close();
this.closePopup();
}
protected clearSelectedTag(input: HTMLInputElement): void {
if (this.isUnavailable()) return;
this.committedOption.set(null);
this.value.set('');
this.query.set('');
this.touched.set(true);
this.complete.emit('');
queueMicrotask(() => input.focus());
}
}