Skip to content

Commit 66575f2

Browse files
jnystadmartijnrusschen
authored andcommitted
fix: Prev/next buttons when using showMonthYearPicker (#1950)
* fix: Prev/next buttons when using showMonthYearPicker Check prev/next year instead of month when showMonthYearPicker is set. * test: Add tests for yearDisabledBefore/After And fix discovered issue.
1 parent b943f20 commit 66575f2

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

src/calendar.jsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import {
3030
isSameDay,
3131
monthDisabledBefore,
3232
monthDisabledAfter,
33+
yearDisabledBefore,
34+
yearDisabledAfter,
3335
getEffectiveMinDate,
3436
getEffectiveMaxDate,
3537
addZero
@@ -349,10 +351,9 @@ export default class Calendar extends React.Component {
349351
return;
350352
}
351353

352-
const allPrevDaysDisabled = monthDisabledBefore(
353-
this.state.date,
354-
this.props
355-
);
354+
const allPrevDaysDisabled = this.props.showMonthYearPicker
355+
? yearDisabledBefore(this.state.date, this.props)
356+
: monthDisabledBefore(this.state.date, this.props);
356357

357358
if (
358359
(!this.props.forceShowMonthNavigation &&
@@ -406,7 +407,9 @@ export default class Calendar extends React.Component {
406407
return;
407408
}
408409

409-
const allNextDaysDisabled = monthDisabledAfter(this.state.date, this.props);
410+
const allNextDaysDisabled = this.props.showMonthYearPicker
411+
? yearDisabledAfter(this.state.date, this.props)
412+
: monthDisabledAfter(this.state.date, this.props);
410413

411414
if (
412415
(!this.props.forceShowMonthNavigation &&

src/date_utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import max from "date-fns/max";
3333
import differenceInCalendarDays from "date-fns/differenceInCalendarDays";
3434
import differenceInCalendarMonths from "date-fns/differenceInCalendarMonths";
3535
import differenceInCalendarWeeks from "date-fns/differenceInCalendarWeeks";
36+
import differenceInCalendarYears from "date-fns/differenceInCalendarYears";
3637
import startOfDay from "date-fns/startOfDay";
3738
import startOfWeek from "date-fns/startOfWeek";
3839
import startOfMonth from "date-fns/startOfMonth";
@@ -525,6 +526,30 @@ export function monthDisabledAfter(day, { maxDate, includeDates } = {}) {
525526
);
526527
}
527528

529+
export function yearDisabledBefore(day, { minDate, includeDates } = {}) {
530+
const previousYear = subYears(day, 1);
531+
return (
532+
(minDate && differenceInCalendarYears(minDate, previousYear) > 0) ||
533+
(includeDates &&
534+
includeDates.every(
535+
includeDate => differenceInCalendarYears(includeDate, previousYear) > 0
536+
)) ||
537+
false
538+
);
539+
}
540+
541+
export function yearDisabledAfter(day, { maxDate, includeDates } = {}) {
542+
const nextYear = addYears(day, 1);
543+
return (
544+
(maxDate && differenceInCalendarYears(nextYear, maxDate) > 0) ||
545+
(includeDates &&
546+
includeDates.every(
547+
includeDate => differenceInCalendarYears(nextYear, includeDate) > 0
548+
)) ||
549+
false
550+
);
551+
}
552+
528553
export function getEffectiveMinDate({ minDate, includeDates }) {
529554
if (includeDates && minDate) {
530555
let minDates = includeDates.filter(

test/date_utils_test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
isQuarterDisabled,
1414
monthDisabledBefore,
1515
monthDisabledAfter,
16+
yearDisabledBefore,
17+
yearDisabledAfter,
1618
getEffectiveMinDate,
1719
getEffectiveMaxDate,
1820
addZero,
@@ -426,6 +428,54 @@ describe("date_utils", function() {
426428
});
427429
});
428430

431+
describe("yearDisabledBefore", () => {
432+
it("should return false by default", () => {
433+
expect(yearDisabledBefore(newDate())).to.be.false;
434+
});
435+
436+
it("should return true if min date is in the same year", () => {
437+
const day = newDate("2016-02-19");
438+
const minDate = newDate("2016-03-01");
439+
expect(yearDisabledBefore(day, { minDate })).to.be.true;
440+
});
441+
442+
it("should return false if min date is in the previous year", () => {
443+
const day = newDate("2016-03-19");
444+
const minDate = newDate("2015-03-29");
445+
expect(yearDisabledBefore(day, { minDate })).to.be.false;
446+
});
447+
448+
it("should return true if previous year is before include dates", () => {
449+
const day = newDate("2016-03-19");
450+
const includeDates = [newDate("2016-03-01")];
451+
expect(yearDisabledBefore(day, { includeDates })).to.be.true;
452+
});
453+
});
454+
455+
describe("yearDisabledAfter", () => {
456+
it("should return false by default", () => {
457+
expect(yearDisabledAfter(newDate())).to.be.false;
458+
});
459+
460+
it("should return true if max date is in the same year", () => {
461+
const day = newDate("2016-03-19");
462+
const maxDate = newDate("2016-08-31");
463+
expect(yearDisabledAfter(day, { maxDate })).to.be.true;
464+
});
465+
466+
it("should return false if max date is in the next year", () => {
467+
const day = newDate("2016-03-19");
468+
const maxDate = newDate("2017-04-01");
469+
expect(yearDisabledAfter(day, { maxDate })).to.be.false;
470+
});
471+
472+
it("should return true if next year is after include dates", () => {
473+
const day = newDate("2016-03-19");
474+
const includeDates = [newDate("2016-03-01")];
475+
expect(yearDisabledAfter(day, { includeDates })).to.be.true;
476+
});
477+
});
478+
429479
describe("getEffectiveMinDate", () => {
430480
it("should return null by default", () => {
431481
expect(getEffectiveMinDate({})).to.not.exist;

0 commit comments

Comments
 (0)