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
120
121
122
123
124
125
126
127
128
129
130
131
|
<script>
import { GlCollapsibleListbox, GlIcon, GlTooltipDirective } from '@gitlab/ui';
import { isString } from 'lodash';
import { isValidDate, getDayDifference } from '~/lib/utils/datetime_utility';
import {
DATE_RANGE_CUSTOM_VALUE,
DEFAULT_DATE_RANGE_OPTIONS,
NUMBER_OF_DAYS_SELECTED,
} from '~/analytics/shared/constants';
import { __ } from '~/locale';
export default {
name: 'DateRangesDropdown',
components: {
GlCollapsibleListbox,
GlIcon,
},
directives: {
GlTooltip: GlTooltipDirective,
},
props: {
dateRangeOptions: {
type: Array,
required: false,
default: () => DEFAULT_DATE_RANGE_OPTIONS,
validator: (options) =>
options.length &&
options.every(
({ text, value, startDate, endDate }) =>
isString(text) &&
isString(value) &&
isValidDate(startDate) &&
isValidDate(endDate) &&
endDate >= startDate,
),
},
selected: {
type: String,
required: false,
default: '',
},
tooltip: {
type: String,
required: false,
default: '',
},
includeCustomDateRangeOption: {
type: Boolean,
required: false,
default: true,
},
includeEndDateInDaysSelected: {
type: Boolean,
required: false,
default: false,
},
},
data() {
return {
selectedValue: this.selected || this.dateRangeOptions[0].value,
};
},
computed: {
items() {
const dateRangeOptions = this.dateRangeOptions.map(({ text, value }) => ({ text, value }));
if (!this.includeCustomDateRangeOption) return dateRangeOptions;
return [...dateRangeOptions, this.$options.customDateRangeItem];
},
isCustomDateRangeSelected() {
return this.selectedValue === DATE_RANGE_CUSTOM_VALUE;
},
groupedDateRangeOptionsByValue() {
return this.dateRangeOptions.reduce((acc, { value, startDate, endDate }) => {
acc[value] = { startDate, endDate };
return acc;
}, {});
},
selectedDateRange() {
if (this.isCustomDateRangeSelected) return null;
return this.groupedDateRangeOptionsByValue[this.selectedValue];
},
showDaysSelectedCount() {
return !this.isCustomDateRangeSelected && this.daysSelectedCount;
},
daysSelectedCount() {
const { selectedDateRange } = this;
if (!selectedDateRange) return '';
const { startDate, endDate } = selectedDateRange;
const daysCount = getDayDifference(startDate, endDate);
return this.$options.i18n.daysSelected(
this.includeEndDateInDaysSelected ? daysCount + 1 : daysCount,
);
},
},
methods: {
onSelect(value) {
if (this.isCustomDateRangeSelected) {
this.$emit('customDateRangeSelected');
} else {
this.$emit('selected', { value, ...this.selectedDateRange });
}
},
},
customDateRangeItem: {
text: __('Custom'),
value: DATE_RANGE_CUSTOM_VALUE,
},
i18n: {
daysSelected: NUMBER_OF_DAYS_SELECTED,
},
};
</script>
<template>
<div class="gl-display-flex gl-align-items-center gl-gap-3">
<gl-collapsible-listbox v-model="selectedValue" :items="items" @select="onSelect" />
<div v-if="showDaysSelectedCount" class="gl-text-gray-500">
<span data-testid="predefined-date-range-days-count">{{ daysSelectedCount }}</span>
<gl-icon v-if="tooltip" v-gl-tooltip class="gl-ml-2" name="information-o" :title="tooltip" />
</div>
</div>
</template>
|