summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/navigation_tabs.vue
blob: b33a0101dbfdcc68d2a461c66980418c9832e70a (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
<script>
  import $ from 'jquery';

  /**
   * Given an array of tabs, renders non linked bootstrap tabs.
   * When a tab is clicked it will trigger an event and provide the clicked scope.
   *
   * This component is used in apps that handle the API call.
   * If you only need to change the URL this component should not be used.
   *
   * @example
   * <navigation-tabs
   *   :tabs="[
   *   {
   *      name: String,
   *      scope: String,
   *      count: Number || Undefined,
   *      isActive: Boolean,
   *    },
   *   ]"
   *   @onChangeTab="onChangeTab"
   *   />
   */
  export default {
    name: 'NavigationTabs',
    props: {
      tabs: {
        type: Array,
        required: true,
      },
      scope: {
        type: String,
        required: false,
        default: '',
      },
    },
    mounted() {
      $(document).trigger('init.scrolling-tabs');
    },
    methods: {
      shouldRenderBadge(count) {
        // 0 is valid in a badge, but evaluates to false, we need to check for undefined
        return count !== undefined;
      },

      onTabClick(tab) {
        this.$emit('onChangeTab', tab.scope);
      },
    },
  };
</script>
<template>
  <ul class="nav-links scrolling-tabs separator">
    <li
      v-for="(tab, i) in tabs"
      :key="i"
      :class="{
        active: tab.isActive,
      }"
    >
      <a
        role="button"
        @click="onTabClick(tab)"
        :class="`js-${scope}-tab-${tab.scope}`"
      >
        {{ tab.name }}

        <span
          v-if="shouldRenderBadge(tab.count)"
          class="badge"
        >
          {{ tab.count }}
        </span>
      </a>
    </li>
  </ul>
</template>