summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects/commit/components/branches_dropdown.vue
blob: 77e809e88ce789275ad9072a8f2c8a9ca869dff3 (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
<script>
import { GlCollapsibleListbox } from '@gitlab/ui';
import { mapActions, mapGetters, mapState } from 'vuex';
import { debounce, uniqBy } from 'lodash';
import {
  I18N_NO_RESULTS_MESSAGE,
  I18N_BRANCH_HEADER,
  I18N_BRANCH_SEARCH_PLACEHOLDER,
} from '../constants';

export default {
  name: 'BranchesDropdown',
  components: {
    GlCollapsibleListbox,
  },
  props: {
    value: {
      type: String,
      required: false,
      default: '',
    },
  },
  i18n: {
    noResultsMessage: I18N_NO_RESULTS_MESSAGE,
    branchHeaderTitle: I18N_BRANCH_HEADER,
    branchSearchPlaceholder: I18N_BRANCH_SEARCH_PLACEHOLDER,
  },
  data() {
    return {
      searchTerm: '',
    };
  },
  computed: {
    ...mapGetters(['joinedBranches']),
    ...mapState(['isFetching', 'branch']),
    listboxItems() {
      const selectedItem = { value: this.branch, text: this.branch };
      const transformedList = this.joinedBranches.map((value) => ({ value, text: value }));

      if (this.searchTerm) {
        return transformedList;
      }

      // Add selected item to top of list if not searching
      return uniqBy([selectedItem].concat(transformedList), 'value');
    },
  },
  mounted() {
    this.fetchBranches();
  },
  methods: {
    ...mapActions(['fetchBranches']),
    selectBranch(branch) {
      this.$emit('input', branch);
    },
    debouncedSearch: debounce(function debouncedSearch() {
      this.fetchBranches(this.searchTerm);
    }, 250),
    searchTermChanged(value) {
      this.searchTerm = value.trim();
      this.debouncedSearch(value);
    },
  },
};
</script>
<template>
  <gl-collapsible-listbox
    class="gl-max-w-full"
    :header-text="$options.i18n.branchHeaderTitle"
    :toggle-text="value"
    toggle-class="gl-w-full"
    :items="listboxItems"
    searchable
    :search-placeholder="$options.i18n.branchSearchPlaceholder"
    :searching="isFetching"
    :selected="value"
    :no-results-text="$options.i18n.noResultsMessage"
    @search="searchTermChanged"
    @select="selectBranch"
  />
</template>