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

Open to time #1748

Merged
merged 4 commits into from Nov 4, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/calendar.jsx
Expand Up @@ -667,6 +667,7 @@ export default class Calendar extends React.Component {
return (
<Time
selected={this.props.selected}
openToDate={this.props.openToDate}
onChange={this.props.onTimeChange}
format={this.props.timeFormat}
includeTimes={this.props.includeTimes}
Expand Down
16 changes: 10 additions & 6 deletions src/time.jsx
Expand Up @@ -18,6 +18,7 @@ export default class Time extends React.Component {
includeTimes: PropTypes.array,
intervals: PropTypes.number,
selected: PropTypes.instanceOf(Date),
openToDate: PropTypes.instanceOf(Date),
onChange: PropTypes.func,
todayButton: PropTypes.node,
minTime: PropTypes.instanceOf(Date),
Expand Down Expand Up @@ -83,7 +84,11 @@ export default class Time extends React.Component {
liClasses = (time, currH, currM) => {
let classes = ["react-datepicker__time-list-item"];

if (currH === getHours(time) && currM === getMinutes(time)) {
if (
this.props.selected &&
currH === getHours(time) &&
currM === getMinutes(time)
) {
classes.push("react-datepicker__time-list-item--selected");
}
if (
Expand All @@ -110,7 +115,9 @@ export default class Time extends React.Component {
let times = [];
const format = this.props.format ? this.props.format : "p";
const intervals = this.props.intervals;
const activeTime = this.props.selected ? this.props.selected : newDate();
const activeTime =
this.props.selected || this.props.openToDate || newDate();

const currH = getHours(activeTime);
const currM = getMinutes(activeTime);
let base = getStartOfDay(newDate());
Expand Down Expand Up @@ -142,10 +149,7 @@ export default class Time extends React.Component {
onClick={this.handleClick.bind(this, time)}
className={this.liClasses(time, currH, currM)}
ref={li => {
if (
(currH === getHours(time) && currM === getMinutes(time)) ||
(currH === getHours(time) && !this.centerLi)
) {
if (currH === getHours(time) && currM >= getMinutes(time)) {
this.centerLi = li;
}
}}
Expand Down
41 changes: 39 additions & 2 deletions test/time_format_test.js
Expand Up @@ -64,16 +64,29 @@ describe("TimeComponent", () => {
expect(spy.args[0][1].innerHTML).to.eq("13:00");
});

it("with five minute time interval, should call calcCenterPosition with centerLi ref, closest to the current time", () => {
mount(<TimeComponent format="HH:mm" intervals={5} />);
expect(spy.args[0][1].innerHTML).to.eq("13:25");
});

it("should call calcCenterPosition with centerLi ref, closest to the selected time", () => {
mount(
<TimeComponent format="HH:mm" selected={new Date("1990-06-14 08:11")} />
<TimeComponent
format="HH:mm"
selected={new Date("1990-06-14 08:11")}
openToDate={new Date("1990-06-14 09:11")}
/>
);
expect(spy.args[0][1].innerHTML).to.eq("08:00");
});

it("should call calcCenterPosition with centerLi ref, which is selected", () => {
mount(
<TimeComponent format="HH:mm" selected={new Date("1990-06-14 08:00")} />
<TimeComponent
format="HH:mm"
selected={new Date("1990-06-14 08:00")}
openToDate={new Date("1990-06-14 09:00")}
/>
);
expect(
spy.args[0][1].classList.contains(
Expand All @@ -82,6 +95,30 @@ describe("TimeComponent", () => {
).to.be.true;
});

it("when no selected time, should call calcCenterPosition with centerLi ref, closest to the opened time", () => {
mount(
<TimeComponent
format="HH:mm"
openToDate={new Date("1990-06-14 09:11")}
/>
);
expect(spy.args[0][1].innerHTML).to.eq("09:00");
});

it("when no selected time, should call calcCenterPosition with centerLi ref, and no time should be selected", () => {
mount(
<TimeComponent
format="HH:mm"
openToDate={new Date("1990-06-14 09:00")}
/>
);
expect(
spy.args[0][1].classList.contains(
"react-datepicker__time-list-item--selected"
)
).to.be.false;
});

it("should calculate scroll for the first item of 4 (even) items list", () => {
expect(
TimeComponent.calcCenterPosition(200, {
Expand Down