perf(Calendar): auto selected to max range (#3026)

fix #3021
This commit is contained in:
rex 2020-04-15 21:16:54 +08:00 committed by GitHub
parent e054aafa2d
commit ad30ba60e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 18 deletions

View File

@ -7,7 +7,8 @@ import {
calcDateNum, calcDateNum,
formatMonthTitle, formatMonthTitle,
compareMonth, compareMonth,
getMonths getMonths,
getDayByOffset
} from './utils'; } from './utils';
import Toast from '../toast/toast'; import Toast from '../toast/toast';
@ -266,6 +267,28 @@ VantComponent({
}, },
select(date, complete) { select(date, complete) {
if (complete && this.data.type === 'range') {
const valid = this.checkRange(date);
if (!valid) {
// auto selected to max range if showConfirm
if (this.data.showConfirm) {
this.emit([date[0], getDayByOffset(date[0], this.data.maxRange - 1)]);
} else {
this.emit(date);
}
return;
}
}
this.emit(date);
if (complete && !this.data.showConfirm) {
this.onConfirm();
}
},
emit(date) {
const getTime = (date: Date | number) => const getTime = (date: Date | number) =>
(date instanceof Date ? date.getTime() : date); (date instanceof Date ? date.getTime() : date);
@ -273,25 +296,16 @@ VantComponent({
currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date) currentDate: Array.isArray(date) ? date.map(getTime) : getTime(date)
}); });
this.$emit('select', copyDates(date)); this.$emit('select', copyDates(date));
if (complete && this.data.type === 'range') {
const valid = this.checkRange();
if (!valid) {
return;
}
}
if (complete && !this.data.showConfirm) {
this.onConfirm();
}
}, },
checkRange() { checkRange(date) {
const { maxRange, currentDate, rangePrompt } = this.data; const { maxRange, rangePrompt } = this.data;
if (maxRange && calcDateNum(currentDate) > maxRange) { if (maxRange && calcDateNum(date) > maxRange) {
Toast(rangePrompt || `选择天数不能超过 ${maxRange}`); Toast({
context: this,
message: rangePrompt || `选择天数不能超过 ${maxRange}`
});
return false; return false;
} }

View File

@ -27,3 +27,5 @@
is="calendar" is="calendar"
data="{{ title, subtitle, showTitle, showSubtitle, minDate, maxDate, type, color, showMark, formatter, rowHeight, currentDate, safeAreaInsetBottom, showConfirm, confirmDisabledText, confirmText, scrollIntoView, allowSameDay }}" data="{{ title, subtitle, showTitle, showSubtitle, minDate, maxDate, type, color, showMark, formatter, rowHeight, currentDate, safeAreaInsetBottom, showConfirm, confirmDisabledText, confirmText, scrollIntoView, allowSameDay }}"
/> />
<van-toast id="van-toast" />

View File

@ -49,7 +49,7 @@ export function compareDay(day1: Date | number, day2: Date | number) {
return compareMonthResult; return compareMonthResult;
} }
function getDayByOffset(date: Date, offset: number) { export function getDayByOffset(date: Date, offset: number) {
date = new Date(date); date = new Date(date);
date.setDate(date.getDate() + offset); date.setDate(date.getDate() + offset);