From 89747d8c17826b8e2c6a43a9c62ee846711111e2 Mon Sep 17 00:00:00 2001 From: Eiinu Date: Wed, 10 Jan 2024 18:32:29 +0800 Subject: [PATCH] =?UTF-8?q?fix(date-picker):=20=E4=BF=AE=E5=A4=8D=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=81=94=E5=8A=A8=E6=9B=B4=E6=96=B0=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#2824)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/packages/__VUE/datepicker/index.taro.vue | 67 +++++++++++--------- src/packages/__VUE/datepicker/index.vue | 61 +++++++++--------- 2 files changed, 67 insertions(+), 61 deletions(-) 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 }); };