Skip to content

Commit

Permalink
Merge pull request Hacker0x01#4336 from qburst/issue-4170/fix/clear-s…
Browse files Browse the repository at this point in the history
…elected-date-on-empty-input-with-showTimeSelectOnly
  • Loading branch information
martijnrusschen authored Oct 23, 2023
2 parents 87c8e0d + d1786ca commit 6ff581d
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 6ff581d

Please sign in to comment.