-
Notifications
You must be signed in to change notification settings - Fork 0
/
dateRangePicker.vue
108 lines (106 loc) · 2.38 KB
/
dateRangePicker.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
<template>
<div class="date-range-picker">
<mu-text-field :hintText="hintText" @focus="handleFocus" @labelClick="handleClick" :value="dateRange"/>
<mu-dialog
dialogClass="mu-date-picker-dialog"
v-if="!disabled"
:open="dialogVisible"
@close="dialogVisible = false">
<calendar
:initialDate="dialogDate"
:shouldDisableDate="shouldDisableDate"
:maxDate="maxDate"
:minDate="minDate"
@accept="handleAccept"
@dismiss="handleDismiss">
</calendar>
</mu-dialog>
</div>
</template>
<script>
import * as dateUtils from '../../../es/dateUtils';
import calendar from './calendar';
export default {
name: 'dateRangePicker',
data() {
return {
dialogVisible: false,
inputValue: this.value,
dialogDate: null,
};
},
components: { calendar },
props: {
value: {
type: Array,
},
hintText: {
type: String,
},
format: {
type: String,
default: 'YYYY-MM-DD',
},
maxDate: {
type: Date,
default() {
return dateUtils.addYears(new Date(), 100);
},
},
minDate: {
type: Date,
default() {
return dateUtils.addYears(new Date(), -100);
},
},
disabled: {
type: Boolean,
default: false,
},
shouldDisableDate: {
type: Function,
},
},
computed: {
dateRange() {
return this.inputValue.length === 2 ? `${this.inputValue[0]} - ${this.inputValue[1]}` : '';
},
},
watch: {
value(val) {
this.inputValue = val;
},
inputValue(val) {
this.$emit('input', val);
},
},
methods: {
handleFocus(event) {
event.target.blur();
this.$emit('focus', event);
},
handleClick() {
if (!this.disabled) {
setTimeout(() => {
this.openDialog();
}, 0);
}
},
openDialog() {
if (this.disabled) return;
this.dialogDate = this.inputValue.length ? dateUtils.strFormatToDate(this.inputValue[0], this.format) : new Date();
this.dialogVisible = true;
},
handleAccept(val) {
const newValue = val.map(date => dateUtils.dateToStr(date, this.format));
this.inputValue = newValue;
this.dialogVisible = false;
this.$emit('change', newValue);
},
handleDismiss() {
this.dialogVisible = false;
this.$emit('dismiss');
},
},
};
</script>