forked from tomalaforge/angular-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.component.ts
More file actions
68 lines (64 loc) · 1.63 KB
/
search.component.ts
File metadata and controls
68 lines (64 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormControl, ReactiveFormsModule, Validators } from '@angular/forms';
import { RouterLink, RouterLinkActive } from '@angular/router';
import { availableBooks } from './book.model';
@Component({
imports: [ReactiveFormsModule, RouterLink, RouterLinkActive],
styles: [
`
:host {
display: flex;
flex-direction: column;
gap: 10px;
}
.error {
color: red;
}
button {
width: 300px;
padding: 5px;
border-radius: 5px;
}
.search label {
margin-right: 15px;
}
`,
],
template: `
<div class="search">
<label for="bookName">Search Book by author or title</label>
<input
type="text"
id="bookName"
name="bookName"
[formControl]="searchBook"
required />
@if (searchBook.errors) {
<div class="error" data-testid="search-error-msg">
Search criteria is required!
</div>
}
</div>
<button
data-cy="borrow-btn"
routerLink="/shelf"
[queryParams]="{ book: searchBook.value }"
[disabled]="searchBook.errors"
routerLinkActive="router-link-active">
Borrow
</button>
<div>
<h3>List of books available:</h3>
<ul>
@for (book of books; track $index) {
<li>{{ book.name }} by {{ book.author }}</li>
}
</ul>
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export default class SearchComponent {
searchBook = new FormControl('', Validators.required);
books = availableBooks;
}