Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "X2-7602 reworked range datepicker" #284

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 67 additions & 81 deletions src/components/DatePicker/DatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
};

Expand All @@ -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 }) => <MonthYearSelector date={date} currentMonth={currentMonth} onChange={handleMonthChange} />
: 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 ? (
<Tooltip placement="top" content={tooltipContent}>
<Day
Expand Down Expand Up @@ -167,59 +148,66 @@ export const DatePicker = ({
<>
<div className="flex">
{upcomingDates ? (
<UpcomingDatePicker
upcomingDates={upcomingDates}
value={value}
onChange={handleDayClick}
onMonthChange={handleMonthChange}
/>
<div className="rounded-l-lg border-r border-gray pt-8">
<p className="mb-2 px-6 text-lg font-bold">Upcoming</p>
{upcomingDates?.length > 0 ? (
<div className="mt-5">
{upcomingDates?.map((date) => {
const isSameDay = dayjs(date).isSame(dayjs(value), "day");
const key = dayjs(date).format();
return (
<div
key={key}
value
className={clsx(
"mx-6 mt-3 flex min-w-40 cursor-pointer items-center justify-center",
"rounded border border-gray py-3 hover:border-blue hover:bg-blue hover:text-white",
{ "border-blue bg-blue text-white": isSameDay },
)}
onClick={(event) => {
handleDayClick(date, {}, event);
handleMonthChange(date);
}}
>
{dayjs(date).format("ddd DD MMMM")}
</div>
);
})}
</div>
) : (
<div className="mx-6 mt-7 max-w-40 items-center justify-center rounded bg-yellow-lighter p-3">
There is no future availability for this product.
</div>
)}
</div>
) : null}

{isRangeVariant ? (
<RangeDatePicker
isDateRangeStyle={useDateRangeStyle}
shouldShowYearPicker={shouldShowYearPicker}
startMonth={startMonth}
endMonth={endMonth}
modifiers={{ ...modifiers, ...rangeModifier }}
getTooltip={getTooltip}
disabledDays={disabledDays}
getDayContent={getDayContent}
value={value}
handleDayClick={handleDayClick}
handleStartMonthChange={handleStartMonthChange}
handleEndMonthChange={handleEndMonthChange}
handleTodayClick={handleTodayClick}
selectedDays={selectedDays}
{...rest}
/>
) : (
<DayPicker
className={clsx(
"ui-date-picker rounded-lg pt-3",
useDateRangeStyle ? "date-range-picker" : null,
getDayContent ? "has-custom-content" : null,
modifiers.waitlist ? "has-custom-content" : null,
)}
todayButton="Today"
selectedDays={selectedDays}
month={currentMonth}
modifiers={{ ...modifiers, ...rangeModifier }}
disabledDays={disabledDays}
captionElement={CaptionElement}
renderDay={renderDay}
navbarElement={NavbarElement}
onDayClick={handleDayClick}
onMonthChange={handleMonthChange}
onTodayButtonClick={handleTodayClick}
{...rest}
/>
)}
<DayPicker
className={clsx(
"ui-date-picker rounded-lg pt-3",
useDateRangeStyle ? "date-range-picker" : null,
getDayContent ? "has-custom-content" : null,
modifiers.waitlist ? "has-custom-content" : null,
)}
todayButton={variant === "single" ? "Today" : undefined}
selectedDays={selectedDays}
month={currentMonth}
modifiers={{ ...modifiers, ...rangeModifier }}
numberOfMonths={isRangeVariant ? 2 : 1}
disabledDays={disabledDays}
captionElement={captionElement}
renderDay={renderDay}
navbarElement={NavbarElement}
onDayClick={handleDayClick}
onMonthChange={handleMonthChange}
onTodayButtonClick={handleTodayClick}
{...rest}
/>
</div>

{components.Footer ? <components.Footer /> : null}

{isDateRangeStyle && shouldShowRelativeRanges && (
{useDateRangeStyle && shouldShowRelativeRanges && (
<div className="ml-auto w-6/12 pl-5 pr-10 pb-5">
<RelativeDateRange
value={rangeName}
Expand All @@ -241,8 +229,6 @@ DatePicker.propTypes = {
onMonthChange: PropTypes.func,
disabledDays: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.func]),
shouldShowYearPicker: PropTypes.bool,
isDateRangeStyle: PropTypes.bool,
isRangeVariant: PropTypes.bool,
getDayContent: PropTypes.func,
modifiers: PropTypes.object,
ranges: PropTypes.arrayOf(PropTypes.oneOf(["day", "week", "month", "quarter", "year"])),
Expand Down
Loading
Loading