summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/timezone_dropdown.vue
blob: b9ee74d6a034a36587530245454f8916082a63dd (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
<script>
import { GlDropdown, GlDropdownItem, GlSearchBoxByType } from '@gitlab/ui';
import { secondsToHours } from '~/lib/utils/datetime_utility';
import { __ } from '~/locale';
import autofocusonshow from '~/vue_shared/directives/autofocusonshow';

export default {
  name: 'TimezoneDropdown',
  components: {
    GlDropdown,
    GlDropdownItem,
    GlSearchBoxByType,
  },
  directives: {
    autofocusonshow,
  },
  props: {
    value: {
      type: String,
      required: true,
      default: '',
    },
    timezoneData: {
      type: Array,
      required: true,
      default: () => [],
    },
  },
  data() {
    return {
      searchTerm: '',
    };
  },
  tranlations: {
    noResultsText: __('No matching results'),
  },
  computed: {
    timezones() {
      return this.timezoneData.map((timezone) => ({
        formattedTimezone: this.formatTimezone(timezone),
        identifier: timezone.identifier,
      }));
    },
    filteredResults() {
      const lowerCasedSearchTerm = this.searchTerm.toLowerCase();
      return this.timezones.filter((timezone) =>
        timezone.formattedTimezone.toLowerCase().includes(lowerCasedSearchTerm),
      );
    },
    selectedTimezoneLabel() {
      return this.value || __('Select timezone');
    },
  },
  methods: {
    selectTimezone(selectedTimezone) {
      this.$emit('input', selectedTimezone);
      this.searchTerm = '';
    },
    isSelected(timezone) {
      return this.value === timezone.formattedTimezone;
    },
    formatTimezone(item) {
      return `[UTC ${secondsToHours(item.offset)}] ${item.name}`;
    },
  },
};
</script>
<template>
  <gl-dropdown :text="selectedTimezoneLabel" block lazy menu-class="gl-w-full!">
    <gl-search-box-by-type v-model.trim="searchTerm" v-autofocusonshow autofocus />
    <gl-dropdown-item
      v-for="timezone in filteredResults"
      :key="timezone.formattedTimezone"
      :is-checked="isSelected(timezone)"
      :is-check-item="true"
      @click="selectTimezone(timezone)"
    >
      {{ timezone.formattedTimezone }}
    </gl-dropdown-item>
    <gl-dropdown-item
      v-if="!filteredResults.length"
      class="gl-pointer-events-none"
      data-testid="noMatchingResults"
    >
      {{ $options.tranlations.noResultsText }}
    </gl-dropdown-item>
  </gl-dropdown>
</template>