Skip to content

Commit 3e4ca32

Browse files
Merge pull request #4683 from yykcool/main
Add support second-level granularity to injectTimes
2 parents f81afc4 + 8ae5a60 commit 3e4ca32

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

docs-site/src/examples/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"DatePicker": false,
1616
"getHours": false,
1717
"setHours": false,
18+
"setSeconds": false,
1819
"setMinutes": false,
1920
"getDate": false,
2021
"addDays": false,

docs-site/src/examples/injectTimes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
selected={startDate}
88
onChange={(date) => setStartDate(date)}
99
showTimeSelect
10-
timeFormat="HH:mm"
10+
timeFormat="HH:mm:ss"
1111
injectTimes={[
12-
setHours(setMinutes(new Date(), 1), 0),
12+
setHours(setMinutes(setSeconds(new Date(), 10), 1), 0),
1313
setHours(setMinutes(new Date(), 5), 12),
1414
setHours(setMinutes(new Date(), 59), 23),
1515
]}

src/date_utils.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import { isWithinInterval } from "date-fns/isWithinInterval";
5555
import { toDate } from "date-fns/toDate";
5656
import { parse } from "date-fns/parse";
5757
import { parseISO } from "date-fns/parseISO";
58+
import { addSeconds } from "date-fns";
5859

5960
export const DEFAULT_YEAR_ITEM_NUMBER = 12;
6061

@@ -295,7 +296,15 @@ export function getEndOfMonth(date) {
295296

296297
// *** Addition ***
297298

298-
export { addMinutes, addDays, addWeeks, addMonths, addQuarters, addYears };
299+
export {
300+
addSeconds,
301+
addMinutes,
302+
addDays,
303+
addWeeks,
304+
addMonths,
305+
addQuarters,
306+
addYears,
307+
};
299308

300309
// *** Subtraction ***
301310

@@ -850,10 +859,11 @@ export function timesToInjectAfter(
850859
const l = injectedTimes.length;
851860
const times = [];
852861
for (let i = 0; i < l; i++) {
853-
const injectedTime = addMinutes(
854-
addHours(startOfDay, getHours(injectedTimes[i])),
855-
getMinutes(injectedTimes[i]),
856-
);
862+
let injectedTime = startOfDay;
863+
injectedTime = addHours(injectedTime, getHours(injectedTimes[i]));
864+
injectedTime = addMinutes(injectedTime, getMinutes(injectedTimes[i]));
865+
injectedTime = addSeconds(injectedTime, getSeconds(injectedTimes[i]));
866+
857867
const nextTime = addMinutes(
858868
startOfDay,
859869
(currentMultiplier + 1) * intervals,

src/time.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getHoursInDay,
1414
isSameMinute,
1515
} from "./date_utils";
16+
import { getSeconds } from "date-fns";
1617

1718
export default class Time extends React.Component {
1819
static get defaultProps() {
@@ -121,9 +122,13 @@ export default class Time extends React.Component {
121122
if (this.isDisabledTime(time)) {
122123
classes.push("react-datepicker__time-list-item--disabled");
123124
}
125+
126+
//convert this.props.intervals and the relevant time to seconds and check if it it's a clean multiple of the interval
124127
if (
125128
this.props.injectTimes &&
126-
(getHours(time) * 60 + getMinutes(time)) % this.props.intervals !== 0
129+
(getHours(time) * 3600 + getMinutes(time) * 60 + getSeconds(time)) %
130+
(this.props.intervals * 60) !==
131+
0
127132
) {
128133
classes.push("react-datepicker__time-list-item--injected");
129134
}

test/inject_times_test.test.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("TimeComponent", () => {
2626
const today = utils.getStartOfDay(utils.newDate());
2727
const { container } = render(
2828
<TimeComponent
29-
timeIntervals={60}
29+
intervals={60}
3030
injectTimes={[
3131
utils.addMinutes(today, 0),
3232
utils.addMinutes(today, 60),
@@ -45,7 +45,7 @@ describe("TimeComponent", () => {
4545
const today = utils.getStartOfDay(utils.newDate());
4646
const { container } = render(
4747
<TimeComponent
48-
timeIntervals={60}
48+
intervals={60}
4949
injectTimes={[
5050
utils.addMinutes(today, 1),
5151
utils.addMinutes(today, 2),
@@ -62,9 +62,10 @@ describe("TimeComponent", () => {
6262

6363
it("should sort injected times automatically", () => {
6464
const today = utils.getStartOfDay(utils.newDate());
65+
6566
const { container } = render(
6667
<TimeComponent
67-
timeIntervals={60}
68+
intervals={60}
6869
injectTimes={[
6970
utils.addMinutes(today, 3),
7071
utils.addMinutes(today, 1),
@@ -82,4 +83,30 @@ describe("TimeComponent", () => {
8283
"12:03 AM",
8384
]);
8485
});
86+
87+
it("should support hours, minutes, and seconds", () => {
88+
const today = utils.getStartOfDay(utils.newDate());
89+
90+
const { container } = render(
91+
<TimeComponent
92+
format="HH:mm:ss"
93+
intervals={60}
94+
injectTimes={[
95+
utils.addSeconds(today, 1),
96+
utils.addMinutes(utils.addSeconds(today, 1), 30),
97+
utils.addHours(utils.addMinutes(utils.addSeconds(today, 1), 30), 1),
98+
]}
99+
/>,
100+
);
101+
102+
const injectedItems = container.querySelectorAll(
103+
".react-datepicker__time-list-item--injected",
104+
);
105+
106+
expect(Array.from(injectedItems).map((node) => node.textContent)).toEqual([
107+
"00:00:01",
108+
"00:30:01",
109+
"01:30:01",
110+
]);
111+
});
85112
});

0 commit comments

Comments
 (0)