Skip to content

Commit d2eaf35

Browse files
committed
date-time-picker: fix incorrect date on time pick
1 parent eb1479d commit d2eaf35

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

packages/date-picker/src/panel/date-range.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
isDate,
191191
modifyDate,
192192
modifyTime,
193-
modifyWithDefaultTime,
193+
modifyWithTimeString,
194194
prevYear,
195195
nextYear,
196196
prevMonth,
@@ -498,8 +498,8 @@
498498
499499
handleRangePick(val, close = true) {
500500
const defaultTime = this.defaultTime || [];
501-
const minDate = modifyWithDefaultTime(val.minDate, defaultTime[0]);
502-
const maxDate = modifyWithDefaultTime(val.maxDate, defaultTime[1]);
501+
const minDate = modifyWithTimeString(val.minDate, defaultTime[0]);
502+
const maxDate = modifyWithTimeString(val.maxDate, defaultTime[1]);
503503
504504
if (this.maxDate === maxDate && this.minDate === minDate) {
505505
return;

packages/date-picker/src/panel/date.vue

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
isDate,
151151
modifyDate,
152152
modifyTime,
153-
modifyWithDefaultTime,
153+
modifyWithTimeString,
154154
clearMilliseconds,
155155
clearTime,
156156
prevYear,
@@ -192,7 +192,7 @@
192192
if (isDate(val)) {
193193
this.date = new Date(val);
194194
} else {
195-
this.date = this.defaultValue ? new Date(this.defaultValue) : new Date();
195+
this.date = this.getDefaultValue();
196196
}
197197
},
198198
@@ -233,7 +233,7 @@
233233
},
234234
235235
handleClear() {
236-
this.date = this.defaultValue ? new Date(this.defaultValue) : new Date();
236+
this.date = this.getDefaultValue();
237237
this.$emit('pick', null);
238238
},
239239
@@ -303,7 +303,9 @@
303303
304304
handleTimePick(value, visible, first) {
305305
if (isDate(value)) {
306-
const newDate = this.value ? modifyTime(this.date, value.getHours(), value.getMinutes(), value.getSeconds()) : modifyWithDefaultTime(value, this.defaultTime);
306+
const newDate = this.value
307+
? modifyTime(this.value, value.getHours(), value.getMinutes(), value.getSeconds())
308+
: modifyWithTimeString(this.getDefaultValue(), this.defaultTime);
307309
this.date = newDate;
308310
this.emit(this.date, true);
309311
} else {
@@ -334,7 +336,9 @@
334336
335337
handleDatePick(value) {
336338
if (this.selectionMode === 'day') {
337-
this.date = this.value ? modifyDate(this.date, value.getFullYear(), value.getMonth(), value.getDate()) : modifyWithDefaultTime(value, this.defaultTime);
339+
this.date = this.value
340+
? modifyDate(this.value, value.getFullYear(), value.getMonth(), value.getDate())
341+
: modifyWithTimeString(value, this.defaultTime);
338342
this.emit(this.date, this.showTime);
339343
} else if (this.selectionMode === 'week') {
340344
this.emit(value.date);
@@ -366,7 +370,7 @@
366370
if (this.selectionMode === 'dates') {
367371
this.emit(this.selectedDate);
368372
} else {
369-
const date = this.value ? this.date : modifyWithDefaultTime(this.date, this.defaultTime);
373+
const date = this.value ? this.date : modifyWithTimeString(this.date, this.defaultTime);
370374
this.emit(date);
371375
}
372376
},
@@ -466,6 +470,12 @@
466470
? !this.disabledDate(value)
467471
: true
468472
);
473+
},
474+
475+
getDefaultValue() {
476+
// if default-value is set, return it
477+
// otherwise, return now (the moment this method gets called)
478+
return this.defaultValue ? new Date(this.defaultValue) : new Date();
469479
}
470480
},
471481
@@ -478,7 +488,7 @@
478488
popperClass: '',
479489
date: new Date(),
480490
value: '',
481-
defaultValue: null,
491+
defaultValue: null, // use getDefaultValue() for time computation
482492
defaultTime: null,
483493
showTime: false,
484494
selectionMode: 'day',

packages/date-picker/src/util/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export const modifyTime = function(date, h, m, s) {
144144
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), h, m, s, date.getMilliseconds());
145145
};
146146

147-
export const modifyWithDefaultTime = (date, time) => {
147+
export const modifyWithTimeString = (date, time) => {
148148
if (date == null || !time) {
149149
return date;
150150
}

test/unit/specs/date-picker.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,39 @@ describe('DatePicker', () => {
13291329
}, DELAY);
13301330
}, DELAY);
13311331
});
1332+
1333+
it('select time honors picked date', done => {
1334+
vm = createVue({
1335+
template: '<el-date-picker type="datetime" v-model="value" ref="compo" />',
1336+
data() {
1337+
return {
1338+
value: new Date(2000, 9, 1, 12, 0, 0) // 2010-10-01 12:00:00
1339+
};
1340+
}
1341+
}, true);
1342+
vm.$refs.compo.$el.querySelector('input').focus();
1343+
setTimeout(_ => {
1344+
// changed month / year should not effect picked time
1345+
vm.$refs.compo.picker.$el.querySelector('.el-date-picker__header .el-icon-arrow-right').click();
1346+
setTimeout(_ => {
1347+
vm.$refs.compo.picker.$el.querySelector('.el-date-picker__header .el-icon-d-arrow-right').click();
1348+
setTimeout(_ => {
1349+
// simulate time selection
1350+
// handleTimePick takes Date object, but it's non-time fields are ignored
1351+
vm.$refs.compo.picker.handleTimePick(new Date(2001, 10, 10, 13, 14, 15), false, false);
1352+
setTimeout(_ => {
1353+
expect(vm.value.getFullYear()).to.equal(2000);
1354+
expect(vm.value.getMonth()).to.equal(9);
1355+
expect(vm.value.getDate()).to.equal(1);
1356+
expect(vm.value.getHours()).to.equal(13);
1357+
expect(vm.value.getMinutes()).to.equal(14);
1358+
expect(vm.value.getSeconds()).to.equal(15);
1359+
done();
1360+
}, DELAY);
1361+
}, DELAY);
1362+
}, DELAY);
1363+
}, DELAY);
1364+
});
13321365
});
13331366

13341367
describe('type:week', () => {

0 commit comments

Comments
 (0)