Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 127x 2x 2x 2x 2x 2x 2x 2x 2x 2x 127x 127x | import { useState } from "react";
import { DateRangePicker } from "./DateRangePicker.styles";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { Range, RangeKeyDict } from "react-date-range";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { Stack } from "@mui/material";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { CaretLeftIcon, CaretRightIcon } from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import moment from "moment";
import { RangeValue } from "./DateRangePicker.types";
import { LocalizationProvider } from "@mui/x-date-pickers";
import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment";
import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFns";
import {
getMomentDate,
getUTCDefaultValue,
useFormatDateInTimezone,
} from "@utils/date.helpers";
import { utcToZonedTime } from "date-fns-tz";
type Props = {
onSelectionChange?: (val: RangeValue) => void;
initialRange: RangeValue;
maxDate?: Date;
minDate?: Date;
useUTCMoment?: boolean;
useTimezone?: boolean;
};
export default function RangePicker({
initialRange,
onSelectionChange,
maxDate,
minDate,
useUTCMoment,
useTimezone,
}: Props) {
const { isMobileView } = useCustomThemeV2();
const { palette } = useAppTheme();
const { currentTimezone } = useFormatDateInTimezone();
const today = useTimezone
? utcToZonedTime(new Date(), currentTimezone)
: new Date();
const { startDate, endDate } = (() => {
const startDate = initialRange?.startDate ? initialRange?.startDate : today;
const endDate = initialRange?.endDate ? initialRange?.endDate : today;
return {
startDate: getUTCDefaultValue(startDate, useUTCMoment),
endDate: getUTCDefaultValue(endDate, useUTCMoment),
};
})();
const [selectionRange, setSelectionRange] = useState<Range>({
startDate,
endDate,
key: "selection",
});
const onChange = (range: RangeKeyDict) => {
if (range?.selection?.startDate && range?.selection?.endDate) {
if (onSelectionChange)
onSelectionChange({
startDate: range.selection.startDate,
endDate: range.selection.endDate,
});
setSelectionRange(range?.selection);
}
};
return (
<LocalizationProvider
dateAdapter={useUTCMoment ? AdapterMoment : AdapterDateFns}
>
<DateRangePicker
ranges={[selectionRange]}
onChange={onChange}
showMonthAndYearPickers={false}
showDateDisplay={false}
showMonthArrow={false}
moveRangeOnFirstSelection={false}
shownDate={undefined}
editableDateInputs
weekdayDisplayFormat="eeeeee"
months={isMobileView ? 1 : 2}
rangeColors={[palette.primitive?.transparent["darken-10"] || ""]}
color={palette.text.primary}
direction="horizontal"
navigatorRenderer={(date: Date, changeShownDate) =>
renderNavigation(date, changeShownDate, isMobileView, selectionRange)
}
scroll={{ enabled: false }}
maxDate={maxDate}
minDate={minDate}
data-testid="range-calendar"
/>
</LocalizationProvider>
);
}
const renderNavigation = (
currentFocusedDate: Date,
changeShownDate: (
value: string | number | Date,
mode?: "set" | "setYear" | "setMonth" | "monthOffset" | undefined,
) => void,
isMobileView: boolean,
selectionRange: Range,
) => {
const leftMonth = getMomentDate(currentFocusedDate).format("MMMM YYYY");
const rightMonth = getMomentDate(currentFocusedDate)
.add(1, "months")
.format("MMMM YYYY");
const navigateBack = (
<GiveIconButton
Icon={CaretLeftIcon}
size="small"
variant="ghost"
onClick={() => changeShownDate(-1, "monthOffset")}
/>
);
const navigateFront = (
<GiveIconButton
Icon={CaretRightIcon}
size="small"
variant="ghost"
onClick={() => changeShownDate(+1, "monthOffset")}
/>
);
const { startDate, endDate } = selectionRange;
const selectedStart = startDate
? moment(startDate).format("MMM D")
: "Start Date";
const selectedEnd = endDate ? moment(endDate).format("MMM D") : "End Date";
const isSameDate = selectedStart === selectedEnd;
return (
<Stack
direction="row"
alignItems="center"
width="100%"
justifyContent="space-between"
gap="32px"
paddingBottom="16px"
>
{!isMobileView && (
<>
<StyledRow>
{navigateBack}
<StyledText>{leftMonth}</StyledText>
</StyledRow>
<StyledRow>
<StyledText>{rightMonth}</StyledText>
{navigateFront}
</StyledRow>
</>
)}
{isMobileView && (
<Stack direction="column" width="100%">
<GiveText variant="h3" fontWeight={300}>
{selectedStart} {!isSameDate && "-"}{" "}
{!isSameDate && (
<GiveText
variant="h3"
fontWeight={300}
component="span"
color="secondary"
>
{selectedEnd}
</GiveText>
)}
</GiveText>
<StyledRow>
<StyledText>{leftMonth}</StyledText>
{navigateBack}
{navigateFront}
</StyledRow>
</Stack>
)}
</Stack>
);
};
const StyledRow = styled(Stack)({
flexDirection: "row",
alignItems: "center",
flexGrow: 1,
});
const StyledText = styled(GiveText)(({ theme }) => ({
variant: "bodyS",
flex: 1,
[theme.breakpoints.up("v2_sm")]: {
textAlign: "center",
},
}));
|