@@ -19,7 +19,6 @@ import { ListItem } from "carbon-components-angular/dropdown";
1919import { NG_VALUE_ACCESSOR } from "@angular/forms" ;
2020import { filter } from "rxjs/operators" ;
2121import {
22- DocumentService ,
2322 getScrollableParents ,
2423 hasScrollableParents
2524} from "carbon-components-angular/utils" ;
@@ -389,7 +388,7 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
389388 * }
390389 * ```
391390 */
392- @Output ( ) submit = new EventEmitter < {
391+ @Output ( ) submit = new EventEmitter < {
393392 items : ListItem [ ] ,
394393 index : number ,
395394 value : {
@@ -425,6 +424,7 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
425424 /** used to update the displayValue */
426425 public selectedValue = "" ;
427426
427+ outsideClick = this . _outsideClick . bind ( this ) ;
428428 keyboardNav = this . _keyboardNav . bind ( this ) ;
429429 /**
430430 * controls whether the `drop-up` class is applied
@@ -448,7 +448,6 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
448448 */
449449 constructor (
450450 protected elementRef : ElementRef ,
451- protected documentService : DocumentService ,
452451 protected dropdownService : DropdownService ,
453452 protected i18n : I18n
454453 ) { }
@@ -464,7 +463,7 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
464463 this . updateSelected ( ) ;
465464 // If new items are added into the combobox while there is search input,
466465 // repeat the search. Search should only trigger for type 'single' when there is no value selected.
467- if ( this . type === "multi" || ( this . type === "single" && ! this . selectedValue ) ) {
466+ if ( this . type === "multi" || ( this . type === "single" && ! this . selectedValue ) ) {
468467 this . onSearch ( this . input . nativeElement . value , false ) ;
469468 }
470469 }
@@ -540,14 +539,6 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
540539 * Binds event handlers against the rendered view
541540 */
542541 ngAfterViewInit ( ) {
543- this . documentService . handleClick ( event => {
544- if ( ! this . elementRef . nativeElement . contains ( event . target ) &&
545- ! this . dropdownMenu . nativeElement . contains ( event . target ) ) {
546- if ( this . open ) {
547- this . closeDropdown ( ) ;
548- }
549- }
550- } ) ;
551542 // if appendInline is default valued (null) we should:
552543 // 1. if there are scrollable parents (not including body) don't append inline
553544 // this should also cover the case where the dropdown is in a modal
@@ -691,6 +682,8 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
691682 if ( ! this . appendInline ) {
692683 this . _appendToDropdown ( ) ;
693684 }
685+
686+ document . removeEventListener ( "click" , this . outsideClick , true ) ;
694687 }
695688
696689 /**
@@ -705,6 +698,8 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
705698 this . _appendToBody ( ) ;
706699 }
707700
701+ document . addEventListener ( "click" , this . outsideClick , true ) ;
702+
708703 // set the dropdown menu to drop up if it is near the bottom of the screen
709704 // setTimeout lets us do the calculations after it is visible in the DOM
710705 setTimeout ( ( ) => {
@@ -809,7 +804,7 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
809804 // this way focus will start on the next focusable item from the dropdown
810805 // not the top of the body!
811806 this . input . nativeElement . focus ( ) ;
812- this . input . nativeElement . dispatchEvent ( new KeyboardEvent ( "keydown" , { bubbles : true , cancelable : true , key : "Tab" } ) ) ;
807+ this . input . nativeElement . dispatchEvent ( new KeyboardEvent ( "keydown" , { bubbles : true , cancelable : true , key : "Tab" } ) ) ;
813808 this . closeDropdown ( ) ;
814809 }
815810 }
@@ -854,9 +849,21 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
854849 return false ;
855850 }
856851
852+ /**
853+ * Handles clicks outside of the `Dropdown` list.
854+ */
855+ _outsideClick ( event ) {
856+ if ( ! this . elementRef . nativeElement . contains ( event . target ) &&
857+ // if we're appendToBody the list isn't within the _elementRef,
858+ // so we've got to check if our target is possibly in there too.
859+ ! this . dropdownMenu . nativeElement . contains ( event . target ) ) {
860+ this . closeDropdown ( ) ;
861+ }
862+ }
863+
857864 protected updateSelected ( ) {
858865 const selected = this . view . getSelected ( ) ;
859- if ( this . type === "multi" ) {
866+ if ( this . type === "multi" ) {
860867 this . updatePills ( ) ;
861868 } else if ( selected ) {
862869 const value = selected [ 0 ] ? selected [ 0 ] . content : "" ;
0 commit comments