Skip to content

Commit 7ff54e7

Browse files
authored
fix: prevent dropdown from closing when querying (#3183)
Signed-off-by: Akshat Patel <[email protected]>
1 parent f8a7492 commit 7ff54e7

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/combobox/combobox.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,8 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
546546
this.elementRef.nativeElement.querySelector("input").focus();
547547
this.view.filterBy("");
548548
this.selected.emit(event.item);
549+
this.closeDropdown();
549550
}
550-
this.closeDropdown();
551551
}
552552
});
553553
// update the rest of combobox with any pre-selected items
@@ -691,8 +691,14 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
691691
});
692692
this.view.items = this.items;
693693
this.updatePills();
694-
// clearSelected can only fire on type=multi
695-
// so we just emit getSelected() (just in case there's any disabled but selected items)
694+
/**
695+
* @todo - In next major version update to the following:
696+
* const selected = this.type === "multi" ? this.view.getSelected() : undefined;
697+
*
698+
* Previously it returned an empty array even for type === 'single'
699+
* Also resolve #ref-1245723
700+
*/
701+
// On type=multi we just emit getSelected() (just in case there's any disabled but selected items)
696702
const selected = this.view.getSelected();
697703

698704
// in case there are disabled items they should be mapped according to itemValueKey

src/combobox/stories/app-mock-query-search.component.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,43 @@ import { Component } from "@angular/core";
66
<cds-combo-box
77
appendInline="true"
88
[items]="filterItems"
9-
(search)="onSearch($event)">
9+
(search)="onSearch($event)"
10+
(selected)="selected($event)">
1011
<cds-dropdown-list></cds-dropdown-list>
1112
</cds-combo-box>
1213
`
1314
})
1415
export class MockQueryCombobox {
1516
filterItems: any = [];
17+
currentlySelected: any;
1618

1719
onSearch() {
1820
// Call API or search through items list
1921
setTimeout(() => {
20-
this.filterItems = [
22+
const array = [
23+
{ content: `Random ${Math.random()}` },
2124
{ content: `Random ${Math.random()}` },
2225
{ content: `Random ${Math.random()}` },
2326
{ content: `Random ${Math.random()}` },
24-
{ content: `Random ${Math.random()}` }
2527
];
28+
29+
// Include current selected in the list to avoid auto clear
30+
if (this.currentlySelected) {
31+
array.push(this.currentlySelected)
32+
}
33+
this.filterItems = array;
2634
}, 1000);
2735
}
36+
37+
selected(event: any) {
38+
/**
39+
* #ref-1245723
40+
* Update this on major release
41+
*/
42+
if (Array.isArray(event) && !event.length) {
43+
this.currentlySelected = undefined;
44+
} else {
45+
this.currentlySelected = event;
46+
}
47+
}
2848
}

0 commit comments

Comments
 (0)