DatePicker: fix date clamping during navigation (#9577)

pull/9581/head
Jiewei Qian 2018-01-31 14:20:07 +08:00 committed by 杨奕
parent ab2f3830d6
commit 5b0b1e7e92
4 changed files with 65 additions and 48 deletions

View File

@ -190,6 +190,8 @@
isDate, isDate,
modifyDate, modifyDate,
modifyTime, modifyTime,
prevYear,
nextYear,
prevMonth, prevMonth,
nextMonth nextMonth
} from '../util'; } from '../util';
@ -557,17 +559,14 @@
} }
}, },
// leftPrev*, rightNext* need to take care of `unlinkPanels`
leftPrevYear() { leftPrevYear() {
this.leftDate = modifyDate(this.leftDate, this.leftYear - 1, this.leftMonth, this.leftMonthDate); this.leftDate = prevYear(this.leftDate);
if (!this.unlinkPanels) { if (!this.unlinkPanels) {
this.rightDate = nextMonth(this.leftDate); this.rightDate = nextMonth(this.leftDate);
} }
}, },
leftNextYear() {
this.leftDate = modifyDate(this.leftDate, this.leftYear + 1, this.leftMonth, this.leftMonthDate);
},
leftPrevMonth() { leftPrevMonth() {
this.leftDate = prevMonth(this.leftDate); this.leftDate = prevMonth(this.leftDate);
if (!this.unlinkPanels) { if (!this.unlinkPanels) {
@ -575,27 +574,15 @@
} }
}, },
leftNextMonth() {
this.leftDate = nextMonth(this.leftDate);
},
rightPrevYear() {
this.rightDate = modifyDate(this.rightDate, this.rightYear - 1, this.rightMonth, this.rightMonthDate);
},
rightNextYear() { rightNextYear() {
if (!this.unlinkPanels) { if (!this.unlinkPanels) {
this.leftDate = modifyDate(this.leftDate, this.leftYear + 1, this.leftMonth, this.leftMonthDate); this.leftDate = nextYear(this.leftDate);
this.rightDate = nextMonth(this.leftDate); this.rightDate = nextMonth(this.leftDate);
} else { } else {
this.rightDate = modifyDate(this.rightDate, this.rightYear + 1, this.rightMonth, this.rightMonthDate); this.rightDate = nextYear(this.rightDate);
} }
}, },
rightPrevMonth() {
this.rightDate = prevMonth(this.rightDate);
},
rightNextMonth() { rightNextMonth() {
if (!this.unlinkPanels) { if (!this.unlinkPanels) {
this.leftDate = nextMonth(this.leftDate); this.leftDate = nextMonth(this.leftDate);
@ -605,6 +592,23 @@
} }
}, },
// leftNext*, rightPrev* are called when `unlinkPanels` is true
leftNextYear() {
this.leftDate = nextYear(this.leftDate);
},
leftNextMonth() {
this.leftDate = nextMonth(this.leftDate);
},
rightPrevYear() {
this.rightDate = prevYear(this.rightDate);
},
rightPrevMonth() {
this.rightDate = prevMonth(this.rightDate);
},
handleConfirm(visible = false) { handleConfirm(visible = false) {
this.$emit('pick', [this.minDate, this.maxDate], visible); this.$emit('pick', [this.minDate, this.maxDate], visible);
}, },

View File

