mirror of https://github.com/ElemeFE/element
DateTimePicker: fix incorrect date on time pick (#11474)
* date-time-picker: fix incorrect date on time pick * date-time-picker: fix incorrect date on confirmpull/11489/head
parent
8c1b4d827d
commit
ecec908da8
|
@ -190,7 +190,7 @@
|
||||||
isDate,
|
isDate,
|
||||||
modifyDate,
|
modifyDate,
|
||||||
modifyTime,
|
modifyTime,
|
||||||
modifyWithDefaultTime,
|
modifyWithTimeString,
|
||||||
prevYear,
|
prevYear,
|
||||||
nextYear,
|
nextYear,
|
||||||
prevMonth,
|
prevMonth,
|
||||||
|
@ -498,8 +498,8 @@
|
||||||
|
|
||||||
handleRangePick(val, close = true) {
|
handleRangePick(val, close = true) {
|
||||||
const defaultTime = this.defaultTime || [];
|
const defaultTime = this.defaultTime || [];
|
||||||
const minDate = modifyWithDefaultTime(val.minDate, defaultTime[0]);
|
const minDate = modifyWithTimeString(val.minDate, defaultTime[0]);
|
||||||
const maxDate = modifyWithDefaultTime(val.maxDate, defaultTime[1]);
|
const maxDate = modifyWithTimeString(val.maxDate, defaultTime[1]);
|
||||||
|
|
||||||
if (this.maxDate === maxDate && this.minDate === minDate) {
|
if (this.maxDate === maxDate && this.minDate === minDate) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -150,7 +150,7 @@
|
||||||
isDate,
|
isDate,
|
||||||
modifyDate,
|
modifyDate,
|
||||||
modifyTime,
|
modifyTime,
|
||||||
modifyWithDefaultTime,
|
modifyWithTimeString,
|
||||||
clearMilliseconds,
|
clearMilliseconds,
|
||||||
clearTime,
|
clearTime,
|
||||||
prevYear,
|
prevYear,
|
||||||
|
@ -192,7 +192,7 @@
|
||||||
if (isDate(val)) {
|
if (isDate(val)) {
|
||||||
this.date = new Date(val);
|
this.date = new Date(val);
|
||||||
} else {
|
} else {
|
||||||
this.date = this.defaultValue ? new Date(this.defaultValue) : new Date();
|
this.date = this.getDefaultValue();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -233,7 +233,7 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
handleClear() {
|
handleClear() {
|
||||||
this.date = this.defaultValue ? new Date(this.defaultValue) : new Date();
|
this.date = this.getDefaultValue();
|
||||||
this.$emit('pick', null);
|
this.$emit('pick', null);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -303,7 +303,9 @@
|
||||||
|
|
||||||
handleTimePick(value, visible, first) {
|
handleTimePick(value, visible, first) {
|
||||||
if (isDate(value)) {
|
if (isDate(value)) {
|
||||||
const newDate = this.value ? modifyTime(this.date, value.getHours(), value.getMinutes(), value.getSeconds()) : modifyWithDefaultTime(value, this.defaultTime);
|
const newDate = this.value
|
||||||
|
? modifyTime(this.value, value.getHours(), value.getMinutes(), value.getSeconds())
|
||||||
|
: modifyWithTimeString(this.getDefaultValue(), this.defaultTime);
|
||||||
this.date = newDate;
|
this.date = newDate;
|
||||||
this.emit(this.date, true);
|
this.emit(this.date, true);
|
||||||
} else {
|
} else {
|
||||||
|
@ -334,7 +336,9 @@
|
||||||
|
|
||||||
handleDatePick(value) {
|
handleDatePick(value) {
|
||||||
if (this.selectionMode === 'day') {
|
if (this.selectionMode === 'day') {
|
||||||
this.date = this.value ? modifyDate(this.date, value.getFullYear(), value.getMonth(), value.getDate()) : modifyWithDefaultTime(value, this.defaultTime);
|
this.date = this.value
|
||||||
|
? modifyDate(this.value, value.getFullYear(), value.getMonth(), value.getDate())
|
||||||
|
: modifyWithTimeString(value, this.defaultTime);
|
||||||
this.emit(this.date, this.showTime);
|
this.emit(this.date, this.showTime);
|
||||||
} else if (this.selectionMode === 'week') {
|
} else if (this.selectionMode === 'week') {
|
||||||
this.emit(value.date);
|
this.emit(value.date);
|
||||||
|
@ -366,8 +370,13 @@
|
||||||
if (this.selectionMode === 'dates') {
|
if (this.selectionMode === 'dates') {
|
||||||
this.emit(this.selectedDate);
|
this.emit(this.selectedDate);
|
||||||
} else {
|
} else {
|
||||||
const date = this.value ? this.date : modifyWithDefaultTime(this.date, this.defaultTime);
|
// value were emitted in handle{Date,Time}Pick, nothing to update here
|
||||||
this.emit(date);
|
// deal with the scenario where: user opens the picker, then confirm without doing anything
|
||||||
|
const value = this.value
|
||||||
|
? this.value
|
||||||
|
: modifyWithTimeString(this.getDefaultValue(), this.defaultTime);
|
||||||
|
this.date = new Date(value); // refresh date
|
||||||
|
this.emit(value);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -466,6 +475,12 @@
|
||||||
? !this.disabledDate(value)
|
? !this.disabledDate(value)
|
||||||
: true
|
: true
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
getDefaultValue() {
|
||||||
|
// if default-value is set, return it
|
||||||
|
// otherwise, return now (the moment this method gets called)
|
||||||
|
return this.defaultValue ? new Date(this.defaultValue) : new Date();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -478,7 +493,7 @@
|
||||||
popperClass: '',
|
popperClass: '',
|
||||||
date: new Date(),
|
date: new Date(),
|
||||||
value: '',
|
value: '',
|
||||||
defaultValue: null,
|
defaultValue: null, // use getDefaultValue() for time computation
|
||||||
defaultTime: null,
|
defaultTime: null,
|
||||||
showTime: false,
|
showTime: false,
|
||||||
selectionMode: 'day',
|
selectionMode: 'day',
|
||||||
|
|
|
@ -144,7 +144,7 @@ export const modifyTime = function(date, h, m, s) {
|
||||||
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), h, m, s, date.getMilliseconds());
|
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), h, m, s, date.getMilliseconds());
|
||||||
};
|
};
|
||||||
|
|
||||||
export const modifyWithDefaultTime = (date, time) => {
|
export const modifyWithTimeString = (date, time) => {
|
||||||
if (date == null || !time) {
|
if (date == null || !time) {
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1329,6 +1329,71 @@ describe('DatePicker', () => {
|
||||||
}, DELAY);
|
}, DELAY);
|
||||||
}, DELAY);
|
}, DELAY);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('select time honors picked date', done => {
|
||||||
|
vm = createVue({
|
||||||
|
template: '<el-date-picker type="datetime" v-model="value" ref="compo" />',
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
value: new Date(2000, 9, 1, 12, 0, 0) // 2010-10-01 12:00:00
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
vm.$refs.compo.$el.querySelector('input').focus();
|
||||||
|
setTimeout(_ => {
|
||||||
|
// changed month / year should not effect picked time
|
||||||
|
vm.$refs.compo.picker.$el.querySelector('.el-date-picker__header .el-icon-arrow-right').click();
|
||||||
|
setTimeout(_ => {
|
||||||
|
vm.$refs.compo.picker.$el.querySelector('.el-date-picker__header .el-icon-d-arrow-right').click();
|
||||||
|
setTimeout(_ => {
|
||||||
|
// simulate time selection
|
||||||
|
// handleTimePick takes Date object, but it's non-time fields are ignored
|
||||||
|
vm.$refs.compo.picker.handleTimePick(new Date(2001, 10, 10, 13, 14, 15), false, false);
|
||||||
|
setTimeout(_ => {
|
||||||
|
expect(vm.value.getFullYear()).to.equal(2000);
|
||||||
|
expect(vm.value.getMonth()).to.equal(9);
|
||||||
|
expect(vm.value.getDate()).to.equal(1);
|
||||||
|
expect(vm.value.getHours()).to.equal(13);
|
||||||
|
expect(vm.value.getMinutes()).to.equal(14);
|
||||||
|
expect(vm.value.getSeconds()).to.equal(15);
|
||||||
|
done();
|
||||||
|
}, DELAY);
|
||||||
|
}, DELAY);
|
||||||
|
}, DELAY);
|
||||||
|
}, DELAY);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('confirm button honors picked date', done => {
|
||||||
|
vm = createVue({
|
||||||
|
template: '<el-date-picker type="datetime" v-model="value" ref="compo" />',
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
value: new Date(2000, 9, 1, 12, 0, 0) // 2010-10-01 12:00:00
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
vm.$refs.compo.$el.querySelector('input').focus();
|
||||||
|
setTimeout(_ => {
|
||||||
|
// changed month / year should not effect picked time
|
||||||
|
vm.$refs.compo.picker.$el.querySelector('.el-date-picker__header .el-icon-arrow-right').click();
|
||||||
|
setTimeout(_ => {
|
||||||
|
vm.$refs.compo.picker.$el.querySelector('.el-date-picker__header .el-icon-d-arrow-right').click();
|
||||||
|
setTimeout(_ => {
|
||||||
|
// click confirm button
|
||||||
|
vm.$refs.compo.picker.$el.querySelector('.el-picker-panel__footer .el-button--default').click();
|
||||||
|
setTimeout(_ => {
|
||||||
|
expect(vm.value.getFullYear()).to.equal(2000);
|
||||||
|
expect(vm.value.getMonth()).to.equal(9);
|
||||||
|
expect(vm.value.getDate()).to.equal(1);
|
||||||
|
expect(vm.value.getHours()).to.equal(12);
|
||||||
|
expect(vm.value.getMinutes()).to.equal(0);
|
||||||
|
expect(vm.value.getSeconds()).to.equal(0);
|
||||||
|
done();
|
||||||
|
}, DELAY);
|
||||||
|
}, DELAY);
|
||||||
|
}, DELAY);
|
||||||
|
}, DELAY);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('type:week', () => {
|
describe('type:week', () => {
|
||||||
|
|
Loading…
Reference in New Issue