-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typeahead): add config service to provide default values
- Loading branch information
1 parent
414d20a
commit 237b4d5
Showing
10 changed files
with
146 additions
and
10 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
demo/src/app/components/typeahead/demos/config/typeahead-config.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,4 @@ | ||
<p>This typeahead shows a hint when the input matches because the default values have been customized.</p> | ||
|
||
<input type="text" class="form-control" [(ngModel)]="model" [ngbTypeahead]="search" /> | ||
|
36 changes: 36 additions & 0 deletions
36
demo/src/app/components/typeahead/demos/config/typeahead-config.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,36 @@ | ||
import {Component} from '@angular/core'; | ||
import {Observable} from 'rxjs/Rx'; | ||
import {NgbTypeaheadConfig} from '@ng-bootstrap/ng-bootstrap'; | ||
import 'rxjs/add/operator/map'; | ||
import 'rxjs/add/operator/debounceTime'; | ||
import 'rxjs/add/operator/distinctUntilChanged'; | ||
|
||
const states = ['Alabama', 'Alaska', 'American Samoa', 'Arizona', 'Arkansas', 'California', 'Colorado', | ||
'Connecticut', 'Delaware', 'District Of Columbia', 'Federated States Of Micronesia', 'Florida', 'Georgia', | ||
'Guam', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', | ||
'Marshall Islands', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', | ||
'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', | ||
'Northern Mariana Islands', 'Ohio', 'Oklahoma', 'Oregon', 'Palau', 'Pennsylvania', 'Puerto Rico', 'Rhode Island', | ||
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virgin Islands', 'Virginia', | ||
'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']; | ||
|
||
@Component({ | ||
selector: 'ngbd-typeahead-config', | ||
templateUrl: './typeahead-config.html', | ||
styles: [`.form-control { width: 300px; }`], | ||
providers: [NgbTypeaheadConfig] // add NgbTypeaheadConfig to the component providers | ||
}) | ||
export class NgbdTypeaheadConfig { | ||
|
||
constructor(config: NgbTypeaheadConfig) { | ||
// customize default values of typeaheads used by this component tree | ||
config.showHint = true; | ||
} | ||
|
||
search = (text$: Observable<string>) => | ||
text$ | ||
.debounceTime(200) | ||
.distinctUntilChanged() | ||
.map(term => term.length < 2 ? [] | ||
: states.filter(v => v.toLowerCase().startsWith(term.toLocaleLowerCase())).splice(0, 10)); | ||
} |
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
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,9 @@ | ||
import {NgbTypeaheadConfig} from './typeahead-config'; | ||
|
||
describe('ngb-typehead-config', () => { | ||
it('should have sensible default values', () => { | ||
const config = new NgbTypeaheadConfig(); | ||
|
||
expect(config.showHint).toBe(false); | ||
}); | ||
}); |
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,11 @@ | ||
import {Injectable} from '@angular/core'; | ||
|
||
/** | ||
* Configuration service for the NgbTypeahead component. | ||
* You can inject this service, typically in your root component, and customize the values of its properties in | ||
* order to provide default values for all the typeaheads used in the application. | ||
*/ | ||
@Injectable() | ||
export class NgbTypeaheadConfig { | ||
showHint = false; | ||
} |
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