added simple shared topic autocompletion component
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<div class="topic-autocomplete">
|
||||
<mat-form-field appearance="outline" class="topic-field">
|
||||
<mat-label>{{ label() }}</mat-label>
|
||||
<input
|
||||
matInput
|
||||
[placeholder]="placeholder()"
|
||||
[formControl]="searchControl"
|
||||
[matAutocomplete]="auto"
|
||||
(click)="$event.preventDefault()"
|
||||
(keydown.enter)="submitCurrentValue(); $event.preventDefault()"
|
||||
(keydown.tab)="onKeydownTab($any($event))" />
|
||||
|
||||
@if (searchControl.value.trim()) {
|
||||
<button
|
||||
mat-icon-button
|
||||
matSuffix
|
||||
type="button"
|
||||
color="primary"
|
||||
aria-label="Thema hinzufügen"
|
||||
(click)="submitCurrentValue()">
|
||||
<mat-icon>add</mat-icon>
|
||||
</button>
|
||||
}
|
||||
|
||||
@if (searchControl.value) {
|
||||
<button
|
||||
mat-icon-button
|
||||
matSuffix
|
||||
type="button"
|
||||
aria-label="Suche löschen"
|
||||
(click)="clear()">
|
||||
<mat-icon>close</mat-icon>
|
||||
</button>
|
||||
}
|
||||
|
||||
<mat-autocomplete
|
||||
#auto="matAutocomplete"
|
||||
(optionSelected)="selectTopic($event.option.value)">
|
||||
@if (loading()) {
|
||||
<mat-option disabled>
|
||||
<mat-spinner diameter="18"></mat-spinner>
|
||||
<span>Lade Themen...</span>
|
||||
</mat-option>
|
||||
}
|
||||
|
||||
@for (topic of topics(); track topic) {
|
||||
<mat-option [value]="topic">{{ topic }}</mat-option>
|
||||
}
|
||||
|
||||
@if (
|
||||
!loading() &&
|
||||
searchControl.value.trim().length >= minCharacters() &&
|
||||
topics().length === 0
|
||||
) {
|
||||
<mat-option disabled>Keine passenden Themen gefunden.</mat-option>
|
||||
}
|
||||
</mat-autocomplete>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
@@ -0,0 +1,16 @@
|
||||
:host {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.topic-autocomplete {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
mat-form-field {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.topic-field {
|
||||
width: 100%;
|
||||
min-width: 320px;
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component, DestroyRef, input, output, signal } from '@angular/core';
|
||||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
||||
import { FormControl, ReactiveFormsModule } from '@angular/forms';
|
||||
import { MatAutocompleteModule } from '@angular/material/autocomplete';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
||||
import { TopicsService } from '../../../api/api/topics.service';
|
||||
import { EventEmitter } from '@angular/core';
|
||||
import {
|
||||
catchError,
|
||||
debounceTime,
|
||||
distinctUntilChanged,
|
||||
finalize,
|
||||
map,
|
||||
of,
|
||||
switchMap,
|
||||
} from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'app-topic-autocomplete',
|
||||
standalone: true,
|
||||
imports: [
|
||||
CommonModule,
|
||||
ReactiveFormsModule,
|
||||
MatAutocompleteModule,
|
||||
MatButtonModule,
|
||||
MatFormFieldModule,
|
||||
MatIconModule,
|
||||
MatInputModule,
|
||||
MatProgressSpinnerModule,
|
||||
],
|
||||
templateUrl: './topic-autocomplete.component.html',
|
||||
styleUrl: './topic-autocomplete.component.scss',
|
||||
})
|
||||
export class TopicAutocompleteComponent {
|
||||
readonly label = input('Thema');
|
||||
readonly placeholder = input('Thema suchen oder eintippen...');
|
||||
readonly minCharacters = input(2);
|
||||
readonly limit = input(12);
|
||||
|
||||
readonly topicSelected = output<string>();
|
||||
|
||||
readonly searchControl = new FormControl('', { nonNullable: true });
|
||||
readonly topics = signal<string[]>([]);
|
||||
readonly loading = signal(false);
|
||||
|
||||
constructor(
|
||||
private readonly topicsService: TopicsService,
|
||||
private readonly destroyRef: DestroyRef,
|
||||
) {
|
||||
this.searchControl.valueChanges
|
||||
.pipe(
|
||||
debounceTime(250),
|
||||
distinctUntilChanged(),
|
||||
switchMap((query) => {
|
||||
const normalizedQuery = query.trim();
|
||||
if (normalizedQuery.length < this.minCharacters()) {
|
||||
this.loading.set(false);
|
||||
return of([] as string[]);
|
||||
}
|
||||
|
||||
this.loading.set(true);
|
||||
return this.topicsService
|
||||
.autocompleteTopics(
|
||||
normalizedQuery,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
this.limit(),
|
||||
)
|
||||
.pipe(
|
||||
map((response) =>
|
||||
Array.from(
|
||||
new Set(
|
||||
(response.results ?? [])
|
||||
.map((topic) => topic.display_name?.trim() ?? '')
|
||||
.filter((topic) => !!topic),
|
||||
),
|
||||
),
|
||||
),
|
||||
catchError((error) => {
|
||||
console.error(
|
||||
'Error fetching topic autocomplete options:',
|
||||
error,
|
||||
);
|
||||
return of([] as string[]);
|
||||
}),
|
||||
finalize(() => this.loading.set(false)),
|
||||
);
|
||||
}),
|
||||
takeUntilDestroyed(this.destroyRef),
|
||||
)
|
||||
.subscribe((topics) => {
|
||||
this.topics.set(topics);
|
||||
});
|
||||
}
|
||||
|
||||
selectTopic(topic: string): void {
|
||||
this.searchControl.setValue('', { emitEvent: false });
|
||||
this.topics.set([]);
|
||||
this.topicSelected.emit(topic);
|
||||
}
|
||||
onKeydownTab(event: KeyboardEvent): void {
|
||||
event.preventDefault();
|
||||
this.searchControl.setValue(this.topics()[0], { emitEvent: false });
|
||||
}
|
||||
submitCurrentValue(): void {
|
||||
const topic = this.searchControl.value.trim();
|
||||
if (!topic) return;
|
||||
this.searchControl.setValue('', { emitEvent: false });
|
||||
this.topics.set([]);
|
||||
this.topicSelected.emit(topic);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.searchControl.setValue('');
|
||||
this.topics.set([]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user