File size: 1,783 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from 'react';
import { searchUsers } from '../../util/user_api_util';
import SearchIndexItem from './search_index_item';

class UsersSearch extends React.Component {
  constructor(props) {
    super(props);
    this.indexItems = [];
    this.focus = false;
    this.searchField;
    this.toggleFocus = this.toggleFocus.bind(this);
  }

  componentDidMount() {
    this.searchField = document.getElementById("user-search")
    this.searchField.addEventListener("focusin", this.toggleFocus);
  }

  toggleFocus() {
    if (!this.focus) {
      this.searchField.removeEventListener("focusin", this.toggleFocus);
      this.searchField.addEventListener("focusout", this.toggleFocus);
      this.focus = true;
      this.forceUpdate();
    } else {
      setTimeout(() => {
        this.searchField.removeEventListener("focusout", this.toggleFocus);
        this.searchField.addEventListener("focusin", this.toggleFocus);
        this.focus = false;
        this.forceUpdate();
      }, 200);
    }
  }
  
  handleInput(e) {
    if (e.target.value === "") {
      this.indexItems = [];
      this.forceUpdate();
    } else {
      searchUsers(e.target.value).then(users => {
        this.indexItems = [];
        
        users.forEach(user => {
          this.indexItems.push(<SearchIndexItem key={user.id} props={user} />);
        });
  
        this.forceUpdate();
      });
    }
  }
  
  render() {
    return (
      <div className="search-container">
        <input type="search" id="user-search" className="user-search" placeholder="Search" onChange={e => this.handleInput(e)} />
        {this.indexItems.length === 0 || !this.focus ? null : <ul className="users-list">
          {this.indexItems}
        </ul>}
      </div>
    )
  }
}

export default UsersSearch;