Commit edca124a by Jaxon

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

移除原有的分隔符相关配置项,新增formate prop统一管理日期格式,删除冗余的getFormat方法,简化代码逻辑
1 parent ed90192c
......@@ -58,6 +58,10 @@ let weekColumnsPromiseKey = "";
export default {
name: "DatePicker",
props: {
formate: {
type: String,
default: "YYYY-MM-DD",
},
value: {
type: Object,
default() {
......@@ -91,18 +95,6 @@ export default {
return dayjs().valueOf();
},
},
separator: {
type: String,
default: "/",
},
valueSeparator: {
type: String,
default: "-",
},
weekSeparator: {
type: String,
default: "/",
},
},
data() {
return {
......@@ -192,14 +184,8 @@ export default {
}
},
methods: {
getFormat({
type = ["YYYY", "MM", "DD"],
isValue = false,
} = {}) {
return type.join(isValue ? this.valueSeparator : this.separator);
},
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) {
this.asyncWeekColumns = weekColumnsCache;
return;
......@@ -217,9 +203,9 @@ export default {
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.getFormat())}${this.weekSeparator}${weekEnd.format(this.getFormat())}`,
startDate: weekStart.format(this.getFormat()),
endDate: weekEnd.format(this.getFormat()),
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");
......@@ -267,7 +253,7 @@ export default {
this.currentWeek = target;
this.defaultIndex = target ? target.index : 0;
if (target) {
this.date = `${target.startDate}${this.weekSeparator}${target.endDate}`;
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;
......@@ -288,8 +274,8 @@ export default {
this.currentWeek = null;
this.currentDate = normalizedDate.toDate();
this.date = type === "year-month"
? normalizedDate.format(this.getFormat({ type: ["YYYY", "MM"] }))
: normalizedDate.format(this.getFormat());
? normalizedDate.format("YYYY-MM")
: normalizedDate.format(this.formate);
this.updateBoundaryStatusByDate(normalizedDate);
},
handleTypeChange(newType, oldType) {
......@@ -314,7 +300,7 @@ export default {
return null;
},
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) {
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!