Skip to content

Commit e9fe0bf

Browse files
committed
fix: remove unneeded code to prevent date change with showTimeSelectOnly
Before this PR, the below function calls: `index.tsx:handleChange -> date_utils:parseDate -> date-fns:parse` were passing `new Date()` to date-fns parse function as the 'reference date' parameter. This meant that when the datepicker had the `showTimeSelectOnly` prop and the date format was just a time (e.g. H:mm) the parse result's year/month/day would be today instead of the previously selected date, which we want. To prevent this problem, the block of code being removed in the present commit would take the current selected date and reset the time to the newly input/parsed time, so that as a result, the year/month/day would not change. Instead, this PR fixes the problem at its root, by passing `this.props.selected` instead of `new Date()` to the date-fns `parse` function as the reference date, such that the parse result of just a time string will have the same year/month/day as `this.props.selected`. This is the desired behavior, and so this block of code is no longer needed. This is already tested in test "when update the datepicker input text while props.showTimeSelectOnly is set and dateFormat has only time related format".
1 parent 053b403 commit e9fe0bf

File tree

1 file changed

+1
-16
lines changed

1 file changed

+1
-16
lines changed

src/index.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, { Component, cloneElement } from "react";
44
import Calendar from "./calendar";
55
import CalendarIcon from "./calendar_icon";
66
import {
7-
set,
87
newDate,
98
isDate,
109
isBefore,
@@ -631,28 +630,14 @@ export default class DatePicker extends Component<
631630
this.props.onChange?.([startDateNew, endDateNew], event);
632631
} else {
633632
// not selectsRange
634-
let date = parseDate(
633+
const date = parseDate(
635634
value,
636635
dateFormat,
637636
this.props.locale,
638637
strictParsing,
639638
this.props.selected ?? undefined,
640639
);
641640

642-
// Use date from `selected` prop when manipulating only time for input value
643-
if (
644-
this.props.showTimeSelectOnly &&
645-
this.props.selected &&
646-
date &&
647-
!isSameDay(date, this.props.selected)
648-
) {
649-
date = set(this.props.selected, {
650-
hours: getHours(date),
651-
minutes: getMinutes(date),
652-
seconds: getSeconds(date),
653-
});
654-
}
655-
656641
// Update selection if either (1) date was successfully parsed, or (2) input field is empty
657642
if (date || !value) {
658643
this.setSelected(date, event, true);

0 commit comments

Comments
 (0)