From 049b6e7d1d553de477cca1de7bf53069b33ea9c6 Mon Sep 17 00:00:00 2001 From: Manoj Vaibhav Date: Fri, 10 Nov 2023 17:36:40 +0530 Subject: [PATCH] Revert "X2-7602 reworked range datepicker" (#284) Revert "X2-7602 reworked range datepicker (#280)" This reverts commit 3a8448da0b7612aa6ab9349eb0d8d42e6cfb3884. --- src/components/DatePicker/DatePicker.jsx | 148 ++++++------- src/components/DatePicker/RangeDatePicker.jsx | 196 ------------------ .../DatePicker/UpcomingDatePicker.jsx | 47 ----- 3 files changed, 67 insertions(+), 324 deletions(-) delete mode 100644 src/components/DatePicker/RangeDatePicker.jsx delete mode 100644 src/components/DatePicker/UpcomingDatePicker.jsx diff --git a/src/components/DatePicker/DatePicker.jsx b/src/components/DatePicker/DatePicker.jsx index be4a5497d..dc8af42cf 100644 --- a/src/components/DatePicker/DatePicker.jsx +++ b/src/components/DatePicker/DatePicker.jsx @@ -9,10 +9,8 @@ import { isArray, isFunction } from "lodash"; import { Tooltip } from "../.."; import { Day } from "./Day"; import { MonthYearSelector } from "./MonthYearSelector"; -import { RelativeDateRange } from "./RelativeDateRange"; -import RangeDatePicker from "./RangeDatePicker"; -import { UpcomingDatePicker } from "./UpcomingDatePicker"; import { NavbarElement } from "./NavbarElement"; +import { RelativeDateRange } from "./RelativeDateRange"; const variants = { single: "single", @@ -42,8 +40,6 @@ export const DatePicker = ({ }) => { const initialValue = variant === variants.single ? value : value.from; const [currentMonth, setCurrentMonth] = useState(initialValue); - const [startMonth, setStartMonth] = useState(value?.from); - const [endMonth, setEndMonth] = useState(value?.to); const [rangeName, setRangeName] = useState(""); const isRangeVariant = variant === variants.range; @@ -91,21 +87,8 @@ export const DatePicker = ({ } }; - const isDisabled = (date) => { - if (isArray(disabledDays)) { - return disabledDays.some((_date) => dayjs(_date).isSame(date, "day")); - } - - if (isFunction(disabledDays)) { - return disabledDays(date); - } - - return disabledDays(date); - }; - const handleRelativeRangeChanged = (rangeName, range) => { setCurrentMonth(range.from); - setStartMonth(range.from); onChange(range, modifiers, null); }; @@ -114,27 +97,25 @@ export const DatePicker = ({ onMonthChange?.(m); }; - const handleStartMonthChange = (m) => { - setStartMonth(m); - onMonthChange?.(m); - }; - - const handleEndMonthChange = (m) => { - setEndMonth(m); - onMonthChange?.(m); - }; - - const CaptionElement = shouldShowYearPicker + const captionElement = shouldShowYearPicker ? ({ date }) => : undefined; - // Comparing `from` and `to` dates hides a weird CSS style when you select the same date twice in a date range. - const isDateRangeStyle = isRangeVariant && value.from?.getTime() !== value.to?.getTime(); + const isDisabled = (date) => { + if (isArray(disabledDays)) { + return disabledDays.some((_date) => dayjs(_date).isSame(date, "day")); + } + + if (isFunction(disabledDays)) { + return disabledDays(date); + } + + return disabledDays(date); + }; const renderDay = (date) => { const tooltipContent = getTooltip?.(date); const disabled = isDisabled(date); - return tooltipContent ? (
{upcomingDates ? ( - +
+

Upcoming

+ {upcomingDates?.length > 0 ? ( +
+ {upcomingDates?.map((date) => { + const isSameDay = dayjs(date).isSame(dayjs(value), "day"); + const key = dayjs(date).format(); + return ( +
{ + handleDayClick(date, {}, event); + handleMonthChange(date); + }} + > + {dayjs(date).format("ddd DD MMMM")} +
+ ); + })} +
+ ) : ( +
+ There is no future availability for this product. +
+ )} +
) : null} - {isRangeVariant ? ( - - ) : ( - - )} +
{components.Footer ? : null} - {isDateRangeStyle && shouldShowRelativeRanges && ( + {useDateRangeStyle && shouldShowRelativeRanges && (
{ - const [initialEndDate] = useState(endMonth); - const isTheSameMonth = - dayjs(startMonth).isSame(dayjs(initialEndDate), "month") && - dayjs(endMonth).isSame(dayjs(initialEndDate), "month"); - - const isStartDateIsTheSameMonth = dayjs(value?.from).isSame(dayjs(value?.to), "month"); - - const CaptionStartElement = shouldShowYearPicker - ? ({ date }) => - : undefined; - - const CaptionEndElement = shouldShowYearPicker - ? ({ date }) => - : undefined; - - const isDisabledStartDays = (date) => { - if (isFunction(disabledDays)) { - return disabledDays(date) || dayjs(date).isAfter(value.to, "day"); - } - - if (isArray(disabledDays)) { - return ( - disabledDays.some((_date) => dayjs(_date).isSame(date, "day")) || dayjs(date).isAfter(value.to, "day") - ); - } - - return dayjs(date).isAfter(value.to, "day"); - }; - - const isDisabledEndDays = (date) => { - if (isStartDateIsTheSameMonth) { - return true; - } - - if (isFunction(disabledDays)) { - return disabledDays(date) || dayjs(date).isBefore(value.from, "day"); - } - - if (isArray(disabledDays)) { - return ( - disabledDays.some((_date) => dayjs(_date).isSame(date, "day")) || - dayjs(date).isBefore(value.from, "day") - ); - } - - return dayjs(date).isBefore(value.from, "day"); - }; - - const renderStartDay = (date) => { - const tooltipContent = getTooltip?.(date); - const disabled = isDisabledStartDays(date); - - return tooltipContent ? ( - - - - ) : ( - - ); - }; - - const renderEndDay = (date) => { - const tooltipContent = getTooltip?.(date); - const disabled = isDisabledEndDays(date); - - return tooltipContent ? ( - - - - ) : ( - - ); - }; - - useEffect(() => { - if (dayjs(value?.from).isSame(dayjs(value?.to), "month")) { - handleStartMonthChange(dayjs(value.from).toDate()); - } - }, [value, handleStartMonthChange]); - - return ( -
- handleDayClick(day, options, event, true)} - onMonthChange={handleStartMonthChange} - onTodayButtonClick={handleTodayClick} - {...rest} - /> - handleDayClick(day, options, event, false)} - onMonthChange={handleEndMonthChange} - onTodayButtonClick={handleTodayClick} - {...rest} - /> -
- ); -}; - -RangeDatePicker.propTypes = { - getTooltip: PropTypes.func, - value: PropTypes.object, - getDayContent: PropTypes.func, - isDisabled: PropTypes.func, - isDateRangeStyle: PropTypes.bool, - startMonth: PropTypes.instanceOf(Date), - endMonth: PropTypes.instanceOf(Date), - modifiers: PropTypes.object, - disabledStartDays: PropTypes.func, - disabledEndDays: PropTypes.func, - handleDayClick: PropTypes.func, - handleStartMonthChange: PropTypes.func, - handleEndMonthChange: PropTypes.func, - handleTodayClick: PropTypes.func, -}; - -export default RangeDatePicker; diff --git a/src/components/DatePicker/UpcomingDatePicker.jsx b/src/components/DatePicker/UpcomingDatePicker.jsx deleted file mode 100644 index a6b509731..000000000 --- a/src/components/DatePicker/UpcomingDatePicker.jsx +++ /dev/null @@ -1,47 +0,0 @@ -import React from "react"; -import dayjs from "dayjs"; -import clsx from "clsx"; -import PropTypes from "prop-types"; - -export const UpcomingDatePicker = ({ value, upcomingDates, onChange, onMonthChange }) => { - return ( -
-

Upcoming

- {upcomingDates?.length > 0 ? ( -
- {upcomingDates?.map((date) => { - const isSameDay = dayjs(date).isSame(dayjs(value), "day"); - const key = dayjs(date).format(); - return ( -
{ - onChange(date, {}, event); - onMonthChange(date); - }} - > - {dayjs(date).format("ddd DD MMMM")} -
- ); - })} -
- ) : ( -
- There is no future availability for this product. -
- )} -
- ); -}; - -UpcomingDatePicker.propTypes = { - value: PropTypes.objectOf(Date), - upcomingDates: PropTypes.arrayOf(Date).isRequired, - onChange: PropTypes.func.isRequired, - onMonthChange: PropTypes.func.isRequired, -};