summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/behaviors/components/json_table.vue
blob: bb38d80c1b54d3aa0dcde614aedea0a43999e719 (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
<script>
import { GlTable, GlFormInput } from '@gitlab/ui';
import { __ } from '~/locale';

export default {
  components: {
    GlTable,
    GlFormInput,
  },
  props: {
    fields: {
      type: Array,
      required: true,
    },
    items: {
      type: Array,
      required: true,
    },
    hasFilter: {
      type: Boolean,
      required: false,
      default: false,
    },
    caption: {
      type: String,
      required: false,
      default: __('Generated with JSON data'),
    },
  },
  data() {
    return {
      filterInput: '',
    };
  },
  computed: {
    cleanedFields() {
      return this.fields.map((field) => {
        if (typeof field === 'string') {
          return field;
        }
        return {
          key: field.key,
          label: field.label,
          sortable: field.sortable || false,
        };
      });
    },
  },
};
</script>
<template>
  <div class="gl-display-inline-block">
    <gl-form-input
      v-if="hasFilter"
      v-model="filterInput"
      :placeholder="__('Type to search')"
      class="gl-mb-2!"
    />
    <gl-table
      :fields="cleanedFields"
      :items="items"
      :filter="filterInput"
      show-empty
      class="gl-mt-0!"
    >
      <template v-if="caption" #table-caption>
        <small>{{ caption }}</small>
      </template>
    </gl-table>
  </div>
</template>