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

Add support second-level granularity to injectTimes #4683

Merged
merged 6 commits into from
Apr 14, 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
1 change: 1 addition & 0 deletions docs-site/src/examples/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"DatePicker": false,
"getHours": false,
"setHours": false,
"setSeconds": false,
"setMinutes": false,
"getDate": false,
"addDays": false,
Expand Down
4 changes: 2 additions & 2 deletions docs-site/src/examples/injectTimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
selected={startDate}
onChange={(date) => setStartDate(date)}
showTimeSelect
timeFormat="HH:mm"
timeFormat="HH:mm:ss"
injectTimes={[
setHours(setMinutes(new Date(), 1), 0),
setHours(setMinutes(setSeconds(new Date(), 10), 1), 0),
setHours(setMinutes(new Date(), 5), 12),
setHours(setMinutes(new Date(), 59), 23),
]}
Expand Down
20 changes: 15 additions & 5 deletions src/date_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { isWithinInterval } from "date-fns/isWithinInterval";
import { toDate } from "date-fns/toDate";
import { parse } from "date-fns/parse";
import { parseISO } from "date-fns/parseISO";
import { addSeconds } from "date-fns";

export const DEFAULT_YEAR_ITEM_NUMBER = 12;

Expand Down Expand Up @@ -295,7 +296,15 @@ export function getEndOfMonth(date) {

// *** Addition ***

export { addMinutes, addDays, addWeeks, addMonths, addQuarters, addYears };
export {
addSeconds,
addMinutes,
addDays,
addWeeks,
addMonths,
addQuarters,
addYears,
};

// *** Subtraction ***

Expand Down Expand Up @@ -850,10 +859,11 @@ export function timesToInjectAfter(
const l = injectedTimes.length;
const times = [];
for (let i = 0; i < l; i++) {
const injectedTime = addMinutes(
addHours(startOfDay, getHours(injectedTimes[i])),
getMinutes(injectedTimes[i]),
);
let injectedTime = startOfDay;
injectedTime = addHours(injectedTime, getHours(injectedTimes[i]));
injectedTime = addMinutes(injectedTime, getMinutes(injectedTimes[i]));
injectedTime = addSeconds(injectedTime, getSeconds(injectedTimes[i]));

const nextTime = addMinutes(
startOfDay,
(currentMultiplier + 1) * intervals,
Expand Down
7 changes: 6 additions & 1 deletion src/time.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getHoursInDay,
isSameMinute,
} from "./date_utils";
import { getSeconds } from "date-fns";

export default class Time extends React.Component {
static get defaultProps() {
Expand Down Expand Up @@ -121,9 +122,13 @@ export default class Time extends React.Component {
if (this.isDisabledTime(time)) {
classes.push("react-datepicker__time-list-item--disabled");
}

//convert this.props.intervals and the relevant time to seconds and check if it it's a clean multiple of the interval
if (
this.props.injectTimes &&
(getHours(time) * 60 + getMinutes(time)) % this.props.intervals !== 0
(getHours(time) * 3600 + getMinutes(time) * 60 + getSeconds(time)) %
(this.props.intervals * 60) !==
0
) {
classes.push("react-datepicker__time-list-item--injected");
}
Expand Down
33 changes: 30 additions & 3 deletions test/inject_times_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("TimeComponent", () => {
const today = utils.getStartOfDay(utils.newDate());
const { container } = render(
<TimeComponent
timeIntervals={60}
intervals={60}
injectTimes={[
utils.addMinutes(today, 0),
utils.addMinutes(today, 60),
Expand All @@ -45,7 +45,7 @@ describe("TimeComponent", () => {
const today = utils.getStartOfDay(utils.newDate());
const { container } = render(
<TimeComponent
timeIntervals={60}
intervals={60}
martijnrusschen marked this conversation as resolved.
Show resolved Hide resolved
injectTimes={[
utils.addMinutes(today, 1),
utils.addMinutes(today, 2),
Expand All @@ -62,9 +62,10 @@ describe("TimeComponent", () => {

it("should sort injected times automatically", () => {
const today = utils.getStartOfDay(utils.newDate());

const { container } = render(
<TimeComponent
timeIntervals={60}
intervals={60}
injectTimes={[
utils.addMinutes(today, 3),
utils.addMinutes(today, 1),
Expand All @@ -82,4 +83,30 @@ describe("TimeComponent", () => {
"12:03 AM",
]);
});

it("should support hours, minutes, and seconds", () => {
const today = utils.getStartOfDay(utils.newDate());

const { container } = render(
<TimeComponent
format="HH:mm:ss"
intervals={60}
injectTimes={[
utils.addSeconds(today, 1),
utils.addMinutes(utils.addSeconds(today, 1), 30),
utils.addHours(utils.addMinutes(utils.addSeconds(today, 1), 30), 1),
]}
/>,
);

const injectedItems = container.querySelectorAll(
".react-datepicker__time-list-item--injected",
);

expect(Array.from(injectedItems).map((node) => node.textContent)).toEqual([
"00:00:01",
"00:30:01",
"01:30:01",
]);
});
});
Loading