summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/registry/persisted_dropdown_selection.vue
blob: 43a8e241d774a89efca047bd13f95a70af881fb1 (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
<script>
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';

export default {
  name: 'PersistedDropdownSelection',
  components: {
    GlDropdown,
    GlDropdownItem,
    LocalStorageSync,
  },
  props: {
    options: {
      type: Array,
      required: true,
    },
    storageKey: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      selected: null,
    };
  },
  computed: {
    dropdownText() {
      const selected = this.parsedOptions.find((o) => o.selected);
      return selected?.label || this.options[0].label;
    },
    parsedOptions() {
      return this.options.map((o) => ({ ...o, selected: o.value === this.selected }));
    },
  },
  methods: {
    setSelected(value) {
      this.selected = value;
      this.$emit('change', value);
    },
  },
};
</script>

<template>
  <local-storage-sync :storage-key="storageKey" :value="selected" as-string @input="setSelected">
    <gl-dropdown :text="dropdownText" lazy>
      <gl-dropdown-item
        v-for="option in parsedOptions"
        :key="option.value"
        :is-checked="option.selected"
        :is-check-item="true"
        @click="setSelected(option.value)"
      >
        {{ option.label }}
      </gl-dropdown-item>
    </gl-dropdown>
  </local-storage-sync>
</template>