refactor(ui): autocomplete pagination and infinite scroll (#1965)
This commit is contained in:
@@ -6,32 +6,20 @@ import {
|
||||
linkedSignal,
|
||||
model,
|
||||
viewChild,
|
||||
viewChildren,
|
||||
} 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 { AppTagComponent } from '../tag/app-tag.component';
|
||||
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: [
|
||||
OverlayModule,
|
||||
Combobox,
|
||||
ComboboxInput,
|
||||
ComboboxPopupContainer,
|
||||
Listbox,
|
||||
Option,
|
||||
AppTagComponent,
|
||||
TranslocoPipe,
|
||||
LucideLoaderCircle,
|
||||
],
|
||||
imports: [Combobox, ComboboxInput, LucideLoaderCircle, AppAutocompletePopupComponent, AppAutocompleteSelectedTagsComponent],
|
||||
host: { class: 'block w-full' },
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
template: `
|
||||
@@ -46,13 +34,13 @@ import { AppAutocompleteBaseDirective } from './app-autocomplete-base.directive'
|
||||
#origin
|
||||
[class]="boxClass()"
|
||||
(pointerdown)="focusInputOnPointerdown($event, input)">
|
||||
@for (tag of value(); track tag) {
|
||||
<app-tag
|
||||
size="sm"
|
||||
@if (selectedTags().length) {
|
||||
<app-autocomplete-selected-tags
|
||||
[tags]="selectedTags()"
|
||||
[size]="selectedTagSize()"
|
||||
[removable]="!isUnavailable()"
|
||||
[label]="tag"
|
||||
[removeLabel]="removeTagLabel() || ('shared.ui.autocomplete.removeTag' | transloco: { tag })"
|
||||
(remove)="removeChip(tag)" />
|
||||
[removeTagLabel]="removeTagLabel()"
|
||||
(remove)="removeChip($event)" />
|
||||
}
|
||||
<input
|
||||
ngComboboxInput
|
||||
@@ -73,49 +61,33 @@ import { AppAutocompleteBaseDirective } from './app-autocomplete-base.directive'
|
||||
[required]="required()"
|
||||
[readonly]="readonly()"
|
||||
(input)="onType(input.value)"
|
||||
(focus)="onInputFocus()"
|
||||
(keydown.enter)="onEnter($event)"
|
||||
(keydown.backspace)="onBackspace()"
|
||||
(blur)="touched.set(true)"
|
||||
[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>
|
||||
`,
|
||||
})
|
||||
@@ -124,14 +96,20 @@ export class AppMultiAutocompleteComponent extends AppAutocompleteBaseDirective
|
||||
|
||||
readonly removeTagLabel = input('');
|
||||
|
||||
private readonly listbox = viewChild<Listbox<string>>(Listbox);
|
||||
protected readonly optionRefs = viewChildren(Option);
|
||||
private readonly popup = viewChild(AppAutocompletePopupComponent);
|
||||
private readonly chipLabels = new Map<string, string>();
|
||||
|
||||
protected readonly query = linkedSignal<string[], string>({
|
||||
source: this.value,
|
||||
computation: () => '',
|
||||
});
|
||||
|
||||
protected readonly selectedTags = computed<readonly AppAutocompleteOption[]>(() =>
|
||||
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 {
|
||||
@@ -143,12 +121,19 @@ export class AppMultiAutocompleteComponent extends AppAutocompleteBaseDirective
|
||||
if (this.isUnavailable()) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
const active = this.optionRefs().find((option) => option.active());
|
||||
const active = this.popup()?.activeOption();
|
||||
if (active) {
|
||||
this.addChip(String(active.value()), true);
|
||||
this.addChip(active);
|
||||
return;
|
||||
}
|
||||
this.addChip(this.query());
|
||||
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 {
|
||||
@@ -159,35 +144,33 @@ export class AppMultiAutocompleteComponent extends AppAutocompleteBaseDirective
|
||||
this.touched.set(true);
|
||||
}
|
||||
|
||||
protected onSelect(values: readonly string[]): void {
|
||||
if (this.isUnavailable() || !values.length) return;
|
||||
this.addChip(values[values.length - 1], true);
|
||||
protected selectOption(option: AppAutocompleteOption): void {
|
||||
this.addChip(option);
|
||||
}
|
||||
|
||||
protected removeChip(tag: string): void {
|
||||
this.value.set(this.value().filter((existing) => existing !== tag));
|
||||
protected removeChip(value: string): void {
|
||||
this.chipLabels.delete(value);
|
||||
this.value.set(this.value().filter((existing) => existing !== value));
|
||||
this.touched.set(true);
|
||||
}
|
||||
|
||||
private addChip(text: string, fromSuggestion = false): void {
|
||||
private addChip(option: AppAutocompleteOption): void {
|
||||
if (this.isUnavailable()) return;
|
||||
const trimmed = text.trim();
|
||||
if (!trimmed) return;
|
||||
if (!fromSuggestion && !this.allowCustom() && !this.suggestions().includes(trimmed)) return;
|
||||
const current = this.value();
|
||||
if (current.includes(trimmed)) {
|
||||
if (current.includes(option.value)) {
|
||||
this.clearTagInput();
|
||||
return;
|
||||
}
|
||||
this.value.set([...current, trimmed]);
|
||||
this.chipLabels.set(option.value, option.label);
|
||||
this.value.set([...current, option.value]);
|
||||
this.clearTagInput();
|
||||
this.touched.set(true);
|
||||
this.complete.emit('');
|
||||
}
|
||||
|
||||
private clearTagInput(): void {
|
||||
this.combobox()?.close();
|
||||
this.listbox()?.values.set([]);
|
||||
this.closePopup();
|
||||
this.popup()?.clearSelection();
|
||||
this.query.set('');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user