Skip to content

Commit 13cc538

Browse files
committed
Fix for #3960
1 parent bdd4fd5 commit 13cc538

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/shared/components/Contentful/SearchBar/SearchBar.jsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class SearchBarInner extends Component {
5555
this.getDropdownPopup = this.getDropdownPopup.bind(this);
5656
this.getSuggestionList = this.getSuggestionList.bind(this);
5757
this.handleSearchChange = this.handleSearchChange.bind(this);
58+
this.handleClickOutside = this.handleClickOutside.bind(this);
5859
// using debounce to avoid processing or requesting too much
5960
this.updateSuggestionListWithNewSearch = _.debounce(
6061
this.updateSuggestionListWithNewSearch.bind(this), 400,
@@ -66,6 +67,10 @@ export class SearchBarInner extends Component {
6667
this.apiService = getService({ spaceName: 'EDU' });
6768
}
6869

70+
componentWillUnmount() {
71+
document.removeEventListener('mousedown', this.handleClickOutside);
72+
}
73+
6974
/**
7075
* Set the search field ref
7176
*/
@@ -317,6 +322,13 @@ export class SearchBarInner extends Component {
317322
));
318323
}
319324

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+
320332
/**
321333
* Update size of popup search result
322334
*/
@@ -453,14 +465,10 @@ export class SearchBarInner extends Component {
453465
ref={this.setSearchFieldRef}
454466
type="text"
455467
placeholder="Search..."
456-
onBlur={() => {
457-
_.delay(() => {
458-
this.setState({ isShowSuggestion: false });
459-
}, 100);
460-
}}
461468
onFocus={(e) => {
462469
this.updateSuggestionListWithNewSearch(e.target.value);
463470
this.setState({ isShowSuggestion: true, isShowFilterPopup: false });
471+
document.addEventListener('mousedown', this.handleClickOutside);
464472
}}
465473
onChange={this.handleSearchChange}
466474
/>

0 commit comments

Comments
 (0)