Skip to content

Commit cc3ec5f

Browse files
committed
refactor: optimized code
1 parent 8907810 commit cc3ec5f

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

src/calendar/table-date.vue

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
import { getWeek, format } from 'date-format-parse';
6464
import IconButton from './icon-button';
6565
import { chunk } from '../util/base';
66-
import { createDate, getCalendar } from '../util/date';
66+
import { getCalendar, setMonth, setYear } from '../util/date';
6767
import { getLocale } from '../locale';
6868
6969
export default {
@@ -141,22 +141,33 @@ export default {
141141
},
142142
},
143143
methods: {
144-
getNextCalendar(diffMonth) {
145-
const year = this.calendar.getFullYear();
146-
const month = this.calendar.getMonth();
147-
return createDate(year, month + diffMonth);
148-
},
149144
handleIconLeftClick() {
150-
this.$emit('changecalendar', this.getNextCalendar(-1), 'last-month');
145+
this.$emit(
146+
'changecalendar',
147+
setMonth(this.calendar, v => v - 1),
148+
'last-month'
149+
);
151150
},
152151
handleIconRightClick() {
153-
this.$emit('changecalendar', this.getNextCalendar(1), 'next-month');
152+
this.$emit(
153+
'changecalendar',
154+
setMonth(this.calendar, v => v + 1),
155+
'next-month'
156+
);
154157
},
155158
handleIconDoubleLeftClick() {
156-
this.$emit('changecalendar', this.getNextCalendar(-12), 'last-year');
159+
this.$emit(
160+
'changecalendar',
161+
setYear(this.calendar, v => v - 1),
162+
'last-year'
163+
);
157164
},
158165
handleIconDoubleRightClick() {
159-
this.$emit('changecalendar', this.getNextCalendar(12), 'next-year');
166+
this.$emit(
167+
'changecalendar',
168+
setYear(this.calendar, v => v + 1),
169+
'next-year'
170+
);
160171
},
161172
handlePanelChange(panel) {
162173
this.$emit('changepanel', panel);

src/calendar/table-month.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import { chunk } from '../util/base';
3636
import IconButton from './icon-button';
3737
import { getLocale } from '../locale';
38-
import { createDate } from '../util/date';
38+
import { setYear } from '../util/date';
3939
4040
export default {
4141
name: 'TableMonth',
@@ -72,16 +72,19 @@ export default {
7272
},
7373
},
7474
methods: {
75-
getNextCalendar(diffYear) {
76-
const year = this.calendar.getFullYear();
77-
const month = this.calendar.getMonth();
78-
return createDate(year + diffYear, month);
79-
},
8075
handleIconDoubleLeftClick() {
81-
this.$emit('changecalendar', this.getNextCalendar(-1), 'last-year');
76+
this.$emit(
77+
'changecalendar',
78+
setYear(this.calendar, v => v - 1),
79+
'last-year'
80+
);
8281
},
8382
handleIconDoubleRightClick() {
84-
this.$emit('changecalendar', this.getNextCalendar(1), 'next-year');
83+
this.$emit(
84+
'changecalendar',
85+
setYear(this.calendar, v => v + 1),
86+
'next-year'
87+
);
8588
},
8689
handlePanelChange() {
8790
this.$emit('changepanel', 'year');

src/calendar/table-year.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<script>
3131
import IconButton from './icon-button';
3232
import { chunk } from '../util/base';
33-
import { createDate } from '../util/date';
33+
import { setYear } from '../util/date';
3434
3535
export default {
3636
name: 'TableYear',
@@ -78,16 +78,19 @@ export default {
7878
}
7979
return chunk(years, 2);
8080
},
81-
getNextCalendar(diffYear) {
82-
const year = this.calendar.getFullYear();
83-
const month = this.calendar.getMonth();
84-
return createDate(year + diffYear, month);
85-
},
8681
handleIconDoubleLeftClick() {
87-
this.$emit('changecalendar', this.getNextCalendar(-10), 'last-decade');
82+
this.$emit(
83+
'changecalendar',
84+
setYear(this.calendar, v => v - 10),
85+
'last-decade'
86+
);
8887
},
8988
handleIconDoubleRightClick() {
90-
this.$emit('changecalendar', this.getNextCalendar(10), 'next-decade');
89+
this.$emit(
90+
'changecalendar',
91+
setYear(this.calendar, v => v + 10),
92+
'next-decade'
93+
);
9194
},
9295
handleClick(evt) {
9396
let { target } = evt;

src/date-picker.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,16 @@ export default {
284284
return this.formatDate(date, this.valueType);
285285
}
286286
},
287-
emitValue(date, type) {
287+
emitValue(date, type, close = true) {
288288
// fix IE11/10 trigger input event when input is focused. (placeholder !== '')
289289
this.userInput = null;
290290
const value = Array.isArray(date) ? date.map(this.date2value) : this.date2value(date);
291291
this.$emit('input', value);
292292
this.$emit('change', value, type);
293-
this.afterEmitValue(type);
294-
return value;
295-
},
296-
afterEmitValue(type) {
297-
// this.type === 'datetime', click the time should close popup
298-
if (!type || type === this.type || type === 'time') {
293+
if (close) {
299294
this.closePopup();
300295
}
296+
return value;
301297
},
302298
isValidValue(value) {
303299
if (this.validMultipleType) {
@@ -336,7 +332,12 @@ export default {
336332
if (this.confirm) {
337333
this.currentValue = val;
338334
} else {
339-
this.emitValue(val, this.validMultipleType ? `multiple-${type}` : type);
335+
this.emitValue(
336+
val,
337+
type,
338+
// this.type === 'datetime', click the time should close popup
339+
!this.validMultipleType && (type === this.type || type === 'time')
340+
);
340341
}
341342
},
342343
clear() {

src/util/date.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,21 @@ export function getCalendar({ firstDayOfWeek, year, month }) {
7777

7878
export function setMonth(dirtyDate, dirtyMonth) {
7979
const date = new Date(dirtyDate);
80-
const month = Number(dirtyMonth);
80+
const month = typeof dirtyMonth === 'function' ? dirtyMonth(date.getMonth()) : Number(dirtyMonth);
8181
const year = date.getFullYear();
8282
const daysInMonth = createDate(year, month + 1, 0).getDate();
8383
const day = date.getDate();
8484
date.setMonth(month, Math.min(day, daysInMonth));
8585
return date;
8686
}
8787

88+
export function setYear(dirtyDate, dirtyYear) {
89+
const date = new Date(dirtyDate);
90+
const year = typeof dirtyYear === 'function' ? dirtyYear(date.getFullYear()) : dirtyYear;
91+
date.setFullYear(year);
92+
return date;
93+
}
94+
8895
export function assignTime(target, source) {
8996
const date = new Date(target);
9097
const time = new Date(source);

0 commit comments

Comments
 (0)