From 7261b67b23248c4d567a9529f831f521ab9096a6 Mon Sep 17 00:00:00 2001 From: DareenMohamed <103316419+Dareenfadel@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:48:05 +0200 Subject: [PATCH] "Fix issue #7361: Block users from entering date out of range" (#7366) Co-authored-by: Dareenfadel --- src/Components/Common/DateInputV2.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Components/Common/DateInputV2.tsx b/src/Components/Common/DateInputV2.tsx index da974fbe0e9..bce5f24b11c 100644 --- a/src/Components/Common/DateInputV2.tsx +++ b/src/Components/Common/DateInputV2.tsx @@ -159,6 +159,22 @@ const DateInputV2: React.FC = ({ return true; }; + const isDateWithinLimits = (parsedDate: dayjs.Dayjs): boolean => { + if (parsedDate?.isValid()) { + if ( + (max && parsedDate.toDate() > max) || + (min && parsedDate.toDate() < min) + ) { + Notification.Error({ + msg: outOfLimitsErrorMessage ?? "Cannot select date out of range", + }); + return false; + } + return true; + } + return false; + }; + const isSelectedMonth = (month: number) => month === datePickerHeaderDate.getMonth(); @@ -261,7 +277,7 @@ const DateInputV2: React.FC = ({ onChange={(e) => { setDisplayValue(e.target.value.replaceAll("/", "")); const value = dayjs(e.target.value, "DD/MM/YYYY", true); - if (value.isValid()) { + if (isDateWithinLimits(value)) { onChange(value.toDate()); close(); setIsOpen?.(false);