diff --git a/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.html b/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.html new file mode 100644 index 0000000000..7c34ba691e --- /dev/null +++ b/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.html @@ -0,0 +1,11 @@ +

Prevents manual text entry and updates the model only when an item from suggestions is selected

+ + + +
+
Model: {{ model | json }}
diff --git a/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.module.ts b/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.module.ts new file mode 100644 index 0000000000..a14b786690 --- /dev/null +++ b/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.module.ts @@ -0,0 +1,16 @@ +import {NgModule} from '@angular/core'; +import {FormsModule} from '@angular/forms'; +import {BrowserModule} from '@angular/platform-browser'; +import {NgbModule} from '@ng-bootstrap/ng-bootstrap'; + +import {NgbdTypeaheadPreventManualEntry} from './typeahead-prevent-manual-entry'; + + +@NgModule({ + imports: [BrowserModule, FormsModule, NgbModule], + declarations: [NgbdTypeaheadPreventManualEntry], + exports: [NgbdTypeaheadPreventManualEntry], + bootstrap: [NgbdTypeaheadPreventManualEntry] +}) +export class NgbdTypeaheadPreventManualEntryModule { +} diff --git a/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.ts b/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.ts new file mode 100644 index 0000000000..9da203a5a4 --- /dev/null +++ b/demo/src/app/components/typeahead/demos/prevent-manual-entry/typeahead-prevent-manual-entry.ts @@ -0,0 +1,86 @@ +import {Component} from '@angular/core'; +import {Observable} from 'rxjs'; +import {debounceTime, distinctUntilChanged, map, filter} from 'rxjs/operators'; + +type State = {id: number, name: string}; + +const states: State[] = [ + {id: 0, name: 'Alabama'}, + {id: 1, name: 'Alaska'}, + {id: 2, name: 'American Samoa'}, + {id: 3, name: 'Arizona'}, + {id: 4, name: 'Arkansas'}, + {id: 5, name: 'California'}, + {id: 6, name: 'Colorado'}, + {id: 7, name: 'Connecticut'}, + {id: 8, name: 'Delaware'}, + {id: 9, name: 'District Of Columbia'}, + {id: 10, name: 'Federated States Of Micronesia'}, + {id: 11, name: 'Florida'}, + {id: 12, name: 'Georgia'}, + {id: 13, name: 'Guam'}, + {id: 14, name: 'Hawaii'}, + {id: 15, name: 'Idaho'}, + {id: 16, name: 'Illinois'}, + {id: 17, name: 'Indiana'}, + {id: 18, name: 'Iowa'}, + {id: 19, name: 'Kansas'}, + {id: 20, name: 'Kentucky'}, + {id: 21, name: 'Louisiana'}, + {id: 22, name: 'Maine'}, + {id: 23, name: 'Marshall Islands'}, + {id: 24, name: 'Maryland'}, + {id: 25, name: 'Massachusetts'}, + {id: 26, name: 'Michigan'}, + {id: 27, name: 'Minnesota'}, + {id: 28, name: 'Mississippi'}, + {id: 29, name: 'Missouri'}, + {id: 30, name: 'Montana'}, + {id: 31, name: 'Nebraska'}, + {id: 32, name: 'Nevada'}, + {id: 33, name: 'New Hampshire'}, + {id: 34, name: 'New Jersey'}, + {id: 35, name: 'New Mexico'}, + {id: 36, name: 'New York'}, + {id: 37, name: 'North Carolina'}, + {id: 38, name: 'North Dakota'}, + {id: 39, name: 'Northern Mariana Islands'}, + {id: 40, name: 'Ohio'}, + {id: 41, name: 'Oklahoma'}, + {id: 42, name: 'Oregon'}, + {id: 43, name: 'Palau'}, + {id: 44, name: 'Pennsylvania'}, + {id: 45, name: 'Puerto Rico'}, + {id: 46, name: 'Rhode Island'}, + {id: 47, name: 'South Carolina'}, + {id: 48, name: 'South Dakota'}, + {id: 49, name: 'Tennessee'}, + {id: 50, name: 'Texas'}, + {id: 51, name: 'Utah'}, + {id: 52, name: 'Vermont'}, + {id: 53, name: 'Virgin Islands'}, + {id: 54, name: 'Virginia'}, + {id: 55, name: 'Washington'}, + {id: 56, name: 'West Virginia'}, + {id: 57, name: 'Wisconsin'}, + {id: 58, name: 'Wyoming'} +]; + +@Component({ + selector: 'ngbd-typeahead-prevent-manual-entry', + templateUrl: './typeahead-prevent-manual-entry.html', + styles: [`.form-control { width: 300px; }`] +}) +export class NgbdTypeaheadPreventManualEntry { + public model: State; + + formatter = (state: State) => state.name; + + search = (text$: Observable) => text$.pipe( + debounceTime(200), + distinctUntilChanged(), + filter(term => term.length >= 2), + map(term => states.filter(state => new RegExp(term, 'mi').test(state.name)).slice(0, 10)) + ) + +} diff --git a/demo/src/app/components/typeahead/typeahead.module.ts b/demo/src/app/components/typeahead/typeahead.module.ts index 020f61ef23..2210639ebc 100644 --- a/demo/src/app/components/typeahead/typeahead.module.ts +++ b/demo/src/app/components/typeahead/typeahead.module.ts @@ -1,22 +1,24 @@ -import { NgModule } from '@angular/core'; +import {NgModule} from '@angular/core'; -import { NgbdSharedModule } from '../../shared'; -import { ComponentWrapper } from '../../shared/component-wrapper/component-wrapper.component'; -import { NgbdComponentsSharedModule, NgbdDemoList } from '../shared'; -import { NgbdApiPage } from '../shared/api-page/api.component'; -import { NgbdExamplesPage } from '../shared/examples-page/examples.component'; -import { NgbdTypeaheadBasic } from './demos/basic/typeahead-basic'; -import { NgbdTypeaheadBasicModule } from './demos/basic/typeahead-basic.module'; -import { NgbdTypeaheadConfig } from './demos/config/typeahead-config'; -import { NgbdTypeaheadConfigModule } from './demos/config/typeahead-config.module'; -import { NgbdTypeaheadFocus } from './demos/focus/typeahead-focus'; -import { NgbdTypeaheadFocusModule } from './demos/focus/typeahead-focus.module'; -import { NgbdTypeaheadFormat } from './demos/format/typeahead-format'; -import { NgbdTypeaheadFormatModule } from './demos/format/typeahead-format.module'; -import { NgbdTypeaheadHttp } from './demos/http/typeahead-http'; -import { NgbdTypeaheadHttpModule } from './demos/http/typeahead-http.module'; -import { NgbdTypeaheadTemplate } from './demos/template/typeahead-template'; -import { NgbdTypeaheadTemplateModule } from './demos/template/typeahead-template.module'; +import {NgbdSharedModule} from '../../shared'; +import {ComponentWrapper} from '../../shared/component-wrapper/component-wrapper.component'; +import {NgbdComponentsSharedModule, NgbdDemoList} from '../shared'; +import {NgbdApiPage} from '../shared/api-page/api.component'; +import {NgbdExamplesPage} from '../shared/examples-page/examples.component'; +import {NgbdTypeaheadBasic} from './demos/basic/typeahead-basic'; +import {NgbdTypeaheadBasicModule} from './demos/basic/typeahead-basic.module'; +import {NgbdTypeaheadConfig} from './demos/config/typeahead-config'; +import {NgbdTypeaheadConfigModule} from './demos/config/typeahead-config.module'; +import {NgbdTypeaheadFocus} from './demos/focus/typeahead-focus'; +import {NgbdTypeaheadFocusModule} from './demos/focus/typeahead-focus.module'; +import {NgbdTypeaheadFormat} from './demos/format/typeahead-format'; +import {NgbdTypeaheadFormatModule} from './demos/format/typeahead-format.module'; +import {NgbdTypeaheadHttp} from './demos/http/typeahead-http'; +import {NgbdTypeaheadHttpModule} from './demos/http/typeahead-http.module'; +import {NgbdTypeaheadTemplate} from './demos/template/typeahead-template'; +import {NgbdTypeaheadTemplateModule} from './demos/template/typeahead-template.module'; +import {NgbdTypeaheadPreventManualEntry} from './demos/prevent-manual-entry/typeahead-prevent-manual-entry'; +import {NgbdTypeaheadPreventManualEntryModule} from './demos/prevent-manual-entry/typeahead-prevent-manual-entry.module'; const DEMOS = { basic: { @@ -49,6 +51,12 @@ const DEMOS = { code: require('!!raw-loader!./demos/template/typeahead-template').default, markup: require('!!raw-loader!./demos/template/typeahead-template.html').default }, + 'prevent-manual-entry': { + title: 'Prevent manual entry', + type: NgbdTypeaheadPreventManualEntry, + code: require('!!raw-loader!./demos/prevent-manual-entry/typeahead-prevent-manual-entry').default, + markup: require('!!raw-loader!./demos/prevent-manual-entry/typeahead-prevent-manual-entry.html').default + }, config: { title: 'Global configuration of typeaheads', type: NgbdTypeaheadConfig, @@ -58,31 +66,20 @@ const DEMOS = { }; export const ROUTES = [ - { path: '', pathMatch: 'full', redirectTo: 'examples' }, - { + {path: '', pathMatch: 'full', redirectTo: 'examples'}, { path: '', component: ComponentWrapper, - children: [ - { path: 'examples', component: NgbdExamplesPage }, - { path: 'api', component: NgbdApiPage } - ] + children: [{path: 'examples', component: NgbdExamplesPage}, {path: 'api', component: NgbdApiPage}] } ]; @NgModule({ imports: [ - NgbdSharedModule, - NgbdComponentsSharedModule, - NgbdTypeaheadFormatModule, - NgbdTypeaheadHttpModule, - NgbdTypeaheadBasicModule, - NgbdTypeaheadFocusModule, - NgbdTypeaheadTemplateModule, - NgbdTypeaheadConfigModule + NgbdSharedModule, NgbdComponentsSharedModule, NgbdTypeaheadFormatModule, NgbdTypeaheadHttpModule, + NgbdTypeaheadBasicModule, NgbdTypeaheadFocusModule, NgbdTypeaheadTemplateModule, NgbdTypeaheadConfigModule, + NgbdTypeaheadPreventManualEntryModule ] }) export class NgbdTypeaheadModule { - constructor(demoList: NgbdDemoList) { - demoList.register('typeahead', DEMOS); - } + constructor(demoList: NgbdDemoList) { demoList.register('typeahead', DEMOS); } }