Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: default time selection #4552

Merged
merged 4 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ export default class DatePicker extends React.Component {
endDate,
selectsMultiple,
selectedDates,
minTime,
} = this.props;

if (
Expand All @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions test/min_time_test.test.js
Original file line number Diff line number Diff line change
@@ -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 (
<DatePicker
open
selected={selected}
onChange={(date) => {
setSelected(date);
}}
showTimeSelect
{...props}
/>
);
};

describe("Datepicker minTime", () => {
it("should select time 12:00 AM when no minTime constraint is set.", () => {
const { getByText, container } = render(<DatePickerWithState />);

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(
<DatePickerWithState minTime={minTime} maxTime={maxTime} />,
);

const day = container.getElementsByClassName("react-datepicker__day")[0];

fireEvent.click(day);

const selectedTime = getByText("1:00 PM");

expect(selectedTime.getAttribute("aria-selected")).toBe("true");
});
});
Loading