@ -150,7 +150,8 @@
prevYear, prevYear,
nextYear, nextYear,
prevMonth, prevMonth,
nextMonth nextMonth,
changeYearMonthAndClampDate
} from '../util'; } from '../util';
import Locale from 'element-ui/src/mixins/locale'; import Locale from 'element-ui/src/mixins/locale';
import ElInput from 'element-ui/packages/input'; import ElInput from 'element-ui/packages/input';
@ -304,7 +305,7 @@
this.date = modifyDate(this.date, this.year, month, 1); this.date = modifyDate(this.date, this.year, month, 1);
this.emit(this.date); this.emit(this.date);
} else { } else {
this.date = modifyDate(this.date, this.year, month, this.monthDate); this.date = changeYearMonthAndClampDate(this.date, this.year, month);
// TODO: should emit intermediate value ?? // TODO: should emit intermediate value ??
// this.emit(this.date); // this.emit(this.date);
this.currentView = 'date'; this.currentView = 'date';
@ -325,7 +326,7 @@
this.date = modifyDate(this.date, year, 0, 1); this.date = modifyDate(this.date, year, 0, 1);
this.emit(this.date); this.emit(this.date);
} else { } else {
this.date = modifyDate(this.date, year, this.month, this.monthDate); this.date = changeYearMonthAndClampDate(this.date, year, this.month);
// TODO: should emit intermediate value ?? // TODO: should emit intermediate value ??
// this.emit(this.date, true); // this.emit(this.date, true);
this.currentView = 'month'; this.currentView = 'month';

View File

@ -181,43 +181,37 @@ export const timeWithinRange = function(date, selectableRange, format) {
return limitedDate.getTime() === date.getTime(); return limitedDate.getTime() === date.getTime();
}; };
export const prevMonth = function(date) { export const changeYearMonthAndClampDate = function(date, year, month) {
let year = date.getFullYear(); // clamp date to the number of days in `year`, `month`
let month = date.getMonth(); // eg: (2010-1-31, 2010, 2) => 2010-2-28
if (month === 0) {
year -= 1;
month = 11;
} else {
month -= 1;
}
const monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month)); const monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month));
return modifyDate(date, year, month, monthDate); return modifyDate(date, year, month, monthDate);
}; };
export const prevMonth = function(date) {
const year = date.getFullYear();
const month = date.getMonth();
return month === 0
? changeYearMonthAndClampDate(date, year - 1, 11)
: changeYearMonthAndClampDate(date, year, month - 1);
};
export const nextMonth = function(date) { export const nextMonth = function(date) {
let year = date.getFullYear(); const year = date.getFullYear();
let month = date.getMonth(); const month = date.getMonth();
if (month === 11) { return month === 11
year += 1; ? changeYearMonthAndClampDate(date, year + 1, 0)
month = 0; : changeYearMonthAndClampDate(date, year, month + 1);
} else {
month += 1;
}
const monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month));
return modifyDate(date, year, month, monthDate);
}; };
// check for leap year Feburary
export const prevYear = function(date, amount = 1) { export const prevYear = function(date, amount = 1) {
const year = date.getFullYear() - amount; const year = date.getFullYear();
const month = date.getMonth(); const month = date.getMonth();
const monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month)); return changeYearMonthAndClampDate(date, year - amount, month);
return modifyDate(date, year, month, monthDate);
}; };
export const nextYear = function(date, amount = 1) { export const nextYear = function(date, amount = 1) {
const year = date.getFullYear() + amount; const year = date.getFullYear();
const month = date.getMonth(); const month = date.getMonth();
const monthDate = Math.min(date.getDate(), getDayCountOfMonth(year, month)); return changeYearMonthAndClampDate(date, year + amount, month);
return modifyDate(date, year, month, monthDate);
}; };

View File

@ -850,6 +850,24 @@ describe('DatePicker', () => {
}); });
}); });
}); });
it('month label with fewer dates', done => {
navigationTest(new Date(2000, 6, 31), _ => {
const $el = vm.$refs.compo.picker.$el;
const monthLabel = $el.querySelectorAll('.el-date-picker__header-label')[1];
click(monthLabel, _ => {
setTimeout(_ => {
const juneLabel = $el.querySelectorAll('.el-month-table td a')[5];
juneLabel.click();
setTimeout(_ => {
expect(getYearLabel()).to.include('2000');
expect(getMonthLabel()).to.include('6');
done();
}, DELAY);
}, DELAY);
});
});
});
}); });
it('type:month', done => { it('type:month', done => {