|
| 1 | +import Box from "@mui/material/Box" |
| 2 | +import { styled } from "@mui/material/styles" |
| 3 | +import { ComponentProps, useRef, useState } from "react" |
| 4 | +import "react-date-range/dist/styles.css" |
| 5 | +import "react-date-range/dist/theme/default.css" |
| 6 | +import Button from "@mui/material/Button" |
| 7 | +import ArrowRightAltOutlined from "@mui/icons-material/ArrowRightAltOutlined" |
| 8 | +import Popover from "@mui/material/Popover" |
| 9 | +import { DateRangePicker } from "react-date-range" |
| 10 | +import { format } from "date-fns" |
| 11 | + |
| 12 | +export type DateRangeValue = { |
| 13 | + startDate: Date |
| 14 | + endDate: Date |
| 15 | +} |
| 16 | + |
| 17 | +type RangesState = NonNullable<ComponentProps<typeof DateRangePicker>["ranges"]> |
| 18 | + |
| 19 | +export const DateRange = ({ |
| 20 | + value, |
| 21 | + onChange, |
| 22 | +}: { |
| 23 | + value: DateRangeValue |
| 24 | + onChange: (value: DateRangeValue) => void |
| 25 | +}) => { |
| 26 | + const selectionStatusRef = useRef<"idle" | "selecting">("idle") |
| 27 | + const anchorRef = useRef<HTMLButtonElement>(null) |
| 28 | + const [isOpen, setIsOpen] = useState(false) |
| 29 | + const [ranges, setRanges] = useState<RangesState>([ |
| 30 | + { |
| 31 | + ...value, |
| 32 | + key: "selection", |
| 33 | + }, |
| 34 | + ]) |
| 35 | + const currentRange = { |
| 36 | + startDate: ranges[0].startDate as Date, |
| 37 | + endDate: ranges[0].endDate as Date, |
| 38 | + } |
| 39 | + const handleClose = () => { |
| 40 | + onChange({ |
| 41 | + startDate: currentRange.startDate, |
| 42 | + endDate: currentRange.endDate, |
| 43 | + }) |
| 44 | + setIsOpen(false) |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <> |
| 49 | + <Button ref={anchorRef} onClick={() => setIsOpen(true)}> |
| 50 | + <span>{format(currentRange.startDate, "MMM d, Y")}</span> |
| 51 | + <ArrowRightAltOutlined sx={{ width: 16, height: 16, mx: 1 }} /> |
| 52 | + <span>{format(currentRange.endDate, "MMM d, Y")}</span> |
| 53 | + </Button> |
| 54 | + <Popover |
| 55 | + anchorEl={anchorRef.current} |
| 56 | + open={isOpen} |
| 57 | + onClose={handleClose} |
| 58 | + anchorOrigin={{ |
| 59 | + vertical: "bottom", |
| 60 | + horizontal: "left", |
| 61 | + }} |
| 62 | + sx={{ |
| 63 | + "& .MuiPaper-root": { |
| 64 | + marginTop: 1, |
| 65 | + }, |
| 66 | + }} |
| 67 | + > |
| 68 | + <DateRangePickerWrapper |
| 69 | + component={DateRangePicker} |
| 70 | + onChange={(item) => { |
| 71 | + const range = item.selection |
| 72 | + setRanges([range]) |
| 73 | + |
| 74 | + // When it is the first selection, we don't want to close the popover |
| 75 | + // We have to do that ourselves because the library doesn't provide a way to do it |
| 76 | + if (selectionStatusRef.current === "idle") { |
| 77 | + selectionStatusRef.current = "selecting" |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + selectionStatusRef.current = "idle" |
| 82 | + const startDate = range.startDate as Date |
| 83 | + const endDate = range.endDate as Date |
| 84 | + onChange({ |
| 85 | + startDate, |
| 86 | + endDate, |
| 87 | + }) |
| 88 | + setIsOpen(false) |
| 89 | + }} |
| 90 | + moveRangeOnFirstSelection={false} |
| 91 | + months={2} |
| 92 | + ranges={ranges} |
| 93 | + maxDate={new Date()} |
| 94 | + direction="horizontal" |
| 95 | + /> |
| 96 | + </Popover> |
| 97 | + </> |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +const DateRangePickerWrapper: typeof Box = styled(Box)(({ theme }) => ({ |
| 102 | + "& .rdrDefinedRangesWrapper": { |
| 103 | + background: theme.palette.background.paper, |
| 104 | + borderColor: theme.palette.divider, |
| 105 | + }, |
| 106 | + |
| 107 | + "& .rdrStaticRange": { |
| 108 | + background: theme.palette.background.paper, |
| 109 | + border: 0, |
| 110 | + fontSize: 14, |
| 111 | + color: theme.palette.text.secondary, |
| 112 | + |
| 113 | + "&:hover .rdrStaticRangeLabel": { |
| 114 | + background: theme.palette.background.paperLight, |
| 115 | + color: theme.palette.text.primary, |
| 116 | + }, |
| 117 | + |
| 118 | + "&.rdrStaticRangeSelected": { |
| 119 | + color: `${theme.palette.text.primary} !important`, |
| 120 | + }, |
| 121 | + }, |
| 122 | + |
| 123 | + "& .rdrInputRanges": { |
| 124 | + display: "none", |
| 125 | + }, |
| 126 | + |
| 127 | + "& .rdrDateDisplayWrapper": { |
| 128 | + backgroundColor: theme.palette.background.paper, |
| 129 | + }, |
| 130 | + |
| 131 | + "& .rdrCalendarWrapper": { |
| 132 | + backgroundColor: theme.palette.background.paperLight, |
| 133 | + }, |
| 134 | + |
| 135 | + "& .rdrDateDisplayItem": { |
| 136 | + background: "transparent", |
| 137 | + borderColor: theme.palette.divider, |
| 138 | + |
| 139 | + "& input": { |
| 140 | + color: theme.palette.text.secondary, |
| 141 | + }, |
| 142 | + |
| 143 | + "&.rdrDateDisplayItemActive": { |
| 144 | + borderColor: theme.palette.text.primary, |
| 145 | + backgroundColor: theme.palette.background.paperLight, |
| 146 | + |
| 147 | + "& input": { |
| 148 | + color: theme.palette.text.primary, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + |
| 153 | + "& .rdrMonthPicker select, & .rdrYearPicker select": { |
| 154 | + color: theme.palette.text.primary, |
| 155 | + appearance: "auto", |
| 156 | + background: "transparent", |
| 157 | + }, |
| 158 | + |
| 159 | + "& .rdrMonthName, & .rdrWeekDay": { |
| 160 | + color: theme.palette.text.secondary, |
| 161 | + }, |
| 162 | + |
| 163 | + "& .rdrDayPassive .rdrDayNumber span": { |
| 164 | + color: theme.palette.text.disabled, |
| 165 | + }, |
| 166 | + |
| 167 | + "& .rdrDayNumber span": { |
| 168 | + color: theme.palette.text.primary, |
| 169 | + }, |
| 170 | + |
| 171 | + "& .rdrDayToday .rdrDayNumber span": { |
| 172 | + fontWeight: 900, |
| 173 | + |
| 174 | + "&:after": { |
| 175 | + display: "none", |
| 176 | + }, |
| 177 | + }, |
| 178 | + |
| 179 | + "& .rdrInRange, & .rdrEndEdge, & .rdrStartEdge": { |
| 180 | + color: theme.palette.primary.main, |
| 181 | + }, |
| 182 | + |
| 183 | + "& .rdrDayDisabled": { |
| 184 | + backgroundColor: "transparent", |
| 185 | + |
| 186 | + "& .rdrDayNumber span": { |
| 187 | + color: theme.palette.text.disabled, |
| 188 | + }, |
| 189 | + }, |
| 190 | +})) |
0 commit comments