Skip to content

Commit

Permalink
fix: disabled left and right button when navigate to the edge month
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanyi-chou committed Nov 28, 2024
1 parent 732dcb5 commit 6cb420a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions components/ui/datetime-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,28 @@ function Calendar({
}, []);

const YEARS = React.useMemo(() => genYears(yearRange), []);
const disableLeftNavigation = () => {
const today = new Date();
const startDate = new Date(today.getFullYear() - yearRange, 0, 1);
if (props.month) {
return (
props.month.getMonth() === startDate.getMonth() &&
props.month.getFullYear() === startDate.getFullYear()
);
}
return false;
};
const disableRightNavigation = () => {
const today = new Date();
const endDate = new Date(today.getFullYear() + yearRange, 11, 31);
if (props.month) {
return (
props.month.getMonth() === endDate.getMonth() &&
props.month.getFullYear() === endDate.getFullYear()
);
}
return false;
};

return (
<DayPicker
Expand All @@ -262,10 +284,12 @@ function Calendar({
button_previous: cn(
buttonVariants({ variant: 'outline' }),
'h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100 absolute left-5 top-5',
disableLeftNavigation() && 'pointer-events-none',
),
button_next: cn(
buttonVariants({ variant: 'outline' }),
'h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100 absolute right-5 top-5',
disableRightNavigation() && 'pointer-events-none',
),
month_grid: 'w-full border-collapse space-y-1',
weekdays: cn('flex', props.showWeekNumber && 'justify-end'),
Expand Down

0 comments on commit 6cb420a

Please sign in to comment.