From ef2b44f215142d80c40ea4ebc1fb05a90ed01e76 Mon Sep 17 00:00:00 2001 From: Austin McDaniel Date: Sat, 15 Jul 2017 12:04:27 -0500 Subject: [PATCH] docs(demos): autocomplete examples --- src/lib/autocomplete/autocomplete.md | 78 +--------------- .../autocomplete-display-example.css | 7 ++ .../autocomplete-display-example.html | 11 +++ .../autocomplete-display-example.ts | 47 ++++++++++ .../autocomplete-filter-example.css | 7 ++ .../autocomplete-filter-example.html | 11 +++ .../autocomplete-filter-example.ts | 38 ++++++++ .../autocomplete-overview-example.css | 7 ++ .../autocomplete-overview-example.html | 28 ++++-- .../autocomplete-overview-example.ts | 90 +++++++------------ .../autocomplete-simple-example.css | 7 ++ .../autocomplete-simple-example.html | 11 +++ .../autocomplete-simple-example.ts | 22 +++++ src/material-examples/example-module.ts | 26 +++++- 14 files changed, 250 insertions(+), 140 deletions(-) create mode 100644 src/material-examples/autocomplete-display/autocomplete-display-example.css create mode 100644 src/material-examples/autocomplete-display/autocomplete-display-example.html create mode 100644 src/material-examples/autocomplete-display/autocomplete-display-example.ts create mode 100644 src/material-examples/autocomplete-filter/autocomplete-filter-example.css create mode 100644 src/material-examples/autocomplete-filter/autocomplete-filter-example.html create mode 100644 src/material-examples/autocomplete-filter/autocomplete-filter-example.ts create mode 100644 src/material-examples/autocomplete-simple/autocomplete-simple-example.css create mode 100644 src/material-examples/autocomplete-simple/autocomplete-simple-example.html create mode 100644 src/material-examples/autocomplete-simple/autocomplete-simple-example.ts diff --git a/src/lib/autocomplete/autocomplete.md b/src/lib/autocomplete/autocomplete.md index 4ebe6fc295f0..d7c23e70db91 100644 --- a/src/lib/autocomplete/autocomplete.md +++ b/src/lib/autocomplete/autocomplete.md @@ -43,6 +43,8 @@ local template variable (here we called it "auto"), and binding that variable to ``` + + ### Adding a custom filter At this point, the autocomplete panel should be toggleable on focus and options should be selectable. But if we want @@ -61,41 +63,7 @@ Below we are also priming our value change stream with `null` so that the option This is especially helpful for screenreader users if you're using a non-standard filter that doesn't limit matches to the beginning of the string. -*my-comp.ts* -```ts -class MyComp { - myControl = new FormControl(); - options = [ - 'One', - 'Two', - 'Three' - ]; - filteredOptions: Observable; - - ngOnInit() { - this.filteredOptions = this.myControl.valueChanges - .startWith(null) - .map(val => val ? this.filter(val) : this.options.slice()); - } - - filter(val: string): string[] { - return this.options.filter(option => new RegExp(`^${val}`, 'gi').test(option)); - } -} -``` - -*my-comp.html* -```html - - - - - - - {{ option }} - - -``` + ### Setting separate control and display values @@ -107,45 +75,7 @@ the option's string properties. To make this work, create a function on your component class that maps the control value to the desired display value. Then bind it to the autocomplete's `displayWith` property. -```html - - - - - - - {{ option.name }} - - -``` - -*my-comp.ts* -```ts -class MyComp { - myControl = new FormControl(); - options = [ - new User('Mary'), - new User('Shelley'), - new User('Igor') - ]; - filteredOptions: Observable; - - ngOnInit() { - this.filteredOptions = this.myControl.valueChanges - .startWith(null) - .map(user => user && typeof user === 'object' ? user.name : user) - .map(name => name ? this.filter(name) : this.options.slice()); - } - - filter(name: string): User[] { - return this.options.filter(option => new RegExp(`^${name}`, 'gi').test(option.name)); - } - - displayFn(user: User): string { - return user ? user.name : user; - } -} -``` + #### Keyboard interaction: diff --git a/src/material-examples/autocomplete-display/autocomplete-display-example.css b/src/material-examples/autocomplete-display/autocomplete-display-example.css new file mode 100644 index 000000000000..269cb291ad0c --- /dev/null +++ b/src/material-examples/autocomplete-display/autocomplete-display-example.css @@ -0,0 +1,7 @@ +.example-form { + width: 500px; +} + +.example-full-width { + width: 100%; +} diff --git a/src/material-examples/autocomplete-display/autocomplete-display-example.html b/src/material-examples/autocomplete-display/autocomplete-display-example.html new file mode 100644 index 000000000000..5eee551c023e --- /dev/null +++ b/src/material-examples/autocomplete-display/autocomplete-display-example.html @@ -0,0 +1,11 @@ +
+ + + + + + + {{ option.name }} + + +
diff --git a/src/material-examples/autocomplete-display/autocomplete-display-example.ts b/src/material-examples/autocomplete-display/autocomplete-display-example.ts new file mode 100644 index 000000000000..97c722813d6a --- /dev/null +++ b/src/material-examples/autocomplete-display/autocomplete-display-example.ts @@ -0,0 +1,47 @@ +import {Component} from '@angular/core'; +import {FormControl} from '@angular/forms'; +import {Observable} from 'rxjs/Observable'; +import 'rxjs/add/operator/startWith'; +import 'rxjs/add/operator/map'; + +export class User { + constructor(public name: string) { } +} + +/** + * @title Display value autocomplete + */ +@Component({ + selector: 'autocomplete-display-example', + templateUrl: 'autocomplete-display-example.html', + styleUrls: ['autocomplete-display-example.css'] +}) +export class AutocompleteDisplayExample { + + myControl = new FormControl(); + + options = [ + new User('Mary'), + new User('Shelley'), + new User('Igor') + ]; + + filteredOptions: Observable; + + ngOnInit() { + this.filteredOptions = this.myControl.valueChanges + .startWith(null) + .map(user => user && typeof user === 'object' ? user.name : user) + .map(name => name ? this.filter(name) : this.options.slice()); + } + + filter(name: string): User[] { + return this.options.filter(option => + option.name.toLowerCase().indexOf(name.toLowerCase()) === 0); + } + + displayFn(user: User): string { + return user ? user.name : user; + } + +} diff --git a/src/material-examples/autocomplete-filter/autocomplete-filter-example.css b/src/material-examples/autocomplete-filter/autocomplete-filter-example.css new file mode 100644 index 000000000000..269cb291ad0c --- /dev/null +++ b/src/material-examples/autocomplete-filter/autocomplete-filter-example.css @@ -0,0 +1,7 @@ +.example-form { + width: 500px; +} + +.example-full-width { + width: 100%; +} diff --git a/src/material-examples/autocomplete-filter/autocomplete-filter-example.html b/src/material-examples/autocomplete-filter/autocomplete-filter-example.html new file mode 100644 index 000000000000..558fcb3f5893 --- /dev/null +++ b/src/material-examples/autocomplete-filter/autocomplete-filter-example.html @@ -0,0 +1,11 @@ +
+ + + + + + + {{ option }} + + +
diff --git a/src/material-examples/autocomplete-filter/autocomplete-filter-example.ts b/src/material-examples/autocomplete-filter/autocomplete-filter-example.ts new file mode 100644 index 000000000000..dde59515e0db --- /dev/null +++ b/src/material-examples/autocomplete-filter/autocomplete-filter-example.ts @@ -0,0 +1,38 @@ +import {Component} from '@angular/core'; +import {FormControl} from '@angular/forms'; +import {Observable} from 'rxjs/Observable'; +import 'rxjs/add/operator/startWith'; +import 'rxjs/add/operator/map'; + +/** + * @title Filter autocomplete + */ +@Component({ + selector: 'autocomplete-filter-example', + templateUrl: 'autocomplete-filter-example.html', + styleUrls: ['autocomplete-filter-example.css'] +}) +export class AutocompleteFilterExample { + + myControl: FormControl = new FormControl(); + + options = [ + 'One', + 'Two', + 'Three' + ]; + + filteredOptions: Observable; + + ngOnInit() { + this.filteredOptions = this.myControl.valueChanges + .startWith(null) + .map(val => val ? this.filter(val) : this.options.slice()); + } + + filter(val: string): string[] { + return this.options.filter(option => + option.toLowerCase().indexOf(val.toLowerCase()) === 0); + } + +} diff --git a/src/material-examples/autocomplete-overview/autocomplete-overview-example.css b/src/material-examples/autocomplete-overview/autocomplete-overview-example.css index e69de29bb2d1..269cb291ad0c 100644 --- a/src/material-examples/autocomplete-overview/autocomplete-overview-example.css +++ b/src/material-examples/autocomplete-overview/autocomplete-overview-example.css @@ -0,0 +1,7 @@ +.example-form { + width: 500px; +} + +.example-full-width { + width: 100%; +} diff --git a/src/material-examples/autocomplete-overview/autocomplete-overview-example.html b/src/material-examples/autocomplete-overview/autocomplete-overview-example.html index e5217f9c2f52..a64853226d42 100644 --- a/src/material-examples/autocomplete-overview/autocomplete-overview-example.html +++ b/src/material-examples/autocomplete-overview/autocomplete-overview-example.html @@ -1,9 +1,21 @@ - - - +
+ + + - - - {{ state }} - - \ No newline at end of file + + + + {{ state.name }} | + Population: {{state.population}} + + + +
+ + + Disable Input? + +
diff --git a/src/material-examples/autocomplete-overview/autocomplete-overview-example.ts b/src/material-examples/autocomplete-overview/autocomplete-overview-example.ts index 0031b0fa9523..ee2a166b91a0 100644 --- a/src/material-examples/autocomplete-overview/autocomplete-overview-example.ts +++ b/src/material-examples/autocomplete-overview/autocomplete-overview-example.ts @@ -1,83 +1,59 @@ import {Component} from '@angular/core'; import {FormControl} from '@angular/forms'; +import {Observable} from 'rxjs/Observable'; import 'rxjs/add/operator/startWith'; import 'rxjs/add/operator/map'; /** - * @title Basic autocomplete + * @title Autocomplete overview */ @Component({ selector: 'autocomplete-overview-example', templateUrl: 'autocomplete-overview-example.html', + styleUrls: ['autocomplete-overview-example.css'] }) export class AutocompleteOverviewExample { stateCtrl: FormControl; - filteredStates: any; + filteredStates: Observable; - states = [ - 'Alabama', - 'Alaska', - 'Arizona', - 'Arkansas', - 'California', - 'Colorado', - 'Connecticut', - 'Delaware', - 'Florida', - 'Georgia', - 'Hawaii', - 'Idaho', - 'Illinois', - 'Indiana', - 'Iowa', - 'Kansas', - 'Kentucky', - 'Louisiana', - 'Maine', - 'Maryland', - 'Massachusetts', - 'Michigan', - 'Minnesota', - 'Mississippi', - 'Missouri', - 'Montana', - 'Nebraska', - 'Nevada', - 'New Hampshire', - 'New Jersey', - 'New Mexico', - 'New York', - 'North Carolina', - 'North Dakota', - 'Ohio', - 'Oklahoma', - 'Oregon', - 'Pennsylvania', - 'Rhode Island', - 'South Carolina', - 'South Dakota', - 'Tennessee', - 'Texas', - 'Utah', - 'Vermont', - 'Virginia', - 'Washington', - 'West Virginia', - 'Wisconsin', - 'Wyoming', + states: any[] = [ + { + name: 'Arkansas', + population: '2.978M', + // https://commons.wikimedia.org/wiki/File:Flag_of_Arkansas.svg + flag: 'https://upload.wikimedia.org/wikipedia/commons/9/9d/Flag_of_Arkansas.svg' + }, + { + name: 'California', + population: '39.14M', + // https://commons.wikimedia.org/wiki/File:Flag_of_California.svg + flag: 'https://upload.wikimedia.org/wikipedia/commons/0/01/Flag_of_California.svg' + }, + { + name: 'Florida', + population: '20.27M', + // https://commons.wikimedia.org/wiki/File:Flag_of_Florida.svg + flag: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Florida.svg' + }, + { + name: 'Texas', + population: '27.47M', + // https://commons.wikimedia.org/wiki/File:Flag_of_Texas.svg + flag: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Flag_of_Texas.svg' + } ]; constructor() { this.stateCtrl = new FormControl(); this.filteredStates = this.stateCtrl.valueChanges .startWith(null) - .map(name => this.filterStates(name)); + .map(state => state ? this.filterStates(state) : this.states.slice()); } - filterStates(val: string) { - return val ? this.states.filter(s => s.toLowerCase().indexOf(val.toLowerCase()) === 0) - : this.states; + filterStates(name: string) { + return this.states.filter(state => + state.name.toLowerCase().indexOf(name.toLowerCase()) === 0); } } diff --git a/src/material-examples/autocomplete-simple/autocomplete-simple-example.css b/src/material-examples/autocomplete-simple/autocomplete-simple-example.css new file mode 100644 index 000000000000..269cb291ad0c --- /dev/null +++ b/src/material-examples/autocomplete-simple/autocomplete-simple-example.css @@ -0,0 +1,7 @@ +.example-form { + width: 500px; +} + +.example-full-width { + width: 100%; +} diff --git a/src/material-examples/autocomplete-simple/autocomplete-simple-example.html b/src/material-examples/autocomplete-simple/autocomplete-simple-example.html new file mode 100644 index 000000000000..a96341236587 --- /dev/null +++ b/src/material-examples/autocomplete-simple/autocomplete-simple-example.html @@ -0,0 +1,11 @@ +
+ + + + + + + {{ option }} + + +
diff --git a/src/material-examples/autocomplete-simple/autocomplete-simple-example.ts b/src/material-examples/autocomplete-simple/autocomplete-simple-example.ts new file mode 100644 index 000000000000..c3f1bcb6dfd0 --- /dev/null +++ b/src/material-examples/autocomplete-simple/autocomplete-simple-example.ts @@ -0,0 +1,22 @@ +import {Component} from '@angular/core'; +import {FormControl} from '@angular/forms'; + +/** + * @title Simple autocomplete + */ +@Component({ + selector: 'autocomplete-simple-example', + templateUrl: 'autocomplete-simple-example.html', + styleUrls: ['autocomplete-simple-example.css'] +}) +export class AutocompleteSimpleExample { + + myControl: FormControl = new FormControl(); + + options = [ + 'One', + 'Two', + 'Three' + ]; + +} diff --git a/src/material-examples/example-module.ts b/src/material-examples/example-module.ts index 9850814f55b6..858eba2509a6 100644 --- a/src/material-examples/example-module.ts +++ b/src/material-examples/example-module.ts @@ -13,7 +13,10 @@ export interface LiveExample { selectorName?: string; } +import {AutocompleteDisplayExample} from './autocomplete-display/autocomplete-display-example'; +import {AutocompleteFilterExample} from './autocomplete-filter/autocomplete-filter-example'; import {AutocompleteOverviewExample} from './autocomplete-overview/autocomplete-overview-example'; +import {AutocompleteSimpleExample} from './autocomplete-simple/autocomplete-simple-example'; import {ButtonOverviewExample} from './button-overview/button-overview-example'; import {ButtonToggleExclusiveExample} from './button-toggle-exclusive/button-toggle-exclusive-example'; import {ButtonToggleOverviewExample} from './button-toggle-overview/button-toggle-overview-example'; @@ -81,12 +84,30 @@ import {TooltipOverviewExample} from './tooltip-overview/tooltip-overview-exampl import {TooltipPositionExample} from './tooltip-position/tooltip-position-example'; export const EXAMPLE_COMPONENTS = { + 'autocomplete-display': { + title: 'Display value autocomplete', + component: AutocompleteDisplayExample, + additionalFiles: null, + selectorName: null + }, + 'autocomplete-filter': { + title: 'Filter autocomplete', + component: AutocompleteFilterExample, + additionalFiles: null, + selectorName: null + }, 'autocomplete-overview': { - title: 'Basic autocomplete', + title: 'Autocomplete overview', component: AutocompleteOverviewExample, additionalFiles: null, selectorName: null }, + 'autocomplete-simple': { + title: 'Simple autocomplete', + component: AutocompleteSimpleExample, + additionalFiles: null, + selectorName: null + }, 'button-overview': { title: 'Basic buttons', component: ButtonOverviewExample, @@ -480,7 +501,10 @@ export const EXAMPLE_COMPONENTS = { }; export const EXAMPLE_LIST = [ + AutocompleteDisplayExample, + AutocompleteFilterExample, AutocompleteOverviewExample, + AutocompleteSimpleExample, ButtonOverviewExample, ButtonToggleExclusiveExample, ButtonToggleOverviewExample,