|
| 1 | +import { MonthType, useMonth } from "@datepicker-react/hooks"; |
| 2 | +import { useDateFormatter } from "@react-aria/i18n"; |
| 3 | +import { useOverlay, useOverlayPosition } from "@react-aria/overlays"; |
| 4 | +import { mergeProps } from "@react-aria/utils"; |
| 5 | +import { FunctionComponent, RefObject, useRef } from "react"; |
| 6 | +import { IconButtonProps } from "../IconButton/createIconButton"; |
| 7 | +import { Box, Stack, Tiles } from "../internal"; |
| 8 | +import { MenuProps } from "../Menu/createMenu"; |
| 9 | +import { Label } from "../Typography/Label/Label"; |
| 10 | +import { Children } from "../util/Children"; |
| 11 | +import { createPortal } from "../util/createPortal"; |
| 12 | +import { unsafeLocalizedString } from "../util/LocalizedString"; |
| 13 | +import { createCalendarHeader } from "./CalendarHeader"; |
| 14 | +import { DateFieldConfig } from "./Config"; |
| 15 | +import { calendar, weekDay } from "./DateField.css"; |
| 16 | +import { createDay } from "./Day"; |
| 17 | + |
| 18 | +export type CommonCalendarProps = { |
| 19 | + inputRef: RefObject<HTMLInputElement>; |
| 20 | + focusedDate: Date | null; |
| 21 | + onDateFocus(date: Date): void; |
| 22 | + onDateSelect(date: Date): void; |
| 23 | + onDateHover(date: Date): void; |
| 24 | + isStartDate(date: Date): boolean; |
| 25 | + isEndDate(date: Date): boolean; |
| 26 | + isDateFocused(date: Date): boolean; |
| 27 | + isDateSelected(date: Date): boolean; |
| 28 | + isDateHovered(date: Date): boolean; |
| 29 | + isDateBlocked(date: Date): boolean; |
| 30 | + isFirstOrLastSelectedDate(date: Date): boolean; |
| 31 | +}; |
| 32 | + |
| 33 | +type Props = CommonCalendarProps & { |
| 34 | + type: "single" | "range"; |
| 35 | + activeDate: MonthType; |
| 36 | + goToPreviousMonth: () => void; |
| 37 | + goToNextMonth: () => void; |
| 38 | + selectActiveDate: (date: Date) => void; |
| 39 | + onClose: () => void; |
| 40 | + shortcuts?: Children; |
| 41 | +}; |
| 42 | + |
| 43 | +function boxShadowFromElevation(config: "none" | "small" | "medium" | "large") { |
| 44 | + switch (config) { |
| 45 | + case "none": |
| 46 | + return "none"; |
| 47 | + case "small": |
| 48 | + return "elevationSmall"; |
| 49 | + case "medium": |
| 50 | + return "elevationMedium"; |
| 51 | + case "large": |
| 52 | + return "elevationLarge"; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export function createCalendar( |
| 57 | + config: DateFieldConfig, |
| 58 | + { |
| 59 | + Menu, |
| 60 | + IconButton, |
| 61 | + }: { |
| 62 | + Menu: FunctionComponent<MenuProps>; |
| 63 | + IconButton: FunctionComponent<IconButtonProps>; |
| 64 | + } |
| 65 | +) { |
| 66 | + const CalendarHeader = createCalendarHeader(config, { Menu, IconButton }); |
| 67 | + const Day = createDay(config); |
| 68 | + |
| 69 | + return function Calendar(props: Props) { |
| 70 | + const weekdayFormatter = useDateFormatter({ |
| 71 | + weekday: "narrow", |
| 72 | + }); |
| 73 | + const overlayRef = useRef(null); |
| 74 | + |
| 75 | + const { days, weekdayLabels } = useMonth({ |
| 76 | + year: props.activeDate.year, |
| 77 | + month: props.activeDate.month, |
| 78 | + weekdayLabelFormat: (date) => weekdayFormatter.format(date), |
| 79 | + }); |
| 80 | + |
| 81 | + const { overlayProps } = useOverlay( |
| 82 | + { |
| 83 | + isOpen: true, |
| 84 | + isDismissable: true, |
| 85 | + onClose: props.onClose, |
| 86 | + }, |
| 87 | + overlayRef |
| 88 | + ); |
| 89 | + const { overlayProps: positionProps } = useOverlayPosition({ |
| 90 | + targetRef: props.inputRef, |
| 91 | + overlayRef, |
| 92 | + placement: "bottom start", |
| 93 | + offset: 35, |
| 94 | + isOpen: true, |
| 95 | + shouldFlip: true, |
| 96 | + }); |
| 97 | + |
| 98 | + return createPortal( |
| 99 | + <Box |
| 100 | + className={calendar} |
| 101 | + borderRadius={config.radius} |
| 102 | + padding={config.padding} |
| 103 | + boxShadow={boxShadowFromElevation(config.elevation)} |
| 104 | + {...mergeProps(overlayProps, positionProps)} |
| 105 | + color={undefined} |
| 106 | + ref={overlayRef} |
| 107 | + > |
| 108 | + <Stack space={16} align="center"> |
| 109 | + <CalendarHeader {...props} /> |
| 110 | + <Tiles columns={7} space={0}> |
| 111 | + {weekdayLabels.map((d, index) => ( |
| 112 | + <Box |
| 113 | + className={weekDay} |
| 114 | + width={config.dayWidth} |
| 115 | + height={config.dayHeight} |
| 116 | + key={`${d}-${index}`} |
| 117 | + > |
| 118 | + <Label size={config.dayOfWeekLabelSize}>{unsafeLocalizedString(d)}</Label> |
| 119 | + </Box> |
| 120 | + ))} |
| 121 | + {days.map((day, index) => { |
| 122 | + if (typeof day === "object") { |
| 123 | + return <Day key={day.dayLabel} {...props} date={day.date} label={day.dayLabel} />; |
| 124 | + } else { |
| 125 | + return <Box key={`empty-${index}`} width={40} height={40} />; |
| 126 | + } |
| 127 | + })} |
| 128 | + </Tiles> |
| 129 | + {props.shortcuts} |
| 130 | + </Stack> |
| 131 | + </Box> |
| 132 | + ); |
| 133 | + }; |
| 134 | +} |
0 commit comments