Skip to content

Commit

Permalink
fix: when year < 100 && year >= 0 should be right
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxiong10 committed Nov 13, 2019
1 parent c03632f commit 8c546cc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
13 changes: 13 additions & 0 deletions __test__/calendar-panel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
Expand Down
10 changes: 5 additions & 5 deletions src/calendar/calendar-panel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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');
Expand Down
7 changes: 4 additions & 3 deletions src/calendar/table-date.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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'));
},
},
Expand Down
9 changes: 9 additions & 0 deletions src/util/date.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down

0 comments on commit 8c546cc

Please sign in to comment.