You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

39 lines
1.3 KiB

  1. import {Component, Input} from '@angular/core';
  2. import {FormGroup} from "@angular/forms";
  3. import {debounceTime, distinctUntilChanged, Observable, OperatorFunction, switchMap} from "rxjs";
  4. import {filter, map} from "rxjs/operators";
  5. @Component({
  6. selector: 'app-search-input',
  7. templateUrl: './search-input.component.html',
  8. styleUrl: './search-input.component.scss'
  9. })
  10. export class SearchInputComponent {
  11. @Input() public formId!: string;
  12. @Input() public formLabelLangKey!: string;
  13. @Input() public dataField!: string;
  14. @Input() public documentForm!: FormGroup;
  15. @Input() public documentFormField!: string;
  16. @Input() public fetchFunction!: (term: string) => Observable<{ id: any; name: any }[]>;
  17. protected formatter = (apiData: any) => apiData.name;
  18. protected searchItem: OperatorFunction<string, readonly {
  19. id: any;
  20. name: any
  21. }[]> = (text$: Observable<string>) =>
  22. text$.pipe(
  23. debounceTime(200),
  24. distinctUntilChanged(),
  25. filter((term) => term.length >= 2),
  26. switchMap((term) => this.fetchFunction(term)),
  27. map((items: {id: any, name: any}[]) => items.slice(0, 10)),
  28. );
  29. protected onItemSelect(selectedItem: any): void {
  30. this.documentForm.get(this.formId)?.setValue(selectedItem.item.id);
  31. }
  32. }