选择日期区间
diff --git a/src/calendar/test/index.spec.js b/src/calendar/test/index.spec.js
index 0799e95e4..1fa7c6f8c 100644
--- a/src/calendar/test/index.spec.js
+++ b/src/calendar/test/index.spec.js
@@ -18,6 +18,10 @@ 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 () => {
const wrapper = mount(Calendar, {
propsData: {
@@ -63,6 +67,36 @@ test('select event when type is range', async () => {
expect(formatRange(emittedSelect[3][0])).toEqual('2010/1/13-');
});
+test('select event when type is multiple', async () => {
+ const wrapper = mount(Calendar, {
+ propsData: {
+ type: 'multiple',
+ minDate,
+ maxDate,
+ poppable: false,
+ },
+ });
+
+ await later();
+
+ const days = wrapper.findAll('.van-calendar__day');
+ days.at(15).trigger('click');
+ days.at(16).trigger('click');
+
+ await later();
+ days.at(15).trigger('click');
+ days.at(12).trigger('click');
+
+ const emittedSelect = wrapper.emitted('select');
+ expect(formatMultiple(emittedSelect[0][0])).toEqual('2010/1/10,2010/1/16');
+ expect(formatMultiple(emittedSelect[1][0])).toEqual(
+ '2010/1/10,2010/1/16,2010/1/17'
+ );
+ expect(formatMultiple(emittedSelect[2][0])).toEqual(
+ '2010/1/10,2010/1/17,2010/1/13'
+ );
+});
+
test('should not trigger select event when click disabled day', async () => {
const wrapper = mount(Calendar, {
propsData: {
diff --git a/src/calendar/utils.ts b/src/calendar/utils.ts
index 4cd606c25..3c469438a 100644
--- a/src/calendar/utils.ts
+++ b/src/calendar/utils.ts
@@ -36,15 +36,37 @@ export function compareDay(day1: Date, day2: Date) {
return compareMonthResult;
}
-export function getNextDay(date: Date) {
+function getDayByOffset(date: Date, offset: number) {
date = new Date(date);
- date.setDate(date.getDate() + 1);
+ date.setDate(date.getDate() + offset);
return date;
}
+export function getPrevDay(date: Date) {
+ return getDayByOffset(date, -1);
+}
+
+export function getNextDay(date: Date) {
+ return getDayByOffset(date, 1);
+}
+
export function calcDateNum(date: [Date, Date]) {
const day1 = date[0].getTime();
const day2 = date[1].getTime();
return (day2 - day1) / (1000 * 60 * 60 * 24) + 1;
}
+
+export function copyDates(dates: Date | Date[]) {
+ if (Array.isArray(dates)) {
+ return dates.map(date => {
+ if (date === null) {
+ return date;
+ }
+
+ return new Date(date);
+ });
+ }
+
+ return new Date(dates);
+}