Skip to content

Commit 467b990

Browse files
Greg Smithmartijnrusschen
Greg Smith
authored andcommitted
feat: Time picker only (Hacker0x01#1281)
* Added showTimeSelectOnly prop * Added unit test for showTimeSelectOnly * Added additional unit tests
1 parent 9ddc73f commit 467b990

File tree

6 files changed

+117
-6
lines changed

6 files changed

+117
-6
lines changed

docs-site/src/example_components.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import Portal from './examples/portal'
4242
import InlinePortal from './examples/inline_portal'
4343
import RawChange from './examples/raw_change'
4444
import ShowTime from './examples/show_time'
45+
import ShowTimeOnly from './examples/show_time_only'
4546
import ExcludeTimes from './examples/exclude_times'
4647
import ExcludeTimePeriod from './examples/exclude_time_period'
4748
import IncludeTimes from './examples/include_times'
@@ -64,6 +65,10 @@ export default class exampleComponents extends React.Component {
6465
title: 'Select Time',
6566
component: <ShowTime />
6667
},
68+
{
69+
title: 'Select Time Only',
70+
component: <ShowTimeOnly />
71+
},
6772
{
6873
title: 'Exclude Times',
6974
component: <ExcludeTimes />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from "react";
2+
import DatePicker from "react-datepicker";
3+
import moment from "moment";
4+
5+
export default class ShowTimeOnly extends React.Component {
6+
constructor(props) {
7+
super(props);
8+
this.state = {
9+
startDate: moment()
10+
};
11+
}
12+
13+
handleChange = date => {
14+
this.setState({
15+
startDate: date
16+
});
17+
};
18+
19+
render() {
20+
return (
21+
<div className="row">
22+
<pre className="column example__code">
23+
<code className="jsx">
24+
{`
25+
<DatePicker
26+
selected={this.state.startDate}
27+
onChange={this.handleChange}`}
28+
<br />
29+
<strong>{` showTimeSelect
30+
showTimeSelectOnly
31+
timeIntervals={15}
32+
dateFormat="LT"
33+
timeCaption="Time"
34+
/>
35+
`}</strong>
36+
</code>
37+
</pre>
38+
<div className="column">
39+
<DatePicker
40+
selected={this.state.startDate}
41+
onChange={this.handleChange}
42+
showTimeSelect
43+
showTimeSelectOnly
44+
timeIntervals={15}
45+
timeCaption="Time"
46+
dateFormat="LT"/>
47+
</div>
48+
</div>
49+
);
50+
}
51+
}

src/calendar.jsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export default class Calendar extends React.Component {
7575
onSelect: PropTypes.func.isRequired,
7676
onWeekSelect: PropTypes.func,
7777
showTimeSelect: PropTypes.bool,
78+
showTimeSelectOnly: PropTypes.bool,
7879
timeFormat: PropTypes.string,
7980
timeIntervals: PropTypes.number,
8081
onTimeChange: PropTypes.func,
@@ -291,9 +292,10 @@ export default class Calendar extends React.Component {
291292
const allPrevDaysDisabled = allDaysDisabledBefore(this.state.date, "month", this.props);
292293

293294
if (
294-
!this.props.forceShowMonthNavigation &&
295+
(!this.props.forceShowMonthNavigation &&
295296
!this.props.showDisabledMonthNavigation &&
296-
allPrevDaysDisabled
297+
allPrevDaysDisabled) ||
298+
this.props.showTimeSelectOnly
297299
) {
298300
return;
299301
}
@@ -322,9 +324,10 @@ export default class Calendar extends React.Component {
322324
const allNextDaysDisabled = allDaysDisabledAfter(this.state.date, "month", this.props);
323325

324326
if (
325-
!this.props.forceShowMonthNavigation &&
327+
(!this.props.forceShowMonthNavigation &&
326328
!this.props.showDisabledMonthNavigation &&
327-
allNextDaysDisabled
329+
allNextDaysDisabled) ||
330+
this.props.showTimeSelectOnly
328331
) {
329332
return;
330333
}
@@ -423,7 +426,7 @@ export default class Calendar extends React.Component {
423426
};
424427

425428
renderTodayButton = () => {
426-
if (!this.props.todayButton) {
429+
if (!this.props.todayButton || this.props.showTimeSelectOnly) {
427430
return;
428431
}
429432
return (
@@ -438,6 +441,10 @@ export default class Calendar extends React.Component {
438441
};
439442

440443
renderMonths = () => {
444+
if (this.props.showTimeSelectOnly) {
445+
return;
446+
}
447+
441448
var monthList = [];
442449
for (var i = 0; i < this.props.monthsShown; ++i) {
443450
var monthDate = addMonths(cloneDate(this.state.date), i);
@@ -521,7 +528,9 @@ export default class Calendar extends React.Component {
521528

522529
render() {
523530
return (
524-
<div className={classnames("react-datepicker", this.props.className)}>
531+
<div className={classnames("react-datepicker", this.props.className, {
532+
"react-datepicker--time-only": this.props.showTimeSelectOnly
533+
})}>
525534
<div className="react-datepicker__triangle" />
526535
{this.renderPreviousMonthButton()}
527536
{this.renderNextMonthButton()}

src/index.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export default class DatePicker extends React.Component {
123123
yearDropdownItemNumber: PropTypes.number,
124124
shouldCloseOnSelect: PropTypes.bool,
125125
showTimeSelect: PropTypes.bool,
126+
showTimeSelectOnly: PropTypes.bool,
126127
timeFormat: PropTypes.string,
127128
timeIntervals: PropTypes.number,
128129
minTime: PropTypes.object,
@@ -529,6 +530,7 @@ export default class DatePicker extends React.Component {
529530
onYearChange={this.props.onYearChange}
530531
dayClassName={this.props.dayClassName}
531532
showTimeSelect={this.props.showTimeSelect}
533+
showTimeSelectOnly={this.props.showTimeSelectOnly}
532534
onTimeChange={this.handleTimeChange}
533535
timeFormat={this.props.timeFormat}
534536
timeIntervals={this.props.timeIntervals}

src/stylesheets/datepicker.scss

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@
1616
position: relative;
1717
}
1818

19+
.react-datepicker--time-only {
20+
.react-datepicker__triangle {
21+
left: 35px;
22+
}
23+
24+
.react-datepicker__time-container {
25+
border-left: 0;
26+
}
27+
28+
.react-datepicker__time {
29+
border-radius: 0.3rem;
30+
}
31+
32+
.react-datepicker__time-box {
33+
border-radius: 0.3rem;
34+
}
35+
}
36+
1937
.react-datepicker__triangle {
2038
position: absolute;
2139
left: 50px;

test/show_time_test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,30 @@ describe("DatePicker", () => {
2626
const caption = timeComponent.find(".react-datepicker-time__header");
2727
expect(caption.text()).to.equal("Custom time");
2828
});
29+
30+
describe("Time Select Only", () => {
31+
var datePicker = mount(
32+
<DatePicker showTimeSelect showTimeSelectOnly todayButton="Today" />
33+
);
34+
35+
it("should not show month container when showTimeSelectOnly prop is present", () => {
36+
var elem = datePicker.find(".react-datepicker__month-container");
37+
expect(elem).to.have.length(0);
38+
});
39+
40+
it("should not show previous month button when showTimeSelectOnly prop is present", () => {
41+
var elem = datePicker.find(".react-datepicker__navigation--previous");
42+
expect(elem).to.have.length(0);
43+
});
44+
45+
it("should not show next month button when showTimeSelectOnly prop is present", () => {
46+
var elem = datePicker.find(".react-datepicker__navigation--next");
47+
expect(elem).to.have.length(0);
48+
});
49+
50+
it("should not show today button when showTimeSelectOnly prop is present", () => {
51+
var elem = datePicker.find(".react-datepicker__today-button");
52+
expect(elem).to.have.length(0);
53+
});
54+
});
2955
});

0 commit comments

Comments
 (0)