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 2 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
52 changes: 52 additions & 0 deletions test/min_time_test.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useState } from "react";
import DatePicker from "../src/index.jsx";
import { mount } from "enzyme";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you avoid using enzyme? You can use React Testing Library.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no prob, can you check it now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import Day from "../src/day.jsx";

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 datepicker = mount(<DatePickerWithState />);
const day = datepicker.find(Day).first();

day.simulate("click");

const selectedTime = datepicker.find(
".react-datepicker__time-list-item--selected",
);

expect(selectedTime.text()).toBe("12:00 AM");
});

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 datepicker = mount(
<DatePickerWithState minTime={minTime} maxTime={maxTime} />,
);
const day = datepicker.find(Day).first();

day.simulate("click");

const selectedTime = datepicker.find(
".react-datepicker__time-list-item--selected",
);

expect(selectedTime.text()).toBe("1:00 PM");
});
});
Loading