diff --git a/src/packages/__VUE/datepicker/index.taro.vue b/src/packages/__VUE/datepicker/index.taro.vue index 167edac13c..7880c80fd8 100644 --- a/src/packages/__VUE/datepicker/index.taro.vue +++ b/src/packages/__VUE/datepicker/index.taro.vue @@ -28,6 +28,7 @@ import { PickerOption } from '../picker/types'; import { createComponent } from '@/packages/utils/create'; import { Formatter, Filter } from './type'; import { padZero, isDate as isDateU } from '@/packages/utils/util'; +import { nextTick } from '@tarojs/taro'; const { create } = createComponent('date-picker'); const currentYear = new Date().getFullYear(); @@ -148,7 +149,7 @@ export default create({ hour = 23; minute = 59; } - const seconds = minute; + let seconds = minute; if (value.getFullYear() === year) { month = boundary.getMonth() + 1; if (value.getMonth() + 1 === month) { @@ -157,6 +158,9 @@ export default create({ hour = boundary.getHours(); if (value.getHours() === hour) { minute = boundary.getMinutes(); + if (value.getMinutes() === minute) { + seconds = boundary.getSeconds(); + } } } } @@ -221,37 +225,35 @@ export default create({ selectedValue: (string | number)[]; selectedOptions: PickerOption[]; }) => { - if (['date', 'datetime', 'datehour', 'month-day', 'year-month', 'hour-minute'].includes(props.type)) { - let formatDate: (number | string)[] = []; - selectedValue.forEach((item) => { - formatDate.push(item); - }); - if (props.type == 'month-day' && formatDate.length < 3) { - formatDate.unshift(new Date(state.currentDate || props.minDate || props.maxDate).getFullYear()); - } - if (props.type == 'year-month' && formatDate.length < 3) { - formatDate.push(new Date(state.currentDate || props.minDate || props.maxDate).getDate()); - } + let formatDate: (number | string)[] = []; + selectedValue.forEach((item) => { + formatDate.push(item); + }); + if (props.type == 'month-day' && formatDate.length < 3) { + formatDate.unshift(new Date(state.currentDate || props.minDate || props.maxDate).getFullYear()); + } + if (props.type == 'year-month' && formatDate.length < 3) { + formatDate.push(new Date(state.currentDate || props.minDate || props.maxDate).getDate()); + } - const year = Number(formatDate[0]); - const month = Number(formatDate[1]) - 1; - const day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1]))); - let date: Date | null = null; - if (props.type === 'date' || props.type === 'month-day' || props.type === 'year-month') { - date = new Date(year, month, day); - } else if (props.type === 'datetime') { - date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4])); - } else if (props.type === 'datehour') { - date = new Date(year, month, day, Number(formatDate[3])); - } else if (props.type === 'hour-minute') { - date = new Date(state.currentDate); - const year = date.getFullYear(); - const month = date.getMonth(); - const day = date.getDate(); - date = new Date(year, month, day, Number(formatDate[0]), Number(formatDate[1])); - } - state.currentDate = formatValue(date as Date); + const year = Number(formatDate[0]); + const month = Number(formatDate[1]) - 1; + const day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1]))); + let date: Date | null = null; + if (props.type === 'date' || props.type === 'month-day' || props.type === 'year-month') { + date = new Date(year, month, day); + } else if (props.type === 'datetime') { + date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4])); + } else if (props.type === 'datehour') { + date = new Date(year, month, day, Number(formatDate[3])); + } else if (props.type === 'hour-minute' || props.type === 'time') { + date = new Date(state.currentDate); + const year = date.getFullYear(); + const month = date.getMonth(); + const day = date.getDate(); + date = new Date(year, month, day, Number(formatDate[0]), Number(formatDate[1]), Number(formatDate[2] || 0)); } + state.currentDate = formatValue(date as Date); emit('change', { columnIndex, selectedValue, selectedOptions }); }; @@ -282,7 +284,7 @@ export default create({ min++; } - if (min <= +val) { + if (min <= Number(val)) { index++; } } @@ -376,6 +378,9 @@ export default create({ const isSameValue = JSON.stringify(newValues) === JSON.stringify(props.modelValue); if (!isSameValue) { emit('update:modelValue', newValues); + nextTick(() => { + state.selectedValue = getSelectedValue(newValues); + }); } } ); diff --git a/src/packages/__VUE/datepicker/index.vue b/src/packages/__VUE/datepicker/index.vue index 0a6f3d69a4..670d05793a 100644 --- a/src/packages/__VUE/datepicker/index.vue +++ b/src/packages/__VUE/datepicker/index.vue @@ -148,7 +148,7 @@ export default create({ hour = 23; minute = 59; } - const seconds = minute; + let seconds = minute; if (value.getFullYear() === year) { month = boundary.getMonth() + 1; if (value.getMonth() + 1 === month) { @@ -157,6 +157,9 @@ export default create({ hour = boundary.getHours(); if (value.getHours() === hour) { minute = boundary.getMinutes(); + if (value.getMinutes() === minute) { + seconds = boundary.getSeconds(); + } } } } @@ -221,37 +224,35 @@ export default create({ selectedValue: (string | number)[]; selectedOptions: PickerOption[]; }) => { - if (['date', 'datetime', 'datehour', 'month-day', 'year-month', 'hour-minute'].includes(props.type)) { - let formatDate: (number | string)[] = []; - selectedValue.forEach((item) => { - formatDate.push(item); - }); - if (props.type == 'month-day' && formatDate.length < 3) { - formatDate.unshift(new Date(state.currentDate || props.minDate || props.maxDate).getFullYear()); - } - if (props.type == 'year-month' && formatDate.length < 3) { - formatDate.push(new Date(state.currentDate || props.minDate || props.maxDate).getDate()); - } + let formatDate: (number | string)[] = []; + selectedValue.forEach((item) => { + formatDate.push(item); + }); + if (props.type == 'month-day' && formatDate.length < 3) { + formatDate.unshift(new Date(state.currentDate || props.minDate || props.maxDate).getFullYear()); + } + if (props.type == 'year-month' && formatDate.length < 3) { + formatDate.push(new Date(state.currentDate || props.minDate || props.maxDate).getDate()); + } - const year = Number(formatDate[0]); - const month = Number(formatDate[1]) - 1; - const day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1]))); - let date: Date | null = null; - if (props.type === 'date' || props.type === 'month-day' || props.type === 'year-month') { - date = new Date(year, month, day); - } else if (props.type === 'datetime') { - date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4])); - } else if (props.type === 'datehour') { - date = new Date(year, month, day, Number(formatDate[3])); - } else if (props.type === 'hour-minute') { - date = new Date(state.currentDate); - const year = date.getFullYear(); - const month = date.getMonth(); - const day = date.getDate(); - date = new Date(year, month, day, Number(formatDate[0]), Number(formatDate[1])); - } - state.currentDate = formatValue(date as Date); + const year = Number(formatDate[0]); + const month = Number(formatDate[1]) - 1; + const day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1]))); + let date: Date | null = null; + if (props.type === 'date' || props.type === 'month-day' || props.type === 'year-month') { + date = new Date(year, month, day); + } else if (props.type === 'datetime') { + date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4])); + } else if (props.type === 'datehour') { + date = new Date(year, month, day, Number(formatDate[3])); + } else if (props.type === 'hour-minute' || props.type === 'time') { + date = new Date(state.currentDate); + const year = date.getFullYear(); + const month = date.getMonth(); + const day = date.getDate(); + date = new Date(year, month, day, Number(formatDate[0]), Number(formatDate[1]), Number(formatDate[2] || 0)); } + state.currentDate = formatValue(date as Date); emit('change', { columnIndex, selectedValue, selectedOptions }); };