Skip to content

Commit

Permalink
Merge pull request #4683 from yykcool/main
Browse files Browse the repository at this point in the history
Add support second-level granularity to injectTimes
  • Loading branch information
martijnrusschen authored Apr 14, 2024
2 parents f81afc4 + 8ae5a60 commit 3e4ca32
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
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}
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",
]);
});
});

0 comments on commit 3e4ca32

Please sign in to comment.