summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/packages/shared/components/package_tags.vue
blob: 5ec950e4d452befde8abce21e8d39c0aea5c7634 (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
<script>
import { GlBadge, GlIcon, GlSprintf, GlTooltipDirective } from '@gitlab/ui';
import { n__ } from '~/locale';

export default {
  name: 'PackageTags',
  components: {
    GlBadge,
    GlIcon,
    GlSprintf,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    tagDisplayLimit: {
      type: Number,
      required: false,
      default: 2,
    },
    tags: {
      type: Array,
      required: true,
      default: () => [],
    },
    hideLabel: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  computed: {
    tagCount() {
      return this.tags.length;
    },
    tagsToRender() {
      return this.tags.slice(0, this.tagDisplayLimit);
    },
    moreTagsDisplay() {
      return Math.max(0, this.tags.length - this.tagDisplayLimit);
    },
    moreTagsTooltip() {
      if (this.moreTagsDisplay) {
        return this.tags
          .slice(this.tagDisplayLimit)
          .map((x) => x.name)
          .join(', ');
      }

      return '';
    },
    tagsDisplay() {
      return n__('%d tag', '%d tags', this.tagCount);
    },
  },
  methods: {
    tagBadgeClass(index) {
      return {
        'gl-display-none': true,
        'gl-display-flex': this.tagCount === 1,
        'd-md-flex': this.tagCount > 1,
        'gl-mr-2': index !== this.tagsToRender.length - 1,
        'gl-ml-3': !this.hideLabel && index === 0,
      };
    },
  },
};
</script>

<template>
  <div class="gl-display-flex gl-align-items-center">
    <div v-if="!hideLabel" data-testid="tagLabel" class="gl-display-flex gl-align-items-center">
      <gl-icon name="labels" class="gl-text-gray-500 gl-mr-3" />
      <span class="gl-font-weight-bold">{{ tagsDisplay }}</span>
    </div>

    <gl-badge
      v-for="(tag, index) in tagsToRender"
      :key="index"
      data-testid="tagBadge"
      :class="tagBadgeClass(index)"
      variant="info"
      size="sm"
      >{{ tag.name }}</gl-badge
    >

    <gl-badge
      v-if="moreTagsDisplay"
      v-gl-tooltip
      data-testid="moreBadge"
      variant="muted"
      :title="moreTagsTooltip"
      size="sm"
      class="gl-display-none gl-md-display-flex gl-ml-2"
      ><gl-sprintf :message="__('+%{tags} more')">
        <template #tags>
          {{ moreTagsDisplay }}
        </template>
      </gl-sprintf></gl-badge
    >

    <gl-badge
      v-if="moreTagsDisplay && hideLabel"
      data-testid="moreBadge"
      variant="muted"
      class="gl-md-display-none gl-ml-2"
      >{{ tagsDisplay }}</gl-badge
    >
  </div>
</template>