Skip to content

Commit

Permalink
fix: don't reset items while selecting
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus committed Mar 14, 2018
1 parent 32e9bc8 commit 68b3726
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/ng-select/ng-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ describe('NgSelectComponent', function () {

fixture.componentInstance.selectedCity = fixture.componentInstance.cities[0];
tickAndDetectChanges(fixture);

const lastSelection: any = fixture.componentInstance.select.selectedItems[0];
expect(lastSelection.selected).toBeTruthy();

Expand Down Expand Up @@ -1206,14 +1206,14 @@ describe('NgSelectComponent', function () {
tickAndDetectChanges(fixture);
const loadingOption = fixture.debugElement.queryAll(By.css('.custom-loading'));
expect(loadingOption.length).toBe(1);


fixture.componentInstance.citiesLoading = false;
tickAndDetectChanges(fixture);
triggerKeyDownEvent(getNgSelectElement(fixture), KeyCode.Space);
tickAndDetectChanges(fixture);
const notFoundOptions = fixture.debugElement.queryAll(By.css('.custom-notfound'));
expect(notFoundOptions.length).toBe(1);
expect(notFoundOptions.length).toBe(1);
});
}));

Expand Down Expand Up @@ -1627,6 +1627,24 @@ describe('NgSelectComponent', function () {
expect(fixture.componentInstance.select.filterValue).toBe(null);
}));

it('should not reset items when selecting option', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectFilterTestCmp,
`<ng-select [items]="cities"
bindLabel="name"
[(ngModel)]="selectedCity"
[multiple]="true">
</ng-select>`);

const clearFilter = spyOn(fixture.componentInstance.select.itemsList, 'clearFilter');
tickAndDetectChanges(fixture);

fixture.componentInstance.select.filterValue = null;
selectOption(fixture, KeyCode.ArrowDown, 1);
tickAndDetectChanges(fixture);
expect(clearFilter).not.toHaveBeenCalled();
}));

it('should filter grouped items', fakeAsync(() => {
const fixture = createTestingModule(
NgSelectGroupingTestCmp,
Expand Down Expand Up @@ -1765,7 +1783,7 @@ describe('NgSelectComponent', function () {

fixture.componentInstance.selectedCityId = fixture.componentInstance.cities[1].id;
tickAndDetectChanges(fixture);

const select = fixture.componentInstance.select;
select.select(select.itemsList.items[0]);
tickAndDetectChanges(fixture);
Expand Down Expand Up @@ -2088,8 +2106,8 @@ function getNgSelectElement(fixture: ComponentFixture<any>): DebugElement {
function triggerKeyDownEvent(element: DebugElement, key: number): void {
element.triggerEventHandler('keydown', {
which: key,
preventDefault: () => {},
stopPropagation: () => {}
preventDefault: () => { },
stopPropagation: () => { }
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/ng-select/ng-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ export class NgSelectComponent implements OnDestroy, OnChanges, AfterViewInit, C
}

private _clearSearch() {
if (!this.filterValue) {
return;
}

this.filterValue = null;
this.itemsList.clearFilter();
}
Expand Down

0 comments on commit 68b3726

Please sign in to comment.