Skip to content

Commit

Permalink
fix: Clear the previously selected date on empty input with showTimeS…
Browse files Browse the repository at this point in the history
…electOnly

Previously, the selected date was not being cleared and retained the previously selected time when an empty value was passed to the date input while showTimeSelectOnly was enabled due to a bug.  This commit address the issue and adds test cases to ensure proper functionality.

Closes Hacker0x01#4170
  • Loading branch information
Balaji Sridharan committed Oct 23, 2023
1 parent 87c8e0d commit d1786ca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -542,21 +542,14 @@ export default class DatePicker extends React.Component {
if (
this.props.showTimeSelectOnly &&
this.props.selected &&
date &&
!isSameDay(date, this.props.selected)
) {
if (date == null) {
date = set(this.props.selected, {
hours: getHours(this.props.selected),
minutes: getMinutes(this.props.selected),
seconds: getSeconds(this.props.selected),
});
} else {
date = set(this.props.selected, {
hours: getHours(date),
minutes: getMinutes(date),
seconds: getSeconds(date),
});
}
date = set(this.props.selected, {
hours: getHours(date),
minutes: getMinutes(date),
seconds: getSeconds(date),
});
}
if (date || !event.target.value) {
this.setSelected(date, event, true);
Expand Down
50 changes: 50 additions & 0 deletions test/datepicker_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { findDOMNode } from "react-dom";
import TestUtils from "react-dom/test-utils";
import { enUS, enGB } from "date-fns/locale";
import { mount } from "enzyme";
import { render, fireEvent } from "@testing-library/react";
import defer from "lodash/defer";
import DatePicker, { registerLocale } from "../src/index.jsx";
import Day from "../src/day.jsx";
Expand Down Expand Up @@ -2102,6 +2103,55 @@ describe("DatePicker", () => {
});
});

it("clears the selected date on empty date input", () => {
let date = "2023-10-23 10:00:00";
const selected = utils.newDate(date);

const { container: datepicker } = render(
<DatePicker
selected={selected}
onChange={(d) => {
date = d;
}}
showTimeSelect
dateFormat="MMMM d, yyyy h:mm aa"
/>,
);

const input = datepicker.querySelector(
".react-datepicker__input-container > input",
);
fireEvent.change(input, { target: { value: "" } });

expect(date).toBe(null);
});

it("clears the selected date on empty date input with showTimeSelectOnly", () => {
const format = "h:mm aa";

let date = "2022-02-24 10:00:00";
const selected = utils.newDate(date);

const { container: datepicker } = render(
<DatePicker
selected={selected}
onChange={(d) => {
date = d;
}}
showTimeSelectOnly
dateFormat={format}
timeFormat={format}
/>,
);

const input = datepicker.querySelector(
".react-datepicker__input-container > input",
);
fireEvent.change(input, { target: { value: "" } });

expect(date).toBe(null);
});

it("should selected month when specified minDate same month", () => {
const selected = utils.newDate("2023-01-09");
let date = null;
Expand Down

0 comments on commit d1786ca

Please sign in to comment.