mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-25 02:41:46 +08:00
chore: improve calendar utils (#8521)
This commit is contained in:
parent
35edb72b5c
commit
9381640d2e
@ -14,8 +14,8 @@ import {
|
|||||||
t,
|
t,
|
||||||
bem,
|
bem,
|
||||||
name,
|
name,
|
||||||
copyDate,
|
cloneDate,
|
||||||
copyDates,
|
cloneDates,
|
||||||
getPrevDay,
|
getPrevDay,
|
||||||
getNextDay,
|
getNextDay,
|
||||||
compareDay,
|
compareDay,
|
||||||
@ -341,12 +341,12 @@ export default defineComponent({
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const onConfirm = () => emit('confirm', copyDates(state.currentDate));
|
const onConfirm = () => emit('confirm', cloneDates(state.currentDate));
|
||||||
|
|
||||||
const select = (date: Date | Date[], complete?: boolean) => {
|
const select = (date: Date | Date[], complete?: boolean) => {
|
||||||
const setCurrentDate = (date: Date | Date[]) => {
|
const setCurrentDate = (date: Date | Date[]) => {
|
||||||
state.currentDate = date;
|
state.currentDate = date;
|
||||||
emit('select', copyDates(state.currentDate));
|
emit('select', cloneDates(state.currentDate));
|
||||||
};
|
};
|
||||||
|
|
||||||
if (complete && props.type === 'range') {
|
if (complete && props.type === 'range') {
|
||||||
@ -422,7 +422,7 @@ export default defineComponent({
|
|||||||
|
|
||||||
if (selected) {
|
if (selected) {
|
||||||
const [unselectedDate] = currentDate.splice(selectedIndex, 1);
|
const [unselectedDate] = currentDate.splice(selectedIndex, 1);
|
||||||
emit('unselect', copyDate(unselectedDate));
|
emit('unselect', cloneDate(unselectedDate));
|
||||||
} else if (props.maxRange && currentDate.length >= props.maxRange) {
|
} else if (props.maxRange && currentDate.length >= props.maxRange) {
|
||||||
Toast(props.rangePrompt || t('rangePrompt', props.maxRange));
|
Toast(props.rangePrompt || t('rangePrompt', props.maxRange));
|
||||||
} else {
|
} else {
|
||||||
|
@ -11,10 +11,10 @@ export function formatMonthTitle(date: Date) {
|
|||||||
export function compareMonth(date1: Date, date2: Date) {
|
export function compareMonth(date1: Date, date2: Date) {
|
||||||
const year1 = date1.getFullYear();
|
const year1 = date1.getFullYear();
|
||||||
const year2 = date2.getFullYear();
|
const year2 = date2.getFullYear();
|
||||||
const month1 = date1.getMonth();
|
|
||||||
const month2 = date2.getMonth();
|
|
||||||
|
|
||||||
if (year1 === year2) {
|
if (year1 === year2) {
|
||||||
|
const month1 = date1.getMonth();
|
||||||
|
const month2 = date2.getMonth();
|
||||||
return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
|
return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,48 +27,28 @@ export function compareDay(day1: Date, day2: Date) {
|
|||||||
if (compareMonthResult === 0) {
|
if (compareMonthResult === 0) {
|
||||||
const date1 = day1.getDate();
|
const date1 = day1.getDate();
|
||||||
const date2 = day2.getDate();
|
const date2 = day2.getDate();
|
||||||
|
|
||||||
return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
|
return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return compareMonthResult;
|
return compareMonthResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const cloneDate = (date: Date) => new Date(date);
|
||||||
|
|
||||||
|
export const cloneDates = (dates: Date | Date[]) =>
|
||||||
|
Array.isArray(dates) ? dates.map(cloneDate) : cloneDate(dates);
|
||||||
|
|
||||||
export function getDayByOffset(date: Date, offset: number) {
|
export function getDayByOffset(date: Date, offset: number) {
|
||||||
date = new Date(date);
|
const cloned = cloneDate(date);
|
||||||
date.setDate(date.getDate() + offset);
|
cloned.setDate(cloned.getDate() + offset);
|
||||||
|
return cloned;
|
||||||
return date;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPrevDay(date: Date) {
|
export const getPrevDay = (date: Date) => getDayByOffset(date, -1);
|
||||||
return getDayByOffset(date, -1);
|
export const getNextDay = (date: Date) => getDayByOffset(date, 1);
|
||||||
}
|
|
||||||
|
|
||||||
export function getNextDay(date: Date) {
|
|
||||||
return getDayByOffset(date, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function calcDateNum(date: [Date, Date]) {
|
export function calcDateNum(date: [Date, Date]) {
|
||||||
const day1 = date[0].getTime();
|
const day1 = date[0].getTime();
|
||||||
const day2 = date[1].getTime();
|
const day2 = date[1].getTime();
|
||||||
return (day2 - day1) / (1000 * 60 * 60 * 24) + 1;
|
return (day2 - day1) / (1000 * 60 * 60 * 24) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function copyDate(dates: Date) {
|
|
||||||
return new Date(dates);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function copyDates(dates: Date | Date[]) {
|
|
||||||
if (Array.isArray(dates)) {
|
|
||||||
return dates.map((date) => {
|
|
||||||
if (date === null) {
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
|
|
||||||
return copyDate(date);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return copyDate(dates);
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user