Skip to content

Commit 5eee56f

Browse files
jimmynguycmartijnrusschen
authored andcommitted
Improvements to injectTimes (Hacker0x01#1330)
* Allow multiple times to be injected between each interval * Better format * Tests * Move sorting out of loop
1 parent ed55d2b commit 5eee56f

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

docs-site/src/examples/inject_times.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export default class InjectTimes extends React.Component {
2727
<br />
2828
<strong>{` showTimeSelect
2929
timeFormat="HH:mm"
30-
injectTimes={[moment().hours(0).minutes(1), moment().hours(12).minutes(5), moment().hours(23).minutes(59)]}
30+
injectTimes={[
31+
moment().hours(0).minutes(1),
32+
moment().hours(12).minutes(5),
33+
moment().hours(23).minutes(59)
34+
]}
3135
dateFormat="LLL"
3236
/>
3337
`}</strong>

src/date_utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,14 +495,15 @@ export function getHightLightDaysMap(
495495
return dateClasses;
496496
}
497497

498-
export function timeToInjectAfter(
498+
export function timesToInjectAfter(
499499
startOfDay,
500500
currentTime,
501501
currentMultiplier,
502502
intervals,
503503
injectedTimes
504504
) {
505505
const l = injectedTimes.length;
506+
const times = [];
506507
for (let i = 0; i < l; i++) {
507508
const injectedTime = addMinutes(
508509
addHours(cloneDate(startOfDay), getHour(injectedTimes[i])),
@@ -514,9 +515,9 @@ export function timeToInjectAfter(
514515
);
515516

516517
if (injectedTime.isBetween(currentTime, nextTime)) {
517-
return injectedTimes[i];
518+
times.push(injectedTimes[i]);
518519
}
519520
}
520521

521-
return false;
522+
return times;
522523
}

src/time.jsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
formatDate,
1111
isTimeInDisabledRange,
1212
isTimeDisabled,
13-
timeToInjectAfter
13+
timesToInjectAfter
1414
} from "./date_utils";
1515

1616
export default class Time extends React.Component {
@@ -97,21 +97,24 @@ export default class Time extends React.Component {
9797
const currM = getMinute(activeTime);
9898
let base = getStartOfDay(newDate());
9999
const multiplier = 1440 / intervals;
100+
const sortedInjectTimes =
101+
this.props.injectTimes &&
102+
this.props.injectTimes.sort(function(a, b) {
103+
return a - b;
104+
});
100105
for (let i = 0; i < multiplier; i++) {
101106
const currentTime = addMinutes(cloneDate(base), i * intervals);
102107
times.push(currentTime);
103108

104-
if (this.props.injectTimes) {
105-
const timeToInject = timeToInjectAfter(
109+
if (sortedInjectTimes) {
110+
const timesToInject = timesToInjectAfter(
106111
base,
107112
currentTime,
108113
i,
109114
intervals,
110-
this.props.injectTimes
115+
sortedInjectTimes
111116
);
112-
if (timeToInject) {
113-
times.push(timeToInject);
114-
}
117+
times = times.concat(timesToInject);
115118
}
116119
}
117120

test/inject_times_test.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ describe("TimeComponent", () => {
3131
/>
3232
);
3333

34-
const disabledItems = timeComponent.find(
34+
const injectedItems = timeComponent.find(
3535
".react-datepicker__time-list-item--injected"
3636
);
37-
expect(disabledItems).to.have.length(3);
37+
expect(injectedItems).to.have.length(3);
3838
});
3939

4040
it("should not affect existing time intervals", () => {
@@ -50,9 +50,51 @@ describe("TimeComponent", () => {
5050
/>
5151
);
5252

53-
const disabledItems = timeComponent.find(
53+
const injectedItems = timeComponent.find(
5454
".react-datepicker__time-list-item--injected"
5555
);
56-
expect(disabledItems).to.have.length(0);
56+
expect(injectedItems).to.have.length(0);
57+
});
58+
59+
it("should allow multiple injected times per interval", () => {
60+
const today = utils.getStartOfDay(utils.newDate());
61+
const timeComponent = mount(
62+
<TimeComponent
63+
timeIntervals={60}
64+
injectTimes={[
65+
utils.addMinutes(cloneDate(today), 1),
66+
utils.addMinutes(cloneDate(today), 2),
67+
utils.addMinutes(cloneDate(today), 3)
68+
]}
69+
/>
70+
);
71+
72+
const injectedItems = timeComponent.find(
73+
".react-datepicker__time-list-item--injected"
74+
);
75+
expect(injectedItems).to.have.length(3);
76+
});
77+
78+
it("should sort injected times automatically", () => {
79+
const today = utils.getStartOfDay(utils.newDate());
80+
const timeComponent = mount(
81+
<TimeComponent
82+
timeIntervals={60}
83+
injectTimes={[
84+
utils.addMinutes(cloneDate(today), 3),
85+
utils.addMinutes(cloneDate(today), 1),
86+
utils.addMinutes(cloneDate(today), 2)
87+
]}
88+
/>
89+
);
90+
91+
const injectedItems = timeComponent.find(
92+
".react-datepicker__time-list-item--injected"
93+
);
94+
expect(injectedItems.map(node => node.text())).eql([
95+
"12:01 AM",
96+
"12:02 AM",
97+
"12:03 AM"
98+
]);
5799
});
58100
});

0 commit comments

Comments
 (0)