Skip to content

Commit 9715c69

Browse files
committed
Allow for date range selection
This patch is a fork of Hacker0x01#79 with the suggested modifications and merge conflicts resolved. The credit should go to the original author. This is simply something I wanted and the original author appears to have gone MIA.
1 parent 4b00e91 commit 9715c69

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

src/calendar.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ var Calendar = React.createClass( {
9999
var minDate = new DateUtil( this.props.minDate ).safeClone(),
100100
maxDate = new DateUtil( this.props.maxDate ).safeClone(),
101101
excludeDates,
102-
disabled;
102+
disabled,
103+
inRange = day.inRange( this.props.startDate, this.props.endDate );
103104

104105
if ( this.props.excludeDates && Array.isArray( this.props.excludeDates ) ) {
105106
excludeDates = map( this.props.excludeDates, function( date ) {
@@ -117,6 +118,7 @@ var Calendar = React.createClass( {
117118
date={this.state.date}
118119
onClick={this.handleDayClick.bind( this, day )}
119120
selected={new DateUtil( this.props.selected )}
121+
inRange={inRange}
120122
disabled={disabled} />
121123
);
122124
},

src/datepicker.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ var DatePicker = React.createClass( {
139139
hideCalendar={this.hideCalendar}
140140
minDate={this.props.minDate}
141141
maxDate={this.props.maxDate}
142+
startDate={this.props.startDate}
143+
endDate={this.props.endDate}
142144
excludeDates={this.props.excludeDates}
143145
weekStart={this.props.weekStart} />
144146
</Popover>

src/day.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ var Day = React.createClass( {
2222
if ( this.props.day.sameDay( this.props.selected ) )
2323
classes.push( "datepicker__day--selected" );
2424

25+
if ( this.props.day.inRange )
26+
classes.push( "datepicker__day--in-range" );
27+
2528
if ( this.props.day.sameDay( moment() ) )
2629
classes.push( "datepicker__day--today" );
2730

28-
if ( this.isWeekend() ) {
31+
if ( this.isWeekend() )
2932
classes.push( "datepicker__day--weekend" );
30-
}
3133

3234
return (
3335
<div className={classes.join( " " )} onClick={this.handleClick}>

src/stylesheets/datepicker.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
font-weight: bold;
106106
}
107107

108-
&--selected {
108+
&--selected,
109+
&--in-range {
109110
border-radius: $border-radius;
110111
background-color: $selected-color;
111112
color: #fff;

src/util/date.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ DateUtil.prototype.sameMonth = function( other ) {
1818
return this._date.isSame( other._date, "month" );
1919
};
2020

21+
DateUtil.prototype.inRange = function( startDate, endDate ) {
22+
var startDate = startDate._date.startOf( "day" ).subtract( 1, "seconds" );
23+
var endDate = endDate._date.startOf( "day" ).add( 1, "seconds" );
24+
return this._date.isBetween( startDate, endDate );
25+
};
26+
2127
DateUtil.prototype.day = function() {
2228
return this._date.date();
2329
};

test/util/date_test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,40 @@ describe( "DateUtil", function() {
9494
} );
9595
} );
9696

97+
describe( "#inRange", function() {
98+
it( "returns true when the date is in range the passed date", function() {
99+
var date = new DateUtil( moment( "2014-02-09" ) );
100+
var start_date = new DateUtil( moment( "2014-02-08" ) );
101+
var end_date = new DateUtil( moment( "2014-02-10" ) );
102+
103+
expect( date.inRange( start_date, end_date ) ).to.eq( true );
104+
} );
105+
106+
it( "returns false when the date is not in range the passed date", function() {
107+
var date = new DateUtil( moment( "2014-02-08" ) );
108+
var start_date = new DateUtil( moment( "2014-02-09" ) );
109+
var end_date = new DateUtil( moment( "2014-02-10" ) );
110+
111+
expect( date.inRange( start_date, end_date ) ).to.eq( false );
112+
} );
113+
114+
it( "returns true when the passed date is the same day as start of range", function() {
115+
var date = new DateUtil( moment( "2014-02-09" ) );
116+
var start_date = new DateUtil( moment( "2014-02-09" ) );
117+
var end_date = new DateUtil( moment( "2014-02-10" ) );
118+
119+
expect( date.inRange( start_date, end_date ) ).to.eq( true );
120+
} );
121+
122+
it( "returns true when the passed date is the same day as end of range", function() {
123+
var date = new DateUtil( moment( "2014-02-10" ) );
124+
var start_date = new DateUtil( moment( "2014-02-09" ) );
125+
var end_date = new DateUtil( moment( "2014-02-10" ) );
126+
127+
expect( date.inRange( start_date, end_date ) ).to.eq( true );
128+
} );
129+
} );
130+
97131
describe( "#day", function() {
98132
it( "returns the day of the month", function() {
99133
var date = new DateUtil( moment( "2014-02-08" ) );

0 commit comments

Comments
 (0)