summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/actions_button.vue
blob: 2dc2c27f7ea4f149c378bcd358d30d05da7ff149 (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
<script>
import {
  GlDropdown,
  GlDropdownItem,
  GlDropdownDivider,
  GlButton,
  GlTooltipDirective,
} from '@gitlab/ui';

export default {
  components: {
    GlDropdown,
    GlDropdownItem,
    GlDropdownDivider,
    GlButton,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    actions: {
      type: Array,
      required: true,
    },
    selectedKey: {
      type: String,
      required: false,
      default: '',
    },
    category: {
      type: String,
      required: false,
      default: 'secondary',
    },
    variant: {
      type: String,
      required: false,
      default: 'default',
    },
  },
  computed: {
    hasMultipleActions() {
      return this.actions.length > 1;
    },
    selectedAction() {
      return this.actions.find((x) => x.key === this.selectedKey) || this.actions[0];
    },
  },
  methods: {
    handleItemClick(action) {
      this.$emit('select', action.key);
    },
    handleClick(action, evt) {
      return action.handle?.(evt);
    },
  },
};
</script>

<template>
  <gl-dropdown
    v-if="hasMultipleActions"
    v-gl-tooltip="selectedAction.tooltip"
    :text="selectedAction.text"
    :split-href="selectedAction.href"
    :variant="variant"
    :category="category"
    split
    @click="handleClick(selectedAction, $event)"
  >
    <template slot="button-content">
      <span class="gl-new-dropdown-button-text" v-bind="selectedAction.attrs">
        {{ selectedAction.text }}
      </span>
    </template>
    <template v-for="(action, index) in actions">
      <gl-dropdown-item
        :key="action.key"
        class="gl-dropdown-item-deprecated-adapter"
        :is-check-item="true"
        :is-checked="action.key === selectedAction.key"
        :secondary-text="action.secondaryText"
        :data-testid="`action_${action.key}`"
        @click="handleItemClick(action)"
      >
        {{ action.text }}
      </gl-dropdown-item>
      <gl-dropdown-divider v-if="index != actions.length - 1" :key="action.key + '_divider'" />
    </template>
  </gl-dropdown>
  <gl-button
    v-else-if="selectedAction"
    v-gl-tooltip="selectedAction.tooltip"
    v-bind="selectedAction.attrs"
    :variant="variant"
    :category="category"
    :href="selectedAction.href"
    @click="handleClick(selectedAction, $event)"
  >
    {{ selectedAction.text }}
  </gl-button>
</template>