summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/registry/registry_search.vue
blob: 62453a25f62ea38a2dd6f7e4412614825ff0dc45 (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
<script>
import { GlSorting, GlSortingItem, GlFilteredSearch } from '@gitlab/ui';

const ASCENDING_ORDER = 'asc';
const DESCENDING_ORDER = 'desc';

export default {
  components: {
    GlSorting,
    GlSortingItem,
    GlFilteredSearch,
  },
  props: {
    filter: {
      type: Array,
      required: true,
    },
    sorting: {
      type: Object,
      required: true,
    },
    tokens: {
      type: Array,
      required: false,
      default: () => [],
    },
    sortableFields: {
      type: Array,
      required: true,
    },
  },
  computed: {
    internalFilter: {
      get() {
        return this.filter;
      },
      set(value) {
        this.$emit('filter:changed', value);
      },
    },
    sortText() {
      const field = this.sortableFields.find((s) => s.orderBy === this.sorting.orderBy);
      return field ? field.label : '';
    },
    isSortAscending() {
      return this.sorting.sort === ASCENDING_ORDER;
    },
  },
  methods: {
    onDirectionChange() {
      const sort = this.isSortAscending ? DESCENDING_ORDER : ASCENDING_ORDER;
      this.$emit('sorting:changed', { sort });
    },
    onSortItemClick(item) {
      this.$emit('sorting:changed', { orderBy: item });
    },
    clearSearch() {
      this.$emit('filter:changed', []);
      this.$emit('filter:submit');
    },
  },
};
</script>

<template>
  <div class="gl-display-flex gl-p-5 gl-bg-gray-10 gl-border-solid gl-border-1 gl-border-gray-100">
    <gl-filtered-search
      v-model="internalFilter"
      class="gl-mr-4 gl-flex-fill-1"
      :placeholder="__('Filter results')"
      :available-tokens="tokens"
      @submit="$emit('filter:submit')"
      @clear="clearSearch"
    />
    <gl-sorting
      :text="sortText"
      :is-ascending="isSortAscending"
      @sortDirectionChange="onDirectionChange"
    >
      <gl-sorting-item
        v-for="item in sortableFields"
        ref="packageListSortItem"
        :key="item.orderBy"
        @click="onSortItemClick(item.orderBy)"
      >
        {{ item.label }}
      </gl-sorting-item>
    </gl-sorting>
  </div>
</template>