Skip to content

chore: enforce consistency in material-examples #11539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/lib/tree/data-source/flat-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {FlatTreeControl, TreeControl} from '@angular/cdk/tree';
import {BehaviorSubject, merge, Observable} from 'rxjs';
import {map, take} from 'rxjs/operators';


/**
* Tree flattener to convert a normal type of node to node with children & level information.
* Transform nested nodes of type `T` to flattened nodes of type `F`.
Expand Down Expand Up @@ -90,7 +89,7 @@ export class MatTreeFlattener<T, F> {
let currentExpand: boolean[] = [];
currentExpand[0] = true;

nodes.forEach((node) => {
nodes.forEach(node => {
let expand = true;
for (let i = 0; i <= this.getLevel(node); i++) {
expand = expand && currentExpand[i];
Expand Down Expand Up @@ -151,4 +150,3 @@ export class MatTreeFlatDataSource<T, F> extends DataSource<F> {
// no op
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{ option }}
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import {map, startWith} from 'rxjs/operators';
@Component({
selector: 'autocomplete-auto-active-first-option-example',
templateUrl: 'autocomplete-auto-active-first-option-example.html',
styleUrls: ['autocomplete-auto-active-first-option-example.css']
styleUrls: ['autocomplete-auto-active-first-option-example.css'],
})
export class AutocompleteAutoActiveFirstOptionExample implements OnInit {
myControl: FormControl = new FormControl();
options = ['One', 'Two', 'Three'];
myControl = new FormControl();
options: string[] = ['One', 'Two', 'Three'];
filteredOptions: Observable<string[]>;

ngOnInit() {
this.filteredOptions = this.myControl.valueChanges.pipe(
startWith(''),
map(val => this.filter(val))
map(value => this._filter(value))
);
}

filter(val: string): string[] {
return this.options.filter(option => option.toLowerCase().indexOf(val.toLowerCase()) === 0);
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();

return this.options.filter(option => option.toLowerCase().indexOf(filterValue) === 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<input type="text" placeholder="Assignee" aria-label="Assignee" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{ option.name }}
{{option.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {FormControl} from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';

export class User {
constructor(public name: string) { }
export interface User {
name: string;
}

/**
Expand All @@ -13,36 +13,33 @@ export class User {
@Component({
selector: 'autocomplete-display-example',
templateUrl: 'autocomplete-display-example.html',
styleUrls: ['autocomplete-display-example.css']
styleUrls: ['autocomplete-display-example.css'],
})
export class AutocompleteDisplayExample implements OnInit {

myControl = new FormControl();

options = [
new User('Mary'),
new User('Shelley'),
new User('Igor')
options: User[] = [
{name: 'Mary'},
{name: 'Shelley'},
{name: 'Igor'}
];

filteredOptions: Observable<User[]>;

ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith<string | User>(''),
map(value => typeof value === 'string' ? value : value.name),
map(name => name ? this.filter(name) : this.options.slice())
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 | undefined {
return user ? user.name : undefined;
}

private _filter(name: string): User[] {
const filterValue = name.toLowerCase();

return this.options.filter(option => option.name.toLowerCase().indexOf(filterValue) === 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{ option }}
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,24 @@ import {map, startWith} from 'rxjs/operators';
@Component({
selector: 'autocomplete-filter-example',
templateUrl: 'autocomplete-filter-example.html',
styleUrls: ['autocomplete-filter-example.css']
styleUrls: ['autocomplete-filter-example.css'],
})
export class AutocompleteFilterExample implements OnInit {

myControl: FormControl = new FormControl();

options = [
'One',
'Two',
'Three'
];

myControl = new FormControl();
options: string[] = ['One', 'Two', 'Three'];
filteredOptions: Observable<string[]>;

ngOnInit() {
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
map(val => this.filter(val))
map(value => this._filter(value))
);
}

filter(val: string): string[] {
return this.options.filter(option =>
option.toLowerCase().includes(val.toLowerCase()));
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();

return this.options.filter(option => option.toLowerCase().includes(filterValue));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<form [formGroup]="stateForm">
<mat-form-field>
<input type="text" matInput placeholder="States Group" formControlName="stateGroup" required [matAutocomplete]="autoGroup"/>
<input type="text" matInput placeholder="States Group" formControlName="stateGroup" required [matAutocomplete]="autoGroup">
<mat-autocomplete #autoGroup="matAutocomplete">
<mat-optgroup *ngFor="let group of stateGroupOptions | async" [label]="group.letter">
<mat-option *ngFor="let name of group.names" [value]="name">
{{ name }}
{{name}}
</mat-option>
</mat-optgroup>
</mat-autocomplete>
</mat-form-field>
</form>
</mat-form-field>
</form>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Component, OnInit} from '@angular/core';
import {FormGroup, FormBuilder} from '@angular/forms';
import {FormBuilder, FormGroup} from '@angular/forms';
import {Observable} from 'rxjs';
import {startWith, map} from 'rxjs/operators';

Expand All @@ -8,6 +8,12 @@ export interface StateGroup {
names: string[];
}

export const _filter = (opt: string[], value: string): string[] => {
const filterValue = value.toLowerCase();

return opt.filter(item => item.toLowerCase().indexOf(filterValue) === 0);
};

/**
* @title Option groups autocomplete
*/
Expand Down Expand Up @@ -84,28 +90,23 @@ export class AutocompleteOptgroupExample implements OnInit {

stateGroupOptions: Observable<StateGroup[]>;

constructor(private fb: FormBuilder) { }
constructor(private fb: FormBuilder) {}

ngOnInit() {
this.stateGroupOptions = this.stateForm.get('stateGroup')!.valueChanges
.pipe(
startWith(''),
map(val => this.filterGroup(val))
map(value => this._filterGroup(value))
);
}

filterGroup(val: string): StateGroup[] {
if (val) {
private _filterGroup(value: string): StateGroup[] {
if (value) {
return this.stateGroups
.map(group => ({ letter: group.letter, names: this._filter(group.names, val) }))
.map(group => ({letter: group.letter, names: _filter(group.names, value)}))
.filter(group => group.names.length > 0);
}

return this.stateGroups;
}

private _filter(opt: string[], val: string) {
const filterValue = val.toLowerCase();
return opt.filter(item => item.toLowerCase().startsWith(filterValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
.example-full-width {
width: 100%;
}

.example-option-img {
vertical-align: middle;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<input matInput placeholder="State" aria-label="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let state of filteredStates | async" [value]="state.name">
<img style="vertical-align:middle;" aria-hidden src="{{state.flag}}" height="25" />
<span>{{ state.name }}</span> |
<img class="example-option-img" aria-hidden [src]="state.flag" height="25">
<span>{{state.name}}</span> |
<small>Population: {{state.population}}</small>
</mat-option>
</mat-autocomplete>
</mat-form-field>

<br />
<br>

<mat-slide-toggle
[checked]="stateCtrl.disabled"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';

import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';

export class State {
constructor(public name: string, public population: string, public flag: string) { }
export interface State {
flag: string;
name: string;
population: string;
}

/**
Expand All @@ -14,11 +15,11 @@ export class State {
@Component({
selector: 'autocomplete-overview-example',
templateUrl: 'autocomplete-overview-example.html',
styleUrls: ['autocomplete-overview-example.css']
styleUrls: ['autocomplete-overview-example.css'],
})
export class AutocompleteOverviewExample {
stateCtrl: FormControl;
filteredStates: Observable<any[]>;
stateCtrl = new FormControl();
filteredStates: Observable<State[]>;

states: State[] = [
{
Expand Down Expand Up @@ -48,17 +49,16 @@ export class AutocompleteOverviewExample {
];

constructor() {
this.stateCtrl = new FormControl();
this.filteredStates = this.stateCtrl.valueChanges
.pipe(
startWith(''),
map(state => state ? this.filterStates(state) : this.states.slice())
map(state => state ? this._filterStates(state) : this.states.slice())
);
}

filterStates(name: string) {
return this.states.filter(state =>
state.name.toLowerCase().indexOf(name.toLowerCase()) === 0);
}
private _filterStates(value: string): State[] {
const filterValue = value.toLowerCase();

return this.states.filter(state => state.name.toLowerCase().indexOf(filterValue) === 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{ option }}
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@ import {FormControl} from '@angular/forms';
@Component({
selector: 'autocomplete-simple-example',
templateUrl: 'autocomplete-simple-example.html',
styleUrls: ['autocomplete-simple-example.css']
styleUrls: ['autocomplete-simple-example.css'],
})
export class AutocompleteSimpleExample {

myControl: FormControl = new FormControl();

options = [
'One',
'Two',
'Three'
];

myControl = new FormControl();
options: string[] = ['One', 'Two', 'Three'];
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {Component} from '@angular/core';


/**
* @title Badge overview
*/
@Component({
selector: 'badge-overview-example',
templateUrl: 'badge-overview-example.html',
styleUrls: ['badge-overview-example.css']
styleUrls: ['badge-overview-example.css'],
})
export class BadgeOverviewExample { }
export class BadgeOverviewExample {}
Loading