Files
Checkmate/client/src/Components/inputs/DatePicker.tsx
T

103 lines
2.6 KiB
TypeScript
Raw Normal View History

2026-02-04 21:45:39 +00:00
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import type { DatePickerProps } from "@mui/x-date-pickers/DatePicker";
import type { Dayjs } from "dayjs";
import { useTheme } from "@mui/material/styles";
import Stack from "@mui/material/Stack";
2026-02-04 22:55:54 +00:00
import Typography from "@mui/material/Typography";
2026-02-04 21:45:39 +00:00
import { FieldLabel } from "./FieldLabel";
import { Calendar } from "lucide-react";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "@mui/x-date-pickers";
2026-09-03 01:38:51 -04:00
import { CONTROL_HEIGHT } from "@/Utils/Theme/constants";
2026-02-04 21:45:39 +00:00
interface DatePickerComponentProps extends Omit<DatePickerProps<Dayjs>, "label"> {
fieldLabel?: string;
required?: boolean;
2026-02-04 22:55:54 +00:00
error?: boolean;
helperText?: string;
2026-02-04 21:45:39 +00:00
}
export const DatePickerComponent = ({
fieldLabel,
required,
2026-02-04 22:55:54 +00:00
error,
helperText,
2026-02-04 21:45:39 +00:00
...props
}: DatePickerComponentProps) => {
const theme = useTheme();
const picker = (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
{...props}
slots={{
openPickerIcon: () => (
<Calendar
size={20}
stroke={theme.palette.text.secondary}
/>
),
...props.slots,
}}
slotProps={{
switchViewButton: { sx: { display: "none" } },
nextIconButton: { sx: { ml: theme.spacing(2) } },
day: {
sx: {
"&.MuiPickersDay-root.Mui-disabled": {
color: theme.palette.text.disabled,
},
},
},
field: {
sx: {
width: "fit-content",
"& > .MuiOutlinedInput-root": {
flexDirection: "row-reverse",
},
"& input": {
2026-09-03 01:38:51 -04:00
minHeight: CONTROL_HEIGHT,
2026-02-04 21:45:39 +00:00
p: 0,
pl: theme.spacing(3),
pr: theme.spacing(5),
},
"& fieldset": {
2026-02-04 22:55:54 +00:00
borderColor: error ? theme.palette.error.main : theme.palette.divider,
2026-02-04 21:45:39 +00:00
borderRadius: theme.shape.borderRadius,
},
"&:not(:has(.Mui-disabled)):not(:has(.Mui-error)) .MuiOutlinedInput-root:not(:has(input:focus)):hover fieldset":
{
borderColor: error
? theme.palette.error.main
: theme.palette.text.disabled,
2026-02-04 21:45:39 +00:00
},
},
},
inputAdornment: { sx: { ml: 0 } },
openPickerButton: {
sx: {
py: 0,
mr: 0,
},
},
...props.slotProps,
}}
/>
</LocalizationProvider>
);
2026-02-04 22:55:54 +00:00
return (
<Stack spacing={theme.spacing(2)}>
{fieldLabel && <FieldLabel required={required}>{fieldLabel}</FieldLabel>}
{picker}
{helperText && (
<Typography
variant="caption"
color={error ? "error" : "text.secondary"}
>
{helperText}
</Typography>
)}
</Stack>
);
2026-02-04 21:45:39 +00:00
};