From 2e92c17f998fb141d9e5c550f3a3c29efcf87d19 Mon Sep 17 00:00:00 2001 From: dan-searle Date: Mon, 28 Jan 2019 16:50:56 +0000 Subject: [PATCH] followedTopicIds prop is expected to be updated when followed topics change. --- components/x-topic-search/src/TopicSearch.jsx | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/components/x-topic-search/src/TopicSearch.jsx b/components/x-topic-search/src/TopicSearch.jsx index cb00d8cbd..284489135 100644 --- a/components/x-topic-search/src/TopicSearch.jsx +++ b/components/x-topic-search/src/TopicSearch.jsx @@ -14,25 +14,38 @@ class TopicSearch extends Component { this.maxSuggestions = props.maxSuggestions || 5; this.apiUrl = props.apiUrl; this.getSuggestions = debounce(getSuggestions, 150); + this.outsideEvents = ['focusout', 'focusin', 'click']; this.handleInputChange = this.handleInputChange.bind(this); this.handleInputClickOrFocus = this.handleInputClickOrFocus.bind(this); + this.handleInteractionOutside = this.handleInteractionOutside.bind(this); this.state = { - followedTopicIds: [], searchTerm: '', showResult: false, followedSuggestions: [], - unFollowedSuggestions: [] + unfollowedSuggestions: [] }; } + componentDidMount() { + this.outsideEvents.forEach(action => { + document.body.addEventListener(action, this.handleInteractionOutside); + }); + } + + componentWillUnmount() { + this.outsideEvents.forEach(action => { + document.body.removeEventListener(action, this.handleInteractionOutside); + }); + } + handleInputChange(event) { const searchTerm = event.target.value.trim(); this.setState({ searchTerm }); if (searchTerm.length >= this.minSearchLength) { - this.getSuggestions(searchTerm, this.maxSuggestions, this.apiUrl, this.state.followedTopicIds) + this.getSuggestions(searchTerm, this.maxSuggestions, this.apiUrl, this.props.followedTopicIds) .then(({ followedSuggestions, unfollowedSuggestions }) => { this.setState({ followedSuggestions, @@ -52,18 +65,28 @@ class TopicSearch extends Component { } } + handleInteractionOutside(event) { + if (!this.rootEl.contains(event.target)) { + this.setState({ + showResult: false + }); + } + } + handleInputClickOrFocus() { - console.log('handleInputClickOrFocus'); - // this.setState({ - // showResult: true - // }); + if (this.state.searchTerm.length >= this.minSearchLength) { + this.setState({ + showResult: true + }); + } } render() { - const { csrfToken, followedSuggestions, followedTopicIds, isLoading, searchTerm, showResult, unfollowedSuggestions } = this.state; + const { followedTopicIds } = this.props; + const { csrfToken, followedSuggestions, searchTerm, showResult, unfollowedSuggestions } = this.state; return ( -
+
this.rootEl = el}>

Search for topics, authors, companies, or other areas of interest

@@ -84,7 +107,7 @@ class TopicSearch extends Component { />
- { showResult && !isLoading && + { showResult &&