Skip to content

Commit ed8859a

Browse files
committed
fix(dropdown): autoClose inside click fails for dynamic content
1 parent b511885 commit ed8859a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
235235
return this.variant === 'input-group' ? {display: 'contents'} : {};
236236
}
237237

238+
private clickedTarget!: HTMLElement;
239+
240+
@HostListener('click', ['$event'])
241+
private onHostClick($event: MouseEvent): void {
242+
this.clickedTarget = $event.target as HTMLElement;
243+
}
244+
238245
dropdownStateSubscribe(subscribe: boolean = true): void {
239246
if (subscribe) {
240247
this.dropdownStateSubscription =
@@ -313,28 +320,24 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
313320
}
314321

315322
private setListeners(): void {
316-
const host = this.elementRef.nativeElement;
317323
this.listeners.push(
318324
this.renderer.listen(this.document, 'click', (event) => {
325+
const target = event.target as HTMLElement;
319326
if (this._toggler?.elementRef.nativeElement.contains(event.target)) {
320327
return;
321328
}
322329
if (this.autoClose === true) {
323330
this.setVisibleState(false);
324331
return;
325332
}
326-
if (!host.contains(event.target)) {
327-
if (this.autoClose === 'outside') {
333+
if (this.clickedTarget === target && this.autoClose === 'inside') {
328334
this.setVisibleState(false);
329335
return;
330336
}
331-
}
332-
if (this._menu.elementRef.nativeElement.contains(event.target)) {
333-
if (this.autoClose === 'inside') {
337+
if (this.clickedTarget !== target && this.autoClose === 'outside') {
334338
this.setVisibleState(false);
335339
return;
336340
}
337-
}
338341
})
339342
);
340343
this.listeners.push(

0 commit comments

Comments
 (0)