fix(Calendar): set current date is inital (#4339)

* feat(Calendar): set current date is inital

* fix(Calendar): adjust limitDateRange
This commit is contained in:
nemo-shen 2021-07-16 17:32:55 +08:00 committed by GitHub
parent 853c56b26d
commit 82811d4058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 14 deletions

View File

@ -1,7 +1,9 @@
import { VantComponent } from '../common/component';
import {
ROW_HEIGHT,
getPrevDay,
getNextDay,
getToday,
compareDay,
copyDates,
calcDateNum,
@ -14,6 +16,16 @@ import {
import Toast from '../toast/toast';
import { requestAnimationFrame } from '../common/utils';
const initialMinDate = getToday().getTime();
const initialMaxDate = (() => {
const now = getToday();
return new Date(
now.getFullYear(),
now.getMonth() + 6,
now.getDate()
).getTime();
})();
VantComponent({
props: {
title: {
@ -56,15 +68,11 @@ VantComponent({
},
minDate: {
type: null,
value: Date.now(),
value: initialMinDate,
},
maxDate: {
type: null,
value: new Date(
new Date().getFullYear(),
new Date().getMonth() + 6,
new Date().getDate()
).getTime(),
value: initialMaxDate,
},
position: {
type: String,
@ -162,22 +170,58 @@ VantComponent({
});
},
getInitialDate() {
const { type, defaultDate, minDate } = this.data;
limitDateRange(
date: number,
minDate: number | null = null,
maxDate: number | null = null
) {
minDate = minDate || (this.data.minDate as number);
maxDate = maxDate || (this.data.maxDate as number);
if (compareDay(date, minDate) === -1) {
return minDate;
}
if (compareDay(date, maxDate) === 1) {
return maxDate;
}
return date;
},
getInitialDate(defaultDate: number | number[] | null = null) {
const { type, minDate, maxDate } = this.data;
const now = getToday().getTime();
if (type === 'range') {
if (!Array.isArray(defaultDate)) {
defaultDate = [];
}
const [startDay, endDay] = defaultDate || [];
return [
startDay || minDate,
endDay || getNextDay(new Date(minDate)).getTime(),
];
const start = this.limitDateRange(
startDay || now,
minDate,
getPrevDay(maxDate).getTime()
);
const end = this.limitDateRange(
endDay || now,
getNextDay(minDate).getTime()
);
return [start, end];
}
if (type === 'multiple') {
return defaultDate || [minDate];
if (Array.isArray(defaultDate)) {
return defaultDate.map((date) => this.limitDateRange(date));
}
return [this.limitDateRange(now)];
}
return defaultDate || minDate;
if (!defaultDate || Array.isArray(defaultDate)) {
defaultDate = now;
}
return this.limitDateRange(defaultDate);
},
scrollIntoView() {

View File

@ -64,6 +64,12 @@ export function getNextDay(date: Date) {
return getDayByOffset(date, 1);
}
export function getToday() {
const today = new Date();
today.setHours(0, 0, 0, 0);
return today;
}
export function calcDateNum(date: [Date, Date]) {
const day1 = new Date(date[0]).getTime();
const day2 = new Date(date[1]).getTime();