-
Notifications
You must be signed in to change notification settings - Fork 59
/
date-range-picker.vue
198 lines (185 loc) · 6.59 KB
/
date-range-picker.vue
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!--
Copyright 2020 ODK Central Developers
See the NOTICE file at the top-level directory of this distribution and at
https://github.com/getodk/central-frontend/blob/master/NOTICE.
This file is part of ODK Central. It is subject to the license terms in
the LICENSE file found in the top-level directory of this distribution and at
https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central,
including this file, may be copied, modified, propagated, or distributed
except according to the terms contained in the LICENSE file.
-->
<template>
<label class="form-group"><!-- eslint-disable-line vuejs-accessibility/label-has-for -->
<!-- We use a class to indicate whether the input is required, because
flatpickr does not support the `required` attribute:
https://github.com/ankurk91/vue-flatpickr-component/issues/47 -->
<flatpickr ref="flatpickr" v-model="flatpickrValue" :config="config"
class="form-control" :class="{ required }"
:aria-disabled="disabled" v-tooltip.aria-describedby="disabledMessage"
:placeholder="requiredLabel(placeholder, required)" autocomplete="off"
@keydown="stopPropagationIfDisabled"
@on-close="close"/>
<template v-if="!required">
<button v-show="modelValue.length === 2 && !disabled" type="button" class="close"
:aria-label="$t('action.clear')" @click="clear">
<span aria-hidden="true">×</span>
</button>
</template>
<span class="form-label">{{ requiredLabel(placeholder, required) }}</span>
</label>
</template>
<script>
import flatpickr from 'flatpickr';
import flatpickrComponent from 'vue-flatpickr-component';
import { DateTime } from 'luxon';
import 'flatpickr/dist/flatpickr.css';
import 'flatpickr/dist/l10n/cs';
import 'flatpickr/dist/l10n/de';
import 'flatpickr/dist/l10n/es';
import 'flatpickr/dist/l10n/fr';
import 'flatpickr/dist/l10n/id';
import 'flatpickr/dist/l10n/it';
import 'flatpickr/dist/l10n/ja';
import 'flatpickr/dist/l10n/zh-tw';
import { locales } from '../i18n';
import { requiredLabel } from '../util/dom';
// Map locales.
const l10ns = {};
for (const locale of locales.keys()) {
const l10n = flatpickr.l10ns[locale];
if (l10n != null) l10ns[locale] = l10n;
}
l10ns['zh-Hant'] = flatpickr.l10ns.zh_tw;
export default {
name: 'DateRangePicker',
components: { flatpickr: flatpickrComponent },
props: {
// Either an array of two DateTime objects or an empty array
modelValue: {
type: Array,
required: true
},
required: {
type: Boolean,
default: false
},
placeholder: {
type: String,
required: true
},
disabled: {
type: Boolean,
default: false
},
disabledMessage: {
type: String,
required: false
}
},
emits: ['update:modelValue'],
setup() {
return { requiredLabel };
},
data() {
return {
// We initialize this.flatpickrValue as an array of Date objects, but
// vue-flatpickr-component will replace it with a string when the user
// makes a selection.
flatpickrValue: this.modelValue.map(dateTime => dateTime.toJSDate())
};
},
computed: {
config() {
return {
mode: 'range',
// See https://github.com/flatpickr/flatpickr/issues/1549
dateFormat: 'Y/m/d',
locale: l10ns[this.$i18n.locale] ?? l10ns[this.$i18n.fallbackLocale],
clickOpens: !this.disabled
};
}
},
watch: {
modelValue(value) {
this.flatpickrValue = value.map(dateTime => dateTime.toJSDate());
}
},
methods: {
// Converts an array of Date objects from a selection to an array of
// DateTime objects. If the selection was incomplete -- if fewer dates were
// selected than expected -- then default values will be used for one or
// both DateTime objects. Returns the DateTime objects along with an
// indicator of whether the selection was complete.
selectedDatesToDateTimes(dates) {
// dates.length === 0 if the user opens the calendar, clears the selection
// (for example, by pressing backspace), then closes the calendar. (There
// doesn't seem to be an easy way to turn off this behavior for if
// this.required is `true`.)
if (dates.length === 0) {
if (!this.required) return [dates, true];
const today = DateTime.local().startOf('day');
return [[today, today], false];
}
// dates.length === 1 if the user opens the calendar, selects a date, then
// closes the calendar without selecting a second date.
if (dates.length === 1) {
const dateTime = DateTime.fromJSDate(dates[0]);
return [[dateTime, dateTime], false];
}
return [dates.map(DateTime.fromJSDate), true];
},
close(selectedDates) {
const [newValue, complete] = this.selectedDatesToDateTimes(selectedDates);
const newEqualsOld = newValue.length === 0
? this.modelValue.length === 0
: this.modelValue.length === 2 &&
newValue[0].valueOf() === this.modelValue[0].valueOf() &&
newValue[1].valueOf() === this.modelValue[1].valueOf();
if (!newEqualsOld) {
this.$emit('update:modelValue', newValue);
} else if (!complete) {
// newValue represents a complete selection. That means that since the
// actual selection was incomplete, it does not match newValue. Because
// of that, even though this.modelValue will not change, we still need
// to set this.flatpickrValue.
this.flatpickrValue = newValue.map(dateTime => dateTime.toJSDate());
}
},
clear() {
this.$emit('update:modelValue', []);
// The .close button will be hidden, so we focus the flatpickr input.
// Focusing it will open the calendar, which we don't want, so we
// immediately close the calendar using the approach described here:
// https://github.com/ankurk91/vue-flatpickr-component/issues/33
this.$refs.flatpickr.$el.focus();
this.$refs.flatpickr.fp.close();
},
stopPropagationIfDisabled(e) {
if (this.disabled) {
e.stopPropagation();
}
}
}
};
</script>
<style lang="scss">
@import '../assets/scss/variables';
// The flatpickr input is readonly by default, but we do not want to style it as
// readonly.
.form-group .flatpickr-input[readonly] {
color: $color-input;
&::placeholder { color: $color-text; }
}
.form-group .flatpickr-input[aria-disabled="true"]::placeholder {
color: $color-input-inactive;
}
.form-inline .flatpickr-input {
// Leave space for the .close button.
width: 205px;
&.required { width: 193px };
&:lang(ja) {
width: 252px;
&.required { width: 240px; }
}
}
</style>