summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects_dropdown/components/search.vue
blob: 53bc76d0f2dff1dded1ec5275748246a5c6f0588 (plain)
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
<script>
import _ from 'underscore';
import eventHub from '../event_hub';

export default {
  data() {
    return {
      searchQuery: '',
    };
  },
  watch: {
    searchQuery() {
      this.handleInput();
    },
  },
  methods: {
    setFocus() {
      this.$refs.search.focus();
    },
    emitSearchEvents() {
      if (this.searchQuery) {
        eventHub.$emit('searchProjects', this.searchQuery);
      } else {
        eventHub.$emit('searchCleared');
      }
    },
    /**
     * Callback function within _.debounce is intentionally
     * kept as ES5 `function() {}` instead of ES6 `() => {}`
     * as it otherwise messes up function context
     * and component reference is no longer accessible via `this`
     */
    // eslint-disable-next-line func-names
    handleInput: _.debounce(function () {
      this.emitSearchEvents();
    }, 500),
  },
  mounted() {
    eventHub.$on('dropdownOpen', this.setFocus);
  },
  beforeDestroy() {
    eventHub.$off('dropdownOpen', this.setFocus);
  },
};
</script>

<template>
  <div
    class="search-input-container hidden-xs"
  >
    <input
      type="search"
      class="form-control"
      ref="search"
      v-model="searchQuery"
      :placeholder="s__('ProjectsDropdown|Search your projects')"
    />
    <i
      v-if="!searchQuery"
      class="search-icon fa fa-fw fa-search"
      aria-hidden="true"
    />
  </div>
</template>