Skip to content

Commit

Permalink
fix(datepicker): range picker default time bug
Browse files Browse the repository at this point in the history
  • Loading branch information
uyarn committed Sep 25, 2024
1 parent dd24990 commit 4cf8c40
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/date-picker/panel/RangePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const RangePanel = forwardRef<HTMLDivElement, RangePanelProps>((originalProps, r
partial={'start'}
year={startYear}
month={startMonth}
time={time[0]}
time={time[activeIndex]}
tableData={startTableData}
value={value}
{...panelContentProps}
Expand All @@ -175,7 +175,7 @@ const RangePanel = forwardRef<HTMLDivElement, RangePanelProps>((originalProps, r
partial={'end'}
year={endYear}
month={endMonth}
time={time[1]}
time={time[activeIndex]}
value={value}
tableData={endTableData}
{...panelContentProps}
Expand Down
13 changes: 12 additions & 1 deletion src/time-picker/TimeRangePicker.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -109,6 +110,16 @@ const TimeRangePicker: FC<TimeRangePickerProps> = (originalProps) => {
handleOnPick(nextCurrentValue, context);
};

const autoSwapTime = (valueBeforeConfirm: Array<string>) => {
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<HTMLInputElement> }) => {
if (allowInput) {
const isValidTime = validateInputValue(currentValue[currentPanelIdx], format);
Expand All @@ -132,7 +143,7 @@ const TimeRangePicker: FC<TimeRangePickerProps> = (originalProps) => {

const handleClickConfirm = () => {
const isValidTime = !currentValue.find((v) => !validateInputValue(v, format));
if (isValidTime) onChange(currentValue);
if (isValidTime) onChange(autoSwapTime(currentValue));
setPanelShow(false);
};

Expand Down

0 comments on commit 4cf8c40

Please sign in to comment.