@@ -55,6 +55,7 @@ export class SearchBarInner extends Component {
55
55
this . getDropdownPopup = this . getDropdownPopup . bind ( this ) ;
56
56
this . getSuggestionList = this . getSuggestionList . bind ( this ) ;
57
57
this . handleSearchChange = this . handleSearchChange . bind ( this ) ;
58
+ this . handleClickOutside = this . handleClickOutside . bind ( this ) ;
58
59
// using debounce to avoid processing or requesting too much
59
60
this . updateSuggestionListWithNewSearch = _ . debounce (
60
61
this . updateSuggestionListWithNewSearch . bind ( this ) , 400 ,
@@ -66,6 +67,10 @@ export class SearchBarInner extends Component {
66
67
this . apiService = getService ( { spaceName : 'EDU' } ) ;
67
68
}
68
69
70
+ componentWillUnmount ( ) {
71
+ document . removeEventListener ( 'mousedown' , this . handleClickOutside ) ;
72
+ }
73
+
69
74
/**
70
75
* Set the search field ref
71
76
*/
@@ -317,6 +322,13 @@ export class SearchBarInner extends Component {
317
322
) ) ;
318
323
}
319
324
325
+ handleClickOutside ( e ) {
326
+ if ( this . popupSearchResultRef && ! this . popupSearchResultRef . contains ( e . target ) ) {
327
+ this . setState ( { isShowSuggestion : false } ) ;
328
+ document . removeEventListener ( 'mousedown' , this . handleClickOutside ) ;
329
+ }
330
+ }
331
+
320
332
/**
321
333
* Update size of popup search result
322
334
*/
@@ -453,14 +465,10 @@ export class SearchBarInner extends Component {
453
465
ref = { this . setSearchFieldRef }
454
466
type = "text"
455
467
placeholder = "Search..."
456
- onBlur = { ( ) => {
457
- _ . delay ( ( ) => {
458
- this . setState ( { isShowSuggestion : false } ) ;
459
- } , 100 ) ;
460
- } }
461
468
onFocus = { ( e ) => {
462
469
this . updateSuggestionListWithNewSearch ( e . target . value ) ;
463
470
this . setState ( { isShowSuggestion : true , isShowFilterPopup : false } ) ;
471
+ document . addEventListener ( 'mousedown' , this . handleClickOutside ) ;
464
472
} }
465
473
onChange = { this . handleSearchChange }
466
474
/>
0 commit comments