diff --git a/src/index.jsx b/src/index.jsx index daa1462f7..86a3ee518 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -633,6 +633,7 @@ export default class DatePicker extends React.Component { endDate, selectsMultiple, selectedDates, + minTime, } = this.props; if ( @@ -655,6 +656,18 @@ export default class DatePicker extends React.Component { second: getSeconds(this.props.selected), }); } + + // If minTime is present then set the time to minTime + if (this.props.showTimeSelect || this.props.showTimeSelectOnly) { + if (minTime) { + changedDate = setTime(changedDate, { + hour: minTime.getHours(), + minute: minTime.getMinutes(), + second: minTime.getSeconds(), + }); + } + } + if (!this.props.inline) { this.setState({ preSelection: changedDate, diff --git a/test/min_time_test.test.js b/test/min_time_test.test.js new file mode 100644 index 000000000..9da15ae30 --- /dev/null +++ b/test/min_time_test.test.js @@ -0,0 +1,49 @@ +import React, { useState } from "react"; +import DatePicker from "../src/index.jsx"; +import { fireEvent, render } from "@testing-library/react"; + +const DatePickerWithState = (props) => { + const [selected, setSelected] = useState(null); + return ( + { + setSelected(date); + }} + showTimeSelect + {...props} + /> + ); +}; + +describe("Datepicker minTime", () => { + it("should select time 12:00 AM when no minTime constraint is set.", () => { + const { getByText, container } = render(); + + const day = container.getElementsByClassName("react-datepicker__day")[0]; + + fireEvent.click(day); + + const selectedTime = getByText("12:00 AM"); + + expect(selectedTime.getAttribute("aria-selected")).toBe("true"); + }); + + it("should select the minimum allowable time upon choosing a day.", () => { + const minTime = new Date("2023-03-10 13:00"); + const maxTime = new Date("2023-03-10 18:00"); + + const { container, getByText } = render( + , + ); + + const day = container.getElementsByClassName("react-datepicker__day")[0]; + + fireEvent.click(day); + + const selectedTime = getByText("1:00 PM"); + + expect(selectedTime.getAttribute("aria-selected")).toBe("true"); + }); +});