Skip to content

Commit

Permalink
feat: toggle dropdown when searchable is false. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad authored and anjmao committed Nov 26, 2017
1 parent b86d7a1 commit 2477717
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ng-select/ng-select.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div (click)="open()" class="ng-control">
<div (click)="searchable ? open() : toggle()" class="ng-control">
<div class="ng-value-container">
<div class="ng-placeholder" [hidden]="!showPlaceholder()">{{placeholder}}</div>
<ng-template #defaultLabelTemplate let-item="item">
Expand Down Expand Up @@ -40,7 +40,7 @@

<div class="ng-menu-outer">
<virtual-scroll role="listbox" class="ng-menu" [disabled]="disableVirtualScroll" [bufferAmount]="4" [items]="itemsList.filteredItems" (update)="viewPortItems = $event">
<div class="ng-option" role="option" (click)="toggle(item)" (mousedown)="$event.preventDefault()" (mouseover)="onItemHover(item)"
<div class="ng-option" role="option" (click)="toggleItem(item)" (mousedown)="$event.preventDefault()" (mouseover)="onItemHover(item)"
*ngFor="let item of viewPortItems"
[class.disabled]="item.disabled"
[class.selected]="item.selected"
Expand Down
21 changes: 21 additions & 0 deletions src/ng-select/ng-select.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,27 @@ describe('NgSelectComponent', function () {
expect(fixture.componentInstance.select.itemsList.filteredItems).toEqual(result);
}));

it('should toggle dropdown when searchable false', fakeAsync(() => {
fixture = createTestingModule(
NgSelectFilterTestCmp,
`<ng-select [items]="cities"
bindLabel="name"
[searchable]="false"
[(ngModel)]="selectedCity">
</ng-select>`);

const selectInput = fixture.debugElement.query(By.css('.ng-control'));
// open
selectInput.triggerEventHandler('click', { stopPropagation: () => { } });
tickAndDetectChanges(fixture);
expect(fixture.componentInstance.select.isOpen).toBe(true);

// close
selectInput.triggerEventHandler('click', { stopPropagation: () => { } });
tickAndDetectChanges(fixture);
expect(fixture.componentInstance.select.isOpen).toBe(false);
}));

it('should not filter when searchable false', fakeAsync(() => {
fixture = createTestingModule(
NgSelectFilterTestCmp,
Expand Down
12 changes: 10 additions & 2 deletions src/ng-select/ng-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
this.isDisabled = isDisabled;
}

toggle() {
if (!this.isOpen) {
this.open();
} else {
this.close();
}
}

open() {
if (this.isDisabled || this.isOpen) {
return;
Expand All @@ -251,7 +259,7 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
this.closeEvent.emit();
}

toggle(item: NgOption) {
toggleItem(item: NgOption) {
if (!item || item.disabled || this.isDisabled) {
return;
}
Expand Down Expand Up @@ -494,7 +502,7 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
private handleEnter($event: KeyboardEvent) {
if (this.isOpen) {
if (this.itemsList.markedItem) {
this.toggle(this.itemsList.markedItem);
this.toggleItem(this.itemsList.markedItem);
} else if (this.addTag) {
this.selectTag();
}
Expand Down

0 comments on commit 2477717

Please sign in to comment.