Skip to content

Commit 824085e

Browse files
committed
fix(Dropdown): add missing autoClose prop and behavior
1 parent aa65987 commit 824085e

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

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

+32-21
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import {
1515
OnInit,
1616
Optional,
1717
Output,
18-
Renderer2,
18+
Renderer2
1919
} from '@angular/core';
20-
20+
import { DOCUMENT } from '@angular/common';
21+
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
2122
import { Subscription } from 'rxjs';
23+
import { createPopper, Instance, Options, Placement } from '@popperjs/core';
24+
2225
import { DropdownService } from '../dropdown.service';
2326
import { DropdownMenuDirective } from '../dropdown-menu/dropdown-menu.directive';
24-
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
25-
import { createPopper, Instance, Options, Placement } from '@popperjs/core';
26-
import { DOCUMENT } from '@angular/common';
2727

2828
@Directive({
2929
selector: '[cDropdownToggle]',
@@ -40,7 +40,6 @@ export class DropdownToggleDirective {
4040
*/
4141
@Input() caret = true;
4242

43-
private _split = false;
4443
/**
4544
* Create split button dropdowns with virtually the same markup as single button dropdowns, but with the addition of `.dropdown-toggle-split` class for proper spacing around the dropdown caret.
4645
* @type boolean
@@ -49,10 +48,10 @@ export class DropdownToggleDirective {
4948
set split(value: boolean) {
5049
this._split = coerceBooleanProperty(value);
5150
}
52-
5351
get split(): boolean {
5452
return this._split;
5553
}
54+
private _split = false;
5655

5756
constructor(
5857
public elementRef: ElementRef,
@@ -85,6 +84,7 @@ export class DropdownToggleDirective {
8584
export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
8685

8786
static ngAcceptInputType_dark: BooleanInput;
87+
static ngAcceptInputType_visible: BooleanInput;
8888

8989
/**
9090
* Set alignment of dropdown menu.
@@ -146,10 +146,11 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
146146
*/
147147
@Input()
148148
set visible(value: boolean) {
149-
this.activeTrap = value;
150-
this._visible = value;
151-
value ? this.createPopperInstance() : this.destroyPopperInstance();
152-
this.visibleChange.emit(value);
149+
const _value = coerceBooleanProperty(value);
150+
this.activeTrap = _value;
151+
this._visible = _value;
152+
_value ? this.createPopperInstance() : this.destroyPopperInstance();
153+
this.visibleChange.emit(_value);
153154
}
154155
get visible(): boolean {
155156
return this._visible;
@@ -248,14 +249,8 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
248249
}
249250

250251
onClick(event: any): void {
251-
if (this.autoClose === true || this.autoClose === 'outside') {
252-
if (
253-
!this._toggler?.elementRef.nativeElement.contains(
254-
event.target?.closest('[cDropdownToggle]')
255-
)
256-
) {
257-
this.toggleDropdown();
258-
}
252+
if (!this._toggler?.elementRef.nativeElement.contains(event.target?.closest('[cDropdownToggle]'))) {
253+
this.toggleDropdown();
259254
}
260255
}
261256

@@ -313,14 +308,30 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
313308
const host = this.elementRef.nativeElement;
314309
this.listeners.push(
315310
this.renderer.listen(this.document, 'click', (event) => {
316-
if (!host.contains(event.target)) {
311+
if (this._toggler?.elementRef.nativeElement.contains(event.target)) {
312+
return;
313+
}
314+
if (this.autoClose === true) {
317315
this.setVisibleState(false);
316+
return;
317+
}
318+
if (!host.contains(event.target)) {
319+
if (this.autoClose === 'outside') {
320+
this.setVisibleState(false);
321+
return;
322+
}
323+
}
324+
if (this._menu.elementRef.nativeElement.contains(event.target)) {
325+
if (this.autoClose === 'inside') {
326+
this.setVisibleState(false);
327+
return;
328+
}
318329
}
319330
})
320331
);
321332
this.listeners.push(
322333
this.renderer.listen(this.document, 'keyup', (event) => {
323-
if (event.key === 'Escape') {
334+
if (event.key === 'Escape' && this.autoClose !== false) {
324335
this.setVisibleState(false);
325336
}
326337
})

0 commit comments

Comments
 (0)