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 month comparison, dont respect day of month when comparing months #2231

Merged
merged 2 commits into from
Sep 1, 2023
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
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