From c1c9830b5aa6b500d120bb24f39cf1cd51ab61a4 Mon Sep 17 00:00:00 2001
From: Peter Maidens
Date: Fri, 1 Dec 2023 20:00:28 -0700
Subject: [PATCH] fix: next and previous buttons should let you see the min and
max date
---
src/Components/DatePickerProvider.tsx | 36 ++++++++++++++++++++++++---
1 file changed, 32 insertions(+), 4 deletions(-)
diff --git a/src/Components/DatePickerProvider.tsx b/src/Components/DatePickerProvider.tsx
index aa1274c..6dae290 100644
--- a/src/Components/DatePickerProvider.tsx
+++ b/src/Components/DatePickerProvider.tsx
@@ -45,7 +45,6 @@ interface IDatePickerProviderProps {
}
const DatePickerProvider = ({ children, options: customOptions, onChange, show, setShow, selectedDateState }: IDatePickerProviderProps) => {
-
const options = { ...defaultOptions, ...customOptions }
const [view, setView] = useState("days")
const [selectedDate, setSelectedDate] = selectedDateState || useState(options?.defaultDate || new Date())
@@ -54,10 +53,39 @@ const DatePickerProvider = ({ children, options: customOptions, onChange, show,
const selectedYear = selectedDate.getFullYear()
const changeSelectedDate = (action: "prev" | "next" | "date" | "today", date: Date) => {
- if (options?.maxDate && date > options.maxDate) return
- if (options?.minDate && date < options.minDate) return
+ let compareDate = new Date(0)
+ if (action === "today" || action === "date") {
+ compareDate = date
+ } else {
+ switch (view) {
+ case "days":
+ if (action === "prev") compareDate.setFullYear(date.getFullYear(), date.getMonth() + 1, 0) // Last day of the month
+ if (action === "next") compareDate.setFullYear(date.getFullYear(), date.getMonth(), 1) // First day of the month
+ break
+ case "months":
+ if (action === "prev") compareDate.setFullYear(date.getFullYear(), 12, 0) // December 31st
+ if (action === "next") compareDate.setFullYear(date.getFullYear(), 0, 1) // January 1st
+ break
+ case "years":
+ if (action === "prev") compareDate.setFullYear(Math.floor(date.getFullYear() / 10) * 10 + 9, 12, 0) // December 31st, ???9
+ if (action === "next") compareDate.setFullYear(Math.floor(date.getFullYear() / 10) * 10, 0, 1) // January 1st, ???0
+ break
+ case "decades":
+ if (action === "prev") compareDate.setFullYear(Math.floor(date.getFullYear() / 100) * 100 + 99, 12, 0) // December 31st, ??99
+ if (action === "next") compareDate.setFullYear(Math.floor(date.getFullYear() / 100) * 100, 0, 1) // January 1st, ??00
+ break
+ }
+ }
+ if (options?.maxDate && compareDate >= options.maxDate) return
+ if (options?.minDate && compareDate <= options.minDate) return
if (options?.disabledDates && options.disabledDates.indexOf(date) >= 0) return
- setSelectedDate(date)
+ if (options?.maxDate && date > options.maxDate) {
+ setSelectedDate(new Date(options.maxDate.toString()))
+ } else if (options?.minDate && date < options.minDate) {
+ setSelectedDate(new Date(options.minDate.toString()))
+ } else {
+ setSelectedDate(date)
+ }
setShowSelectedDate(true)
if (options?.autoHide && view === "days" && action === "date") setShow(false)
if (onChange) onChange(date)