-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathrange.vue
More file actions
91 lines (79 loc) · 2.83 KB
/
range.vue
File metadata and controls
91 lines (79 loc) · 2.83 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
<template>
<t-space direction="vertical">
<!-- 受控面板 + 函数范围:仅允许未来 90 天 -->
<t-date-picker
v-model="valueFn"
v-model:panelActiveDate="panelActiveDate"
mode="date"
format="YYYY-MM-DD"
:range="rangeFn"
:need-confirm="false"
placeholder="选择未来90天内的日期"
@panel-active-date="handlePanelActiveDate"
@change="handleChange"
@month-change="handleMonthChange"
@year-change="handleYearChange"
/>
<!-- 数组范围 + 默认面板日期:仅允许 2026 年 -->
<t-date-picker
v-model="valueArr"
mode="date"
format="YYYY-MM-DD"
:range="rangeArr"
:default-panel-active-date="defaultPanelActiveDate"
placeholder="仅可选 2026 年内的日期,同时禁用周六"
:disable-date="disableDate"
@change="handleChange"
@month-change="handleMonthChange"
@year-change="handleYearChange"
/>
年选择:2019 到 2024:
<t-date-picker mode="year" clearable :range="['2019', '2024']" />
月份选择:2022-10 到 2025-01:
<t-date-picker mode="month" clearable :range="['2022-10', '2025-01']" />
季度选择:2022-10 到 2025-01:
<t-date-picker mode="quarter" clearable :range="['2022-10', '2025-01']" />
周选择:2000-10 到 2025-01:
<t-date-picker mode="week" clearable :range="['2000-10', '2025-01']" />
</t-space>
</template>
<script setup>
import { ref } from 'vue';
import dayjs from 'dayjs';
const disableDate = (date) => dayjs(date).day() === 6;
// 示例1:函数范围 + 受控面板
const valueFn = ref('');
const panelActiveDate = ref({
year: dayjs().year() - 5,
month: 10, // 0-11
});
// 仅允许今天到未来 90 天(返回 true 表示可选)
const rangeFn = (d) => {
const now = dayjs().startOf('day');
const target = dayjs(d).startOf('day');
const diff = target.diff(now, 'day');
return diff >= 0 && diff <= 90;
};
function handlePanelActiveDate(val, ctx) {
// ctx.trigger: 'year-select' | 'month-select' | 'today' | 'year-arrow-next' | 'year-arrow-previous' | ...
if (String(ctx?.trigger).includes('year')) {
panelActiveDate.value.year = typeof val === 'number' ? val : dayjs(val).year();
}
if (String(ctx?.trigger).includes('month') || ctx?.trigger === 'today') {
panelActiveDate.value.month = typeof val === 'number' ? val : dayjs(val).month();
}
}
// 示例2:数组范围 + 默认面板日期
const valueArr = ref('');
const rangeArr = ['2026-01-01', '2026-11-20'];
const defaultPanelActiveDate = { year: 2026, month: 5 }; // 0-11,5 表示 6 月
function handleChange(value, context) {
console.log('onChange:', value, context);
}
function handleMonthChange(context) {
console.log('onMonthChange:', context);
}
function handleYearChange(context) {
console.log('onYearChange:', context);
}
</script>