Commit 5cbb97a3 authored by tao.huang's avatar tao.huang

fix: 修复日期默认

parent c8b89655
......@@ -118,6 +118,17 @@ const futureYear = futureDate.getFullYear();
const futureMonth = futureDate.getMonth() + 1;
const futureDay = futureDate.getDate();
// 工具函数:根据日期字符串获取索引
function getPickerIndexByDate(dateStr) {
if (!dateStr) return null;
const [year, month, day] = dateStr.split('-').map(Number);
const yearIdx = years.value.findIndex(y => y === year);
const monthIdx = months.value.findIndex(m => m === month);
const dayIdx = days.value.findIndex(d => d === day);
if (yearIdx < 0 || monthIdx < 0 || dayIdx < 0) return null;
return [yearIdx, monthIdx, dayIdx];
}
const years = computed(() => {
const arr = [];
const startYear = currentYear - 3;
......@@ -184,8 +195,43 @@ const columns = computed(() => {
const defaultValue = computed(() => {
if (props.mode === "date") {
// 默认选中今年1月1日
return [years.value.length - 1, 0, 0];
console.log("defaultValue", props.value);
// 1. value为索引数组
if (Array.isArray(props.value) && props.value.length === 3) {
return props.value;
}
// 2. value为日期字符串
if (typeof props.value === "string" && props.value.match(/^\d{4}-\d{2}-\d{2}$/)) {
const idxArr = getPickerIndexByDate(props.value);
if (idxArr) return idxArr;
}
// 3. 默认选中今天
const yearIdx = years.value.findIndex(y => y === currentYear);
// 直接用当前年推算可用月份
let tempMonths = [];
let startMonth = 1, endMonth = 12;
if (yearIdx === 0) startMonth = currentMonth;
if (yearIdx === years.value.length - 1) endMonth = futureMonth;
for (let i = startMonth; i <= endMonth; i++) tempMonths.push(i);
const monthIdx = tempMonths.findIndex(m => m === currentMonth);
// days
let tempDays = ["请选择"];
if (yearIdx >= 0 && monthIdx >= 0) {
const selectedYear = years.value[yearIdx];
const selectedMonth = tempMonths[monthIdx];
const dayCount = new Date(selectedYear, selectedMonth, 0).getDate();
let startDay = 1, endDay = dayCount;
if (yearIdx === 0 && selectedMonth === currentMonth) startDay = currentDay;
if (yearIdx === years.value.length - 1 && selectedMonth === futureMonth) endDay = futureDay;
for (let i = startDay; i <= endDay; i++) tempDays.push(i);
}
const dayIdx = tempDays.findIndex(d => d === currentDay);
return [
yearIdx >= 0 ? yearIdx : 0,
monthIdx >= 0 ? monthIdx : 0,
dayIdx >= 0 ? dayIdx : 0,
];
} else if (Array.isArray(props.value)) {
return props.value;
} else {
......@@ -199,7 +245,7 @@ watch(
() => props.value,
(val) => {
if (props.mode === "date") {
pickerValue.value = [...val];
pickerValue.value = [...defaultValue.value];
} else if (Array.isArray(val)) {
pickerValue.value = [...val];
} else {
......@@ -211,7 +257,6 @@ watch(
function open() {
if (props.disabled) return;
pickerValue.value = [...defaultValue.value];
show.value = true;
props.onLayerVisibleChange(true);
}
......
......@@ -96,7 +96,7 @@
v-else-if="item.type === 'picker'"
:mode="item.mode"
:range="item.range"
:value="getPickerIndex(item)"
:value="item.mode === 'date' ? formData[item.name] : getPickerIndex(item)"
:onPickerChange="(e) => onPickerChange(e, item.name)"
:onLayerVisibleChange="(e) => (pageStatus.btnStatus = !e)"
:onStatusChange="onDateStatusChange"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment