|
| 1 | +import * as React from 'react'; |
| 2 | +import { styled } from '@mui/material/styles'; |
| 3 | +import TextField from '@mui/material/TextField'; |
| 4 | +import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; |
| 5 | +import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; |
| 6 | +import { StaticDatePicker } from '@mui/x-date-pickers/StaticDatePicker'; |
| 7 | +import { PickersDay, PickersDayProps } from '@mui/x-date-pickers/PickersDay'; |
| 8 | +import endOfWeek from 'date-fns/endOfWeek'; |
| 9 | +import isSameDay from 'date-fns/isSameDay'; |
| 10 | +import isWithinInterval from 'date-fns/isWithinInterval'; |
| 11 | +import startOfWeek from 'date-fns/startOfWeek'; |
| 12 | + |
| 13 | +type CustomPickerDayProps = PickersDayProps<Date> & { |
| 14 | + dayIsBetween: boolean; |
| 15 | + isFirstDay: boolean; |
| 16 | + isLastDay: boolean; |
| 17 | +}; |
| 18 | + |
| 19 | +const CustomPickersDay = styled(PickersDay, { |
| 20 | + shouldForwardProp: (prop) => |
| 21 | + prop !== 'dayIsBetween' && prop !== 'isFirstDay' && prop !== 'isLastDay', |
| 22 | +})<CustomPickerDayProps>(({ theme, dayIsBetween, isFirstDay, isLastDay }) => ({ |
| 23 | + ...(dayIsBetween && { |
| 24 | + borderRadius: 0, |
| 25 | + backgroundColor: theme.palette.primary.main, |
| 26 | + color: theme.palette.common.white, |
| 27 | + '&:hover, &:focus': { |
| 28 | + backgroundColor: theme.palette.primary.dark, |
| 29 | + }, |
| 30 | + }), |
| 31 | + ...(isFirstDay && { |
| 32 | + borderTopLeftRadius: '50%', |
| 33 | + borderBottomLeftRadius: '50%', |
| 34 | + }), |
| 35 | + ...(isLastDay && { |
| 36 | + borderTopRightRadius: '50%', |
| 37 | + borderBottomRightRadius: '50%', |
| 38 | + }), |
| 39 | +})) as React.ComponentType<CustomPickerDayProps>; |
| 40 | + |
| 41 | +export default function CustomDay() { |
| 42 | + const [value, setValue] = React.useState<Date | null>(new Date()); |
| 43 | + |
| 44 | + const renderWeekPickerDay = ( |
| 45 | + date: Date, |
| 46 | + selectedDates: Array<Date | null>, |
| 47 | + pickersDayProps: PickersDayProps<Date>, |
| 48 | + ) => { |
| 49 | + if (!value) { |
| 50 | + return <PickersDay {...pickersDayProps} />; |
| 51 | + } |
| 52 | + |
| 53 | + const start = startOfWeek(value); |
| 54 | + const end = endOfWeek(value); |
| 55 | + |
| 56 | + const dayIsBetween = isWithinInterval(date, { start, end }); |
| 57 | + const isFirstDay = isSameDay(date, start); |
| 58 | + const isLastDay = isSameDay(date, end); |
| 59 | + |
| 60 | + return ( |
| 61 | + <CustomPickersDay |
| 62 | + {...pickersDayProps} |
| 63 | + disableMargin |
| 64 | + dayIsBetween={dayIsBetween} |
| 65 | + isFirstDay={isFirstDay} |
| 66 | + isLastDay={isLastDay} |
| 67 | + /> |
| 68 | + ); |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <LocalizationProvider dateAdapter={AdapterDateFns}> |
| 73 | + <StaticDatePicker |
| 74 | + displayStaticWrapperAs="desktop" |
| 75 | + label="Week picker" |
| 76 | + value={value} |
| 77 | + onChange={(newValue) => { |
| 78 | + setValue(newValue); |
| 79 | + }} |
| 80 | + renderDay={renderWeekPickerDay} |
| 81 | + renderInput={(params) => <TextField {...params} />} |
| 82 | + inputFormat="'Week of' MMM d" |
| 83 | + /> |
| 84 | + </LocalizationProvider> |
| 85 | + ); |
| 86 | +} |
0 commit comments