Commit ca3eb5ae by Jaxon

fix: 修复 datePicker 在选为周的时候, 会调用两次change的问题

1 parent b21b967b
......@@ -48,14 +48,19 @@
<script>
import dayjs from "dayjs";
import api from "@/api/baseApi";
let weekColumnsCache = [];
let weekColumnsPromise = null;
let weekColumnsCacheKey = "";
let weekColumnsPromiseKey = "";
export default {
name: "DatePicker",
props: {
formate: {
type: String,
default: "YYYY-MM-DD",
},
value: {
type: Object,
default() {
......@@ -183,29 +188,41 @@ export default {
},
methods: {
async getWeeklyData() {
if (weekColumnsCache.length) {
const cacheKey = `${this.formate}|${dayjs(this.minDate).valueOf()}|${dayjs(this.maxDate).valueOf()}`;
if (weekColumnsCache.length && weekColumnsCacheKey === cacheKey) {
this.asyncWeekColumns = weekColumnsCache;
if (this.type === "week" && !this.currentWeek)
this.syncInnerValue();
return;
}
if (!weekColumnsPromise) {
weekColumnsPromise = api.getWeekSelectData().then((result) => {
const cols = result?.map((item, index) => ({
label: `${dayjs(item.startDate).format("YYYY/MM/DD")}-${dayjs(item.endDate).format("YYYY/MM/DD")}`,
startDate: dayjs(item.startDate).format("YYYY/MM/DD"),
endDate: dayjs(item.endDate).format("YYYY/MM/DD"),
index,
})) || [];
if (!weekColumnsPromise || weekColumnsPromiseKey !== cacheKey) {
weekColumnsPromiseKey = cacheKey;
weekColumnsPromise = Promise.resolve().then(() => {
const min = this.normalizeDayjs(this.minDate)?.startOf("day") || dayjs().subtract(3, "year").startOf("day");
const max = this.normalizeDayjs(this.maxDate)?.startOf("day") || dayjs().startOf("day");
const cols = [];
let cursor = max;
while (cursor.isSame(min, "day") || cursor.isAfter(min)) {
const weekday = cursor.day();
const weekStart = cursor.subtract(weekday === 0 ? 6 : weekday - 1, "day").startOf("day");
const weekEnd = weekStart.add(6, "day").startOf("day");
cols.push({
label: `${weekStart.format(this.formate)}-${weekEnd.format(this.formate)}`,
startDate: weekStart.format(this.formate),
endDate: weekEnd.format(this.formate),
index: cols.length,
});
cursor = weekStart.subtract(1, "day");
}
weekColumnsCache = cols;
weekColumnsCacheKey = cacheKey;
return cols;
}).catch(() => []).finally(() => {
}).finally(() => {
weekColumnsPromise = null;
weekColumnsPromiseKey = "";
});
}
this.asyncWeekColumns = await weekColumnsPromise;
if (this.type === "week" && !this.currentWeek)
this.syncInnerValue();
},
syncInnerValue() {
const nextValue = this.normalizeValueByType(this.value, this.type);
......@@ -236,8 +253,8 @@ export default {
this.currentWeek = target;
this.defaultIndex = target ? target.index : 0;
if (target) {
this.date = `${value.startDate}-${value.endDate}`;
this.currentDate = value.startDayjs.toDate();
this.date = `${target.startDate}-${target.endDate}`;
this.currentDate = this.normalizeDayjs(target.startDate).toDate();
this.rightCurrent = target.index === 0;
this.leftCurrent = target.index === this.innerWeekColumns.length - 1;
} else {
......@@ -257,8 +274,8 @@ export default {
this.currentWeek = null;
this.currentDate = normalizedDate.toDate();
this.date = type === "year-month"
? normalizedDate.format("YYYY/MM")
: normalizedDate.format("YYYY/MM/DD");
? normalizedDate.format(this.formate)
: normalizedDate.format(this.formate);
this.updateBoundaryStatusByDate(normalizedDate);
},
handleTypeChange(newType, oldType) {
......@@ -273,6 +290,8 @@ export default {
return input;
if (input instanceof Date)
return dayjs(input);
if (typeof input === "number")
return dayjs(input);
if (typeof input === "string")
return dayjs(input.replace(/-/g, "/"));
if (typeof input === "object") {
......@@ -280,6 +299,9 @@ export default {
}
return null;
},
formatValueDate(input) {
return this.normalizeDayjs(input).format(this.formate);
},
normalizeValueByType(value, type = this.type, oldType = this.type) {
const anchorDayjs = this.getAnchorDayjs(value, oldType);
if (type === "week") {
......@@ -301,8 +323,8 @@ export default {
const end = endDayjs.startOf("day");
const anchor = (anchorDayjs && dayjs.isDayjs(anchorDayjs) ? anchorDayjs : start).startOf("day");
return {
startDate: start.format("YYYY/MM/DD"),
endDate: end.format("YYYY/MM/DD"),
startDate: this.formatValueDate(start),
endDate: this.formatValueDate(end),
startDayjs: start,
endDayjs: end,
date: anchor,
......@@ -322,8 +344,8 @@ export default {
if (!weekItem) {
return this.buildBaseValue(target.startOf("week").add(1, "day"), target.endOf("week"), target);
}
const startDayjs = dayjs(weekItem.startDate, "YYYY/MM/DD").startOf("day");
const endDayjs = dayjs(weekItem.endDate, "YYYY/MM/DD").startOf("day");
const startDayjs = this.normalizeDayjs(weekItem.startDate).startOf("day");
const endDayjs = this.normalizeDayjs(weekItem.endDate).startOf("day");
const anchor = target.isBefore(startDayjs) || target.isAfter(endDayjs) ? startDayjs : target;
return this.buildBaseValue(startDayjs, endDayjs, anchor);
},
......@@ -332,8 +354,8 @@ export default {
if (!target.isValid())
return null;
return this.innerWeekColumns.find((item) => {
const start = dayjs(item.startDate, "YYYY/MM/DD").startOf("day");
const end = dayjs(item.endDate, "YYYY/MM/DD").endOf("day");
const start = this.normalizeDayjs(item.startDate).startOf("day");
const end = this.normalizeDayjs(item.endDate).endOf("day");
return (target.isSame(start) || target.isAfter(start))
&& (target.isSame(end) || target.isBefore(end));
}) || null;
......@@ -405,7 +427,7 @@ export default {
this.$emit("popupvisible", { visible: false });
},
onWeekConfirm(value) {
const nextValue = this.buildWeekValue(dayjs(value?.startDate, "YYYY/MM/DD"));
const nextValue = this.buildWeekValue(this.normalizeDayjs(value?.startDate));
this.applyValueState(nextValue, this.type);
this.emitValueChange(nextValue, { confirm: true });
this.popupVisible = false;
......@@ -431,7 +453,7 @@ export default {
}
if (this.currentWeek && this.currentWeek.index < this.innerWeekColumns.length - 1) {
const prevWeek = this.innerWeekColumns[this.currentWeek.index + 1];
const nextValue = this.buildWeekValue(dayjs(prevWeek.startDate, "YYYY/MM/DD"));
const nextValue = this.buildWeekValue(this.normalizeDayjs(prevWeek.startDate));
this.applyValueState(nextValue, this.type);
this.emitValueChange(nextValue);
}
......@@ -453,7 +475,7 @@ export default {
}
if (this.currentWeek && this.currentWeek.index > 0) {
const nextWeek = this.innerWeekColumns[this.currentWeek.index - 1];
const nextValue = this.buildWeekValue(dayjs(nextWeek.startDate, "YYYY/MM/DD"));
const nextValue = this.buildWeekValue(this.normalizeDayjs(nextWeek.startDate));
this.applyValueState(nextValue, this.type);
this.emitValueChange(nextValue);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!