@@ -15,15 +15,15 @@ import {
15
15
OnInit ,
16
16
Optional ,
17
17
Output ,
18
- Renderer2 ,
18
+ Renderer2
19
19
} from '@angular/core' ;
20
-
20
+ import { DOCUMENT } from '@angular/common' ;
21
+ import { BooleanInput , coerceBooleanProperty } from '@angular/cdk/coercion' ;
21
22
import { Subscription } from 'rxjs' ;
23
+ import { createPopper , Instance , Options , Placement } from '@popperjs/core' ;
24
+
22
25
import { DropdownService } from '../dropdown.service' ;
23
26
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' ;
27
27
28
28
@Directive ( {
29
29
selector : '[cDropdownToggle]' ,
@@ -40,7 +40,6 @@ export class DropdownToggleDirective {
40
40
*/
41
41
@Input ( ) caret = true ;
42
42
43
- private _split = false ;
44
43
/**
45
44
* 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.
46
45
* @type boolean
@@ -49,10 +48,10 @@ export class DropdownToggleDirective {
49
48
set split ( value : boolean ) {
50
49
this . _split = coerceBooleanProperty ( value ) ;
51
50
}
52
-
53
51
get split ( ) : boolean {
54
52
return this . _split ;
55
53
}
54
+ private _split = false ;
56
55
57
56
constructor (
58
57
public elementRef : ElementRef ,
@@ -85,6 +84,7 @@ export class DropdownToggleDirective {
85
84
export class DropdownComponent implements AfterContentInit , OnDestroy , OnInit {
86
85
87
86
static ngAcceptInputType_dark : BooleanInput ;
87
+ static ngAcceptInputType_visible : BooleanInput ;
88
88
89
89
/**
90
90
* Set alignment of dropdown menu.
@@ -146,10 +146,11 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
146
146
*/
147
147
@Input ( )
148
148
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 ) ;
153
154
}
154
155
get visible ( ) : boolean {
155
156
return this . _visible ;
@@ -248,14 +249,8 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
248
249
}
249
250
250
251
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 ( ) ;
259
254
}
260
255
}
261
256
@@ -313,14 +308,30 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
313
308
const host = this . elementRef . nativeElement ;
314
309
this . listeners . push (
315
310
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 ) {
317
315
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
+ }
318
329
}
319
330
} )
320
331
) ;
321
332
this . listeners . push (
322
333
this . renderer . listen ( this . document , 'keyup' , ( event ) => {
323
- if ( event . key === 'Escape' ) {
334
+ if ( event . key === 'Escape' && this . autoClose !== false ) {
324
335
this . setVisibleState ( false ) ;
325
336
}
326
337
} )
0 commit comments