diff --git a/src/app/modules/ngx-magic-search/ngx-magic-search.component.ts b/src/app/modules/ngx-magic-search/ngx-magic-search.component.ts index 5973a88..cb99046 100644 --- a/src/app/modules/ngx-magic-search/ngx-magic-search.component.ts +++ b/src/app/modules/ngx-magic-search/ngx-magic-search.component.ts @@ -236,7 +236,7 @@ export class NgxMagicSearchComponent implements OnInit, OnChanges, DoCheck { this.filteredObj = this.facetsObj; for (i = 0; i < this.filteredObj.length; i++) { const facet = this.filteredObj[i]; - idx = facet.label.toLowerCase().indexOf(searchVal); + idx = facet.label.toLowerCase().indexOf(searchVal.toLowerCase()); if (idx > -1) { label = [ facet.label.substring(0, idx), @@ -252,6 +252,7 @@ export class NgxMagicSearchComponent implements OnInit, OnChanges, DoCheck { this.filteredObj = filtered; }, 0.1); } else { + this.filteredObj = []; this.textSearchEvent.emit(searchVal); this.hideMenu(); } @@ -262,7 +263,7 @@ export class NgxMagicSearchComponent implements OnInit, OnChanges, DoCheck { } for (i = 0; i < this.filteredOptions.length; i++) { const option = this.filteredOptions[i]; - idx = option.label.toLowerCase().indexOf(searchVal); + idx = option.label.toLowerCase().indexOf(searchVal.toLowerCase()); if (idx > -1) { label = [ option.label.substring(0, idx), @@ -277,6 +278,8 @@ export class NgxMagicSearchComponent implements OnInit, OnChanges, DoCheck { setTimeout(() => { this.filteredOptions = filtered; }, 0.1); + } else { + this.filteredObj = []; } } } @@ -426,11 +429,23 @@ export class NgxMagicSearchComponent implements OnInit, OnChanges, DoCheck { const searchVal = this.searchInput; const key = event.keyCode || event.charCode; if (key === 9) { // tab, so select facet if narrowed down to 1 - if (this.facetSelected === undefined) { - if (this.filteredObj.length !== 1) { return; } + if (this.facetSelected === undefined && this.filteredObj.length > 0) { this.facetClicked(0, this.filteredObj[0].name); + } else if (this.facetSelected === undefined && this.filteredObj.length === 0) { + // If there's nothing to select treat it as text search + for (let i = 0; i < this.currentSearch.length; i++) { + if (this.currentSearch[i].name.indexOf('text') === 0) { + this.currentSearch.splice(i, 1); + } + } + this.currentSearch.push({ 'name': 'text=' + searchVal, 'label': [this.strings.text, searchVal] }); + + this.hideMenu(); + this.searchInput = ''; + this.textSearchEvent.emit(searchVal); + this.textSearch = searchVal; } else { - if (this.filteredOptions === undefined || this.filteredOptions.length !== 1) { return; } + if (this.filteredOptions === undefined || this.filteredOptions.length === 0) { return; } this.optionClicked(0, this.filteredOptions[0].key); this.resetState(); }