Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for month being reset when month prop is the same month #695

Merged
merged 2 commits into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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