Skip to content

Commit 2477717

Browse files
aitboudadanjmao
authored andcommitted
feat: toggle dropdown when searchable is false. (#137)
1 parent b86d7a1 commit 2477717

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/ng-select/ng-select.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div (click)="open()" class="ng-control">
1+
<div (click)="searchable ? open() : toggle()" class="ng-control">
22
<div class="ng-value-container">
33
<div class="ng-placeholder" [hidden]="!showPlaceholder()">{{placeholder}}</div>
44
<ng-template #defaultLabelTemplate let-item="item">
@@ -40,7 +40,7 @@
4040

4141
<div class="ng-menu-outer">
4242
<virtual-scroll role="listbox" class="ng-menu" [disabled]="disableVirtualScroll" [bufferAmount]="4" [items]="itemsList.filteredItems" (update)="viewPortItems = $event">
43-
<div class="ng-option" role="option" (click)="toggle(item)" (mousedown)="$event.preventDefault()" (mouseover)="onItemHover(item)"
43+
<div class="ng-option" role="option" (click)="toggleItem(item)" (mousedown)="$event.preventDefault()" (mouseover)="onItemHover(item)"
4444
*ngFor="let item of viewPortItems"
4545
[class.disabled]="item.disabled"
4646
[class.selected]="item.selected"

src/ng-select/ng-select.component.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,27 @@ describe('NgSelectComponent', function () {
697697
expect(fixture.componentInstance.select.itemsList.filteredItems).toEqual(result);
698698
}));
699699

700+
it('should toggle dropdown when searchable false', fakeAsync(() => {
701+
fixture = createTestingModule(
702+
NgSelectFilterTestCmp,
703+
`<ng-select [items]="cities"
704+
bindLabel="name"
705+
[searchable]="false"
706+
[(ngModel)]="selectedCity">
707+
</ng-select>`);
708+
709+
const selectInput = fixture.debugElement.query(By.css('.ng-control'));
710+
// open
711+
selectInput.triggerEventHandler('click', { stopPropagation: () => { } });
712+
tickAndDetectChanges(fixture);
713+
expect(fixture.componentInstance.select.isOpen).toBe(true);
714+
715+
// close
716+
selectInput.triggerEventHandler('click', { stopPropagation: () => { } });
717+
tickAndDetectChanges(fixture);
718+
expect(fixture.componentInstance.select.isOpen).toBe(false);
719+
}));
720+
700721
it('should not filter when searchable false', fakeAsync(() => {
701722
fixture = createTestingModule(
702723
NgSelectFilterTestCmp,

src/ng-select/ng-select.component.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
231231
this.isDisabled = isDisabled;
232232
}
233233

234+
toggle() {
235+
if (!this.isOpen) {
236+
this.open();
237+
} else {
238+
this.close();
239+
}
240+
}
241+
234242
open() {
235243
if (this.isDisabled || this.isOpen) {
236244
return;
@@ -251,7 +259,7 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
251259
this.closeEvent.emit();
252260
}
253261

254-
toggle(item: NgOption) {
262+
toggleItem(item: NgOption) {
255263
if (!item || item.disabled || this.isDisabled) {
256264
return;
257265
}
@@ -494,7 +502,7 @@ export class NgSelectComponent implements OnInit, OnDestroy, OnChanges, AfterVie
494502
private handleEnter($event: KeyboardEvent) {
495503
if (this.isOpen) {
496504
if (this.itemsList.markedItem) {
497-
this.toggle(this.itemsList.markedItem);
505+
this.toggleItem(this.itemsList.markedItem);
498506
} else if (this.addTag) {
499507
this.selectTag();
500508
}

0 commit comments

Comments
 (0)