From 4cf8c40d543b75a3d94bb7e3aae6da0d140439a5 Mon Sep 17 00:00:00 2001 From: Uyarn Date: Wed, 25 Sep 2024 21:48:07 +0800 Subject: [PATCH] fix(datepicker): range picker default time bug --- src/date-picker/panel/RangePanel.tsx | 4 ++-- src/time-picker/TimeRangePicker.tsx | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/date-picker/panel/RangePanel.tsx b/src/date-picker/panel/RangePanel.tsx index 33c4c7ccad..33d73a258e 100644 --- a/src/date-picker/panel/RangePanel.tsx +++ b/src/date-picker/panel/RangePanel.tsx @@ -165,7 +165,7 @@ const RangePanel = forwardRef((originalProps, r partial={'start'} year={startYear} month={startMonth} - time={time[0]} + time={time[activeIndex]} tableData={startTableData} value={value} {...panelContentProps} @@ -175,7 +175,7 @@ const RangePanel = forwardRef((originalProps, r partial={'end'} year={endYear} month={endMonth} - time={time[1]} + time={time[activeIndex]} value={value} tableData={endTableData} {...panelContentProps} diff --git a/src/time-picker/TimeRangePicker.tsx b/src/time-picker/TimeRangePicker.tsx index 0930e11533..21496150f0 100644 --- a/src/time-picker/TimeRangePicker.tsx +++ b/src/time-picker/TimeRangePicker.tsx @@ -1,5 +1,6 @@ import React, { FC, useState, useEffect } from 'react'; import classNames from 'classnames'; +import dayjs from 'dayjs'; import { TimeIcon as TdTimeIcon } from 'tdesign-icons-react'; import isArray from 'lodash/isArray'; @@ -109,6 +110,16 @@ const TimeRangePicker: FC = (originalProps) => { handleOnPick(nextCurrentValue, context); }; + const autoSwapTime = (valueBeforeConfirm: Array) => { + const [startTime, endTime] = valueBeforeConfirm; + const startDayjs = dayjs(startTime, props.format); + const endDayjs = dayjs(endTime, props.format); + + if (startDayjs.isAfter(endDayjs, 'second')) return [endTime, startTime]; + + return [startTime, endTime]; + }; + const handleInputBlur = (value: TimeRangeValue, { e }: { e: React.FocusEvent }) => { if (allowInput) { const isValidTime = validateInputValue(currentValue[currentPanelIdx], format); @@ -132,7 +143,7 @@ const TimeRangePicker: FC = (originalProps) => { const handleClickConfirm = () => { const isValidTime = !currentValue.find((v) => !validateInputValue(v, format)); - if (isValidTime) onChange(currentValue); + if (isValidTime) onChange(autoSwapTime(currentValue)); setPanelShow(false); };