diff --git a/__test__/calendar-panel.test.js b/__test__/calendar-panel.test.js index ddd439b..d6f457a 100644 --- a/__test__/calendar-panel.test.js +++ b/__test__/calendar-panel.test.js @@ -43,6 +43,19 @@ describe('CalendarPanel', () => { expect(wrapper.emitted().select[0][0]).toEqual(new Date(2010, 0)); }); + it('feat: when year >= 0 && year < 100, should be emit right', () => { + wrapper = mount(CalendarPanel, { + propsData: { + type: 'year', + defaultValue: new Date().setFullYear(11), + }, + }); + const tds = wrapper.findAll('.mx-table-year td > div'); + tds.at(0).trigger('click'); + const expectedDate = new Date(10, 0).setFullYear(10); + expect(wrapper.emitted().select[0][0].getTime()).toBe(expectedDate); + }); + it('feat: active class', () => { wrapper = mount(CalendarPanel); const td = wrapper.find('.mx-table-date td:nth-child(6)'); diff --git a/src/calendar/calendar-panel.vue b/src/calendar/calendar-panel.vue index cf8028f..3485e4a 100644 --- a/src/calendar/calendar-panel.vue +++ b/src/calendar/calendar-panel.vue @@ -98,7 +98,7 @@ import { } from 'date-fns'; import localeMixin from '../mixin/locale'; import formatMixin from '../mixin/format'; -import { getValidDate, isValidDate } from '../util/date'; +import { getValidDate, isValidDate, createDate } from '../util/date'; import TableDate from './table-date'; import TableMonth from './table-month'; import TableYear from './table-year'; @@ -114,7 +114,7 @@ export default { props: { value: {}, defaultValue: { - validator: value => isValidDate(value), + type: [Date, Number], default() { const date = new Date(); date.setHours(0, 0, 0, 0); @@ -272,12 +272,12 @@ export default { }, getCellDate(value, type) { if (type === 'year') { - return new Date(value, 0); + return createDate(value, 0); } if (type === 'month') { - return new Date(this.calendarYear, value); + return createDate(this.calendarYear, value); } - return new Date(this.calendarYear, this.calendarMonth, value); + return createDate(this.calendarYear, this.calendarMonth, value); }, getDateClasses(day) { const cellDate = this.getCellDate(day, 'date'); diff --git a/src/calendar/table-date.vue b/src/calendar/table-date.vue index 8ed608c..40d4bba 100644 --- a/src/calendar/table-date.vue +++ b/src/calendar/table-date.vue @@ -31,6 +31,7 @@ import { getWeek } from 'date-format-parse'; import localeMixin from '../mixin/locale'; import formatMixin from '../mixin/format'; import { chunk } from '../util/base'; +import { createDate } from '../util/date'; export default { name: 'TableDate', @@ -84,7 +85,7 @@ export default { const month = this.calendarMonth; // change to the last day of the last month - const calendar = new Date(year, month, 0); + const calendar = createDate(year, month, 0); const lastDayInLastMonth = calendar.getDate(); // getDay() 0 is Sunday, 1 is Monday const firstDayInLastMonth = @@ -124,13 +125,13 @@ export default { const year = this.calendarYear; const month = this.calendarMonth; const format = this.titleFormat; - const date = new Date(year, month, day); + const date = createDate(year, month, day); return this.formatDate(date, format); }, getWeekNumber(day) { const year = this.calendarYear; const month = this.calendarMonth; - const date = new Date(year, month, day); + const date = createDate(year, month, day); return getWeek(date, this.t('formatLocale')); }, }, diff --git a/src/util/date.js b/src/util/date.js index e3883c1..0b524a5 100644 --- a/src/util/date.js +++ b/src/util/date.js @@ -1,3 +1,12 @@ +// new Date(10, 0, 1) The year from 0 to 99 will be incremented by 1900 automatically. +export function createDate(y, M = 0, d = 1, h = 0, m = 0, s = 0, ms = 0) { + const date = new Date(y, M, d, h, m, s, ms); + if (y < 100 && y >= 0) { + date.setFullYear(y); + } + return date; +} + export function isValidDate(date) { return date instanceof Date && !isNaN(date); }