feat: implement a search for professors

This commit is contained in:
Polianin Nikita 2024-02-16 17:50:58 +03:00
parent d9764bfc0e
commit 967f5572fa
3 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,5 @@
.search-content {
display: flex;
align-items: center;
justify-content: center;
}

View File

@ -0,0 +1,18 @@
<div class="search-content">
@if (teachers.length === 0) {
<app-data-spinner/>
} @else {
<mat-form-field color="accent" style="width: 100%;">
<input type="text" placeholder="Поиск..." matInput [formControl]="teacherControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="onOptionSelected($event)"
[autoActiveFirstOption]="false" [hideSingleSelectionIndicator]="true">
@for (option of filteredTeachers | async; track option) {
<mat-option [value]="option.id">
{{ option.name }}
</mat-option>
}
</mat-autocomplete>
</mat-form-field>
}
</div>

View File

@ -0,0 +1,71 @@
import {Component, EventEmitter, Input, OnInit, Output} from "@angular/core";
import {MatFormField, MatInput} from "@angular/material/input";
import {FormControl, FormsModule, ReactiveFormsModule} from "@angular/forms";
import {
MatAutocomplete,
MatAutocompleteSelectedEvent,
MatAutocompleteTrigger,
MatOptgroup,
MatOption
} from "@angular/material/autocomplete";
import {AsyncPipe} from "@angular/common";
import {DataSpinnerComponent} from "@component/data-spinner/data-spinner.component";
import {map, Observable, startWith} from "rxjs";
export interface Professors {
id: number,
name: string
}
@Component({
selector: 'app-schedule-tabs-professor',
standalone: true,
imports: [
MatFormField,
MatInput,
FormsModule,
MatAutocompleteTrigger,
MatAutocomplete,
MatOptgroup,
ReactiveFormsModule,
MatOption,
AsyncPipe,
DataSpinnerComponent
],
templateUrl: './schedule-tabs-professor.component.html',
styleUrl: './schedule-tabs-professor.component.css'
})
export class ScheduleTabsProfessorComponent implements OnInit {
protected teacherControl = new FormControl();
protected filteredTeachers!: Observable<Professors[]>;
@Input() teachers: Professors[] = [];
@Output() optionSelected = new EventEmitter<number>();
ngOnInit(): void {
this.filteredTeachers = this.teacherControl.valueChanges.pipe(
startWith(''),
map(value => this._filterTeachers(value))
);
}
private _filterTeachers(value: string | number): Professors[] {
if (typeof value === 'string') {
if (value === '') return [];
const filterValue = value.toLowerCase();
return this.teachers.filter(teacher => teacher.name.toLowerCase().includes(filterValue));
} else {
const selectedTeacher = this.teachers.find(teacher => teacher.id === value);
return selectedTeacher ? [selectedTeacher] : [];
}
}
protected onOptionSelected(event: MatAutocompleteSelectedEvent) {
const selectedOption = this.teachers.find(teacher => teacher.id === event.option.value);
if (selectedOption) {
this.teacherControl.setValue(selectedOption.name);
this.optionSelected.emit(selectedOption.id);
}
}
}