Skip to content

Commit d536caf

Browse files
fix(datepicker) duplicate month display #1456 (#1457)
1 parent 1a5dadd commit d536caf

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

packages/ui/src/components/Datepicker/Views/Months.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { FC } from "react";
22
import { twMerge } from "tailwind-merge";
33
import { mergeDeep } from "../../../helpers/merge-deep";
44
import { useDatePickerContext } from "../DatepickerContext";
5-
import { getFormattedDate, isDateEqual, isDateInRange, Views } from "../helpers";
5+
import { getFormattedDate, isDateInRange, isMonthEqual, Views } from "../helpers";
66

77
export interface FlowbiteDatepickerViewsMonthsTheme {
88
items: {
@@ -20,27 +20,19 @@ export interface DatepickerViewsMonthsProps {
2020
}
2121

2222
export const DatepickerViewsMonth: FC<DatepickerViewsMonthsProps> = ({ theme: customTheme = {} }) => {
23-
const {
24-
theme: rootTheme,
25-
minDate,
26-
maxDate,
27-
selectedDate,
28-
viewDate,
29-
language,
30-
setViewDate,
31-
setView,
32-
} = useDatePickerContext();
23+
const { theme: rootTheme, minDate, maxDate, selectedDate, language, setViewDate, setView } = useDatePickerContext();
3324

3425
const theme = mergeDeep(rootTheme.views.months, customTheme);
3526

3627
return (
3728
<div className={theme.items.base}>
3829
{[...Array(12)].map((_month, index) => {
39-
const newDate = new Date(viewDate.getTime());
40-
newDate.setMonth(index);
30+
const newDate = new Date();
31+
// setting day to 1 to avoid overflow issues
32+
newDate.setMonth(index, 1);
4133
const month = getFormattedDate(language, newDate, { month: "short" });
4234

43-
const isSelected = isDateEqual(selectedDate, newDate);
35+
const isSelected = isMonthEqual(selectedDate, newDate);
4436
const isDisabled = !isDateInRange(newDate, minDate, maxDate);
4537

4638
return (

packages/ui/src/components/Datepicker/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export const isDateEqual = (date: Date, selectedDate: Date): boolean => {
4444
return date.getTime() === selectedDate.getTime();
4545
};
4646

47+
export const isMonthEqual = (date: Date, selectedDate: Date): boolean => {
48+
return date.getMonth() === selectedDate.getMonth();
49+
};
50+
4751
export const getFirstDateInRange = (date: Date, minDate?: Date, maxDate?: Date): Date => {
4852
if (!isDateInRange(date, minDate, maxDate)) {
4953
if (minDate && date < minDate) {

0 commit comments

Comments
 (0)