From e943c1e5a27c07368dd3d88f8045b2dd07da360b Mon Sep 17 00:00:00 2001 From: Giampaolo Bellavite Date: Sun, 23 Jun 2024 14:05:19 -0500 Subject: [PATCH] fix: range includes disabled days --- src/selection/useRange.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/selection/useRange.tsx b/src/selection/useRange.tsx index 46d4ba539..5851be752 100644 --- a/src/selection/useRange.tsx +++ b/src/selection/useRange.tsx @@ -2,7 +2,7 @@ import React from "react"; import { useProps } from "../contexts"; import { DateRange, Modifiers, PropsRange } from "../types"; -import { addToRange } from "../utils"; +import { addToRange, dateMatchModifiers } from "../utils"; import { isDateInRange } from "../utils/isDateInRange"; export type RangeContextValue = { @@ -32,7 +32,7 @@ function useRangeContextValue({ selected, onSelect }: T): RangeContextValue { - const { dateLib } = useProps(); + const { dateLib, disabled } = useProps(); const { differenceInCalendarDays } = dateLib; const [range, setRange] = React.useState(selected); @@ -82,6 +82,18 @@ function useRangeContextValue({ } } + if (newRange?.from && newRange.to) { + let newDate = newRange.from; + while (dateLib.differenceInCalendarDays(newRange.to, newDate) > 0) { + newDate = dateLib.addDays(newDate, 1); + if (disabled && dateMatchModifiers(newDate, disabled)) { + newRange.from = triggerDate; + newRange.to = undefined; + break; + } + } + } + setRange(newRange); onSelect?.(newRange, triggerDate, modifiers, e);