summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/filtered_search_bar/tokens/weight_token.vue
blob: 72116f0e991f0fe9e210c55b27af8f11e0d9dbf0 (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
<script>
import { GlDropdownDivider, GlFilteredSearchSuggestion, GlFilteredSearchToken } from '@gitlab/ui';
import { DEFAULT_NONE_ANY } from '../constants';

export default {
  baseWeights: ['0', '1', '2', '3', '4', '5'],
  components: {
    GlDropdownDivider,
    GlFilteredSearchSuggestion,
    GlFilteredSearchToken,
  },
  props: {
    config: {
      type: Object,
      required: true,
    },
    value: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      weights: this.$options.baseWeights,
      defaultWeights: this.config.defaultWeights || DEFAULT_NONE_ANY,
    };
  },
  methods: {
    updateWeights({ data }) {
      const weight = parseInt(data, 10);
      this.weights = Number.isNaN(weight) ? this.$options.baseWeights : [String(weight)];
    },
  },
};
</script>

<template>
  <gl-filtered-search-token
    :config="config"
    v-bind="{ ...$props, ...$attrs }"
    v-on="$listeners"
    @input="updateWeights"
  >
    <template #suggestions>
      <gl-filtered-search-suggestion
        v-for="weight in defaultWeights"
        :key="weight.value"
        :value="weight.value"
      >
        {{ weight.text }}
      </gl-filtered-search-suggestion>
      <gl-dropdown-divider v-if="defaultWeights.length" />
      <gl-filtered-search-suggestion v-for="weight of weights" :key="weight" :value="weight">
        {{ weight }}
      </gl-filtered-search-suggestion>
    </template>
  </gl-filtered-search-token>
</template>