Skip to content

Commit 6a4193c

Browse files
Add an includeTimes param
This option is the same idea as "includeDates" but applies to the time select. Only times that are included in the "includeDates" list and also match up with settings like "timeIntervals" will be included. All other times are disabled. This uses the same helper function as "excludeTimes" for implementation.
1 parent 8e1eea0 commit 6a4193c

File tree

9 files changed

+123
-4
lines changed

9 files changed

+123
-4
lines changed

docs-site/src/example_components.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import RawChange from './examples/raw_change'
4444
import ShowTime from './examples/show_time'
4545
import ExcludeTimes from './examples/exclude_times'
4646
import ExcludeTimePeriod from './examples/exclude_time_period'
47+
import IncludeTimes from './examples/include_times'
4748
import DontCloseOnSelect from './examples/dont_close_onSelect'
4849
import OpenByDefault from './examples/open_by_default'
4950

@@ -67,6 +68,10 @@ export default class exampleComponents extends React.Component {
6768
title: 'Exclude Times',
6869
component: <ExcludeTimes />
6970
},
71+
{
72+
title: 'Include Times',
73+
component: <IncludeTimes />
74+
},
7075
{
7176
title: 'Specific Time Range',
7277
component: <ExcludeTimePeriod />
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React from "react";
2+
import DatePicker from "react-datepicker";
3+
import moment from "moment";
4+
5+
export default class IncludeTimes extends React.Component {
6+
state = {
7+
startDate: moment()
8+
.hours(16)
9+
.minutes(30)
10+
};
11+
12+
handleChange = date => {
13+
this.setState({
14+
startDate: date
15+
});
16+
};
17+
18+
render() {
19+
return (
20+
<div className="row">
21+
<pre className="column example__code">
22+
<code className="jsx">
23+
{"<DatePicker"}
24+
<br />
25+
{" selected={this.state.startDate}"}
26+
<br />
27+
{" onChange={this.handleChange}"}
28+
<br />
29+
<strong>
30+
{" showTimeSelect"}
31+
<br />
32+
{
33+
" includeTimes={[moment().hours(17).minutes(0), moment().hours(18).minutes(30), moment().hours(19).minutes(30)], moment().hours(17).minutes(30)}"
34+
}
35+
</strong>
36+
<br />
37+
<strong>{' dateFormat="LLL"'}</strong>
38+
<br />
39+
{"/>"}
40+
</code>
41+
</pre>
42+
<div className="column">
43+
<DatePicker
44+
selected={this.state.startDate}
45+
onChange={this.handleChange}
46+
showTimeSelect
47+
includeTimes={[
48+
moment()
49+
.hours(17)
50+
.minutes(0),
51+
moment()
52+
.hours(18)
53+
.minutes(30),
54+
moment()
55+
.hours(19)
56+
.minutes(30),
57+
moment()
58+
.hours(17)
59+
.minutes(30)
60+
]}
61+
dateFormat="LLL"/>
62+
</div>
63+
</div>
64+
);
65+
}
66+
}

docs/datepicker.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ General datepicker component.
2828
| `highlightDates` | `array` | | |
2929
| `id` | `string` | | |
3030
| `includeDates` | `array` | | |
31+
| `includeTimes` | `array` | | |
3132
| `inline` | `bool` | | |
3233
| `isClearable` | `bool` | | |
3334
| `locale` | `string` | | |

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ General datepicker component.
2929
| `highlightDates` | `array` | | |
3030
| `id` | `string` | | |
3131
| `includeDates` | `array` | | |
32+
| `includeTimes` | `array` | | |
3233
| `inline` | `bool` | | |
3334
| `isClearable` | `bool` | | |
3435
| `locale` | `string` | | |

src/calendar.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export default class Calendar extends React.Component {
6161
formatWeekNumber: PropTypes.func,
6262
highlightDates: PropTypes.instanceOf(Map),
6363
includeDates: PropTypes.array,
64+
includeTimes: PropTypes.array,
6465
inline: PropTypes.bool,
6566
locale: PropTypes.string,
6667
maxDate: PropTypes.object,
@@ -500,6 +501,7 @@ export default class Calendar extends React.Component {
500501
selected={this.props.selected}
501502
onChange={this.props.onTimeChange}
502503
format={this.props.timeFormat}
504+
includeTimes={this.props.includeTimes}
503505
intervals={this.props.timeIntervals}
504506
minTime={this.props.minTime}
505507
maxTime={this.props.maxTime}

src/date_utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ export function getMonthInLocale(locale, date, format) {
336336
return locale.months(date, format);
337337
}
338338

339-
export function getMonthShortInLocale (locale, date) {
340-
return locale.monthsShort(date)
339+
export function getMonthShortInLocale(locale, date) {
340+
return locale.monthsShort(date);
341341
}
342342

343343
// ** Utils for some components **

src/index.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default class DatePicker extends React.Component {
7070
highlightDates: PropTypes.array,
7171
id: PropTypes.string,
7272
includeDates: PropTypes.array,
73+
includeTimes: PropTypes.array,
7374
inline: PropTypes.bool,
7475
isClearable: PropTypes.bool,
7576
locale: PropTypes.string,
@@ -492,6 +493,7 @@ export default class DatePicker extends React.Component {
492493
formatWeekNumber={this.props.formatWeekNumber}
493494
highlightDates={this.state.highlightDates}
494495
includeDates={this.props.includeDates}
496+
includeTimes={this.props.includeTimes}
495497
inline={this.props.inline}
496498
peekNextMonth={this.props.peekNextMonth}
497499
showMonthDropdown={this.props.showMonthDropdown}

src/time.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
export default class Time extends React.Component {
1616
static propTypes = {
1717
format: PropTypes.string,
18+
includeTimes: PropTypes.array,
1819
intervals: PropTypes.number,
1920
selected: PropTypes.object,
2021
onChange: PropTypes.func,
@@ -46,7 +47,8 @@ export default class Time extends React.Component {
4647
if (
4748
((this.props.minTime || this.props.maxTime) &&
4849
isTimeInDisabledRange(time, this.props)) ||
49-
(this.props.excludeTimes && isTimeDisabled(time, this.props.excludeTimes))
50+
(this.props.excludeTimes && isTimeDisabled(time, this.props.excludeTimes)) ||
51+
(this.props.includeTimes && !isTimeDisabled(time, this.props.includeTimes))
5052
) {
5153
return;
5254
}
@@ -63,7 +65,8 @@ export default class Time extends React.Component {
6365
if (
6466
((this.props.minTime || this.props.maxTime) &&
6567
isTimeInDisabledRange(time, this.props)) ||
66-
(this.props.excludeTimes && isTimeDisabled(time, this.props.excludeTimes))
68+
(this.props.excludeTimes && isTimeDisabled(time, this.props.excludeTimes)) ||
69+
(this.props.includeTimes && !isTimeDisabled(time, this.props.includeTimes))
6770
) {
6871
classes.push("react-datepicker__time-list-item--disabled");
6972
}

test/include_times_test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from "react";
2+
import { mount } from "enzyme";
3+
import * as utils from "../src/date_utils";
4+
import { setTime, cloneDate, newDate } from "../src/date_utils";
5+
import TimeComponent from "../src/time";
6+
7+
function cloneDateWithTime(date, time) {
8+
return setTime(cloneDate(date), time);
9+
}
10+
11+
describe("TimeComponent", () => {
12+
let sandbox;
13+
14+
beforeEach(() => {
15+
sandbox = sinon.sandbox.create();
16+
});
17+
18+
afterEach(() => {
19+
sandbox.restore();
20+
});
21+
22+
it("should only enable times specified in includeTimes props", () => {
23+
const today = utils.getStartOfDay(utils.newDate());
24+
const timeComponent = mount(
25+
<TimeComponent
26+
includeTimes={[
27+
utils.addMinutes(cloneDate(today), 60),
28+
utils.addMinutes(cloneDate(today), 120),
29+
utils.addMinutes(cloneDate(today), 150)
30+
]}
31+
/>
32+
);
33+
34+
const disabledItems = timeComponent.find(
35+
".react-datepicker__time-list-item--disabled"
36+
);
37+
expect(disabledItems).to.have.length(45);
38+
});
39+
});

0 commit comments

Comments
 (0)