Skip to content

Commit

Permalink
Fix for month being reset when month prop is the same month (#695)
Browse files Browse the repository at this point in the history
* Add isSameMonth to DateUtils

* Fix month props comparison
  • Loading branch information
gpbl authored Apr 14, 2018
1 parent a66560a commit b9598cd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/src/pages/api/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ console.log(newRange.from) // 2015-05-24`}</CodeBlock>
Return <code>true</code> if <code>day1</code> and
<code>day2</code> are the same day.
</p>
<h3>
<Anchor id="isSameMonth" />
isSameMonth <code>(day1: ?Date, day2: ?Date) ⇒ boolean</code>
</h3>
<p>
Return <code>true</code> if <code>day1</code> and
<code>day2</code> fall in the same month.
</p>
</ApiDocs>
</DocPage>
);
18 changes: 18 additions & 0 deletions src/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ export function isSameDay(d1, d2) {
);
}

/**
* Return `true` if two dates fall in the same month.
*
* @export
* @param {Date} d1
* @param {Date} d2
* @return {Boolean}
*/
export function isSameMonth(d1, d2) {
if (!d1 || !d2) {
return false;
}
return (
d1.getMonth() === d2.getMonth() && d1.getFullYear() === d2.getFullYear()
);
}

/**
* Returns `true` if the first day is before the second day.
*
Expand Down Expand Up @@ -194,4 +211,5 @@ export default {
isFutureDay,
isPastDay,
isSameDay,
isSameMonth,
};
2 changes: 1 addition & 1 deletion src/DayPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export default class DayPicker extends Component {
}

componentWillReceiveProps(nextProps) {
if (this.props.month !== nextProps.month) {
if (!DateUtils.isSameMonth(this.props.month, nextProps.month)) {
this.setState(this.getStateFromProps(nextProps));
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/DateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ describe('DateUtils', () => {
});
});

describe('isSameMonth', () => {
it('returns true if two days differ only by time', () => {
const day1 = new Date(2015, 10, 11, 5, 25);
const day2 = new Date(2015, 10, 11, 7, 40);
const isSameMonth = DateUtils.isSameMonth(day1, day2);
expect(isSameMonth).toBe(true);
});
it('returns true for different days', () => {
const day1 = new Date(2015, 8, 12);
const day2 = new Date(2015, 8, 11);
const isSameMonth = DateUtils.isSameMonth(day1, day2);
expect(isSameMonth).toBe(true);
});
it('returns false if one of the days is not specified', () => {
const day1 = new Date(2015, 8, 12);
const isSameMonth = DateUtils.isSameMonth(day1, null);
expect(isSameMonth).toBe(false);
});
});

describe('isPastDay', () => {
it('detects a day is in the past', () => {
const isPastDay = DateUtils.isPastDay(new Date(2015, 5, 11));
Expand Down

0 comments on commit b9598cd

Please sign in to comment.