Skip to content

Commit

Permalink
Fix month comparison, dont respect day of month when comparing months (
Browse files Browse the repository at this point in the history
  • Loading branch information
sebassonav authored Sep 1, 2023
1 parent 960a082 commit c67e051
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-pumas-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Fix bug in monthpicker, only compare year and month for equality on date object
12 changes: 5 additions & 7 deletions @navikt/core/react/src/date/monthpicker/MonthButton.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import cl from "clsx";
import compareAsc from "date-fns/compareAsc";
import compareDesc from "date-fns/compareDesc";
import format from "date-fns/format";
import isSameMonth from "date-fns/isSameMonth";
import isSameYear from "date-fns/isSameYear";
import setYear from "date-fns/setYear";
import React, { useEffect, useRef } from "react";
import { useDayPicker } from "react-day-picker";
Expand All @@ -20,13 +19,12 @@ interface MonthType {

const disableMonth = (month: Date, fromDate?: Date, toDate?: Date) => {
if (fromDate && toDate) {
return (
compareAsc(month, fromDate) === -1 || compareDesc(month, toDate) === -1
);
return (isSameMonth(month, fromDate) && isSameYear(month, fromDate))
|| (isSameMonth(month, toDate) && isSameYear(month, toDate))
} else if (fromDate) {
return compareAsc(month, fromDate) === -1;
return isSameMonth(month, fromDate) && isSameYear(month, fromDate)
} else if (toDate) {
return compareDesc(month, toDate) === -1;
return isSameMonth(month, toDate) && isSameYear(month, toDate)
}
return false;
};
Expand Down

0 comments on commit c67e051

Please sign in to comment.