Commit edca124a by Jaxon

refactor(datePicker): 重构日期选择器的日期格式化逻辑

移除原有的分隔符相关配置项,新增formate prop统一管理日期格式,删除冗余的getFormat方法,简化代码逻辑
1 parent ed90192c
...@@ -58,6 +58,10 @@ let weekColumnsPromiseKey = ""; ...@@ -58,6 +58,10 @@ 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() {
...@@ -91,18 +95,6 @@ export default { ...@@ -91,18 +95,6 @@ export default {
return dayjs().valueOf(); return dayjs().valueOf();
}, },
}, },
separator: {
type: String,
default: "/",
},
valueSeparator: {
type: String,
default: "-",
},
weekSeparator: {
type: String,
default: "/",
},
}, },
data() { data() {
return { return {
...@@ -192,14 +184,8 @@ export default { ...@@ -192,14 +184,8 @@ export default {
} }
}, },
methods: { methods: {
getFormat({
type = ["YYYY", "MM", "DD"],
isValue = false,
} = {}) {
return type.join(isValue ? this.valueSeparator : this.separator);
},
async getWeeklyData() { async getWeeklyData() {
const cacheKey = `${dayjs(this.minDate).valueOf()}|${dayjs(this.maxDate).valueOf()}`; const cacheKey = `${this.formate}|${dayjs(this.minDate).valueOf()}|${dayjs(this.maxDate).valueOf()}`;
if (weekColumnsCache.length && weekColumnsCacheKey === cacheKey) { if (weekColumnsCache.length && weekColumnsCacheKey === cacheKey) {
this.asyncWeekColumns = weekColumnsCache; this.asyncWeekColumns = weekColumnsCache;
return; return;
...@@ -217,9 +203,9 @@ export default { ...@@ -217,9 +203,9 @@ export default {
const weekStart = cursor.subtract(weekday === 0 ? 6 : weekday - 1, "day").startOf("day"); const weekStart = cursor.subtract(weekday === 0 ? 6 : weekday - 1, "day").startOf("day");
const weekEnd = weekStart.add(6, "day").startOf("day"); const weekEnd = weekStart.add(6, "day").startOf("day");
cols.push({ cols.push({
label: `${weekStart.format(this.getFormat())}${this.weekSeparator}${weekEnd.format(this.getFormat())}`, label: `${weekStart.format(this.formate)}/${weekEnd.format(this.formate)}`,
startDate: weekStart.format(this.getFormat()), startDate: weekStart.format(this.formate),
endDate: weekEnd.format(this.getFormat()), endDate: weekEnd.format(this.formate),
index: cols.length, index: cols.length,
}); });
cursor = weekStart.subtract(1, "day"); cursor = weekStart.subtract(1, "day");
...@@ -267,7 +253,7 @@ export default { ...@@ -267,7 +253,7 @@ 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 = `${target.startDate}${this.weekSeparator}${target.endDate}`; this.date = `${target.startDate}/${target.endDate}`;
this.currentDate = this.normalizeDayjs(target.startDate).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;
...@@ -288,8 +274,8 @@ export default { ...@@ -288,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(this.getFormat({ type: ["YYYY", "MM"] })) ? normalizedDate.format("YYYY-MM")
: normalizedDate.format(this.getFormat()); : normalizedDate.format(this.formate);
this.updateBoundaryStatusByDate(normalizedDate); this.updateBoundaryStatusByDate(normalizedDate);
}, },
handleTypeChange(newType, oldType) { handleTypeChange(newType, oldType) {
...@@ -314,7 +300,7 @@ export default { ...@@ -314,7 +300,7 @@ export default {
return null; return null;
}, },
formatValueDate(input) { formatValueDate(input) {
return this.normalizeDayjs(input).format(this.getFormat({ isValue: true })); 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);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!