64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { Component, computed, inject, input, output } from '@angular/core';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { DebounceDirective } from '../../../directives/debounce.directive';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
|
|
import { FormsModule } from '@angular/forms';
|
|
|
|
@Component({
|
|
selector: 'app-query-param-input',
|
|
imports: [
|
|
FormsModule,
|
|
MatInputModule,
|
|
MatButtonModule,
|
|
MatIconModule,
|
|
MatFormFieldModule,
|
|
DebounceDirective,
|
|
],
|
|
templateUrl: './query-param-input.component.html',
|
|
styleUrl: './query-param-input.component.scss',
|
|
})
|
|
export class QueryParamInputComponent {
|
|
queryParamKey = input.required<string>();
|
|
queryParamPageKey = input<string>('page');
|
|
label = input<string>('Suchen');
|
|
debounceTime = input<number>(300);
|
|
handleValueChange = output<string>();
|
|
|
|
private readonly activatedRoute = inject(ActivatedRoute);
|
|
private readonly router = inject(Router);
|
|
|
|
private readonly queryParams = toSignal(
|
|
this.activatedRoute.queryParams.pipe(takeUntilDestroyed()),
|
|
);
|
|
|
|
_urlValue = computed<string>(() => {
|
|
const params = this.queryParams();
|
|
const key = this.queryParamKey();
|
|
|
|
return params && key ? params[key] || '' : '';
|
|
});
|
|
|
|
clearInput(): void {
|
|
this.router.navigate([], {
|
|
queryParams: { [this.queryParamKey()]: null },
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
}
|
|
|
|
_handleValueChange(newValue: string): void {
|
|
this.handleValueChange?.emit(newValue);
|
|
|
|
this.router.navigate([], {
|
|
queryParams: {
|
|
[this.queryParamKey()]: newValue || null,
|
|
[this.queryParamPageKey()]: 0,
|
|
},
|
|
queryParamsHandling: 'merge',
|
|
});
|
|
}
|
|
}
|