-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split autocomple to another component
Refs: #302
- Loading branch information
1 parent
475941e
commit 082ee93
Showing
15 changed files
with
405 additions
and
269 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
projects/design-angular-kit/src/lib/components/form/autocomplete/autocomplete.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<div class="form-group"> | ||
<label *ngIf="label" [for]="id" class="visually-hidden">{{ label }}</label> | ||
|
||
<input | ||
[id]="id" | ||
type="search" | ||
class="autocomplete" | ||
[placeholder]="placeholder" | ||
[formControl]="control" | ||
[class.is-invalid]="isInvalid" | ||
[class.is-valid]="isValid" | ||
(blur)="markAsTouched()" | ||
(keydown)="onKeyDown()" /> | ||
|
||
<span class="autocomplete-icon" aria-hidden="true"> | ||
<it-icon name="search" size="sm"></it-icon> | ||
</span> | ||
|
||
<ng-container *ngIf="autocompleteResults$ | async as autocomplete"> | ||
<ul class="autocomplete-list" [class.autocomplete-list-show]="autocomplete.relatedEntries?.length && showAutocompletion"> | ||
<li *ngFor="let entry of autocomplete.relatedEntries; trackBy: autocompleteItemTrackByValueFn"> | ||
<a [href]="entry.link" (click)="onEntryClick(entry, $event)"> | ||
<div class="avatar size-sm" *ngIf="entry.avatarSrcPath"> | ||
<img [src]="entry.avatarSrcPath" [alt]="entry.avatarAltText" /> | ||
</div> | ||
|
||
<it-icon *ngIf="entry.icon" [name]="entry.icon" size="sm"></it-icon> | ||
|
||
<span class="autocomplete-list-text"> | ||
<span [innerHTML]="entry.value | markMatchingText: autocomplete.searchedValue"></span> | ||
<em *ngIf="entry.label">{{ entry.label }}</em> | ||
</span> | ||
</a> | ||
</li> | ||
</ul> | ||
</ng-container> | ||
|
||
<div *ngIf="isInvalid" class="form-feedback just-validate-error-label" [id]="id + '-error'"> | ||
<div #customError> | ||
<ng-content select="[error]"></ng-content> | ||
</div> | ||
<ng-container *ngIf="!customError.hasChildNodes()">{{ invalidMessage | async }}</ng-container> | ||
</div> | ||
</div> |
21 changes: 21 additions & 0 deletions
21
...ts/design-angular-kit/src/lib/components/form/autocomplete/autocomplete.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ItAutocompleteComponent } from './autocomplete.component'; | ||
|
||
describe('AutocompleteComponent', () => { | ||
let component: ItAutocompleteComponent; | ||
let fixture: ComponentFixture<ItAutocompleteComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ItAutocompleteComponent], | ||
}); | ||
fixture = TestBed.createComponent(ItAutocompleteComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
109 changes: 109 additions & 0 deletions
109
projects/design-angular-kit/src/lib/components/form/autocomplete/autocomplete.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; | ||
import { ItAbstractFormComponent } from 'design-angular-kit/abstracts/abstract-form.component'; | ||
import { AutocompleteItem } from 'design-angular-kit/interfaces/form'; | ||
import { debounceTime, distinctUntilChanged, map, Observable, of, switchMap } from 'rxjs'; | ||
import { AsyncPipe, NgForOf, NgIf, NgTemplateOutlet } from '@angular/common'; | ||
import { ItIconComponent } from 'design-angular-kit/components/utils/icon/icon.component'; | ||
import { MarkMatchingTextPipe } from 'design-angular-kit/pipes/mark-matching-text.pipe'; | ||
import { ReactiveFormsModule } from '@angular/forms'; | ||
|
||
@Component({ | ||
standalone: true, | ||
selector: 'it-autocomplete[autocompleteData]', | ||
templateUrl: './autocomplete.component.html', | ||
imports: [AsyncPipe, ItIconComponent, MarkMatchingTextPipe, NgForOf, NgIf, NgTemplateOutlet, ReactiveFormsModule], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class ItAutocompleteComponent extends ItAbstractFormComponent<string | null | undefined> implements OnInit { | ||
/** | ||
* Indicates the list of searchable elements on which to base the input autocomplete system | ||
* If you need to retrieve items via API, can pass a function of Observable | ||
* @default undefined | ||
*/ | ||
@Input({ required: true }) autocompleteData!: Array<AutocompleteItem> | ((search?: string | null) => Observable<Array<AutocompleteItem>>); | ||
|
||
/** | ||
* Time span [ms] has passed without another source emission, to delay data filtering. | ||
* Useful when the user is typing multiple letters | ||
* @default 300 [ms] | ||
*/ | ||
@Input() debounceTime = 300; | ||
|
||
/** | ||
* The input placeholder | ||
*/ | ||
@Input() placeholder = ''; | ||
|
||
/** | ||
* Fired when the Autocomplete Item has been selected | ||
*/ | ||
@Output() autocompleteSelectedEvent: EventEmitter<AutocompleteItem> = new EventEmitter(); | ||
|
||
protected showAutocompletion = false; | ||
|
||
/** Observable da cui vengono emessi i risultati dell'auto completamento */ | ||
protected autocompleteResults$: Observable<{ | ||
searchedValue: string | undefined | null; | ||
relatedEntries: Array<AutocompleteItem>; | ||
}> = new Observable(); | ||
|
||
override ngOnInit() { | ||
super.ngOnInit(); | ||
this.autocompleteResults$ = this.getAutocompleteResults$(); | ||
} | ||
|
||
/** | ||
* Create the autocomplete list | ||
*/ | ||
private getAutocompleteResults$(): Observable<{ | ||
searchedValue: string | null | undefined; | ||
relatedEntries: Array<AutocompleteItem>; | ||
}> { | ||
return this.control.valueChanges.pipe( | ||
debounceTime(this.debounceTime), // Delay filter data after time span has passed without another source emission, useful when the user is typing multiple letters | ||
distinctUntilChanged(), // Only if searchValue is distinct in comparison to the last value | ||
switchMap(searchedValue => { | ||
if (!this.autocompleteData) { | ||
return of({ | ||
searchedValue, | ||
relatedEntries: <Array<AutocompleteItem>>[], | ||
}); | ||
} | ||
|
||
const autoCompleteData$ = Array.isArray(this.autocompleteData) ? of(this.autocompleteData) : this.autocompleteData(searchedValue); | ||
return autoCompleteData$.pipe( | ||
map(autocompleteData => { | ||
if (!searchedValue || typeof searchedValue === 'number') { | ||
return { searchedValue, relatedEntries: [] }; | ||
} | ||
|
||
const lowercaseValue = searchedValue.toLowerCase(); | ||
const relatedEntries = autocompleteData.filter(item => item.value?.toLowerCase().includes(lowercaseValue)); | ||
|
||
return { searchedValue, relatedEntries }; | ||
}) | ||
); | ||
}) | ||
); | ||
} | ||
|
||
protected onEntryClick(entry: AutocompleteItem, event: Event) { | ||
// Se non è stato definito un link associato all'elemento dell'autocomplete, probabilmente il desiderata | ||
// non è effettuare la navigazione al default '#', pertanto in tal caso meglio annullare la navigazione. | ||
if (!entry.link) { | ||
event.preventDefault(); | ||
} | ||
|
||
this.autocompleteSelectedEvent.next(entry); | ||
this.control.setValue(entry.value); | ||
this.showAutocompletion = false; | ||
} | ||
|
||
protected autocompleteItemTrackByValueFn(index: number, item: AutocompleteItem) { | ||
return item.value; | ||
} | ||
|
||
protected onKeyDown() { | ||
this.showAutocompletion = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.