2022-06-15 20:54:31 +00:00
|
|
|
import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/solid";
|
2022-06-27 21:01:46 +00:00
|
|
|
import dayjs, { Dayjs } from "dayjs";
|
2022-06-15 20:54:31 +00:00
|
|
|
import isToday from "dayjs/plugin/isToday";
|
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns";
|
|
|
|
import { weekdayNames } from "@calcom/lib/weekday";
|
2022-06-28 00:04:43 +00:00
|
|
|
import { SkeletonText } from "@calcom/ui/skeleton";
|
2022-06-15 20:54:31 +00:00
|
|
|
|
|
|
|
dayjs.extend(isToday);
|
|
|
|
|
|
|
|
export type DatePickerProps = {
|
|
|
|
/** which day of the week to render the calendar. Usually Sunday (=0) or Monday (=1) - default: Sunday */
|
|
|
|
weekStart?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
|
|
/** Fires whenever a selected date is changed. */
|
2022-06-27 21:01:46 +00:00
|
|
|
onChange: (date: Dayjs) => void;
|
2022-06-15 20:54:31 +00:00
|
|
|
/** Fires when the month is changed. */
|
2022-06-27 21:01:46 +00:00
|
|
|
onMonthChange?: (date: Dayjs) => void;
|
2022-06-15 20:54:31 +00:00
|
|
|
/** which date is currently selected (not tracked from here) */
|
2022-06-27 21:01:46 +00:00
|
|
|
selected?: Dayjs;
|
2022-06-15 20:54:31 +00:00
|
|
|
/** defaults to current date. */
|
2022-06-27 21:01:46 +00:00
|
|
|
minDate?: Dayjs;
|
2022-06-15 20:54:31 +00:00
|
|
|
/** Furthest date selectable in the future, default = UNLIMITED */
|
2022-06-27 21:01:46 +00:00
|
|
|
maxDate?: Dayjs;
|
2022-06-15 20:54:31 +00:00
|
|
|
/** locale, any IETF language tag, e.g. "hu-HU" - defaults to Browser settings */
|
|
|
|
locale: string;
|
|
|
|
/** Defaults to [], which dates are not bookable. Array of valid dates like: ["2022-04-23", "2022-04-24"] */
|
|
|
|
excludedDates?: string[];
|
|
|
|
/** defaults to all, which dates are bookable (inverse of excludedDates) */
|
|
|
|
includedDates?: string[];
|
|
|
|
/** allows adding classes to the container */
|
|
|
|
className?: string;
|
|
|
|
/** Shows a small loading spinner next to the month name */
|
|
|
|
isLoading?: boolean;
|
|
|
|
};
|
|
|
|
|
2022-06-19 15:02:00 +00:00
|
|
|
export const Day = ({
|
2022-06-15 20:54:31 +00:00
|
|
|
date,
|
|
|
|
active,
|
|
|
|
...props
|
2022-06-27 21:01:46 +00:00
|
|
|
}: JSX.IntrinsicElements["button"] & { active: boolean; date: Dayjs }) => {
|
2022-06-15 20:54:31 +00:00
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className={classNames(
|
2022-06-27 21:01:46 +00:00
|
|
|
"hover:border-brand disabled:text-bookinglighter absolute top-0 left-0 right-0 bottom-0 mx-auto w-full rounded-sm border border-transparent text-center font-medium disabled:cursor-default disabled:border-transparent disabled:font-light dark:hover:border-white disabled:dark:border-transparent",
|
2022-06-15 20:54:31 +00:00
|
|
|
active
|
|
|
|
? "bg-brand text-brandcontrast dark:bg-darkmodebrand dark:text-darkmodebrandcontrast"
|
|
|
|
: !props.disabled
|
|
|
|
? " bg-gray-100 dark:bg-gray-600 dark:text-white"
|
|
|
|
: ""
|
|
|
|
)}
|
|
|
|
data-testid="day"
|
|
|
|
data-disabled={props.disabled}
|
|
|
|
{...props}>
|
2022-06-27 21:01:46 +00:00
|
|
|
{date.date()}
|
|
|
|
{date.isToday() && <span className="absolute left-0 bottom-0 mx-auto -mb-px w-full text-4xl">.</span>}
|
2022-06-15 20:54:31 +00:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Days = ({
|
2022-06-27 21:01:46 +00:00
|
|
|
// minDate,
|
2022-06-15 20:54:31 +00:00
|
|
|
excludedDates = [],
|
2022-06-20 14:11:18 +00:00
|
|
|
includedDates,
|
2022-06-15 20:54:31 +00:00
|
|
|
browsingDate,
|
|
|
|
weekStart,
|
2022-06-19 15:02:00 +00:00
|
|
|
DayComponent = Day,
|
2022-06-15 20:54:31 +00:00
|
|
|
selected,
|
|
|
|
...props
|
|
|
|
}: Omit<DatePickerProps, "locale" | "className" | "weekStart"> & {
|
2022-06-19 15:02:00 +00:00
|
|
|
DayComponent?: React.FC<React.ComponentProps<typeof Day>>;
|
2022-06-27 21:01:46 +00:00
|
|
|
browsingDate: Dayjs;
|
2022-06-15 20:54:31 +00:00
|
|
|
weekStart: number;
|
|
|
|
}) => {
|
|
|
|
// Create placeholder elements for empty days in first week
|
2022-06-27 21:01:46 +00:00
|
|
|
const weekdayOfFirst = browsingDate.day();
|
2022-06-15 20:54:31 +00:00
|
|
|
|
2022-06-27 21:01:46 +00:00
|
|
|
const days: (Dayjs | null)[] = Array((weekdayOfFirst - weekStart + 7) % 7).fill(null);
|
2022-06-15 20:54:31 +00:00
|
|
|
for (let day = 1, dayCount = daysInMonth(browsingDate); day <= dayCount; day++) {
|
2022-06-27 21:01:46 +00:00
|
|
|
const date = browsingDate.set("date", day);
|
2022-06-15 20:54:31 +00:00
|
|
|
days.push(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-27 21:01:46 +00:00
|
|
|
{days.map((day, idx) => (
|
|
|
|
<div key={day === null ? `e-${idx}` : `day-${day.format()}`} className="relative w-full pt-[100%]">
|
|
|
|
{day === null ? (
|
|
|
|
<div key={`e-${idx}`} />
|
|
|
|
) : props.isLoading ? (
|
2022-06-28 00:04:43 +00:00
|
|
|
<button
|
|
|
|
className="absolute top-0 left-0 right-0 bottom-0 mx-auto flex w-full items-center justify-center rounded-sm border-transparent bg-gray-50 text-center text-gray-400 opacity-50 dark:bg-gray-900 dark:text-gray-400"
|
|
|
|
key={`e-${idx}`}
|
|
|
|
disabled>
|
|
|
|
<SkeletonText width="5" height="4" />
|
|
|
|
</button>
|
2022-06-27 21:01:46 +00:00
|
|
|
) : (
|
|
|
|
<DayComponent
|
|
|
|
date={day}
|
|
|
|
onClick={() => {
|
|
|
|
props.onChange(day);
|
|
|
|
window.scrollTo({
|
|
|
|
top: 360,
|
|
|
|
behavior: "smooth",
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
disabled={
|
|
|
|
(includedDates && !includedDates.includes(yyyymmdd(day))) ||
|
|
|
|
excludedDates.includes(yyyymmdd(day))
|
|
|
|
}
|
|
|
|
active={selected ? yyyymmdd(selected) === yyyymmdd(day) : false}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
))}
|
2022-06-15 20:54:31 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DatePicker = ({
|
|
|
|
weekStart = 0,
|
|
|
|
className,
|
|
|
|
locale,
|
|
|
|
selected,
|
|
|
|
onMonthChange,
|
|
|
|
...passThroughProps
|
2022-06-19 15:02:00 +00:00
|
|
|
}: DatePickerProps & Partial<React.ComponentProps<typeof Days>>) => {
|
2022-06-27 21:01:46 +00:00
|
|
|
const browsingDate = passThroughProps.browsingDate || dayjs().startOf("month");
|
2022-06-15 20:54:31 +00:00
|
|
|
|
|
|
|
const changeMonth = (newMonth: number) => {
|
|
|
|
if (onMonthChange) {
|
2022-06-27 21:01:46 +00:00
|
|
|
onMonthChange(browsingDate.add(newMonth, "month"));
|
2022-06-15 20:54:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={className}>
|
|
|
|
<div className="mb-4 flex justify-between text-xl font-light">
|
|
|
|
<span className="w-1/2 dark:text-white">
|
2022-06-27 21:01:46 +00:00
|
|
|
{browsingDate ? (
|
|
|
|
<>
|
|
|
|
<strong className="text-bookingdarker dark:text-white">
|
|
|
|
{browsingDate.toDate().toLocaleString(locale, {
|
|
|
|
month: "long",
|
|
|
|
})}
|
|
|
|
</strong>{" "}
|
|
|
|
<span className="text-bookinglight">{browsingDate.format("YYYY")}</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<SkeletonText width="24" height="8" />
|
|
|
|
)}
|
2022-06-15 20:54:31 +00:00
|
|
|
</span>
|
2022-06-18 15:03:24 +00:00
|
|
|
<div className="text-black dark:text-white">
|
2022-06-15 20:54:31 +00:00
|
|
|
<button
|
2022-06-27 21:01:46 +00:00
|
|
|
onClick={() => changeMonth(-1)}
|
2022-06-15 20:54:31 +00:00
|
|
|
className={classNames(
|
2022-06-18 15:03:24 +00:00
|
|
|
"group p-1 opacity-50 hover:opacity-100 ltr:mr-2 rtl:ml-2",
|
2022-06-27 21:01:46 +00:00
|
|
|
!browsingDate.isAfter(dayjs()) && "disabled:text-bookinglighter hover:opacity-50"
|
2022-06-15 20:54:31 +00:00
|
|
|
)}
|
2022-06-27 21:01:46 +00:00
|
|
|
disabled={!browsingDate.isAfter(dayjs())}
|
2022-06-15 20:54:31 +00:00
|
|
|
data-testid="decrementMonth">
|
|
|
|
<ChevronLeftIcon className="h-5 w-5" />
|
|
|
|
</button>
|
2022-06-18 15:03:24 +00:00
|
|
|
<button
|
|
|
|
className="group p-1 opacity-50 hover:opacity-100"
|
2022-06-27 21:01:46 +00:00
|
|
|
onClick={() => changeMonth(+1)}
|
2022-06-18 15:03:24 +00:00
|
|
|
data-testid="incrementMonth">
|
|
|
|
<ChevronRightIcon className="h-5 w-5" />
|
2022-06-15 20:54:31 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-27 17:23:20 +00:00
|
|
|
<div className="border-bookinglightest mb-2 grid grid-cols-7 gap-4 border-t border-b text-center dark:border-gray-800 md:mb-0 md:border-0">
|
2022-06-15 20:54:31 +00:00
|
|
|
{weekdayNames(locale, weekStart, "short").map((weekDay) => (
|
|
|
|
<div key={weekDay} className="text-bookinglight my-4 text-xs uppercase tracking-widest">
|
|
|
|
{weekDay}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
<div className="grid grid-cols-7 gap-2 text-center">
|
2022-06-27 21:01:46 +00:00
|
|
|
<Days weekStart={weekStart} selected={selected} {...passThroughProps} browsingDate={browsingDate} />
|
2022-06-15 20:54:31 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DatePicker;
|