feat(Calendar): auto selected to max range (#5992)

This commit is contained in:
neverland 2020-04-03 15:22:02 +08:00 committed by GitHub
parent 3f8928b02d
commit 64e77f3d2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 41 deletions

View File

@ -65,6 +65,7 @@
"devDependencies": { "devDependencies": {
"@ls-lint/ls-lint": "^1.8.0", "@ls-lint/ls-lint": "^1.8.0",
"@vant/cli": "^2.4.0", "@vant/cli": "^2.4.0",
"prettier": "^2.0.2",
"vue": "^2.6.11", "vue": "^2.6.11",
"vue-template-compiler": "^2.6.11" "vue-template-compiler": "^2.6.11"
}, },

View File

@ -7,10 +7,11 @@ import {
copyDates, copyDates,
getNextDay, getNextDay,
compareDay, compareDay,
ROW_HEIGHT,
calcDateNum,
compareMonth, compareMonth,
createComponent, createComponent,
calcDateNum, getDayByOffset,
ROW_HEIGHT,
} from './utils'; } from './utils';
// Components // Components
@ -294,26 +295,36 @@ export default createComponent({
}, },
select(date, complete) { select(date, complete) {
const emit = (date) => {
this.currentDate = date; this.currentDate = date;
this.$emit('select', copyDates(this.currentDate)); this.$emit('select', copyDates(this.currentDate));
};
if (complete && this.type === 'range') { if (complete && this.type === 'range') {
const valid = this.checkRange(); const valid = this.checkRange(date);
if (!valid) { if (!valid) {
// auto selected to max range if showConfirm
if (this.showConfirm) {
emit([date[0], getDayByOffset(date[0], this.maxRange - 1)]);
} else {
emit(date);
}
return; return;
} }
} }
emit(date);
if (complete && !this.showConfirm) { if (complete && !this.showConfirm) {
this.onConfirm(); this.onConfirm();
} }
}, },
checkRange() { checkRange(date) {
const { maxRange, currentDate, rangePrompt } = this; const { maxRange, rangePrompt } = this;
if (maxRange && calcDateNum(currentDate) > maxRange) { if (maxRange && calcDateNum(date) > maxRange) {
Toast(rangePrompt || t('rangePrompt', maxRange)); Toast(rangePrompt || t('rangePrompt', maxRange));
return false; return false;
} }
@ -322,10 +333,6 @@ export default createComponent({
}, },
onConfirm() { onConfirm() {
if (this.type === 'range' && !this.checkRange()) {
return;
}
this.$emit('confirm', copyDates(this.currentDate)); this.$emit('confirm', copyDates(this.currentDate));
}, },

View File

@ -1,26 +1,14 @@
import Calendar from '..'; import Calendar from '..';
import { mount, later } from '../../../test'; import { mount, later } from '../../../test';
import { getNextDay } from '../utils'; import { getNextDay } from '../utils';
import {
const now = new Date(); now,
const minDate = new Date(2010, 0, 10); minDate,
const maxDate = new Date(2010, 0, 20); maxDate,
formatDate,
function formatDate(date) { formatRange,
if (date) { formatMultiple,
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`; } from './utils';
}
return '';
}
function formatRange([start, end]) {
return `${formatDate(start)}-${formatDate(end)}`;
}
function formatMultiple(dates) {
return dates.map(formatDate).join(',');
}
test('select event when type is single', async () => { test('select event when type is single', async () => {
const wrapper = mount(Calendar, { const wrapper = mount(Calendar, {

View File

@ -1,16 +1,39 @@
import Calendar from '..'; import Calendar from '..';
import { mount, later } from '../../../test'; import { mount, later } from '../../../test';
import { minDate, maxDate, formatRange } from './utils';
const minDate = new Date(2010, 0, 10); test('max-range prop when showConfirm is false', async () => {
const maxDate = new Date(2010, 0, 20);
test('max-range prop', async () => {
const wrapper = mount(Calendar, { const wrapper = mount(Calendar, {
propsData: { propsData: {
type: 'range', type: 'range',
minDate, minDate,
maxDate, maxDate,
maxRange: 1, maxRange: 3,
poppable: false,
showConfirm: false,
},
});
await later();
const days = wrapper.findAll('.van-calendar__day');
days.at(12).trigger('click');
days.at(18).trigger('click');
expect(formatRange(wrapper.emitted('select')[0][0])).toEqual('2010/1/13-');
expect(formatRange(wrapper.emitted('select')[1][0])).toEqual(
'2010/1/13-2010/1/19'
);
expect(wrapper.emitted('confirm')).toBeFalsy();
});
test('max-range prop when showConfirm is true', async () => {
const wrapper = mount(Calendar, {
propsData: {
type: 'range',
minDate,
maxDate,
maxRange: 3,
poppable: false, poppable: false,
}, },
}); });
@ -18,10 +41,13 @@ test('max-range prop', async () => {
await later(); await later();
const days = wrapper.findAll('.van-calendar__day'); const days = wrapper.findAll('.van-calendar__day');
days.at(15).trigger('click'); days.at(12).trigger('click');
days.at(18).trigger('click'); days.at(18).trigger('click');
wrapper.find('.van-calendar__confirm').trigger('click');
expect(formatRange(wrapper.emitted('select')[0][0])).toEqual('2010/1/13-');
expect(formatRange(wrapper.emitted('select')[1][0])).toEqual(
'2010/1/13-2010/1/15'
);
expect(wrapper.emitted('confirm')).toBeFalsy(); expect(wrapper.emitted('confirm')).toBeFalsy();
}); });

View File

@ -0,0 +1,19 @@
export const now = new Date();
export const minDate = new Date(2010, 0, 10);
export const maxDate = new Date(2010, 0, 20);
export function formatDate(date) {
if (date) {
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
}
return '';
}
export function formatRange([start, end]) {
return `${formatDate(start)}-${formatDate(end)}`;
}
export function formatMultiple(dates) {
return dates.map(formatDate).join(',');
}

View File

@ -36,7 +36,7 @@ export function compareDay(day1: Date, day2: Date) {
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);

View File

@ -9031,7 +9031,7 @@ prettier@^1.18.2:
prettier@^2.0.2: prettier@^2.0.2:
version "2.0.2" version "2.0.2"
resolved "https://registry.npm.taobao.org/prettier/download/prettier-2.0.2.tgz?cache=0&sync_timestamp=1585003173514&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fprettier%2Fdownload%2Fprettier-2.0.2.tgz#1ba8f3eb92231e769b7fcd7cb73ae1b6b74ade08" resolved "https://registry.npm.taobao.org/prettier/download/prettier-2.0.2.tgz?cache=0&sync_timestamp=1585003590220&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fprettier%2Fdownload%2Fprettier-2.0.2.tgz#1ba8f3eb92231e769b7fcd7cb73ae1b6b74ade08"
integrity sha1-G6jz65IjHnabf818tzrhtrdK3gg= integrity sha1-G6jz65IjHnabf818tzrhtrdK3gg=
pretty-error@^2.1.1: pretty-error@^2.1.1: