index.vue
2.53 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
<!--
* @Date: 2022-09-14 11:00:01
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-09-14 14:24:41
* @FilePath: /data-table/src/components/CalendarField/index.vue
* @Description: 文件描述
-->
<template>
<div class="calendar-page">
<div class="label">{{ item.label }}<span v-if="item.required"> *</span></div>
<van-field v-model="item.value" is-link readonly :name="item.key" :required="item.required"
:placeholder="item.placeholder" :rules="item.rules" @click="show = true" />
<van-calendar v-model:show="show"
:type="item.component_props.type" :max-range="item.component_props.maxRange"
:min-date="item.component_props.minDate" :max-date="item.component_props.maxDate"
:formatter="formatter"
first-day-of-week="1"
@month-show="onMonthShow"
@confirm="onConfirm" allow-same-day />
</div>
</template>
<script setup>
const props = defineProps({
item: Object
})
const show = ref(false);
const formatDate = (date) => `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
const onConfirm = (value) => {
console.warn(props.item.component_props.type);
show.value = false;
if (props.item.component_props.type === 'range') { // 日期区间
const [start, end] = value;
props.item.value = `${formatDate(start)} ~ ${formatDate(end)}`;
} else if (props.item.component_props.type === 'multiple') { // 多个日期
const arr = []
value.forEach(element => {
arr.push(formatDate(element))
});
props.item.value = arr.join(',');
} else {
props.item.value = formatDate(value);
}
};
// 每一格内容格式化
const formatter = (day) => {
const month = day.date.getMonth() + 1;
const date = day.date.getDate();
const year = day.date.getFullYear();
if (month === 5) {
if (date === 1) {
day.topInfo = '劳动节';
} else if (date === 4) {
day.topInfo = '青年节';
} else if (date === 11) {
day.text = '今天';
}
}
if (month === 10) {
if (date === 1) {
day.topInfo = '国庆节';
day.type = 'disabled'
}
}
if (day.type === 'start') {
day.bottomInfo = '开始';
} else if (day.type === 'end') {
day.bottomInfo = '结束';
}
return day;
};
const onMonthShow = ({ date, title }) => {
// console.warn(date);
// console.warn(title);
if (title === '2022年12月') {
}
}
</script>
<style lang="less" scoped>
.calendar-page {
.label {
padding: 1rem 1rem 0 1rem;
font-size: 0.9rem;
font-weight: bold;
span {
color: red;
}
}
}
</style>