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

X2-7602 reworked range datepicker #280

Merged
merged 20 commits into from
Nov 8, 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: 81 additions & 67 deletions src/components/DatePicker/DatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { isArray, isFunction } from "lodash";
import { Tooltip } from "../..";
import { Day } from "./Day";
import { MonthYearSelector } from "./MonthYearSelector";
import { NavbarElement } from "./NavbarElement";
import { RelativeDateRange } from "./RelativeDateRange";
import RangeDatePicker from "./RangeDatePicker";
import { UpcomingDatePicker } from "./UpcomingDatePicker";
import { NavbarElement } from "./NavbarElement";

const variants = {
single: "single",
Expand Down Expand Up @@ -40,6 +42,8 @@ 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 @@ -87,8 +91,21 @@ 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 @@ -97,25 +114,27 @@ export const DatePicker = ({
onMonthChange?.(m);
};

const captionElement = shouldShowYearPicker
? ({ date }) => <MonthYearSelector date={date} currentMonth={currentMonth} onChange={handleMonthChange} />
: undefined;
const handleStartMonthChange = (m) => {
setStartMonth(m);
onMonthChange?.(m);
};

const isDisabled = (date) => {
if (isArray(disabledDays)) {
return disabledDays.some((_date) => dayjs(_date).isSame(date, "day"));
}
const handleEndMonthChange = (m) => {
setEndMonth(m);
onMonthChange?.(m);
};

if (isFunction(disabledDays)) {
return disabledDays(date);
}
const CaptionElement = shouldShowYearPicker
? ({ date }) => <MonthYearSelector date={date} currentMonth={currentMonth} onChange={handleMonthChange} />
: undefined;

return disabledDays(date);
};
// 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 renderDay = (date) => {
const tooltipContent = getTooltip?.(date);
const disabled = isDisabled(date);

return tooltipContent ? (
<Tooltip placement="top" content={tooltipContent}>
<Day
Expand Down Expand Up @@ -148,66 +167,59 @@ export const DatePicker = ({
<>
<div className="flex">
{upcomingDates ? (
<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>
<UpcomingDatePicker
upcomingDates={upcomingDates}
value={value}
onChange={handleDayClick}
onMonthChange={handleMonthChange}
/>
) : null}

<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}
/>
{isRangeVariant ? (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract some code around this to a separate component. This component is getting really big. There are three different invocations of the the CustomDayPicker based on different arguments

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also feel free to extract it into a new file to make things cleaner if needed.

<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}
/>
)}
</div>

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

{useDateRangeStyle && shouldShowRelativeRanges && (
{isDateRangeStyle && shouldShowRelativeRanges && (
<div className="ml-auto w-6/12 pl-5 pr-10 pb-5">
<RelativeDateRange
value={rangeName}
Expand All @@ -229,6 +241,8 @@ 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