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 | 29x 352x 352x 361x 1x 1x 1x 4x 418x | import { Controller, useFormContext } from "react-hook-form";
import DatePicker, { DatePickerProps } from "./DatePicker";
import { InputProps } from "@common/Input/Input";
import { subYears } from "date-fns";
import {
MAXIMUM_REQUIRED_AGE,
MINIMUM_REQUIRED_AGE,
} from "@constants/constants";
import { Input } from "@common/Input";
import { memo } from "react";
import { LocalizationProvider } from "@mui/x-date-pickers";
import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment";
import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFns";
type BirthDatePickerProps = InputProps & {
name: string;
placeholder?: string;
label?: string;
datePickerProps?: DatePickerProps;
datePickerPopperProps?: DatePickerProps["popperProps"];
maxDate?: any;
minDate?: any;
openPickerOnFocus?: boolean;
adornmentPosition?: "start" | "end";
helper?: string;
useGMT?: boolean;
useUTCMoment?: boolean;
sx?: any;
};
const BirthDatePicker = ({
name,
label,
placeholder,
datePickerProps,
helper,
datePickerPopperProps = {},
useGMT = false,
sx,
useUTCMoment,
...props
}: BirthDatePickerProps) => {
const { control, setValue } = useFormContext();
return (
<LocalizationProvider
dateAdapter={useUTCMoment ? AdapterMoment : AdapterDateFns}
>
<Controller
name={name}
control={control}
render={({
field: { ref, value, onChange, onBlur, ...rest },
fieldState: { error },
}) => (
<DatePicker
{...rest}
inputFormat={useUTCMoment ? "MM/DD/YYYY" : "MM/dd/yyyy"}
inputRef={ref}
value={value}
onChange={(value) => {
onChange(value);
const formValue = useUTCMoment && value ? value.toDate() : value; //we are still expecting the dates accross our app to be returned as JS Date, not moment. to be able to use as moment, we need to change all the places
setValue(name, formValue, { shouldValidate: true });
}}
setInitialValue={(val: string) => {
!useUTCMoment &&
setValue(name, val, {
shouldDirty: false,
shouldTouch: false,
});
}}
maxDate={subYears(new Date(), MINIMUM_REQUIRED_AGE)}
minDate={subYears(new Date(), MAXIMUM_REQUIRED_AGE)}
openTo="year"
popperProps={datePickerPopperProps}
useGMT={useGMT}
useUTCMoment={useUTCMoment}
{...datePickerProps}
{...props}
renderInput={({ inputProps, ...params }) => (
<Input
name="date"
inputRef={ref}
{...params}
fullWidth
autoComplete="off"
error={!!error}
onBlur={props.onBlur ?? onBlur}
helperText={error?.message || helper}
label={label}
value={value}
inputProps={{
...inputProps,
placeholder,
}}
sx={{
"@media (min-width: 600px)": {
"& .MuiInputLabel-root": {
left: props.adornmentPosition === "end" ? "0px" : "28px",
},
"& .MuiIconButton-root": {
marginRight: "2px !important",
},
},
"@media (max-width: 600px)": {
"& .MuiInputAdornment-positionStart": {
display: "none",
},
},
...sx,
}}
/>
)}
/>
)}
/>
</LocalizationProvider>
);
};
export default memo(BirthDatePicker);
|