forked from getodk/central-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate-range-picker.vue
168 lines (158 loc) · 5.75 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
<!--
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">
<!-- 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 }"
:placeholder="`${placeholder}${star}`" autocomplete="off"
@on-close="close"/>
<template v-if="!required">
<button v-show="modelValue.length === 2" type="button" class="close"
:aria-label="$t('action.clear')" @click="clear">
<span aria-hidden="true">×</span>
</button>
</template>
<span class="form-label">{{ placeholder }}{{ star }}</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';
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
}
},
emits: ['update:modelValue'],
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() {
const config = {
mode: 'range',
// See https://github.com/flatpickr/flatpickr/issues/1549
dateFormat: 'Y/m/d'
};
const l10n = flatpickr.l10ns[this.$i18n.locale];
if (l10n != null) config.locale = l10n;
return config;
},
star() {
return this.required ? '*' : '';
}
},
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();
}
}
};
</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-inline .flatpickr-input {
// Leave space for the .close button.
width: 205px;
&.required { width: 193px };
&:lang(ja) {
width: 252px;
&.required { width: 240px; }
}
}
</style>