mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(DatetimePicker): add default-value prop to fix incorrecrt inital value (#7832)
This commit is contained in:
parent
1e8eb07465
commit
8078e30cbc
@ -36,7 +36,7 @@ export default createComponent({
|
|||||||
value(val) {
|
value(val) {
|
||||||
val = this.formatValue(val);
|
val = this.formatValue(val);
|
||||||
|
|
||||||
if (val.valueOf() !== this.innerValue.valueOf()) {
|
if (val && val.valueOf() !== this.innerValue.valueOf()) {
|
||||||
this.innerValue = val;
|
this.innerValue = val;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -50,7 +50,10 @@ export default createComponent({
|
|||||||
maxMonth,
|
maxMonth,
|
||||||
maxHour,
|
maxHour,
|
||||||
maxMinute,
|
maxMinute,
|
||||||
} = this.getBoundary('max', this.innerValue);
|
} = this.getBoundary(
|
||||||
|
'max',
|
||||||
|
this.innerValue ? this.innerValue : this.minDate
|
||||||
|
);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
minYear,
|
minYear,
|
||||||
@ -58,7 +61,10 @@ export default createComponent({
|
|||||||
minMonth,
|
minMonth,
|
||||||
minHour,
|
minHour,
|
||||||
minMinute,
|
minMinute,
|
||||||
} = this.getBoundary('min', this.innerValue);
|
} = this.getBoundary(
|
||||||
|
'min',
|
||||||
|
this.innerValue ? this.innerValue : this.minDate
|
||||||
|
);
|
||||||
|
|
||||||
let result = [
|
let result = [
|
||||||
{
|
{
|
||||||
@ -114,7 +120,7 @@ export default createComponent({
|
|||||||
methods: {
|
methods: {
|
||||||
formatValue(value) {
|
formatValue(value) {
|
||||||
if (!isDate(value)) {
|
if (!isDate(value)) {
|
||||||
value = this.minDate;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
value = Math.max(value, this.minDate.getTime());
|
value = Math.max(value, this.minDate.getTime());
|
||||||
@ -178,7 +184,7 @@ export default createComponent({
|
|||||||
let month;
|
let month;
|
||||||
let day;
|
let day;
|
||||||
if (type === 'month-day') {
|
if (type === 'month-day') {
|
||||||
year = this.innerValue.getFullYear();
|
year = (this.innerValue ? this.innerValue : this.minDate).getFullYear();
|
||||||
month = getValue('month');
|
month = getValue('month');
|
||||||
day = getValue('day');
|
day = getValue('day');
|
||||||
} else {
|
} else {
|
||||||
@ -218,7 +224,7 @@ export default createComponent({
|
|||||||
},
|
},
|
||||||
|
|
||||||
updateColumnValue() {
|
updateColumnValue() {
|
||||||
const value = this.innerValue;
|
const value = this.innerValue ? this.innerValue : this.minDate;
|
||||||
const { formatter } = this;
|
const { formatter } = this;
|
||||||
|
|
||||||
const values = this.originColumns.map((column) => {
|
const values = this.originColumns.map((column) => {
|
||||||
|
@ -56,8 +56,12 @@ export const TimePickerMixin = {
|
|||||||
watch: {
|
watch: {
|
||||||
columns: 'updateColumnValue',
|
columns: 'updateColumnValue',
|
||||||
|
|
||||||
innerValue(val) {
|
innerValue(val, oldVal) {
|
||||||
this.$emit('input', val);
|
if (!oldVal) {
|
||||||
|
this.$emit('input', null);
|
||||||
|
} else {
|
||||||
|
this.$emit('input', val)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -76,6 +80,7 @@ export const TimePickerMixin = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
onConfirm() {
|
onConfirm() {
|
||||||
|
this.$emit('input', this.innerValue)
|
||||||
this.$emit('confirm', this.innerValue);
|
this.$emit('confirm', this.innerValue);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -212,3 +212,39 @@ test('use min-date with filter', async () => {
|
|||||||
wrapper.find('.van-picker__confirm').trigger('click');
|
wrapper.find('.van-picker__confirm').trigger('click');
|
||||||
expect(wrapper.emitted('confirm')[0][0]).toEqual(new Date(2030, 0, 0, 0, 30));
|
expect(wrapper.emitted('confirm')[0][0]).toEqual(new Date(2030, 0, 0, 0, 30));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('v-model', async () => {
|
||||||
|
const minDate = new Date(2030, 0, 0, 0, 3);
|
||||||
|
|
||||||
|
const wrapper = mount({
|
||||||
|
template: `
|
||||||
|
<van-datetime-picker
|
||||||
|
v-model="date"
|
||||||
|
:min-date="minDate"
|
||||||
|
></van-datetime-picker>
|
||||||
|
`,
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
date: null,
|
||||||
|
minDate: new Date(2030, 0, 0, 0, 3)
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await later();
|
||||||
|
|
||||||
|
wrapper.find('.van-picker__confirm').trigger('click');
|
||||||
|
expect(wrapper.vm.date).toEqual(minDate);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('value has an inital value', () => {
|
||||||
|
const defaultValue = new Date(2020, 0, 0, 0, 0);
|
||||||
|
const wrapper = mount(DatePicker, {
|
||||||
|
propsData: {
|
||||||
|
value: defaultValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
wrapper.find('.van-picker__confirm').trigger('click');
|
||||||
|
expect(wrapper.emitted('confirm')[0][0]).toEqual(defaultValue);
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user