-
Notifications
You must be signed in to change notification settings - Fork 4
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
Bug: Year selection is not working when prop mode="range" is passed #10
Comments
I have look into the code and this is because of this code shadcn-date-picker/src/components/ui/calendar.tsx Lines 491 to 499 in 504f735
In range mode I tried something but the I'm working on a workaround by forcing TS to override the type. I will come back here once I have a working solution. P.S. it is the same issue as #9 |
Here is a workaround for #9 and #10 that works. I don't really like the "hack" but I believe the issue come from the returned type from function YearGrid({
className,
startMonth,
endMonth,
displayYears,
navView,
setNavView,
...props
}: React.HTMLAttributes<HTMLDivElement> & {
className?: string;
startMonth?: Date;
endMonth?: Date;
displayYears: { from: number; to: number };
navView: NavView;
setNavView: React.Dispatch<React.SetStateAction<NavView>>;
}) {
const { goToMonth, selected: selectedRange } = useDayPicker<{
mode: "range";
}>();
const { selected: selectedSingle } = useDayPicker<{
mode: "single";
}>();
const { selected: selectedMultiple } = useDayPicker<{
mode: "multiple";
}>();
const getSelectedMonth = React.useCallback((): number => {
if (selectedRange && selectedRange.from instanceof Date) {
return selectedRange.from.getMonth();
}
if (selectedSingle && selectedSingle instanceof Date) {
return selectedSingle.getMonth();
}
if (
selectedMultiple &&
selectedMultiple.length > 0 &&
selectedMultiple[0] instanceof Date
) {
return selectedMultiple[0].getMonth();
}
return new Date().getMonth();
}, [selectedRange, selectedSingle, selectedMultiple]);
return (
<div className={cn("grid grid-cols-4 gap-y-2", className)} {...props}>
{Array.from(
{ length: displayYears.to - displayYears.from + 1 },
(_, i) => {
const isBefore =
differenceInCalendarDays(
new Date(displayYears.from + i, 11, 31),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
startMonth!,
) < 0;
const isAfter =
differenceInCalendarDays(
new Date(displayYears.from + i, 0, 0),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
endMonth!,
) > 0;
const isDisabled = isBefore || isAfter;
return (
<Button
className={cn(
"text-foreground h-7 w-full text-sm font-normal",
displayYears.from + i === new Date().getFullYear() &&
"bg-accent text-accent-foreground font-medium",
)}
disabled={navView === "years" ? isDisabled : undefined}
key={i}
variant="ghost"
onClick={() => {
setNavView("days");
goToMonth(new Date(displayYears.from + i, getSelectedMonth()));
}}
>
{displayYears.from + i}
</Button>
);
},
)}
</div>
);
} |
My workaround setNavView("days");
if (mode === "range") {
const selectedRange = selected as unknown as { from: Date; to: Date };
// if mode is range, use `selectedRange.from`
goToMonth(
new Date(displayYears.from + i, selectedRange.from?.getMonth() ?? new Date().getMonth())
);
return;
}
goToMonth(new Date(displayYears.from + i, (selected as Date | undefined)?.getMonth() ?? 0)); |
I have started to use the
DatePicker
, however when I use the range mode, the year selection does not work as expected, the bug can be easily recreated on a demo pageScreen.Recording.2025-02-12.at.14.55.23.mov
The text was updated successfully, but these errors were encountered: