Skip to content

Commit

Permalink
refactor(autcomplete): simplify sample; add exact match logic #3585
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipata committed Mar 5, 2019
1 parent 4755c24 commit 721af1f
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/app/autocomplete/autocomplete.sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,18 @@ import { FormBuilder, FormControl, Validators, FormGroup } from '@angular/forms'
import { worldInfo, attractions } from './data';
import { IgxDialogComponent } from 'igniteui-angular';

@Pipe({ name: 'contains' })
export class AutocompletePipeContains implements PipeTransform {

public transform = (items: any[], term = '') => this.filterItems(items, term);

protected filterItems = (items: any[], term: string) =>
items.filter((item) =>
(item.name ? item.name : item).toString().toLowerCase().indexOf(term.toString().toLowerCase()) > -1)
}

@Pipe({ name: 'groupContains' })
export class AutocompleteGroupPipeContains extends AutocompletePipeContains implements PipeTransform {

public transform = (continents: any[], term = '') => this.filterContinents(continents, term);

private filterContinents = (countries: any[], term = '') =>
countries.filter((continent) => this.filterItems(continent.countries, term).length > 0)
}
@Component({
selector: 'app-autocomplete-sample',
styleUrls: ['autocomplete.sample.css'],
templateUrl: `autocomplete.sample.html`,
providers: [AutocompletePipeContains, AutocompleteGroupPipeContains]
templateUrl: `autocomplete.sample.html`
})
export class AutocompleteSampleComponent {
@ViewChild('alert', { read: IgxDialogComponent }) public alert: IgxDialogComponent;
public travel: FormGroup;
worldInfo;
attractions;

constructor(fb: FormBuilder,
public containsPipe: AutocompletePipeContains,
public containsGroupPipe: AutocompleteGroupPipeContains) {
constructor(fb: FormBuilder) {
this.worldInfo = worldInfo;
this.attractions = attractions;

Expand All @@ -46,8 +25,8 @@ export class AutocompleteSampleComponent {
}

onSearch() {
if (this.containsGroupPipe.transform(this.worldInfo, this.travel.value.country).length > 0 &&
this.containsPipe.transform(this.attractions, this.travel.value.attraction).length > 0) {
if (filterGroupContains(this.worldInfo, this.travel.value.country, true).length > 0 &&
filterContains(this.attractions, this.travel.value.attraction, true).length > 0) {
this.alert.message = 'You can visit ' + (100 + Math.floor(Math.random() * 100)) + ' ' + this.travel.value.attraction +
' in ' + this.travel.value.country + '.';
} else {
Expand All @@ -56,3 +35,25 @@ export class AutocompleteSampleComponent {
this.alert.open();
}
}

@Pipe({ name: 'contains' })
export class AutocompletePipeContains implements PipeTransform {
public transform = (items: any[], term = '') => filterContains(items, term);
}

@Pipe({ name: 'groupContains' })
export class AutocompleteGroupPipeContains implements PipeTransform {
public transform = (continents: any[], term = '') => filterGroupContains(continents, term);
}

const filterContains = (items: any[], term: string, exactMatch = false): any[] => {
return items.filter((item) => {
const itm = (item.name ? item.name : item).toString().toLowerCase();
const trm = term.toString().toLowerCase();
return exactMatch ? itm === trm : itm.indexOf(trm) > -1;
});
};

const filterGroupContains = (groupItems: any[], term = '', exactMatch = false): any[] => {
return groupItems.filter((groupItem) => filterContains(groupItem.countries, term, exactMatch).length > 0);
};

0 comments on commit 721af1f

Please sign in to comment.