|
| 1 | +import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core'; |
| 2 | +import { toSignal } from '@angular/core/rxjs-interop'; |
| 3 | +import { FormControl, ReactiveFormsModule } from '@angular/forms'; |
| 4 | +import { KbqHighlightBackgroundPipe } from '@koobiq/components/core'; |
| 5 | +import { KbqFormFieldModule } from '@koobiq/components/form-field'; |
| 6 | +import { KbqIconModule } from '@koobiq/components/icon'; |
| 7 | +import { KbqInputModule } from '@koobiq/components/input'; |
| 8 | +import { KbqUserInfo, KbqUsernameModule, KbqUsernamePipe } from '@koobiq/components/username'; |
| 9 | +import { startWith } from 'rxjs'; |
| 10 | + |
| 11 | +/** |
| 12 | + * @title Username search |
| 13 | + */ |
| 14 | +@Component({ |
| 15 | + selector: 'username-search-example', |
| 16 | + imports: [ |
| 17 | + ReactiveFormsModule, |
| 18 | + KbqFormFieldModule, |
| 19 | + KbqInputModule, |
| 20 | + KbqUsernameModule, |
| 21 | + KbqIconModule, |
| 22 | + KbqHighlightBackgroundPipe |
| 23 | + ], |
| 24 | + template: ` |
| 25 | + <kbq-form-field> |
| 26 | + <i kbqPrefix kbq-icon="kbq-magnifying-glass_16"></i> |
| 27 | + <input kbqInput type="text" placeholder="Search" autocomplete="off" [formControl]="searchControl" /> |
| 28 | + <kbq-cleaner /> |
| 29 | + </kbq-form-field> |
| 30 | +
|
| 31 | + <div class="example__users-list"> |
| 32 | + @for (user of filteredUsers(); track user.login) { |
| 33 | + <kbq-username> |
| 34 | + <kbq-username-custom-view> |
| 35 | + @let fullName = user | kbqUsername; |
| 36 | + <span |
| 37 | + kbqUsernamePrimary |
| 38 | + [innerHTML]="fullName | kbqHighlightBackground: searchControl.value" |
| 39 | + ></span> |
| 40 | +
|
| 41 | + @if (user.login) { |
| 42 | + <span |
| 43 | + kbqUsernameSecondary |
| 44 | + [innerHTML]="user.login | kbqHighlightBackground: searchControl.value" |
| 45 | + ></span> |
| 46 | + } |
| 47 | + </kbq-username-custom-view> |
| 48 | + </kbq-username> |
| 49 | + } @empty { |
| 50 | + <span class="kbq-text-normal kbq-second">Nothing found</span> |
| 51 | + } |
| 52 | + </div> |
| 53 | + `, |
| 54 | + styles: ` |
| 55 | + .example__users-list { |
| 56 | + display: flex; |
| 57 | + flex-direction: column; |
| 58 | + gap: var(--kbq-size-s); |
| 59 | + } |
| 60 | + `, |
| 61 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 62 | + host: { |
| 63 | + class: 'layout-column layout-gap-m layout-padding-m' |
| 64 | + } |
| 65 | +}) |
| 66 | +export class UsernameSearchExample { |
| 67 | + private readonly usernamePipe = inject(KbqUsernamePipe); |
| 68 | + |
| 69 | + protected readonly searchControl = new FormControl(''); |
| 70 | + |
| 71 | + private readonly searchText = toSignal(this.searchControl.valueChanges.pipe(startWith('')), { initialValue: '' }); |
| 72 | + |
| 73 | + protected readonly users: KbqUserInfo[] = [ |
| 74 | + { firstName: 'Maxwell', middleName: 'Alan', lastName: 'Root', login: 'mroot', site: 'corp' }, |
| 75 | + { firstName: 'Alice', middleName: 'Marie', lastName: 'Stone', login: 'astone' }, |
| 76 | + { firstName: 'Robert', lastName: 'Green', login: 'rgreen', site: 'dev' }, |
| 77 | + { firstName: 'Elena', middleName: 'Vera', lastName: 'Fox', login: 'efox' } |
| 78 | + ]; |
| 79 | + |
| 80 | + protected readonly filteredUsers = computed(() => { |
| 81 | + const query = (this.searchText() ?? '').toLowerCase().trim(); |
| 82 | + |
| 83 | + if (!query) return this.users; |
| 84 | + |
| 85 | + return this.users.filter((user) => { |
| 86 | + const formatted = this.usernamePipe.transform(user).toLowerCase(); |
| 87 | + |
| 88 | + return formatted.includes(query) || (user.login?.toLowerCase().includes(query) ?? false); |
| 89 | + }); |
| 90 | + }); |
| 91 | +} |
0 commit comments