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
111
112
113
114
115
116
117
118
119
|
<script>
import { GlDropdown, GlDropdownItem, GlSearchBoxByType } from '@gitlab/ui';
import { __ } from '~/locale';
import autofocusonshow from '~/vue_shared/directives/autofocusonshow';
import { formatTimezone } from '~/lib/utils/datetime_utility';
export default {
name: 'TimezoneDropdown',
components: {
GlDropdown,
GlDropdownItem,
GlSearchBoxByType,
},
directives: {
autofocusonshow,
},
props: {
value: {
type: String,
required: true,
default: '',
},
name: {
type: String,
required: false,
default: '',
},
timezoneData: {
type: Array,
required: true,
default: () => [],
},
},
data() {
return {
searchTerm: '',
tzValue: this.initialTimezone(this.timezoneData, this.value),
};
},
translations: {
noResultsText: __('No matching results'),
},
computed: {
timezones() {
return this.timezoneData.map((timezone) => ({
formattedTimezone: formatTimezone(timezone),
identifier: timezone.identifier,
}));
},
filteredResults() {
const lowerCasedSearchTerm = this.searchTerm.toLowerCase();
return this.timezones.filter((timezone) =>
timezone.formattedTimezone.toLowerCase().includes(lowerCasedSearchTerm),
);
},
selectedTimezoneLabel() {
return this.tzValue || __('Select timezone');
},
timezoneIdentifier() {
return this.tzValue
? this.timezones.find((timezone) => timezone.formattedTimezone === this.tzValue).identifier
: undefined;
},
},
methods: {
selectTimezone(selectedTimezone) {
this.tzValue = selectedTimezone.formattedTimezone;
this.$emit('input', selectedTimezone);
this.searchTerm = '';
},
isSelected(timezone) {
return this.tzValue === timezone.formattedTimezone;
},
initialTimezone(timezones, value) {
if (!value) {
return undefined;
}
const initialTimezone = timezones.find((timezone) => timezone.identifier === value);
if (initialTimezone) {
return formatTimezone(initialTimezone);
}
return undefined;
},
},
};
</script>
<template>
<div>
<input
v-if="name"
id="user_timezone"
:name="name"
:value="timezoneIdentifier || value"
type="hidden"
/>
<gl-dropdown :text="selectedTimezoneLabel" block lazy menu-class="gl-w-full!" v-bind="$attrs">
<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
@click="selectTimezone(timezone)"
>
{{ timezone.formattedTimezone }}
</gl-dropdown-item>
<gl-dropdown-item
v-if="!filteredResults.length"
class="gl-pointer-events-none"
data-testid="noMatchingResults"
>
{{ $options.translations.noResultsText }}
</gl-dropdown-item>
</gl-dropdown>
</div>
</template>
|