-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Custom no entries found element (#382)
* add ngxMatSelectNoEntriesFound directive * change ngx-mat-select-search to use ngxMatSelectNoEntriesFound directive * add custom no entries found element example * rename no entries found example component * export MatSelectNoEntriesFoundDirective in NgxMatSelectSearchModule * fix typo * format mat-select-search.component.html * move custom-no-entries-found-example.component * add README.md entry for customize no entries found element * adjust custom no entries found example Co-authored-by: Esteban Gehring <[email protected]>
- Loading branch information
Showing
10 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
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
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
23 changes: 23 additions & 0 deletions
23
...xamples/09-custom-no-entries-found-example/custom-no-entries-found-example.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,23 @@ | ||
<h3>Single selection with custom no entries found element</h3> | ||
<p> | ||
<mat-form-field> | ||
<mat-select [formControl]="bankCtrl" placeholder="Bank" #singleSelect> | ||
<mat-option> | ||
<ngx-mat-select-search *ngIf="true" [formControl]="bankFilterCtrl" [preventHomeEndKeyPropagation]="true"> | ||
<span ngxMatSelectNoEntriesFound> | ||
No entries found | ||
<button mat-button color="primary"> | ||
Add <mat-icon>add</mat-icon> | ||
</button> | ||
</span> | ||
</ngx-mat-select-search> | ||
</mat-option> | ||
<mat-option *ngFor="let bank of filteredBanks | async" [value]="bank"> | ||
{{bank.name}} | ||
</mat-option> | ||
</mat-select> | ||
</mat-form-field> | ||
</p> | ||
<p> | ||
Selected Bank: {{bankCtrl.value?.name}} | ||
</p> |
Empty file.
92 changes: 92 additions & 0 deletions
92
.../examples/09-custom-no-entries-found-example/custom-no-entries-found-example.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,92 @@ | ||
import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core'; | ||
import { Bank, BANKS } from '../demo-data'; | ||
import { FormControl } from '@angular/forms'; | ||
import { ReplaySubject, Subject } from 'rxjs'; | ||
import { MatSelect } from '@angular/material/select'; | ||
import { take, takeUntil } from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'app-custom-no-entries-found-example', | ||
templateUrl: './custom-no-entries-found-example.component.html', | ||
styleUrls: ['./custom-no-entries-found-example.component.scss'] | ||
}) | ||
export class CustomNoEntriesFoundExampleComponent implements OnInit, AfterViewInit, OnDestroy { | ||
|
||
/** list of banks */ | ||
protected banks: Bank[] = BANKS; | ||
|
||
/** control for the selected bank */ | ||
public bankCtrl: FormControl = new FormControl(); | ||
|
||
/** control for the MatSelect filter keyword */ | ||
public bankFilterCtrl: FormControl = new FormControl(); | ||
|
||
/** list of banks filtered by search keyword */ | ||
public filteredBanks: ReplaySubject<Bank[]> = new ReplaySubject<Bank[]>(1); | ||
|
||
@ViewChild('singleSelect', { static: true }) singleSelect: MatSelect; | ||
|
||
/** Subject that emits when the component has been destroyed. */ | ||
protected _onDestroy = new Subject<void>(); | ||
|
||
|
||
constructor() { } | ||
|
||
ngOnInit() { | ||
// set initial selection | ||
this.bankCtrl.setValue(this.banks[10]); | ||
|
||
// load the initial bank list | ||
this.filteredBanks.next(this.banks.slice()); | ||
|
||
// listen for search field value changes | ||
this.bankFilterCtrl.valueChanges | ||
.pipe(takeUntil(this._onDestroy)) | ||
.subscribe(() => { | ||
this.filterBanks(); | ||
}); | ||
} | ||
|
||
ngAfterViewInit() { | ||
this.setInitialValue(); | ||
} | ||
|
||
ngOnDestroy() { | ||
this._onDestroy.next(); | ||
this._onDestroy.complete(); | ||
} | ||
|
||
/** | ||
* Sets the initial value after the filteredBanks are loaded initially | ||
*/ | ||
protected setInitialValue() { | ||
this.filteredBanks | ||
.pipe(take(1), takeUntil(this._onDestroy)) | ||
.subscribe(() => { | ||
// setting the compareWith property to a comparison function | ||
// triggers initializing the selection according to the initial value of | ||
// the form control (i.e. _initializeSelection()) | ||
// this needs to be done after the filteredBanks are loaded initially | ||
// and after the mat-option elements are available | ||
this.singleSelect.compareWith = (a: Bank, b: Bank) => a && b && a.id === b.id; | ||
}); | ||
} | ||
|
||
protected filterBanks() { | ||
if (!this.banks) { | ||
return; | ||
} | ||
// get the search keyword | ||
let search = this.bankFilterCtrl.value; | ||
if (!search) { | ||
this.filteredBanks.next(this.banks.slice()); | ||
return; | ||
} else { | ||
search = search.toLowerCase(); | ||
} | ||
// filter the banks | ||
this.filteredBanks.next( | ||
this.banks.filter(bank => bank.name.toLowerCase().indexOf(search) > -1) | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/app/mat-select-search/mat-select-no-entries-found.directive.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,15 @@ | ||
import { Directive } from '@angular/core'; | ||
|
||
/** | ||
* Directive for providing a custom no entries found element. | ||
* e.g. | ||
* <ngx-mat-select-search [formControl]="bankFilterCtrl"> | ||
* <span ngxMatSelectNoEntriesFound> | ||
* No entries found <button>Add</button> | ||
* </span> | ||
* </ngx-mat-select-search> | ||
*/ | ||
@Directive({ | ||
selector: '[ngxMatSelectNoEntriesFound]' | ||
}) | ||
export class MatSelectNoEntriesFoundDirective {} |
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
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