summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/filtered_search_dropdown.vue
blob: 460fa6ad72e0ea4f9239454071da01f7a98ec83b (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<script>
import $ from 'jquery';
import Icon from '~/vue_shared/components/icon.vue';
/**
 * Renders a split dropdown with
 * an input that allows to search through the given
 * array of options.
 */
export default {
  name: 'FilteredSearchDropdown',
  components: {
    Icon,
  },
  props: {
    title: {
      type: String,
      required: false,
      default: '',
    },
    buttonType: {
      required: false,
      validator: value =>
        ['primary', 'default', 'secondary', 'success', 'info', 'warning', 'danger'].indexOf(
          value,
        ) !== -1,
      default: 'default',
    },
    size: {
      required: false,
      type: String,
      default: 'sm',
    },
    items: {
      type: Array,
      required: true,
    },
    visibleItems: {
      type: Number,
      required: false,
      default: 5,
    },
    filterKey: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      filter: '',
    };
  },
  computed: {
    className() {
      return `btn btn-${this.buttonType} btn-${this.size}`;
    },
    filteredResults() {
      if (this.filter !== '') {
        return this.items.filter(
          item => item[this.filterKey] && item[this.filterKey].toLowerCase().includes(this.filter.toLowerCase()),
        );
      }

      return this.items.slice(0, this.visibleItems);
    }
  },
  mounted() {
    /**
     * Resets the filter every time the user closes the dropdown
     */
    $(this.$el)
      .on('shown.bs.dropdown', () => {
        this.$nextTick(() => this.$refs.searchInput.focus());
      })
      .on('hidden.bs.dropdown', () => {
        this.filter = '';
      });
  },
};
</script>
<template>
  <div class="dropdown">
    <div class="btn-group">
      <slot
        name="mainAction"
        :class-name="className"
      >
        <button
          type="button"
          :class="className"
        >
          {{ title }}
        </button>
      </slot>

      <button
        type="button"
        :class="className"
        class="dropdown-toggle dropdown-toggle-split"
        data-toggle="dropdown"
        aria-haspopup="true"
        aria-expanded="false"
        aria-label="Expand dropdown"
      >
        <icon
          name="angle-down"
          :size="12"
        />
      </button>
      <div class="dropdown-menu dropdown-menu-right">
        <div class="dropdown-input">
          <input
            ref="searchInput"
            v-model="filter"
            type="search"
            placeholder="Filter"
            class="js-filtered-dropdown-input dropdown-input-field"
          />
          <icon
            class="dropdown-input-search"
            name="search"
          />
        </div>

        <div class="dropdown-content">
          <ul>
            <li
              v-for="(result, i) in filteredResults"
              :key="i"
              class="js-filtered-dropdown-result"
            >
              <slot
                name="result"
                :result="result"
              >
                {{ result[filterKey] }}
              </slot>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>