DateNav.vue
2.08 KB
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
<template>
<div class="date-nav">
<el-button class="date-nav__arrow"
icon="el-icon-arrow-left"
type="text"
@click="handlePrev" />
<el-date-picker v-model="innerDate"
class="date-nav__picker"
:clearable="false"
:editable="false"
:picker-options="pickerOptions"
type="date"
value-format="yyyy-MM-dd"
@change="handleChange" />
<el-button class="date-nav__arrow"
:disabled="nextDisabled"
icon="el-icon-arrow-right"
type="text"
@click="handleNext" />
</div>
</template>
<script>
import dayjs from "dayjs";
export default {
name: "DateNav",
props: {
value: {
type: String,
default: "",
},
},
data() {
return {
innerDate: this.value || dayjs().format("YYYY-MM-DD"),
pickerOptions: {
disabledDate: (time) => time.getTime() > Date.now(),
},
};
},
computed: {
nextDisabled() {
return dayjs(this.innerDate).isSame(dayjs(), "day");
},
},
watch: {
value(val) {
if (val && val !== this.innerDate) {
this.innerDate = val;
}
},
},
methods: {
emitChange() {
this.$emit("input", this.innerDate);
this.$emit("change", this.innerDate);
},
handlePrev() {
this.innerDate = dayjs(this.innerDate).subtract(1, "day").format("YYYY-MM-DD");
this.emitChange();
},
handleNext() {
if (this.nextDisabled) {
return;
}
this.innerDate = dayjs(this.innerDate).add(1, "day").format("YYYY-MM-DD");
this.emitChange();
},
handleChange() {
this.emitChange();
},
},
};
</script>
<style scoped lang="scss">
.date-nav {
display: flex;
align-items: center;
&__arrow {
font-size: 16px;
color: #606266;
padding: 4px;
}
&__picker {
width: 130px;
::v-deep .el-input__inner {
border: none;
background: transparent;
text-align: center;
font-size: 14px;
color: #303133;
cursor: pointer;
padding: 0;
}
::v-deep .el-input__prefix {
display: none;
}
}
}
</style>