Commit ca3eb5ae by Jaxon

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

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